SublimeLinter-phpcs icon indicating copy to clipboard operation
SublimeLinter-phpcs copied to clipboard

Misplaced highlights when tabs are used instead of spaces

Open rusproject opened this issue 2 years ago • 30 comments

Hello! I don't know if it is a SublimeLinter-phpcs or SublimeLinter or even PHP_Code_Sniffer problem, so If I opened the issue in a wrong place, please point me where I should do it.

The problem is: When CodeSniffer highlight errors on lines that is indented with tabs, not spaces, even if the tabs are allowed by rules, we get misplaced highlights.

Here's what I mean:

image

Expected look (tabs are converted to spaces):

image

rusproject avatar Jan 05 '23 22:01 rusproject

Hm, probably https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-default-tab-width

It looks like phpcs reports the same line:column pair for both cases, which would mean it interprets one tab has a width of 4 spaces/characters. Maybe setting a tab-width can fix the problem.

kaste avatar Jan 06 '23 01:01 kaste

It was already tab-width=4 in my ruleset:

    <arg name="tab-width" value="0"/>

I've tried to set tab-width=0 or delete it, and then highlights were in right places. But it's leading us to another problem, incorrect work of Generic.WhiteSpace.ScopeIndent rule:

image

I tried to configure rule like this:

    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <property name="indent" value="4" />
            <property name="tabIndent" value="true" />
        </properties>
    </rule>

and this:

    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <!-- <property name="indent" value="4" /> -->
            <property name="tabIndent" value="true" />
        </properties>
    </rule>

But it gives the same result: phpcs tells expected 1 tabs, found 1 spaces although there are really 1 tab in the code.

So I don't think that it's the proper way to configure indent rules with tab-width=0.

According to this gist we really need to set tab-width=4, as it was in my rules from the beginning.

Btw, if we use ruleset from that gist above, we get exactly same problem with misplaced highlights.

So I think it is not a configuration problem, but some bug. Can't figure out where - phpcs, st-linter, or st-linter-phpcs.

rusproject avatar Jan 06 '23 13:01 rusproject

I looked at ale, a vim plugin, and they actually do the same as we do here. I did not find a similar issue over there, so I would suggest asking on the phpcs forum for some advice and help. I don't see what we could do wrong here, and it looks like phpcs reports columns with tabs expanded. Did you check this happens also on the command line?

kaste avatar Jan 06 '23 16:01 kaste

looks like phpcs reports columns with tabs expanded

I don't think this is the problem, LSP reports same line:column but highlights correctly:

image

rusproject avatar Jan 07 '23 20:01 rusproject

Heh? Here it looks like it reports 9:3 which is clearly tabs-not-expanded style. In the original post it looked like it reported 9:9. What does it report on the command line in --report=emacs mode?

kaste avatar Jan 07 '23 21:01 kaste

Also this is intelephense. Does this use phpcs under the hood?

kaste avatar Jan 07 '23 21:01 kaste

Heh? Here it looks like it reports 9:3 which is clearly tabs-not-expanded style. In the original post it looked like it reported 9:9. What does it report on the command line in --report=emacs mode?

Yes, sorry, looked at the wrong screenshot.

--report=emacs reports same line:column with tabs-expanded style, just like Sublime linter panel:

C:\Users\Alex>phpcs --report=emacs --standard=D:\example\phpcs.xml D:\example\test.php
D:\example\test.php:7:5: error - Visibility must be declared on method "__construct"
D:\example\test.php:9:9: warning - Inline control structures are not allowed

image

Also this is intelephense. Does this use phpcs under the hood?

Can't find that information quickly.

rusproject avatar Jan 10 '23 06:01 rusproject

As I understand the documentation, tab-width MUST NOT be set because it dictates if tabs are converted to spaces. Of course you then have to change the scope-ident rule because its defaults are wrong after that, e.g. you expect 1 indent as it now counts 1 tab but the default is 4, typically 4 spaces but 4 tabs if tabIndent is set.

If you see "found n spaces" it indicates it converted tabs to spaces. But it might translate 1 tab to 1 space instead of 4. Then the markers are drawn correctly as the column is reported as we expect it but other rules have to be adjusted as well.

kaste avatar Jan 10 '23 11:01 kaste

Thank you for detailed response. Anyway, why is it so complicated... Can't it be like ESlint for example? With clear list of available rules, and obvious ways to use them.

rusproject avatar Jan 12 '23 09:01 rusproject

I don't know why phpcs made their decisions.

kaste avatar Jan 12 '23 09:01 kaste