Sublime Forum

Everything you (n)ever wanted to know about indentation in ST3

#22

Really useful article, thanks! I raised an issue for the problem you already mentioned “lines that don’t conform to the tab stop size” - perhaps you could link it?

I also raised github issues 2029 and 2030 for new problems “Reindent forces indent on line that should have none” and “Indendation wrong for CSS close brace with comment” (I didn’t insert direct links as it seems that new users are only allowed two links in a post!)

0 Likes

#23

I have some more observations on comment indentation after testing with PHP and Java.

  1. After fixing Indentation Rules - Comments.tmPreferences as you suggested I also had to edit the language specific file Indentation Rules Annex.tmPreferences. I removed // from unIndentedLinePattern which allows single-line comments to be re-indented.

  2. In PHP and Java, the re-indent command does not corrupt DocBlock comments. The fix comes from the same setting as in item 1, which excludes lines with ’ *’ and ’ */’ from indenting.

This combination works pretty well and I would recommend it for all languages:

  • preserveIndent false for comments
  • unIndentedLinePattern excludes single line comments and start of block comments
  • unIndentedLinePattern includes continuation and end of block comments
0 Likes

#24

done :slight_smile:

(I’ve also mentioned this at https://github.com/sublimehq/Packages/issues/1156#issuecomment-340426582 -) The problem with excluding single line comments from the unIndentedLinePattern is that this would break:

<?php
if (true)
    // I'm a comment after the if statement that should be indented, and I shouldn't cause the next code line to be unindented
    var_dump(false);

so I guess its up to users to choose what’s best for them while ST is still constrained to the TextMate indentation system

0 Likes

#25

I can un-indent using shift+tab.
Is there any shortcut to un-dent selected content completely with one key? If not, how can I create such shortcut?

0 Likes

Is there any shortcut to un-dent selected content completely?
#26

Hi Keith, do you think it’s possible to make indentation of docblocks work? For example:

/**
 *
 *
 *
 */

The body has an space before the asterisk, and when I reindent the file, it removes that space:

/**
*
*
*
*/
0 Likes

#27

Hi,

I’m not at a computer right now to check, but from memory, I think that it is unfortunately impossible with the TextMate indentation engine that ST uses. Setting preserveIndent to true in the .tmPreferences file should stop ST removing the space, but then it won’t correctly indent the comments to line up with the surrounding code iirc. I can’t think of any way to fix it, even giving the space a special scope won’t make any difference in all the scenarios I can imagine, but is probably something that should be done anyway for future use.

0 Likes

#28

Is it possible to execute a python function after the reindent command? I am not sure how to intercept sublime events.

0 Likes

#29

You can use an EventListener to tell when a command is either about to run or just finished running. An example would be:

import sublime
import sublime_plugin

class AfterIndentListener(sublime_plugin.EventListener):
    def on_post_text_command(self, view, command, args):
        if command == "reindent":
            print("reindent text command was just executed")

A regular EventListener is always active; you can also use a ViewEventListener as well, in which case the listener can be selectively applied only to a specific view based on it’s settings, such as the syntax that’s in use.

More info on all of the events you can monitor and how they work can be found in the API docs.

1 Like

#30

Is bracketIndentNextLinePattern only used when smart_indent is turned on?

0 Likes

#31

Hi: Is there a feature where I can set the desired indentation pattern for a block of text (e.g. chapter 1), then use a “paste special” feature like in excel to format the remaining text (e.g. chapter 2 to 40)

for example, for indentation
Section 1
[tab]Chapter One: Fractions. , 18
[tab tab]Equivalent fractions…,18
[tab tab]Adding and subtracting ,18

Currently, I am indenting each child block manually e.g. highlighting the child rows and clicking command ] on mac. With many chapters this takes a bit of effort.

then I have a similar question but to resolve space between text and page number (often many variations ) to have text[comma]page number
Section 1
[tab]Chapter One: Fractions,18
[tab tab]Equivalent fractions,18
[tab tab]Adding and subtracting,18

apologises if this not the correct place to post this question. I am very new to sublime and this forum.

0 Likes

#32

this thread mainly deals with the indentation engine which is based on syntax scopes, so if you want to make use of it, you would first need to work out how to distinguish (using regular expressions) which text should be indented at level 1, which at level 2 etc. Then you could just run a Reindent command (as opposed to paste special) to get everything indented nicely.

But this only deals with leading indentation, not spaces which are not at the beginning of lines. So if you have a desire to pretty-print/format your text, likely a custom Python plugin or external tool would be the way to go.

0 Likes

#33

A very quick question: How to set bracket indentation behavior in ST3?

0 Likes

#34

Thanks for sharing this detailed overview of indentation in ST3! It’s clear you’ve put a lot of thought into this, and I appreciate you taking the time to write it up.
I agree that the current indentation behavior is pretty good, but there are definitely some areas where it could be improved. For example, I think it would be helpful if the unIndentedLinePattern regex could be more flexible, so that it could be used to ignore more than just lines that contain only whitespace and comments.
I also think it would be helpful if there was a way to specify different indentation rules for different parts of a file. For example, I might want to use different indentation for functions and for classes.
Overall, I think the indentation in ST3 is a great foundation, and I’m excited to see how it continues to improve in the future.

0 Likes

#35

I started working on an indentation tool that will allow the user (or language package) to define some opening and closing patterns (supporting multiline pattern) between which the code will be indented. It should easily handle situations like instantiation since the closing semicolon of the port map can close (in terms of indentation region) the port map itself as well as the instantiation. Thus also the problem of switch/case statements can be solved.

0 Likes