Sublime Forum

JavaScript Indentation broken after comment

#1

Hi,

When writing JavaScript the indentation seems to be broken after a comment.

The comments do not “reindent”, then any code after the comment indents at the comment level, which is wrong, so everything after that is wrong.

Does anyone know how to fix this?

0 Likes

#2

Am I the only one with this issue?

I thought it was because I was using Sublime Text 3, but it happens on 2 as well.

I can’t believe this issue would be around still as popular as sublime text is, so I am assuming it is just my setup?

0 Likes

#3

From your description, I am not really sure what your problem is, but I have no problem writing a comment and moving on to the next line with proper indentation.

Did you try disabling 3rd party plugins?

0 Likes

#4

Yes I tried disabling the plugins I have ( Emmet, subemacspro ), but that did not work.

Can you try this and tell me what you get?

  1. Create a js file with this:

function test() { if(true) { //This unindented comment will never indent console.log("This line will get unindented to the beginning of the previous comment"); } }

  1. Now select everything and do Edit -> Line -> Reindent

I would want the following as a result:

function test() { if(true) { //This unindented comment will never indent console.log("This line will get unindented to the beginning of the previous comment"); } }
but instead I get this:

function test() { if(true) { //This unindented comment will never indent console.log("This line will get unindented to the beginning of the previous comment"); } }
You see the comment never moves in my case.

0 Likes

#5

If I “force” the comment to be at the wrong indentation level, yes, I can replicate this. I never unindent my comments though, so I’ve never ran into this issue for the past 2-3 year of using ST. In that if I were adding a comment on that line via super+/ the indentation is correct.

If you’re looking for away to clean up old code I’d suggest using JSFormat sublime.wbond.net/packages/JsFormat

0 Likes

#6

Thanks for the JSFormat idea, I ll try that.

I don’t unindent on purpose either. What happens is that sometimes I have to copy a block (A) inside another block (B), but now the indentation levels are wrong so I must reindent, and that is when the comments end up in the wrong place.

0 Likes

#7

Ahh, gotcha. The reindent command works OK in some situations for me, mainly tag based markup, so I always turn to JSFormat for JS clean up. Also, when I’m pasting I’ll usually paste via super+shift+v which does a pretty good job of preserving the correct indentation on paste.

0 Likes

#8

You are not the only one :pensive:I have the same issue

0 Likes

#9

You can get the line after the comment to be indented properly by doing this:

  1. Install PackageResourceViewer from Package Control
  2. Open the Command Palette
  3. Type PRV: O and select PackageResourceViewer: Open Resource
  4. Select JavaScript
  5. Select JavaScript Indent.tmPreferences
  6. Inside the <dict> that corresponds to <key>settings</key>, add: <key>unIndentedLinePattern</key><string>^\s*//.*$</string>
  7. Save the file

Then future re-indents should at least work for the line after the comment. Not sure how to fix comments, though…

0 Likes

#10

I have done more investigation and found that it is seemingly by design that comments are not reindented. I have logged an issue, because I believe it should at least be configurable. Meanwhile, the issue I have logged also details how to work around it and enable comments to be reindented. https://github.com/SublimeTextIssues/Core/issues/1271

3 Likes

#11

Thanks kingkeith, the resource viewer trick helped…
However if you have a comment at the end of a line all further indentation is knocked out of whack

The expected formatting would be like this…

function something(){
    var thing = 0;
    var otherthing = 10;
    $(".allthethings").click(function(){
        var thisthing = -10;  // this is this thing
        var thatthing = 10;
        var bigthing = thisthing * thatthing;
    });
}

but because of the comment we get this instead:

function something(){
    var thing = 0;
    var otherthing = 10;
    $(".allthethings").click(function(){
var thisthing = -10;  // this is this thing
var thatthing = 10;
var bigthing = thisthing * thatthing;
});
}
0 Likes

#12

I can confirm that the fix for this is as mentioned in the issue I linked to in my post above - need to set preserveIndent to false for the comment scope in the rules in the Default package.

1 Like