def run(self, edit, language="", undo=False, debug=True): view = self.view if (undo): string = view.substr(sublime.Region(0, view.size())) start = view.sel()[0].begin() end = view.sel()[0].end() result = expand_region_handler.undo(string, start, end, view.settings()) if (result): view.sel().clear() view.sel().add(sublime.Region(result["start"], result["end"])) return if not language: language = (_detect_language(view, "ExpandRegion") or _detect_language(view, "ExpandRegionFallback")) if debug: print("Determined language: '{0}'".format(language)) for region in view.sel(): string = view.substr(sublime.Region(0, view.size())) start = region.begin() end = region.end() result = expand_region_handler.expand(string, start, end, language, view.settings()) if result: view.sel().add(sublime.Region(result["start"], result["end"])) if debug: print("startIndex: {0}, endIndex: {1}, type: {2}".format(result["start"], result["end"], result["type"]))
def run(self, edit, undo=False, debug=True): if (undo): string = self.view.substr(sublime.Region(0, self.view.size())) start = self.view.sel()[0].begin() end = self.view.sel()[0].end() result = expand_region_handler.undo(string, start, end, self.view.settings()) if (result): self.view.sel().clear() self.view.sel().add(sublime.Region(result["start"], result["end"])) return extension = "" if (self.view.file_name()): name, fileex = os.path.splitext(self.view.file_name()) extension = fileex[1:] for region in self.view.sel(): string = self.view.substr(sublime.Region(0, self.view.size())) start = region.begin() end = region.end() result = expand_region_handler.expand(string, start, end, extension, self.view.settings()) if result: self.view.sel().add(sublime.Region(result["start"], result["end"])) if debug: print("startIndex: {0}, endIndex: {1}, type: {2}".format(result["start"], result["end"], result["type"]))
def run(self, edit, language="", undo=False, debug=True): view = self.view utils.is_debug_enabled = debug if (undo): string = view.substr(sublime.Region(0, view.size())) start = view.sel()[0].begin() end = view.sel()[0].end() result = expand_region_handler.undo(string, start, end, view.settings()) if (result): view.sel().clear() view.sel().add(sublime.Region(result["start"], result["end"])) return if not language: language = (_detect_language(view, "ExpandRegion") or _detect_language(view, "ExpandRegionFallback")) if debug: print("ExpandRegion, ExpandRegion.py, Determined language: '{0}'". format(language)) new_regions = [] is_region_expanded = True for region in self.view.sel(): string = self.view.substr(sublime.Region(0, self.view.size())) start = region.begin() end = region.end() result = expand_region_handler.expand(string, start, end, language, self.view.settings()) if result: new_regions.append( sublime.Region(result["start"], result["end"])) if debug: print( "ExpandRegion, ExpandRegion.py, startIndex: {0}, endIndex: {1}, type: {2}" .format(result["start"], result["end"], result["type"])) else: # if there is no result, call the built-in sublime text expand_selection to scope command is_region_expanded = False if is_region_expanded: # replace the selections with the new regions view.sel().clear() for sel in new_regions: view.sel().add(sel) settings = sublime.load_settings("ExpandRegion.sublime-settings") do_force_enable_soft_undo = settings.get( "force_soft_undo_integration") if do_force_enable_soft_undo: _force_enable_soft_undo(view, edit, new_regions) else: if debug: print( "ExpandRegion, ExpandRegion.py, calling Sublime Text expand_selection to scope..." ) view.run_command("expand_selection", {"to": "scope"})
def run(self, edit, debug=False): region = self.view.sel()[0] string = self.view.substr(sublime.Region(0, self.view.size())) start = region.begin() end = region.end() result = expand_region_handler.expand(string, start, end) if result: self.view.sel().add(sublime.Region(result["start"], result["end"])) if debug: print("startIndex: {0}, endIndex: {1}, type: {2}".format( result["start"], result["end"], result["type"]))
def run(self, edit, debug=False): region = self.view.sel()[0] string = self.view.substr(sublime.Region(0, self.view.size())) start = region.begin() end = region.end() result = expand_region_handler.expand(string, start, end) if result: self.view.sel().add(sublime.Region(result["start"], result["end"])) if debug: print( "startIndex: {0}, endIndex: {1}, type: {2}".format(result["start"], result["end"], result["type"]) )
def run(self, edit, language="", undo=False, debug=True): view = self.view if (undo): string = view.substr(sublime.Region(0, view.size())) start = view.sel()[0].begin() end = view.sel()[0].end() result = expand_region_handler.undo(string, start, end, view.settings()) if (result): view.sel().clear() view.sel().add(sublime.Region(result["start"], result["end"])) return if not language: language = (_detect_language(view, "ExpandRegion") or _detect_language(view, "ExpandRegionFallback")) if debug: print("Determined language: '{0}'".format(language)) new_regions = [] for region in self.view.sel(): string = self.view.substr(sublime.Region(0, self.view.size())) start = region.begin() end = region.end() result = expand_region_handler.expand(string, start, end, language, self.view.settings()) if result: new_regions.append( sublime.Region(result["start"], result["end"])) if debug: print("startIndex: {0}, endIndex: {1}, type: {2}".format( result["start"], result["end"], result["type"])) else: # if there is no result, keep the current region new_regions.append(region) # replace the selections with the new regions view.sel().clear() for sel in new_regions: view.sel().add(sel) settings = sublime.load_settings("ExpandRegion.sublime-settings") do_force_enable_soft_undo = settings.get("force_soft_undo_integration") if do_force_enable_soft_undo: _force_enable_soft_undo(view, edit, new_regions)
def run(self, edit, language="", undo=False, debug=True): view = self.view if (undo): string = view.substr(sublime.Region(0, view.size())) start = view.sel()[0].begin() end = view.sel()[0].end() result = expand_region_handler.undo(string, start, end, view.settings()) if (result): view.sel().clear() view.sel().add(sublime.Region(result["start"], result["end"])) return if not language: language = (_detect_language(view, "ExpandRegion") or _detect_language(view, "ExpandRegionFallback")) if debug: print("Determined language: '{0}'".format(language)) new_regions = [] for region in self.view.sel(): string = self.view.substr(sublime.Region(0, self.view.size())) start = region.begin() end = region.end() result = expand_region_handler.expand(string, start, end, language, self.view.settings()) if result: new_regions.append(sublime.Region(result["start"], result["end"])) if debug: print("startIndex: {0}, endIndex: {1}, type: {2}".format(result["start"], result["end"], result["type"])) else: # if there is no result, keep the current region new_regions.append(region) # replace the selections with the new regions view.sel().clear() for sel in new_regions: view.sel().add(sel) settings = sublime.load_settings("ExpandRegion.sublime-settings") do_force_enable_soft_undo = settings.get("force_soft_undo_integration") if do_force_enable_soft_undo: _force_enable_soft_undo(view, edit, new_regions)
def test_word (self): self.assertEqual(expand_region_handler.expand(self.string1, 7, 7), {"start": 6, "end": 9, "string": "bar", "type": "word"})
def test_fix_look_back (self): self.assertEqual(expand_region_handler.expand(self.string2, 32, 32), {"start": 12, "end": 35, "string": "foo.indexOf('bar') > -1", "type": "symbol"})
def test_look_ahead_dont_hang (self): testString = "( " self.assertEqual(expand_region_handler.expand(testString, 2, 2), None)
def test_look_back_dont_hang (self): testString = " " self.assertEqual(expand_region_handler.expand(testString, 1, 2), None)
def test_symbol_inner (self): self.assertEqual(expand_region_handler.expand(self.string1, 1, 10), {"start": 1, "end": 16, "string": "\"foo bar\" + \"x\"", "type": "symbol"})
def test_quotes_outer (self): result = expand_region_handler.expand(self.string1, 2, 9); self.assertEqual(result["start"], 1) self.assertEqual(result["end"], 10) self.assertEqual(result["string"], "\"foo bar\"") self.assertEqual(result["type"], "quotes")
def test_quotes_inner (self): result = expand_region_handler.expand(self.string1, 6, 9); self.assertEqual(result["start"], 2) self.assertEqual(result["end"], 9) self.assertEqual(result["string"], "foo bar") self.assertEqual(result["type"], "quotes")
def test_word (self): result = expand_region_handler.expand(self.string1, 7, 7); self.assertEqual(result["start"], 6) self.assertEqual(result["end"], 9) self.assertEqual(result["string"], "bar") self.assertEqual(result["type"], "word")
def test_quotes_inner (self): self.assertEqual(expand_region_handler.expand(self.string1, 6, 9), {"start": 2, "end": 9, "string": "foo bar", "type": "quotes"})
def test_quotes_outer (self): self.assertEqual(expand_region_handler.expand(self.string1, 2, 9), {"start": 1, "end": 10, "string": "\"foo bar\"", "type": "quotes"})
def test_symbol_inner (self): result = expand_region_handler.expand(self.string1, 1, 10); self.assertEqual(result["start"], 1) self.assertEqual(result["end"], 16) self.assertEqual(result["string"], "\"foo bar\" + \"x\"") self.assertEqual(result["type"], "symbol")
def test_dont_expand_to_dots (self): self.assertEqual(expand_region_handler.expand(self.string2, 2, 5), {"start": 1, "end": 10, "string": " foo.bar ", "type": "quotes"})
def test_dont_expand_to_dots (self): result = expand_region_handler.expand(self.string2, 2, 5); self.assertEqual(result["start"], 1) self.assertEqual(result["end"], 10) self.assertEqual(result["string"], " foo.bar ") self.assertEqual(result["type"], "quotes")