Home Download Buy Blog Forum Support

API: Set cursor position/view location?

API: Set cursor position/view location?

Postby jdc0589 on Thu Aug 25, 2011 12:31 am

I have a javascript formatting plugin that passes the contents of the buffer through a python library then calls:
Code: Select all
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
jdc0589
 
Posts: 32
Joined: Fri Jun 03, 2011 2:32 am

Re: API: Set cursor position/view location?

Postby bizoo on Thu Aug 25, 2011 9:37 am

Code: Select all
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:
Code: Select all
pos = view.sel()


To put the cursor(s) (selection) somewhere, simply modify the sel() RegionSet values:
Code: Select all
view.sel().clear()
view.sel().add(sublime.Region(pos))


Take care of multi-selection.
bizoo
 
Posts: 873
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: API: Set cursor position/view location?

Postby jdc0589 on Fri Aug 26, 2011 12:09 am

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

bizoo wrote:
Code: Select all
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:
Code: Select all
pos = view.sel()


To put the cursor(s) (selection) somewhere, simply modify the sel() RegionSet values:
Code: Select all
view.sel().clear()
view.sel().add(sublime.Region(pos))


Take care of multi-selection.
jdc0589
 
Posts: 32
Joined: Fri Jun 03, 2011 2:32 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 4 guests