Skip to content

Commit

Permalink
Support mapping to tab key. Closes atombender#2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Staubo committed Sep 7, 2011
1 parent cfcc60f commit fe8c1a8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
27 changes: 26 additions & 1 deletion README.md
Expand Up @@ -23,7 +23,7 @@ Drop the entire folder in Sublime's `Packages` folder. You can do this using `gi
$ cd .../Packages # Whatever the location is
$ git clone git://github.com/alexstaubo/sublime_text_alternative_autocompletion.git

Add something like the following to your key bindings:
To map to the escape key, like TextMate:

{ "keys": ["escape"], "command": "alternative_autocomplete", "context":
[
Expand All @@ -38,6 +38,31 @@ Add something like the following to your key bindings:
]
},

To map to the tab key:

{ "keys": ["tab"], "command": "alternative_autocomplete", "args": {"default": "\t"}, "context":
[
{ "key": "num_selections", "operator": "equal", "operand": 1 },
{ "key": "overlay_visible", "operator": "equal", "operand": false }
]
},
{ "keys": ["shift+tab"], "command": "alternative_autocomplete", "args": {"cycle": "previous"}, "context":
[
{ "key": "num_selections", "operator": "equal", "operand": 1 },
{ "key": "overlay_visible", "operator": "equal", "operand": false }
]
},
{ "keys": ["tab"], "command": "indent", "context":
[
{ "key": "text", "operator": "regex_contains", "operand": "\n" }
]
},
{ "keys": ["shift+tab"], "command": "unindent", "context":
[
{ "key": "text", "operator": "regex_contains", "operand": "\n" }
]
},

License
-------

Expand Down
13 changes: 9 additions & 4 deletions alternative_autocomplete.py
Expand Up @@ -32,18 +32,20 @@ class AlternativeAutocompleteCommand(sublime_plugin.TextCommand):
candidates = []
previous_completion = None

def run(self, edit, cycle = 'next'):
view = self.view
self.insert_completion(view.sel()[0].b, view.substr(sublime.Region(0, view.size())), cycle)
def run(self, edit, cycle = 'next', default = ''):
self.edit = edit
self.insert_completion(self.view.sel()[0].b,
self.view.substr(sublime.Region(0, self.view.size())), cycle, default)

def insert_completion(self, position, text, cycle):
def insert_completion(self, position, text, cycle, default):
prefix_match = re.search(r'([\w\d_]+)\Z', text[0:position], re.M | re.U)
if prefix_match:
current_word_match = re.search(r'^([\w\d_]+)', text[prefix_match.start(1):], re.M | re.U)
if current_word_match:
current_word = current_word_match.group(1)
else:
current_word = None

prefix = prefix_match.group(1)
if self.previous_completion is None or prefix != self.previous_completion:
self.previous_completion = None
Expand All @@ -64,6 +66,9 @@ def insert_completion(self, position, text, cycle):
self.view.insert(edit, prefix_match.start(1), completion)
self.view.end_edit(edit)
self.previous_completion = completion
else:
if default and default != '':
self.view.insert(self.edit, position, default)

def find_candidates(self, prefix, position, text):
candidates = []
Expand Down

0 comments on commit fe8c1a8

Please sign in to comment.