I’m trying to add a functionality to “bubble” the current selection up/down/left/right through the text via the keyboard.
I’ve surprised myself by getting everything to work, except that I don’t know how to preserve the xpos
value of my region as I bubble it around the text, which is necessary for full functionality. I tried this way, but it’s not working and I don’t know if the fault is with my program logic or with my misusing the API (the following code assumes a single nonempty selection):
def paste_and_select(view, xpos):
start = view.sel()[0].begin()
view.run_command("paste")
pasted_region = sublime.Region(start, view.sel()[0].b)
pasted_region.xpos = xpos
view.sel().add(pasted_region)
class GrabUp(sublime_plugin.TextCommand):
def run(self, edit):
xpos = self.view.sel()[0].xpos
self.view.run_command("cut")
self.view.run_command("move", {"by": "lines", "forward": False})
paste_and_select(self.view, xpos)
class GrabDown(sublime_plugin.TextCommand):
def run(self, edit):
xpos = regions[0].xpos
self.view.run_command("cut")
self.view.run_command("move", {"by": "lines", "forward": True})
paste_and_select(view, xpos)
class GrabLeft(sublime_plugin.TextCommand):
def run(self, edit):
xpos = self.view.sel()[0].xpos
self.view.run_command("cut")
self.view.run_command("move", {"by": "characters", "forward": False})
paste_and_select(self.view, xpos)
class GrabRight(sublime_plugin.TextCommand):
def run(self, edit):
xpos = self.view.sel()[0].xpos
self.view.run_command("cut")
self.view.run_command("move", {"by": "characters", "forward": True})
paste_and_select(self.view, xpos)
Currently it seems to completely ignore my efforts to set xpos
…