Skip to content

Commit c22321b

Browse files
committed
DocumentView.open_document ():Ensure scroll to selection; DRY
1 parent 1dcf003 commit c22321b

1 file changed

Lines changed: 28 additions & 19 deletions

File tree

src/Widgets/DocumentView.vala

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -321,49 +321,58 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
321321
}
322322
}
323323

324-
public async void open_document (Services.Document doc, bool focus = true, int cursor_position = 0, SelectionRange range = SelectionRange.EMPTY) {
324+
public async void open_document (
325+
Services.Document doc,
326+
bool focus = true,
327+
int cursor_position = 0,
328+
SelectionRange range = SelectionRange.EMPTY
329+
) {
325330
for (int n = 0; n <= docs.length (); n++) {
326331
var nth_doc = docs.nth_data (n);
327332
if (nth_doc == null) {
328333
continue;
329334
}
330335

331336
if (nth_doc.file != null && nth_doc.file.get_uri () == doc.file.get_uri ()) {
332-
if (focus) {
333-
current_document = nth_doc;
334-
}
335-
336-
debug ("This Document was already opened! Not opening a duplicate!");
337-
if (range != SelectionRange.EMPTY) {
338-
Idle.add_full (GLib.Priority.LOW, () => { // This helps ensures new tab is drawn before opening document.
339-
current_document.source_view.select_range (range);
340-
save_opened_files ();
341-
342-
return false;
343-
});
344-
}
345-
337+
after_insert_document (nth_doc, focus, cursor_position, range);
346338
return;
347339
}
348340
}
349341

350342
insert_document (doc, (int) docs.length ());
351-
if (focus) {
352-
current_document = doc;
353-
}
354343

355344
yield doc.open (false);
356345

357-
if (focus && doc == current_document) {
346+
after_insert_document (doc, focus, cursor_position, range);
347+
}
348+
349+
private void after_insert_document (
350+
Scratch.Services.Document doc,
351+
bool focus = true,
352+
int cursor_position = 0,
353+
SelectionRange range = SelectionRange.EMPTY
354+
) {
355+
if (focus) {
356+
current_document = doc;
358357
doc.focus ();
359358
}
360359

361360
if (range != SelectionRange.EMPTY) {
362361
doc.source_view.select_range (range);
362+
// It is recommended to run scroll to mark in a low priority idle otherwise may not work
363+
Idle.add_full (Priority.LOW, () => {
364+
doc.source_view.scroll_to_mark (doc.source_view.buffer.get_insert (), 0.1, true, 0.5, 0.5);
365+
return Source.REMOVE;
366+
});
363367
} else if (cursor_position > 0) {
364368
doc.source_view.cursor_position = cursor_position;
365369
}
366370

371+
if (Scratch.saved_state.get_boolean ("outline-visible")) {
372+
debug ("setting outline visible");
373+
doc.show_outline (true);
374+
}
375+
367376
save_opened_files ();
368377
}
369378

0 commit comments

Comments
 (0)