From dcc789ae0bbb29171ff2c61a7472132bbc0ed8e8 Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Fri, 20 Sep 2019 00:38:42 -0300 Subject: [PATCH] Initial implementation for stop `auto_match_enabled` to insert a https://github.com/evandrocoan/ITE/issues/142 matching bracket when it is already there --- Default (Linux).sublime-keymap.hide | 70 +++++++++++++++++++++++++++ Default (OSX).sublime-keymap.hide | 70 +++++++++++++++++++++++++++ Default (Windows).sublime-keymap.hide | 70 +++++++++++++++++++++++++++ settings.py | 29 +++++++++++ 4 files changed, 239 insertions(+) diff --git a/Default (Linux).sublime-keymap.hide b/Default (Linux).sublime-keymap.hide index 6746489..b97c74d 100644 --- a/Default (Linux).sublime-keymap.hide +++ b/Default (Linux).sublime-keymap.hide @@ -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 }, @@ -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 }, @@ -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 }, @@ -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 }, @@ -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 }, diff --git a/Default (OSX).sublime-keymap.hide b/Default (OSX).sublime-keymap.hide index 6746489..b97c74d 100644 --- a/Default (OSX).sublime-keymap.hide +++ b/Default (OSX).sublime-keymap.hide @@ -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 }, @@ -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 }, @@ -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 }, @@ -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 }, @@ -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 }, diff --git a/Default (Windows).sublime-keymap.hide b/Default (Windows).sublime-keymap.hide index 6746489..b97c74d 100644 --- a/Default (Windows).sublime-keymap.hide +++ b/Default (Windows).sublime-keymap.hide @@ -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 }, @@ -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 }, @@ -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 }, @@ -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 }, @@ -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 }, diff --git a/settings.py b/settings.py index b33cf4c..075d025 100644 --- a/settings.py +++ b/settings.py @@ -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 ) +