Skip to content

Commit 5773e58

Browse files
authored
Merge pull request #39 from rwols/add-expand-to-whole-string
Add StringUtilitiesExpandStringCommand
2 parents 4d64de5 + fb8141a commit 5773e58

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Context.sublime-menu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
{ "command": "convert_camel_dash", "caption" : "Convert camelCase <-> Dash" },
1717
{ "command": "convert_pascal_underscores", "caption" : "Convert PascalCase <-> Underscores" },
1818
{ "caption": "-" },
19+
{ "command": "string_utilities_expand_string", "caption": "Expand Selection To Whole String" },
1920
{ "command": "convert_single_quotes_to_double", "caption" : "Convert Single Quotes To Double" },
2021
{ "command": "convert_double_quotes_to_single", "caption" : "Convert Double Quotes To Single" },
2122
{ "command": "url_encode", "caption" : "Encode as URL Notation" },

Default.sublime-commands

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
[
2+
{
3+
"caption": "String Utilities: Expand Selection to Whole String",
4+
"command": "string_utilities_expand_string"
5+
},
26
{
37
"caption": "String Utilities: Convert Tabs to Spaces",
48
"command": "convert_tabs_to_spaces"

stringutilities.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@
2424
def unichr(c):
2525
return chr(c)
2626

27+
class StringUtilitiesExpandStringCommand(sublime_plugin.TextCommand):
28+
"""If the region is contained in a string scope, expands the region to
29+
the whole string. If the region is not contained in a string scope, this
30+
command does nothing. It is applied to all regions in the current
31+
selection."""
32+
33+
def run(self, edit):
34+
for region in self.view.sel():
35+
self._run(edit, region)
36+
37+
def _run(self, edit, region):
38+
if (not self.view.match_selector(region.a, "string") or
39+
not self.view.match_selector(region.b, "string")):
40+
return
41+
selector = "string punctuation.definition.string"
42+
p = region.begin()
43+
while not self.view.match_selector(p, selector):
44+
p = self.view.find_by_class(p, False, sublime.CLASS_PUNCTUATION_START)
45+
q = region.end()
46+
while not self.view.match_selector(q, selector):
47+
# sublime.CLASS_PUNCTUATION_END is broken
48+
# this works too
49+
q = self.view.find_by_class(q, True, sublime.CLASS_PUNCTUATION_START)
50+
self.view.sel().add(sublime.Region(p, q + 1))
2751

2852
class ConvertTabsToSpacesCommand(sublime_plugin.TextCommand):
2953
#Convert Tabs To Spaces

0 commit comments

Comments
 (0)