Sublime Forum

API: Set cursor position/view location?

#1

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

1 Like

#2

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:

view.sel().clear() view.sel().add(sublime.Region(pos))

Take care of multi-selection.

3 Likes

#3

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:

view.sel().clear() view.sel().add(sublime.Region(pos))

Take care of multi-selection.[/quote]

1 Like

#4

Brilliant,THX.

0 Likes

#5

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.

0 Likes

#6

If you only want to the first selection, take care when there are no selections on the view:

selections = view.sel()

if len( selections ) > 0
     selection = selections[0]
else:
     return
0 Likes

#7

In python you should use the EAFP approach:

try:
    selection = selections[0]
except IndexError:
    return
2 Likes

#8
  1. Better to ‘try’ something and catch the exception or test if its possible first to avoid an exception?
  2. Python: Do Python Lists keep a count for len() or does it count for each call?
  3. Do try/catch blocks hurt performance when exceptions are not thrown?
  4. Python performance: Try-except or not in?
  5. Python using exceptions for control flow considered bad?
  6. In what ways do C++ exceptions slow down code when there are no exceptions thown?
0 Likes

#9

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]))
0 Likes