Sublime Forum

Python/C++: Indenting Comments

#1

Dear all,

I have been searching this forum for a solution for a while now, but somehow haven’t found a way to achieve what I am looking for. I am on ST4, latest stable release 4143.

I mapped “reindent lines” to by TAB key to have Emacs-style indentation - which works pretty well, but in C++ (and also Python, the main languages I am working with) comment lines are not indented. I would like to change that so comments are also included in the reindent.

I have looked at

  • Can I prevent indentation for toggle comment? but have not succeeded in making this work. The C++ file for comments.tmPreferences has a section for the regular // comment style:

    <dict>
      <key>name</key>
      <string>TM_COMMENT_START</string>
      <key>value</key>
      <string>// </string>
    </dict>
    

    but there is no corresponding TM_COMMENT_DISABLE_INDENT setting. So I added it below - with the effect that reindentation goes completely crazy :smiley:

  • Everything you (n)ever wanted to know about indentation in ST3 here I must admit I was completely lost. I am quite happy with the indentation in general and don’t want to tune or change anything - apart from the comment lines…

I hope someone has a pointer for me!

All the best,
herodot

0 Likes

#2

I’ve also tried changing Indentation Rules - Comments.tmPreferences of the Default package to

<dict>
    <key>scope</key>
    <string>comment</string>
    <key>settings</key>
    <dict>
        <key>preserveIndent</key>
        <false/>
    </dict>
</dict>

But also this didn’t change anything in indentation behavior - I’m a bit lost…

Continuing the journey,
herodot

0 Likes

#3

Hi all,

I have now updated to the dev build (I am on Build 4150 now) in the hope that it fixes this behavior, but unfortunately I don’t see any change.

Can someone help me please? :innocent:

Herodot

0 Likes

#4

I don’t quite understand why it doesn’t work for you, so I just want to show some steps I took and the results I saw so we can swap notes.

  1. Open ST in safe mode subl --safe-mode
  2. View Package File -> Default/Indentation - Comments.tmPreferences
  3. View menu -> Show Console
  4. view.set_read_only(False) Enter
  5. Edit the true to false under preserveIndent
  6. Save it (I got a prompt about needing super user permissions, clicked Cancel, clicked Save again and created the Default folder in ~/.config/sublime-text-safe-mode/Packages and it worked without needing elevation)
  7. New Tab
  8. Set syntax to Python
  9. Type defTab
  10. Click before pass
  11. Type # comment Enter
  12. Select All (Ctrl+A)
  13. Deindent (Shift+Tab)
  14. Command Palette (Ctrl+P
  15. Type rein and Reindent Lines will be highlighted. Enter
  16. # comment lines up with pass as expected

Creating a keybinding for Tab to execute reindent { "single_line": false } should work the same way as using the command palette to execute reindent lines.

0 Likes

#5

Dear @kingkeith

thank you for taking the time to test this - indeed, when I follow your instructions one-by-one, comments in Python are properly indented! Probably I have not tested again with Python after changing this setting…

However, if I change your instructions 8-11 with

  1. Set syntax to C++
  2. Type void main Tab
  3. Click before return
  4. Type // comment

Then re-indenting everything ignores the comment line:

void main(int argc, char const *argv[])
{
// comment
	return 0;
}

Do you have any clue why C++ behaves differently here? Is this a bug in the C++ language package? :thinking:

Best regards,
herodot

0 Likes

#6

It’s the unIndentedLinePattern in Packages/C++/Indentation Rules.tmPreferences that causes this behavior, as it matches on //. Removing that fixes it.

0 Likes

#7

Hi @kingkeith

wooow, thank you so much, it works now! :heart_eyes:

There is a comment in that file:

# standalone begin or end of block comments
#   ST doesn't apply `Indentation Rules - Comments`, because
#   comment scope doesn't cover the whole line. So they need
#   to be excluded from indentation engine here.

…which I don’t fully understand: the line is neither begin or end of a block comment nor does the comment scope only cover the fraction of a line - it’s the full line. :thinking:

Also, if I wanted to have indentation also for block, I’d not only have to remove // | but also the other regex in the same line?

Best regards,
herodot

0 Likes

#8

it’s possible that it means that if a comment has whitespace (indentation) before it, then the comment scope doesn’t cover the entire line. It’s also possible that this comment was written before the \n at the end of a line comment was also scoped as comment.line by the syntax definition.
But, for a block comment like /* ends on the same line */, I guess the idea was to not indent it or unindent it, but leave it where it is. Or perhaps a comment like /* if blah { */ should be ignored and the next line not indented a level due to it thinking there is an if statement/unclosed open curly brace there. Not sure. But yes, you’d have to remove or alter the regex in the same line to get block comments to reindent as expected.

0 Likes

#9

Great, thanks a lot for your help and the accompanying explanations @kingkeith! :slight_smile:

0 Likes