Skip to content

Commit

Permalink
Merge DocumentView.open_document methods into one method
Browse files Browse the repository at this point in the history
  • Loading branch information
colinkiama committed Nov 27, 2023
1 parent 7a01b75 commit da98778
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ namespace Scratch {

FolderManager.ProjectFolderItem? project = folder_manager_view.get_project_for_file (doc.file);
doc.source_view.project = project;
document_view.open_document_at_selected_range (doc, focus, range);
document_view.open_document (doc, focus, 0, range);
}

// Close a document
Expand Down
44 changes: 4 additions & 40 deletions src/Widgets/DocumentView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -213,46 +213,7 @@ public class Scratch.Widgets.DocumentView : Granite.Widgets.DynamicNotebook {
}
}

public void open_document (Services.Document doc, bool focus = true, int cursor_position = 0) {
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!");
return;
}
}

insert_document (doc, -1);
if (focus) {
current_document = doc;
}

Idle.add_full (GLib.Priority.LOW, () => { // This helps ensures new tab is drawn before opening document.
doc.open.begin (false, (obj, res) => {
doc.open.end (res);
if (focus && doc == current_document) {
doc.focus ();
}

if (cursor_position > 0) {
doc.source_view.cursor_position = cursor_position;
}
save_opened_files ();
});

return false;
});
}

public void open_document_at_selected_range (Services.Document doc, bool focus = true, SelectionRange range = SelectionRange.EMPTY) {
public 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) {
Expand Down Expand Up @@ -292,7 +253,10 @@ public class Scratch.Widgets.DocumentView : Granite.Widgets.DynamicNotebook {

if (range != SelectionRange.EMPTY) {
doc.source_view.select_range (range);
} else if (cursor_position > 0) {
doc.source_view.cursor_position = cursor_position;
}

save_opened_files ();
});

Expand Down

0 comments on commit da98778

Please sign in to comment.