perl-language-server icon indicating copy to clipboard operation
perl-language-server copied to clipboard

Code formatting isn't working for me with VS Code

Open fgimian opened this issue 3 years ago • 8 comments

Hey there, thanks so much for your work on this language server.

I'm using the latest v0.902 with v0.0.15 of the VS Code extension running over Remote SSH to a Ubuntu 22.04 server with Perl 5.36 installed via perlbrew. Everything works as expected except code formatting despite the fact that perltidy is installed and available in $PATH. Running the Format Document command simply does nothing at all.

When I switch to the perltidy extension for formatting, everything works correctly.

I've also attempted to specify my exact location of .perltidyrc and PLS still won't budge, while the perltidy extension I linked above works.

I'm not entirely sure how to troubleshoot this to provide more information. Do you have any suggestions?

Thanks Fotis

fgimian avatar Nov 08 '22 03:11 fgimian

Appears to be the same issue as https://github.com/FractalBoy/perl-language-server/issues/50 Adding -nst resolves the problem. Perhaps it may be worth adding to the README @FractalBoy?

I'll close this one then 😄 Fotis

fgimian avatar Nov 08 '22 03:11 fgimian

I guess I hadn't considered adding -nst in the code. I can go ahead and do that.

FractalBoy avatar Nov 08 '22 03:11 FractalBoy

I guess I hadn't considered adding -nst in the code. I can go ahead and do that.

Thank you so much! 😄

fgimian avatar Nov 08 '22 03:11 fgimian

Just wanted to re-open this as there seem to be multiple issues (at least for me). The -nst flag is the first. But even after that, PLS doesn't seem to comply to my .perltidyrc.

For example, I increased the indent to 8 for demonstrational purposes:

This is my .perltidyrc:

--perl-best-practices
--indent-columns=8
--continuation-indentation=8
--nostandard-output

My little input file is:

package Greeter;
use 5.036;

our $VERSION = 1.0;

sub get_greeting ($name) {
    return "hi there ${name}";
}

1;

Using the perltidy CLI, I get the expected result:

package Greeter;
use 5.036;

our $VERSION = 1.0;

sub get_greeting ($name) {
        return "hi there ${name}";
}

1;

However, donig this in VS Code with PLS indents to a 2-space indent:

package Greeter;
use 5.036;

our $VERSION = 1.0;

sub get_greeting ($name) {
  return "hi there ${name}";
}

1;

Using the other mentioned extension works as expected 😄

Any ideas? Fotis

fgimian avatar Nov 08 '22 03:11 fgimian

What about your VSCode settings? Your tab settings in VSCode are also taken into account when formatting.

FractalBoy avatar Nov 08 '22 03:11 FractalBoy

What about your VSCode settings? Your tab settings in VSCode are also taken into account when formatting.

Thanks heaps for the reply. I've tried this with an empty VS Code configuration file only containing the pls.perltidy.perltidyrc setting and sadly see the same problem.

However, I did some more digging based on your comment and as you mentioned, PLS will indent completely based on the indent level of the file (which may be auto-detected).

I got this working as follows in my VS Code config:

    "[perl]": {
        "editor.tabSize": 8,
        "editor.detectIndentation": false
    }

However, I'm not sure that PLS should consider the VS Code indent level; after all this is why Perl Tidy exists, to ensure that code conforms to a specific style and this should ideally be fully specified in .perltidyrc IMHO.

What do you think? Fotis

fgimian avatar Nov 08 '22 04:11 fgimian

Just a further thought, the current behaviour will likely cause conflicts with [CodeLayout::RequireTidyCode] in Perl Critic as PLS ultimately may not indent a file based on the .perltidyrc setting; but rather the editor setting.

I'd at least love the option to turn this behaviour off and make .perltidyrc the single point of truth for all formatting 😄

fgimian avatar Nov 08 '22 04:11 fgimian

While I don't necessarily disagree, I'm not sure there's a "right" answer here. If I were to remove this behavior, there might be someone who is relying on it and files an issue asking me to change it back.

I think I will err on the side of respecting the options that the Language Server Protocol provides.

Edit: I suppose your suggestion (an option) would be the best of both worlds. If you are able, feel free to submit a PR. Otherwise I can get to it when I have time.

FractalBoy avatar Nov 08 '22 13:11 FractalBoy