Skip to content

Instantly share code, notes, and snippets.

@kaste
Last active May 25, 2017 12:56
Show Gist options
  • Save kaste/5508286 to your computer and use it in GitHub Desktop.
Save kaste/5508286 to your computer and use it in GitHub Desktop.
Improved? find_under_expand for sublime

initial version: http://www.sublimetext.com/forum/viewtopic.php?f=5&t=10821

"It's handy if you're a terrible human being and use single-letter variable names other than [u-z] (because there's really no better way to refer to texture coordinates and vector components than with UVW and XYZ), which ⌘D will punish you for doing by finding every instance of that character. So, your best bet is to just not be an absolutely loathsome person who uses single letter variables (like all you horrible web developers do), but if you happen to be someone the entire world should despise because you absolutely cannot resist the allure of single letter variables like the absolutely awful depth-spawn that you might be, then this plugin is probably for you.

It's also handy if you have variable names that share words, like event and event_name and event_id, in which case ⌘D will unintentionally hate you and you're not an awful person, just unlucky. In those cases, it's probably fairly useful, though chances are you could ⌘K⌘D around the false positives without a lot of problems."

# bounded_grab_next.py
import sublime, sublime_plugin, re
class BoundedGrabNextCommand(sublime_plugin.TextCommand):
def __init__(self, p):
super(BoundedGrabNextCommand, self).__init__(p)
self._last_word = None
self._last_regex = None
#/__init__
def run(self, edit, skip_last = False):
last_selection = None
view = self.view
selections = view.sel()
last_selection = selections[-1]
# If the last selection is empty, expand to the current word. In other
# words, just do the usual Cmd+D operation.
if last_selection.a == last_selection.b:
view.window().run_command('find_under_expand')
return
#/if
regex = None
word = view.substr(last_selection)
if self._last_word == word:
regex = self._last_regex
else:
re_word = re.escape(word)
if re.match("^\\w+$", word):
regex = "\\b{0}\\b".format(re_word)
else:
regex = re_word
#/if
#/if
if skip_last:
selections.subtract(last_selection)
#/if
last_point = max(last_selection.a, last_selection.b)
next_find = view.find(regex, last_point)
if next_find is None:
next_find = view.find(regex, 0)
while next_find and selections.contains(next_find):
next_find = view.find(regex, next_find.b)
#/while
#/if
if next_find:
selections.add(next_find)
view.show(next_find)
#/if
self._last_word = word
self._last_regex = regex
#/run
#/BoundedGrabNextCommand
[
{ "keys": ["ctrl+d"], "command": "bounded_grab_next",
"args": {"skip_last": false}
},
{ "keys": ["ctrl+k", "ctrl+d"], "command": "bounded_grab_next",
"args": {"skip_last": true}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment