-
-
Save robertcollier4/5276374 to your computer and use it in GitHub Desktop.
class ExpandSelectionToDelimsCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
begindelims = ["\"", "\'", "(", "<", "[", "{"] | |
enddelims = ["\"", "\'", ")", ">", "]", "}"] | |
view = self.view | |
oldSelRegions = list(view.sel()) | |
for thisregion in oldSelRegions: | |
thisRegionBegin = thisregion.begin() - 1 | |
thisRegionEnd = thisregion.end() | |
if( (thisregion.begin() != thisRegionEnd) and (view.substr(thisRegionBegin) in begindelims) ): | |
thisRegionBegin -= 1 | |
while ((view.substr(thisRegionBegin) not in begindelims) and (thisRegionBegin >= 0)): | |
thisRegionBegin -= 1 | |
thisRegionBegin += 1 | |
if( (thisregion.begin() != thisRegionEnd) and (view.substr(thisRegionEnd) in enddelims) ): | |
thisRegionEnd += 1 | |
while((view.substr(thisRegionEnd) not in enddelims) and (thisRegionEnd < view.size())): | |
thisRegionEnd += 1 | |
view.sel().add(sublime.Region(thisRegionBegin, thisRegionEnd)) |
It turns out that the original Gist is unncessary since Sublime has a built-in command for "expand_selection" which takes various arguments as shown in the following:
Add to .sublime-menu
{
"caption": "Selection Expand",
"id": "selection",
"children":
[
{ "command": "expand_selection_to_whitespace", "caption": "Expand Selection to Whitespace" },
{ "command": "expand_selection", "args": {"to": "line"}, "caption": "Expand Selection to Line" },
{ "command": "find_under_expand", "caption": "Expand Selection to Word" },
{ "command": "expand_selection_to_paragraph", "caption": "Expand Selection to Paragraph" },
{ "command": "expand_selection", "args": {"to": "scope"}, "caption": "Expand Selection to Scope" },
{ "command": "expand_selection", "args": {"to": "brackets"}, "caption": "Expand Selection to Brackets" },
{ "command": "expand_selection", "args": {"to": "indentation"}, "caption": "Expand Selection to Indentation" },
{ "command": "expand_selection", "args": {"to": "tag"}, "caption": "Expand Selection to Tag" },
{ "command": "invert_selection" },
]
},
Add to .sublime-keymap
{ "keys": ["ctrl+shift+q"], "command": "expand_selection_to_quotes"},
{ "keys": ["ctrl+shift+b"], "command": "expand_selection", "args": {"to": "brackets"} },
{ "keys": ["ctrl+space"], "command": "expand_selection_to_delims" },
This does not work ok, after the first expansion it expands to wrong (non mirrored) delimiters.