Skip to content

Instantly share code, notes, and snippets.

@skuroda
Last active December 14, 2015 15:09
Show Gist options
  • Save skuroda/5105635 to your computer and use it in GitHub Desktop.
Save skuroda/5105635 to your computer and use it in GitHub Desktop.
[
{ "keys": ["ctrl+p"], "command": "previous_keyword_match", "context":
[
{ "key": "auto_complete_visible", "operator": "equal", "operand": true }
]
},
{ "keys": ["ctrl+n"], "command": "next_keyword_match" }
]
import sublime_plugin
class PreviousKeywordMatchCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
initial_word_region = view.word(view.sel()[0])
initial_pos = initial_word_region.a
word = view.substr(initial_word_region)
wrapped = False
position = initial_word_region.begin() - 1
size_checked = 0
while size_checked < 5000 and not (wrapped and position < initial_pos):
compare_word_region = view.word(position)
compare_word = view.substr(compare_word_region)
if compare_word.startswith(word):
view.replace(edit, initial_word_region, compare_word)
break
position = compare_word_region.begin() - 1
size_checked += compare_word_region.size()
if position == -1:
position = view.size()
wrapped = True
class NextKeywordMatchCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
initial_word_region = view.word(view.sel()[0])
initial_pos = initial_word_region.a
word = view.substr(initial_word_region)
wrapped = False
position = initial_word_region.end() + 1
size_checked = 0
while size_checked < 5000 and not (wrapped and position > initial_pos):
compare_word_region = view.word(position)
compare_word = view.substr(compare_word_region)
if compare_word.startswith(word):
view.replace(edit, initial_word_region, compare_word)
break
position = compare_word_region.end() + 1
size_checked += compare_word_region.size()
if position > view.size():
position = 0
wrapped = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment