Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart Indenting of brackets (parenthesis) in sublime text 2

I've got a pretty sweet setup for editing and running maxscript from inside sublime text 2.

The one thing I've been wanting recently is to emulate or copy the behaviour of the curly brackets with the normal round brackets

EDIT: Sorry - Chrome decided I was finished editing there when I wasn't :(

If I type if (x) then { then enter I will get a nicely formatted block, with the caret now at the arrow

if(x) then {
    <-
}

but I cant find where sublime text is hiding it.

I want to copy this behaviour to the normal round brackets () instead of getting

if (x) then (
    <-)
like image 789
finlaybob Avatar asked Apr 04 '14 14:04

finlaybob


People also ask

How do I fix indentations in Sublime Text?

That's quite simple in Sublime. Just Ctrl+Shift+P (or Command+Shift+P on MacOS) to open the tools pallet, type reindent , and pick Indentation: Reindent Lines . It should reindent all the file you are in, just remember to save before running the command, or it may not appear.

How do you match braces in sublime?

Put the cursor on any on the opening or closing bracket and press Ctrl + m , will take your cursor to the respective closing or opening bracket.


1 Answers

I've found where it does this on curly brackets, it's simply in the default key bindings. I copied the section "keys": ["enter"] and replaced the regex with "(" instead of "{".

I also had to copy the built in AddLineInBraces.sublime-macro and add {"command": "left_delete" }, to it:

[
    {"command": "insert", "args": {"characters": "\n\n"} },
    {"command": "left_delete" },
    {"command": "move", "args": {"by": "lines", "forward": false} },
    {"command": "move_to", "args": {"to": "hardeol", "extend": false} },
    {"command": "reindent", "args": {"single_line": true} }
]

And make the enter keystroke call that macro if the regex matches. This gives the perfect result!

I've actually been doing less MXS the last while so haven't really been playing with it much.

Thanks to Ghoul Fool for the suggestions but after having a look I wanted a simpler solution.

In answer to FrozenKiwi, I have mashed together a few plugins from various places so I can send maxscript to Max from ST2. Here is a link for the heavy lifting. It does involve some work to get it running but is very handy indeed.

The rest is all just dribs and drabs of various ST2 goodness: shift+enter to evaluate selection, ctrl+e to evaluate all, lots (and lots) of macros e.g. "for" + tab will write format "var: %\n" (var as string)

I don't know if I can release it as a package as it's taken from quite a few different sources. I could check the licensing for each thing though I suppose.

My only remaining hurdle is not being able to redirect output from the maxscript listener to ST2, I've trawled the internet and nobody seems to have done it yet, and I'm not keen on trying it myself. I don't even know if Maxscript itself can be used - it might have to be a C++ plugin using the SDK - but my experience thus far with C++ plugins is minimal. I don't doubt it can be done though.

like image 168
finlaybob Avatar answered Oct 05 '22 02:10

finlaybob