Sublime Forum

Some help with regex for custom Key Bindings

#1

I’m trying to tweak Sublime Text to accommodate my coding style a little bit better.

To start with, I’m trying to insert a space before the angle brackets when I’m writing a function.

//given the following typed out code:
function( args, args, ... )

//a space is inserted after typing only "{", and with 'auto_match_enabled' = true the would be:
function( args, args, ... ) {}

I know that I can grab the keybindings code from line 372-379 of the default keybindings:

// Auto-pair curly brackets { "keys": ["{"], "command": "insert_snippet", "args": {"contents": "{$0}"}, "context": [ { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|$)", "match_all": true } ] },
And modify it to make this work.

I set up a custom settings variable “custom_indent_enabled” in my user settings, and added that for testing:

// Auto-pair curly brackets { "keys": ["{"], "command": "insert_snippet", "args": {"contents": " {$0}"}, "context": [ { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true }, { "key": "setting.custom_indent_enabled", "operator": "equal", "operand": true }, { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true }, { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|$)", "match_all": true }, { "key": "preceding_text", "operator": "regex_contains", "operand": "<regex magic>", "match_all": true } ] }I’m fairly confident the above code will do what I want, assuming I add the correct regex code on the last key.

My regex is not very good, so I was wondering if someone could answer these questions for me:

[ol]
[li]What does the regex code in the ‘following_text’ section test for specifically?[/li]
[li]What would the regex for the preceding_text be, assuming that the following conditions need to be met:
[ul]
[]the preceding text contains “function” followed by “(” followed by “)”, ignoring any characters in-between.[/li]
[li]the preceding text does not already contain a space[/li]
[li]the above consecutive text snippets should not be inside of quotes.[/li]
[li]The preceding text should be checked up to the first preceding semicolon.[/li][/ul][/
:m][/ol]

0 Likes

#2

A friend helped me out a bit, and we came up with the following:

[code] { “keys”: “{”], “command”: “insert_snippet”, “args”: {“contents”: " {$0}"}, “context”:

		{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
		{ "key": "setting.custom_indent_enabled", "operator": "equal", "operand": true },
		{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
		{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|$)", "match_all": true },
		{ "key": "preceding_text", "operator": "regex_contains", "operand": "function.*\\(.*\\)$", "match_all": true }
	]
}[/code]

It’s not quite perfect since it seems the regex only looks at one line at a time but I think it should work for the most part.

0 Likes