gtk3pygobjectgtksourceview

GTK SourceView scroll to mark not working


I'm trying to fill a SourceView's buffer with text and then scroll to make a specific line visible, like this:

lines = '....'.split('\n')
line_number = 76 # For instance, assuming lines has at least this many lines
buffer = view.get_buffer()
for line in lines:
  buffer.insert(end_iter, line + '\n')
iter = buffer.get_iter_at_line()
mark = buffer.get_mark('insert')
buffer.move_mark(mark, iter)
mark = buffer.get_mark('selection_bound')
buffer.move_mark(mark, iter)
view.scroll_to_mark(mark, 0.3, True, 0, 0.5)

This scrolls to more-or-less random places in the buffer. Is there something I'm doing wrong here? Or does this just not work?


Solution

  • You are right, scroll_to_iter depends on the idle recalculate. For that matter, scroll_to_mark does too. This works for me:

    from gi.repository import GLib
    #........ code here
    GLib.idle_add(view.scroll_to_mark, mark, 0.1, True, 0.0, 0.5)