Sublime Forum

Refresh viewport on view selection change

#1

When I set the cursor to a different position programmatically the cursor is not redrawn in the viewport until I change the viewport by clicking somewhere, or scrolling. Is there a way to make the viewport refresh? Here is what I’m doing inside of a WindowCommand where lastCurPos is a function returning a (row, col) tuple from a history of cursor positions

v = self.window.active_view() row, col = lastCurPos() point = v.text_point(row, col) v.sel().clear() v.sel().add(sublime.Region(point)) v.show(point)

0 Likes

#2

I’m having the same behavior. I’ve added a bug on sublimetext.userecho.com for this (please upvote!)

http://userecho.com/FZqP

0 Likes

#3

very hacky solution (for plugin authors):

pos = v.viewport_position()
v.show(point)
new_pos = v.viewport_position()
if abs(new_pos[0] - pos[0]) <= 1.0 and abs(new_pos[1] - pos[1]) <= 1.0:
    v.set_viewport_position((new_pos[0], new_pos[1] + 1))
0 Likes

#4

I have found another workaround for this issue.

0 Likes

#5

@jps or @wbond
Could you please take a look at this issue that is very annoying.
Still present in: startup, version: 3109 windows x64 channel: dev

As explained by poster, modifying the view.sel() (in a WindowCommand, not sure about TextCommand) isn’t displayed on the viewport until you hit a key. click with the mouse or scroll.

Even this code doesn’t works if the new region is close of the current one and there’s no scroll of the viewport:

self.view.sel().clear()
self.view.sel().add(region)
self.view.show_at_center(region)

Thanks

0 Likes

#6

I believe this issue is related - try the workaround there (which doesnt involve messing with viewports) for now until it is fixed: https://github.com/SublimeTextIssues/Core/issues/485

0 Likes

#7

Is scrolling the issue or the selections not being updates visually? For the former, I suppose that is intended and should require you to explicitly tell ST to update the viewport (e.g. with show_at_center).

0 Likes

#8

After some testing I’ve found the issue is only present when using WindowCommand (probably ApplicationCommand too), TextCommand works flawlessly.

So the viewport is updated after a TextCommand exit, but not after an ApplicationCommand.
Make some sense because as you don’t have an edit token, you can’t modify the buffer.
But view.sel() (and view.add_regions()) is somewhat special because it doesn’t actually modify the buffer and can be used outside an edit token.

So please @jps or @wbond, is it wrong to change view.sel() in a WindowsCommand or is it a bug in ST ?

This is my test code:

import sublime, sublime_plugin

class NotworkingCommand(sublime_plugin.WindowCommand):
     toggle = True
     def run(self):
        self.toggle = not self.toggle
        self.view = self.window.active_view()
        if self.toggle:
            region = sublime.Region(100, 105)
        else:
            region = sublime.Region(130, 135)

        self.view.sel().clear()
        self.view.sel().add(region)
        self.view.show_at_center(region)

class WorkingCommand(sublime_plugin.TextCommand):
     toggle = True
     def run(self, edit):
        self.toggle = not self.toggle

        if self.toggle:
            region = sublime.Region(100, 105)
        else:
            region = sublime.Region(130, 135)

        self.view.sel().clear()
        self.view.sel().add(region)
        self.view.show_at_center(region)
3 Likes

#9

With that code (and slight adjustments for better toggling) I managed to properly reproduce the issue with TextCommand working but not WindowCommand.

I recorded a gif of the behavior:

The adjusted code bit(s):

        if list(self.view.sel()) != [sublime.Region(100, 105)]:
            region = sublime.Region(100, 105)

This seems to be very similar to https://github.com/SublimeTextIssues/Core/issues/485 indeed, but in that ticket it is reported to fail for TextCommands instead.

1 Like