I have a javascript formatting plugin that passes the contents of the buffer through a python library then calls: self.view.replace(edit, replaceRegion, formatted)
An annoying side effect is that this causes the cursor to be at position 0 on the first line and the buffer view scrolls all the way to the top. Whats my best option for getting the line/position of the cursor prior to calling the replacement, and then resetting the cursor position to position 0 of that line after the replacement occurs (and have that in the field of view obviously)?
I’m either blind, or there isnt anything obvious in the API. Thanks in advance
Class sublime.View:
sel() RegionSet Returns a reference to the selection.
show(point, <show_surrounds>) Scroll the view to show the given point.
show_at_center(point) Scroll the view to center on the point.
To read cursor(s) postion:
pos = view.sel()
To put the cursor(s) (selection) somewhere, simply modify the sel() RegionSet values:
Thanks, show and show_at_center is what I was looking for. I see it in the API doc now, not sure how I missed it
[quote=“bizoo”]Class sublime.View:
sel() RegionSet Returns a reference to the selection.
show(point, <show_surrounds>) Scroll the view to show the given point.
show_at_center(point) Scroll the view to center on the point.
To read cursor(s) postion:
pos = view.sel()
To put the cursor(s) (selection) somewhere, simply modify the sel() RegionSet values:
Just wanted to say that it should probably be pos = view.sel()[0], since view.sel() returns a list of regions (even if there’s only 1 cursor). My code didn’t work without it.
Doing this makes Sublime Text to throw an exception:
File "F:\SublimeText\sublime.py", line 672, in add
sublime_api.view_selection_add_region(self.view_id, x.a, x.b, x.xpos)
TypeError: an integer is required (got type Selection)
I manged to do it with:
last_caret_region = [(selection.begin(), selection.end()) for selection in view.sel()]
# Some time later ...
view.sel().clear()
for selection in last_caret_region:
view.sel().add(sublime.Region(selection[0], selection[1]))