Sublime Forum

Indent to a column


I want to modify the C++ sublime package to indent to a particular column. The coding standards where I work require methods to be formatted this way:



Is there a way to express this in the XML indentation tmpreferences? If not, would it be possible to express via a plugin? And is there any documentation for the XML stuff in the pristine packages directory.

(Note: I’m using the Linux alpha, not Sublime 1.)



I have spent a little more time thinking about this and I think I have a solution. I wanted to run it by the fora in hopes of getting a comment.

The solution I’ve come up with is to create a new indent function that behaves like the normal tab except in certain special circumstances. Any time I am within a parenthesis ("()") or angle bracket ("<>") block, I need to indent to the previous line’s column if there is anything on it, or to two indents below the previous line if not. I also need to do this whenever the user inserts a newline.

I should be able to add a small Python extension to my user directory to implement this with very little trouble. Any time I’m in a C++ file, I would override tab and return with my new commands, which would just search back to the previous (, ), <, >, or ;, and calculate the desired start position of the cursor from that point.

Do you think that this would work? Are there more efficient solutions I am overlooking? Thanks for your feedback.



That sounds like it could work. You can tell if you’re inside parens by seeing if your scope is “meta.parens.c meta.function.c source.c”, and then you can use expand_selection with the “scope” argument to set the beginning of the selection to just before the opening parenthesis. If it’s the last character on the line, indent the next line with two tabs (view.insert(edit,pt,"\t\t") will work even if you’re using spaces to indent); if not, use view.rowcol to get the column, add one to account for the width of the parenthesis, and indent the next line that far.
Good luck!



Great, and, just to put my further discoveries here:

In order to avoid reimplementing or breaking other tabbing behavior, I should probably make my plugin keep a setting describing when it is active. For example, it could be active in C++ or Java scopes only. I can also make a command within my plugin to return whether the plugin should be given the current point or selection. Using that command, I would add something like this to the keymap:

[code] { “keys”: “tab”], “command”: “indent_to_column”, “context”:

		{ "key": "should_indent_to_column", "operator": "equal", "operand": true },

The should_indent_to_column command checks whether I’m in the right scope. The indent to column command always indents selected lines to the correct column. This won’t behave quite right when there are parts of a region that are inside parens and parts outside. For that I’d want to apply my specialized indenting behavior to one part and the ordinary indenting behavior to the other. But for that I suspect we would need some more unified view of auto-formatting than I think we have available at the moment.



Hi JamesAguilar

Did you manage to get something to work ? Any chance you could “publish” your code ?
(I’ll no doubt get told off for “necro-ing” (or what ever it is) an old post, but finding a solution is like getting blood out of a stone, so telling’s off will have to be endured. :stuck_out_tongue_winking_eye:)

Cheers, Lozminda



Lozminda, no, I did not. The best way to deal with this problem today is to use clang-format or other language-specific formatter program and set it up with your desired formatting behavior.



Thanks for your reply, wicked.

Sorry you couldn’t get it to work. There’s a similar thread I posted on today and that’s given me a solution (tho I’ve yet to implement it)

I know your OP is a very long time ago, maybe there’s something useful in it ?

Thanks again for your reply, I thought it might be one of those posts that gets ignored occasionally, phew !

All the best :hugs: :unicorn:

Ps Sorry that’s terrible emoji action…