Hello. I’m trying to do something fairly straight-forward. If there is nothing selected in the view, I want to insert a couple of words at the cursor position, but then extend the selection to include these new words.
The following code works in a round-about way, by creating a Region of a known size and adding it to sel(). If I don’t specify the size (13) then the text is added but not selected .
How can I extend the current cursor/ selection to include my new text (without specifying a length for my region) please? Andy.
[code]def run(self, edit):
sels = self.view.sel() # sels: RegionSet
for a_sel in sels:
# handle multi-select
pass
if len(sels) != 1: # cancel if multi-select
return
the_region = sels[0]
if the_region.empty(): # that is, begin() == end()
# print 'nothing selected'
self.view.sel().clear()
my_edit = self.view.begin_edit()
new_region = sublime.Region(the_region.begin(), the_region.end()+13)
self.view.insert(my_edit, new_region.begin(), ' after cursor')
self.view.sel().add(new_region)
self.view.end_edit(my_edit)
print '#' + self.view.substr(new_region) + '#'
return[/code]