Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reindent command breaks on (multiline) comments #1271

Open
2 of 3 tasks
keith-hall opened this issue Jul 8, 2016 · 41 comments
Open
2 of 3 tasks

Reindent command breaks on (multiline) comments #1271

keith-hall opened this issue Jul 8, 2016 · 41 comments

Comments

@keith-hall
Copy link
Collaborator

keith-hall commented Jul 8, 2016

Summary

Selecting an entire document that contains comments that span multiple lines, and using the Edit -> Line -> Reindent command, stops indenting at the comment.

I believe that this is not a Indentation Rules issue for the Default packages repo, because they don't specify any rules for handling comments, and there are no problems while typing the document, just when re-indenting.

Expected behavior

The entire document should be re-indented correctly.

Actual behavior

Indentation stops at the comment and starts from the indentation level of the comment (i.e. 0) again after it.

Steps to reproduce

  1. Create a new tab
  2. Set the syntax to XML or HTML
  3. Type some valid markup that includes a comment that spans multiple lines
  4. Select all
  5. Completely unindent the document
  6. Edit -> Line -> Reindent

Example:

<root>
<level>
<!-- example
comment -->
<example />
<foobar>
<hello world="yes">
</hello>
</foobar>
</level>
<level>
</level>
</root>

becomes:

<root>
    <level>
<!-- example
comment -->
<example />
<foobar>
    <hello world="yes">
    </hello>
</foobar>
</level>
<level>
</level>
</root> 

same with Javascript:

if (true) {
while (false) {
var x = 2;
/* multiline
comment here */
x += 3;
}
}

becomes:

if (true) {
    while (false) {
        var x = 2;
/* multiline
comment here */
x += 3;
}
}

and even single line comments:

if (true) {
while (false) {
// single line comment
var x = 2;
/* multiline
comment here */
x += 3;
}
}

becomes

if (true) {
    while (false) {
// single line comment
var x = 2;
/* multiline
comment here */
x += 3;
}
}

Environment

  • Operating system and version:
    • Windows 7 x64
    • Mac OSX (untested)
    • Linux - Ubuntu 16.04 x64
  • Sublime Text:
    • Build 3118, 3114
    • Clean install
@keith-hall keith-hall changed the title Reindent command breaks on HTML/XML multiline comments Reindent command breaks on (multiline) comments Jul 8, 2016
@keith-hall
Copy link
Collaborator Author

keith-hall commented Jul 8, 2016

In fact, this can be fixed by editing the Indentation Rules - Comments.tmPreferences file in the Default package, to change:

<key>preserveIndent</key>
<true/>

to

<key>preserveIndent</key>
<false/>

although the way things stand, indentation can become broken when the comments include lines that would affect the indentation, and the contents of comments can become indented i.e.

<root>
<level>
<!--
<blah>
test
</blah>
comment
-->
<example />
<foobar>
<hello world="yes">
</hello>
</foobar>
</level>
<level>
</level>
</root>

becomes:

<root>
    <level>
        <!--
            <blah>
                test
            </blah>
            comment
        -->
        <example />
        <foobar>
            <hello world="yes">
            </hello>
        </foobar>
    </level>
    <level>
    </level>
</root>

Related issue: using a scope of text.xml - comment in the XML Indentation tmPreferences file, the --> is still unindented by the reindent command. I guess because the comment scope ends after the -->, so the rules take effect again, and because --> is on the line, it ignores that it should be ignored.

More detailed instructions can be found on SO: http://stackoverflow.com/a/38261481/4473405


I guess you could argue that comments shouldn't be reindented, because the programmer may have had the intention to make commented-out lines more visible by placing them out-of-indent. But then, a) it should be configurable and b) the text below the comment should be indented as though the comment wasn't there.

@315234
Copy link

315234 commented Jul 8, 2016

I guess because the comment scope ends after the -->

This was my experience too, although I can't pin down exactly what the issue is. Indentation was broken when my syntax file applied the comment scope to the newline at the end of the comment. When I changed it so that the comment scope did not include the newline the indentation worked more reliably. (Comments are not ignored in tmPreferences)

@FichteFoll
Copy link
Collaborator

I suppose the most reasonable approach would be to only reindent comments if the preceding or following line has the same indentation originally (e.g. none) and generally not have comments affect the indentation level at all. There may still be edge cases on occasions, I suppose.

@315234
Copy link

315234 commented Jul 11, 2016

I disagree. I think the reindent command should produce the same result no matter what the indentation of the original file was. It already acts weird where it will indent things differently depending on whether you remove all indentation first. Additionally, I prefer the "don't change the indentation of comments" option which already exists, because for some languages the comment character must be the first character on the line (i.e. no whitespace before it)

@keith-hall
Copy link
Collaborator Author

Additionally, I prefer the "don't change the indentation of comments" option which already exists

where is this option @315234 ? the only way I found to alter this functionality was as mentioned earlier - to edit the Indentation Rules - Comments.tmPreferences file in the Default package - which IMO doesn't really count as an option :)

@315234
Copy link

315234 commented Jul 11, 2016

Yes that's what I'm referring to. It's a setting/option for package maintainers, but it can still be overwritten by the user.

@stahlnow
Copy link

where can i find Indentation Rules - Comments.tmPreferences file?

@keith-hall
Copy link
Collaborator Author

Note that disabling preserveIndent will break the alignment of docblock style comments upon reindent.

@fnicastri
Copy link

It's broken everywhere.

@dyspop
Copy link

dyspop commented Aug 3, 2017

Particularly annoying in HTML email framework code like Zurb etc with conditional MSO comments:

                </table>
                <!--[if (gte mso 9)|(IE)]>
              </td>
            </tr>
          </table>
          <![endif]-->
        </td>
      </tr>
</table>
                <!--[if (gte mso 9)|(IE)]>
              </td>
            </tr>
          </table>
          <![endif]-->
        </td>
      </tr>

end up getting these huge blocks of resets that have no correlated top-indent tag... the other option preserveindent false also doesn't work.

what makes sense to me is:
if there is indentable code in the comments do either of these two things:
1: indent the document as if there were no comments. keep comments at their existing indentation.
2: indent the document with indentable comment code as part of the document.
if they're inline with non-comment code keep them where they were in line.

@zubeldialuis
Copy link

+1

@AdamPS
Copy link

AdamPS commented Oct 29, 2017

Note that as well as the fix to Default/Indentation Rules - Comments.tmPreferences you often also need an extension-specific fix to take care of unIndentedLinePattern.

@droiddk
Copy link

droiddk commented Mar 29, 2018

Issue still exists :/

@datgame
Copy link

datgame commented May 21, 2018

applies to c++ too, since many years back.
int x = 5; // comment
you can't tab to reindent such lines because of the comment at the end.

@BB-000
Copy link

BB-000 commented Oct 4, 2018

Changed Comments.tmPreferences, tried playing round with this a few times, still can't get it working properly ( .scss/.less ) so annoying, I need comments and it's indent chaos!
Anyone got a .scss fix?

@edwardj728
Copy link

There should be a global fix to this!

@SalmanRavoof
Copy link

Is there a fix to this? I have a document that needs serious indenting and doing it manually is a PITA.

@sublimehq sublimehq deleted a comment from bolechen Mar 15, 2019
@JoshKisb
Copy link

JoshKisb commented Oct 4, 2019

Well it's still broken

@fnicastri
Copy link

This is one of the biggest disappointments of SublimeText, one of the overlooked issues that are pushing me away from it.
I love Sublime but this is simply too annoying.
I bought it, btw

@tinyCoder32
Copy link

The problem is still exists.

@AlexFinch
Copy link

How can people claim sublime text is wonderful when simply putting a comment in you code breaks its ability to indent code. In addition the solution mentioned here refers to a non existent file so is of no help. SOOOOOO infuriating.

@fnicastri
Copy link

@AlexFinch Sublime is Wonderful, this is basically the only serious glitch and it is not present for every language.
How can I love VSCode, 700MB of ram to open a project and run a few extensions!
Almost the memory needed by Android Studio, four times the memory needed by XCode :/

Anyway Sublime need more love, more modern features (a debugger) to survive to this new wave of editors, mainly to attract more people and more plugins developers. IMO

@Tomicapo
Copy link

Tomicapo commented Jul 2, 2020

I got tired of this.
Sadly transitioning to Visual Studio Code.

@tinyCoder32
Copy link

I got tired too much also from this stupid thing. But Visual Studio is very slow.
Why would Sublime Text developers ignore such an important issue?

@wbond
Copy link
Member

wbond commented Jul 2, 2020

My recommendation, in general, would be to look for a language-specific formatting tool. This will typically give you more control, and generally they come out-of-the-box following the language community's idiomatic style.

I am sure someone on the team will take the time to dig in and see what can be done. I don't, however, know exactly when that will be. We are still a very small organization, and only have a handful of engineers. This, combined with putting in extra effort to make sure our solutions aren't slow and can be maintained over the long term, means that some things end up taking longer.


On the meta comments: we definitely do spend quite a bit of time working on "little" issues. However, the nature of little bugs is that they tend to affect a relatively small percentage of users. Thus you won't notice most of the little bugs fixed since you are unlikely to hit them. But if your little bug is not fixed, then you definitely notice. Also, sometimes little bugs take an extreme amount of work to fix, because they would require changing a general approach to some fundamental part of the codebase.

I can't say for sure exactly what will go into fixing this, but people adding their voice about details where it doesn't work and adding reactions definitely helps us gauge where changes might be most felt.

@fnicastri
Copy link

@wbond
Hi Will, thanks for intervening in this old discussion.

Your recommendation makes sense but nonetheless this bug is so frustrating and so old.

Honestly I don't like to rely on an external tool just to reindent my code, especially for small projects where this
would mean to install a lot of useless software.
It's madness to install a whole node chain just to have a linter/beautifier and just to have a good reindentation of the code when Sublime is supposed do it correctly.
For medium/large projects this is not a problem, obviously.

For me Sublime is (or was) the default software, the first one I install on a new machine. It's my go to editor from years.
I usually keep it open even when I work with other editors, to fast edit files outside the project, to fast analyze some jsons and so on.
But these small bugs and the constant lack of a better integration with debuggers is changing all this.
For sure I don't want to succumb to VSCode, it is a nice software but it's so slow and resources consuming.
It's madness to write a text editor in JS.

Just my two cents

@JoshKisb
Copy link

JoshKisb commented Jul 4, 2020

They could just open source the project if man power is the issue.
just sayin

@fnicastri
Copy link

They could just open source the project if man power is the issue.
just sayin

Old story, no way it will happen (probably)

@wbond
Copy link
Member

wbond commented Jul 4, 2020

They could just open source the project if man power is the issue.
just sayin

🤣

@vulkd
Copy link

vulkd commented Nov 13, 2020

Been suffering with this issue for years, been putting up with it until now as I've reinstalled Sublime after reformatting my rig. Had to hunt down the fixes in https://forum.sublimetext.com/t/javascript-indentation-broken-after-comment/13609/12 again, which lets us re-indent code fine, sans-comments. Time spent having to re-indent comments one-by-one after one does a 'reindent all' adds up and quickly becomes meaningless work. Would love for indentation to work properly in my preferred editor.

If someone knows of a package to make JS indentation work (and not a full re-formatter like jsformat or prettier) in Sublime please comment here, it's quite frustrating for many I'm sure.

@vulkd
Copy link

vulkd commented May 19, 2021

This seems to work for me on ST3:

  • Install package resource viewer.
  • ctrl+shift+p, package resource viewer: open resource
  • select javascript
  • select javascript indent.tmPreferences
  • Change the contents to the following and save (ctrl+s)
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
    <key>name</key>
    <string>JavaScript Indent</string>
    <key>scope</key>
    <string>source.js</string>
    <key>settings</key>
    <dict>
        <key>decreaseIndentPattern</key>
        <string>^(.*\*/)?\s*[}\]].*$</string>
        <key>increaseIndentPattern</key>
        <string>^.*[{\[][^}"'\]]*$</string>
        <key>bracketIndentNextLinePattern</key>
        <string>(?x)
        ^ \s* \b(if|while|else)\b [^;]* $
        | ^ \s* \b(for)\b .* $
        </string>
        <key>unIndentedLinePattern</key>
        <string>^\s*//.*$</string>
    </dict>
</dict>
</plist>
  • ctrl+shift+p, package resource viewer: open resource
  • select default
  • select indentation rules - comments.tmPreferences
  • Change the contents to the following and save (ctrl+s)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>scope</key>
    <string>comment</string>
    <key>settings</key>
    <dict>
        <key>preserveIndent</key>
        <false/>
    </dict>
</dict>
</plist>

@gsailland
Copy link

gsailland commented Jun 14, 2021

By editing also this file : Sublime Text 3\Data\Packages\PHP\Indentation Rules - Annex.tmPreferences
As well as the file Sublime Text 3\Data\Packages\Default\Indentation Rules - Comments.tmPreferences

<key>preserveIndent</key>
<false/>

Solves the problem for me

PHP\Indentation Rules - Annex.tmPreferences

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
	<dict>
		<key>scope</key>
		<string>source.php</string>
		<key>settings</key>
		<dict>
			<key>unIndentedLinePattern</key>
			<string></string>
			<!--string>^\s*((\*/|#|//| \*).*)?$</string-->
		</dict>
	</dict>
</plist>

@sumit-pradhan
Copy link

what about single line? i want a shortcut single line multiple HTML tag in proper structure format

@wisnoskij
Copy link

wisnoskij commented Nov 27, 2022

Since the instructions of how to fix this are hard to find in this massive thread and not for the general case, I will post the steps I took below.

  1. Install PackageResourceViewer
    Ctrl+Shift+p > Package Control: Install Package > PackageResourceViewer (if it does not show up, it is probably already installed)
  2. Open the Comment.tmPreferences file (This can be done on the language level or default everything level)
    Ctrl+Shift+p > PackageResourceViewer: Open Resource > Default (Or the name of the language you want this fix to apply to) > Indentation Rules - Comments.tmPrefences
  3. Change true to false
    preserveIndent
    <false/>

@datgame
Copy link

datgame commented Dec 8, 2022

wisnoskij, this works!
You just answered/fixed a bug the SublimeText authors haven't been able to solve for 8 years.
It's not behaving perfectly if i have already moved the line a few spaces to the side, but in most cases it does what it should, even when there's a //-comment on the same line!

@neildmd
Copy link

neildmd commented Jan 6, 2023

Hopefully the devs will apply wisnoskii's fix to future updates of ST3 and thank him, considering they couldn't figure it out for 8 years. Thank you wisnoskii's. I never thought I would see this fixed...

@FichteFoll
Copy link
Collaborator

Just for clarification, everything relevant to this issue (including a workaround that does not satisfy all situations) has been posted by @keith-hall in the first comment.

A better fix (or workaround) would require changes to the core reindentation code and it seemingly hasn't been a priority for the team. Please continue upvoting the issue if you're affected by it.

@wisnoskij
Copy link

wisnoskij commented Jan 8, 2023

Yes, nothing I wrote is my own work, other than the exact wording which I tried to make as simple and general as possible.

I would urge the devs to just use this fix to the default configuration. I realize it is slightly off from being perfect in all cases, and it does not work with JS, php, and html, but it makes sublime text c9ompletl functional for languages like C or Rust. I realize that JS is really popular, but their are still a great many developers using languages who could have a out of the box working sublimeText auto-indentation.

Also, maybe I am wrong here. But it sounds like this fix does not break some languages and fix others. This fix helps even js and html, it just does not completely fix them?

@rtek1000
Copy link

rtek1000 commented Dec 9, 2023

Any news? This auto-indentation failure on lines that also have comments invalidates the use of the text editor.

If someone needs auto indentation that works with lines that have comments, they can use VS code, successfully tested on Linux.

On Windows Shift + Alt + F
On Mac Shift + Option + F
On Ubuntu Ctrl + Shift + I

Ref.: https://stackoverflow.com/questions/68884005/what-is-the-shortcut-to-auto-indent-code-in-vscode

@datgame

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests