Home Download Buy Blog Forum Support

Indent to a column

Indent to a column

Postby JamesAguilar on Wed Mar 09, 2011 10:19 pm

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:

Code: Select all
IfItsOnTheSameLine(param1,
                   param2);

IfItsOnADifferentLine(
    param1,
    param2);


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.)
JamesAguilar
 
Posts: 5
Joined: Wed Mar 09, 2011 10:08 pm

Re: Indent to a column

Postby JamesAguilar on Thu Mar 10, 2011 8:11 pm

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.
JamesAguilar
 
Posts: 5
Joined: Wed Mar 09, 2011 10:08 pm

Re: Indent to a column

Postby adzenith on Thu Mar 10, 2011 10:20 pm

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!
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Indent to a column

Postby JamesAguilar on Sun Mar 13, 2011 7:41 am

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: Select all
   { "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.
JamesAguilar
 
Posts: 5
Joined: Wed Mar 09, 2011 10:08 pm


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], subhaze and 13 guests