Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions src/Widgets/DocumentView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -321,49 +321,58 @@ public class Scratch.Widgets.DocumentView : Gtk.Box {
}
}

public async void open_document (Services.Document doc, bool focus = true, int cursor_position = 0, SelectionRange range = SelectionRange.EMPTY) {
public async void open_document (
Services.Document doc,
bool focus = true,
int cursor_position = 0,
SelectionRange range = SelectionRange.EMPTY
) {
for (int n = 0; n <= docs.length (); n++) {
var nth_doc = docs.nth_data (n);
if (nth_doc == null) {
continue;
}

if (nth_doc.file != null && nth_doc.file.get_uri () == doc.file.get_uri ()) {
if (focus) {
current_document = nth_doc;
}

debug ("This Document was already opened! Not opening a duplicate!");
if (range != SelectionRange.EMPTY) {
Idle.add_full (GLib.Priority.LOW, () => { // This helps ensures new tab is drawn before opening document.
current_document.source_view.select_range (range);
save_opened_files ();

return false;
});
}

after_insert_document (nth_doc, focus, cursor_position, range);
return;
}
}

insert_document (doc, (int) docs.length ());
if (focus) {
current_document = doc;
}

yield doc.open (false);

if (focus && doc == current_document) {
after_insert_document (doc, focus, cursor_position, range);
}

private void after_insert_document (
Scratch.Services.Document doc,
bool focus = true,
int cursor_position = 0,
SelectionRange range = SelectionRange.EMPTY
) {
if (focus) {
current_document = doc;
doc.focus ();
}

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

if (Scratch.saved_state.get_boolean ("outline-visible")) {
debug ("setting outline visible");
doc.show_outline (true);
}

save_opened_files ();
}

Expand Down