Skip to content

Commit

Permalink
Initial implementation for stop auto_match_enabled to insert a
Browse files Browse the repository at this point in the history
evandrocoan/ITE#142
matching bracket when it is already there
  • Loading branch information
evandrocoan committed Sep 20, 2019
1 parent c9d4ba5 commit dcc789a
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Default (Linux).sublime-keymap.hide
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["\""], "command": "insert_selection_match", "args": {"start": "\"", "end": "\""}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\"", "match_all": true },
]
},
{ "keys": ["\""], "command": "insert_selection_match", "args": {"start": "\"", "end": "\""}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\"", "match_all": true },
]
},
{ "keys": ["\""], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -578,6 +592,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["'"], "command": "insert_selection_match", "args": {"start": "'", "end": "'"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "'", "match_all": true },
]
},
{ "keys": ["'"], "command": "insert_selection_match", "args": {"start": "'", "end": "'"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "'", "match_all": true },
]
},
{ "keys": ["'"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -613,6 +641,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["("], "command": "insert_selection_match", "args": {"start": "(", "end": ")"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\\)", "match_all": true },
]
},
{ "keys": ["("], "command": "insert_selection_match", "args": {"start": "(", "end": ")"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\(", "match_all": true },
]
},
{ "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -644,6 +686,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["["], "command": "insert_selection_match", "args": {"start": "[", "end": "]"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\\]", "match_all": true },
]
},
{ "keys": ["["], "command": "insert_selection_match", "args": {"start": "[", "end": "]"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\[", "match_all": true },
]
},
{ "keys": ["]"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -683,6 +739,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["{"], "command": "insert_selection_match", "args": {"start": "{", "end": "}"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\\}", "match_all": true },
]
},
{ "keys": ["{"], "command": "insert_selection_match", "args": {"start": "{", "end": "}"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{", "match_all": true },
]
},
{ "keys": ["}"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down
70 changes: 70 additions & 0 deletions Default (OSX).sublime-keymap.hide
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["\""], "command": "insert_selection_match", "args": {"start": "\"", "end": "\""}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\"", "match_all": true },
]
},
{ "keys": ["\""], "command": "insert_selection_match", "args": {"start": "\"", "end": "\""}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\"", "match_all": true },
]
},
{ "keys": ["\""], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -578,6 +592,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["'"], "command": "insert_selection_match", "args": {"start": "'", "end": "'"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "'", "match_all": true },
]
},
{ "keys": ["'"], "command": "insert_selection_match", "args": {"start": "'", "end": "'"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "'", "match_all": true },
]
},
{ "keys": ["'"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -613,6 +641,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["("], "command": "insert_selection_match", "args": {"start": "(", "end": ")"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\\)", "match_all": true },
]
},
{ "keys": ["("], "command": "insert_selection_match", "args": {"start": "(", "end": ")"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\(", "match_all": true },
]
},
{ "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -644,6 +686,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["["], "command": "insert_selection_match", "args": {"start": "[", "end": "]"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\\]", "match_all": true },
]
},
{ "keys": ["["], "command": "insert_selection_match", "args": {"start": "[", "end": "]"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\[", "match_all": true },
]
},
{ "keys": ["]"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -683,6 +739,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["{"], "command": "insert_selection_match", "args": {"start": "{", "end": "}"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\\}", "match_all": true },
]
},
{ "keys": ["{"], "command": "insert_selection_match", "args": {"start": "{", "end": "}"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{", "match_all": true },
]
},
{ "keys": ["}"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down
70 changes: 70 additions & 0 deletions Default (Windows).sublime-keymap.hide
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["\""], "command": "insert_selection_match", "args": {"start": "\"", "end": "\""}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\"", "match_all": true },
]
},
{ "keys": ["\""], "command": "insert_selection_match", "args": {"start": "\"", "end": "\""}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\"", "match_all": true },
]
},
{ "keys": ["\""], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -578,6 +592,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["'"], "command": "insert_selection_match", "args": {"start": "'", "end": "'"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "'", "match_all": true },
]
},
{ "keys": ["'"], "command": "insert_selection_match", "args": {"start": "'", "end": "'"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "'", "match_all": true },
]
},
{ "keys": ["'"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -613,6 +641,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["("], "command": "insert_selection_match", "args": {"start": "(", "end": ")"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\\)", "match_all": true },
]
},
{ "keys": ["("], "command": "insert_selection_match", "args": {"start": "(", "end": ")"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\(", "match_all": true },
]
},
{ "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -644,6 +686,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["["], "command": "insert_selection_match", "args": {"start": "[", "end": "]"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\\]", "match_all": true },
]
},
{ "keys": ["["], "command": "insert_selection_match", "args": {"start": "[", "end": "]"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\[", "match_all": true },
]
},
{ "keys": ["]"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down Expand Up @@ -683,6 +739,20 @@
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
]
},
{ "keys": ["{"], "command": "insert_selection_match", "args": {"start": "{", "end": "}"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "\\}", "match_all": true },
]
},
{ "keys": ["{"], "command": "insert_selection_match", "args": {"start": "{", "end": "}"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{", "match_all": true },
]
},
{ "keys": ["}"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
Expand Down
29 changes: 29 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,32 @@ def run(self):

def is_enabled(self):
return self.window.active_view() is not None


class InsertSelectionMatchCommand(sublime_plugin.TextCommand):
def run(self, edit, start, end):
# import time; print( time.time(), 'start', start, 'end', end)
view = self.view
selections = view.sel()

for selection in selections:
first_char = sublime.Region( selection.begin() - 1, selection.begin())
first_char = view.substr(first_char)
last_char = sublime.Region( selection.end() + 1, selection.end())
last_char = view.substr(last_char)
# import time; print( time.time(), 'first_char', first_char, 'last_char', last_char )

if first_char == start and last_char == end:
view.insert( edit, selection.begin(), start )
view.insert( edit, selection.end() + 1, end )

elif first_char != start and last_char != end:
view.insert( edit, selection.begin(), start )
view.insert( edit, selection.end() + 1, end )

elif first_char != start:
view.insert( edit, selection.begin(), start )

else:
view.insert( edit, selection.end(), end )

0 comments on commit dcc789a

Please sign in to comment.