sublimehq / sublime_text

Issue tracker for Sublime Text
https://www.sublimetext.com
804 stars 39 forks source link

Stop `auto_match_enabled` to insert a matching bracket when it is already there #2982

Open evandrocoan opened 5 years ago

evandrocoan commented 5 years ago
  1. Set auto_match_enabled true.
  2. When you see |, it is the caret/cursor.
  3. When you see [ showing up, is because I typed it.
  4. When you see ], it is because Sublime Text inserted it, or it was already there before I typed [

Expected behavior

  1. |]
  2. [|]

Actual behavior

  1. |]
  2. [|]]

Environment

Demonstration

bug

Related threads

  1. Snippet substitution stops working when other snippet is inserted, such as auto_match_enabled and '[' #1017
FichteFoll commented 5 years ago

I can pretty much guarantee you that this is going to be a wontfix, because this behavior has stayed the same for over 7 years now (since I started using ST2). It can be configured by the user anyway.

I also strongly oppose this change, because I regularly encounter situations where you are tying nested brackets, e.g. when typing [[0], [0, 1]], which would mean that the inner closing brackets would not be automatched because you'd be consistently typing right before ]. It would also conflict with the current behavior of ] moving over a following ] instead of inserting one, as the opening bracket wouldn't have caused a closing ] to appear. And if behavior was removed, too, you might as well just disable auto-matching.

Just my 2 cents.

evandrocoan commented 5 years ago

Well, I just exposed a simple problem but did not discoursed about how to actually fix and how it would affect other situations. A simple fix I did on my installation for now was this:

{ "key": "following_text", "operator": "not_regex_match", "operand": ".*\\].*", "match_all": true },

With this, I dumbly cut the insertion of the matching bracket, if any closing bracket was found. Indeed, this is not ideal. A more elegant solution would be to detect whether there are unbalanced brackets (this should be context aware, for example, if I inside a comment, and I am typing, this should isolate itself into that comment. But if I am on a code block, it should ignore comments and count all non-commented brackets and decide whether a matching bracket is missing/required. Then, fill it/fulfill it or do nothing.

Now I discoursed a little about its implementation, I think a plugin could do that "smart" behavior by overriding the default brackets keys [, (, etc. Or either this could be something a Language Server (LSP) could do/provide).