Sublime Forum

ST3 eats right parentheses, brackets, quotes

#1

ST3 has an incredibly annoying habit of eating right parentheses, brackets, and quotation marks, resulting in unbalanced expressions.

Here is an example:

Begin with
foo(1, 2)
I want to edit the term 2 to be list(2, 3). When I carry out the edit the right hand parenthesis is eaten, resulting in unbalanced parentheses:
foo(1, 2) =>
foo(1, list(2, 3)
Same result with brackets:
foo[1, 2] =>
foo[1, list[2, 3]
Same result with single-quotes:
foo’1, 2’ =>
foo’1, list’2, 3’
Same result with double-quotes:
foo"1, 2" =>
foo"1, list"2, 3"

Is there a fix for this bug? If not, please provide one ASAP.

0 Likes

#2

This behavior is caused by the auto_match_enabled and is meant to prevent to accidentally add extra closing brackets, but it’s a kind of stupid in terms of detecting unbalanced brackets.

The easiest way to disable this “habit” without loosing auto-matching is to add some key bindings to User\Default.sublime-keybindings.

	{ "keys": [")"], "command": "insert", "args": {"characters": ")"} },
	{ "keys": ["]"], "command": "insert", "args": {"characters": "]"} },
	{ "keys": ["}"], "command": "insert", "args": {"characters": "}"} },

	{ "keys": ["'"], "command": "insert", "args": {"characters": "'"}, "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": "^'", "match_all": true },
			{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
			{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true },
		]
	},
	{ "keys": ["\""], "command": "insert", "args": {"characters": "\""}, "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": "^\"", "match_all": true },
			{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
			{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true },
		]
	},

Be sure to duplicate the context for quotations from the default file to only overwrite the default “move” command.

1 Like

#3

@deathaxe Thanks very much for this. However, your comment goes above my head.
The contexts that you show–are they defaults that should be copied and pasted? And how does a move command come into the picture, as we’re talking here about inserting characters?
I’m, to put it mildly, not a heavy hacker of ST3 files, so I may be unfamiliar with the terminology.

0 Likes

#4

@deathaxe Although I don’t understand all the context stuff I copied and pasted your code into my Default (Windows).sublime-keymap and it works!

0 Likes

#5

As deadaxe mentioned (between the lines) you can alternativly add this to your settings file:

    // Controls auto pairing of quotes, brackets etc
    "auto_match_enabled": false,

However this also has other side effects (like the not inserting the closing brace if you type a opening one) . Hence you can just overwrite the keybinding, which move over a closing brace to always insert one using the provided keybinding.

0 Likes

#6

Yes, contexts are copied over from the default files.
The context is basically nothing else than a list of checks being performed to decide, whether the command is called or not, if the keys are pressed.

0 Likes