-
-
Notifications
You must be signed in to change notification settings - Fork 117
Cleanup: Simplify open document #1773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fb1e59f
3dca109
dae14ba
e1c62c6
bc2b7f0
3a5e5fb
43aebb8
1e3b742
8781033
bb8d389
71b042d
4c95c6a
db8e315
5c2f120
a34240b
a668380
c89de60
6d3742c
79573d7
8b1ae96
4684b4e
70991ef
47d04f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -293,78 +293,97 @@ public class Scratch.Widgets.DocumentView : Gtk.Box { | |
| } | ||
|
|
||
| public void new_document () { | ||
| var file = File.new_for_path (unsaved_file_path_builder ()); | ||
| var new_doc_path = unsaved_file_path_builder (); | ||
| var file = File.new_for_path (new_doc_path); | ||
| try { | ||
| file.create (FileCreateFlags.PRIVATE); | ||
|
|
||
| var doc = new Services.Document (window.actions, file); | ||
| // Must open document in order to unlock it. | ||
| open_document.begin (doc); | ||
| open_document.begin (new_doc_path); | ||
| } catch (Error e) { | ||
| critical (e.message); | ||
| } | ||
| } | ||
|
|
||
| public void new_document_from_clipboard (string clipboard) { | ||
| var file = File.new_for_path (unsaved_file_path_builder ()); | ||
| var new_doc_path = unsaved_file_path_builder (); | ||
| var file = File.new_for_path (new_doc_path); | ||
|
|
||
| // Set clipboard content | ||
| try { | ||
| file.create (FileCreateFlags.PRIVATE); | ||
| file.replace_contents (clipboard.data, null, false, 0, null); | ||
| var doc = new Services.Document (window.actions, file); | ||
|
|
||
| open_document.begin (doc); | ||
|
|
||
|
|
||
| open_document.begin (new_doc_path); | ||
| } catch (Error e) { | ||
| critical ("Cannot insert clipboard: %s", clipboard); | ||
| } | ||
| } | ||
|
|
||
| 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) { | ||
| // Open document from path to avoid unnecessarily creating Document objects and | ||
| // to confine Document creation to this class. | ||
| // Documents are treated the same regarding the focus, cursor_position and range parameters | ||
| // whether already open or not. | ||
| // Cursor position may have any value > -2 to set cursor. Lower values are ignored (default) | ||
| // Specifying a valid non-empty selection range overrides the cursor position | ||
| public async void open_document ( | ||
| string doc_path, | ||
| bool focus = true, | ||
| int cursor_position = -2, | ||
| SelectionRange range = SelectionRange.EMPTY | ||
| ) { | ||
| Scratch.Services.Document? doc = null; // The document to be created and/or focused | ||
| // Check whether document already open | ||
| bool found = false; | ||
| for (int n = 0; n < docs.length (); n++) { | ||
| doc = docs.nth_data (n); | ||
| //TODO Is this check necessary? | ||
| if (doc == null || doc.file == null) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like if we are checking any doc of being null before adding it to the list of docs, or the view, we probably don't need the check
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are probably right about Thanks for the review! |
||
| critical ("Invalid document at position %i. %s", n, doc == null ? "Null document" : "Null document file"); | ||
| 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); | ||
| update_opened_files_setting (); | ||
|
|
||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| return; | ||
| if (doc.file.get_uri () == doc_path) { | ||
| found = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| insert_document (doc, (int) docs.length ()); | ||
| if (focus) { | ||
| current_document = doc; | ||
| if (!found) { | ||
| // Document not already open, create and insert it now | ||
| doc = new Scratch.Services.Document ( | ||
| window.actions, | ||
| GLib.File.new_for_commandline_arg (doc_path) | ||
| ); | ||
| // Temporary fix, the get_project_for_file function is intended to be moved | ||
| // to GitManager instance. | ||
| var project = window.folder_manager_view.get_project_for_file (doc.file); | ||
| doc.source_view.project = project; | ||
| insert_document (doc, (int) docs.length ()); | ||
| // Load contents before proceeding | ||
| yield doc.open (false); | ||
| } else { | ||
| debug ("This Document was already opened! Not opening a duplicate!"); | ||
| } | ||
|
|
||
| yield doc.open (false); | ||
|
|
||
| if (focus && doc == current_document) { | ||
| // @doc must have been assigned at this point | ||
| assert (doc != null); | ||
| // If we need to select a range we have to focus document anyway | ||
| if (focus || range != SelectionRange.EMPTY) { | ||
| doc.focus (); | ||
| current_document = doc; | ||
| } | ||
|
|
||
| if (range != SelectionRange.EMPTY) { | ||
| doc.source_view.select_range (range); | ||
| } else if (cursor_position > 0) { | ||
| if (cursor_position > -2) { | ||
| doc.source_view.cursor_position = cursor_position; | ||
| } | ||
|
|
||
| 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); | ||
| update_opened_files_setting (); //Records changed cursor position | ||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| update_opened_files_setting (); | ||
| } | ||
|
|
||
|
|
@@ -517,9 +536,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box { | |
| } | ||
|
|
||
| public void restore_closed_tab (string path) { | ||
| var file = File.new_for_path (path); | ||
| var doc = new Services.Document (window.actions, file); | ||
| open_document.begin (doc); | ||
| open_document.begin (path); | ||
|
|
||
| var menu = (Menu) tab_history_button.menu_model; | ||
| for (var i = 0; i < menu.get_n_items (); i++) { | ||
|
|
@@ -634,9 +651,7 @@ public class Scratch.Widgets.DocumentView : Gtk.Box { | |
| if (info == TargetType.URI_LIST) { | ||
| var uris = sel.get_uris (); | ||
| foreach (var filename in uris) { | ||
| var file = File.new_for_uri (filename); | ||
| var doc = new Services.Document (window.actions, file); | ||
| open_document.begin (doc); | ||
| open_document.begin (filename); | ||
| } | ||
|
|
||
| Gtk.drag_finish (ctx, true, false, time); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,9 +67,16 @@ namespace Scratch.Widgets { | |
| } | ||
|
|
||
| set { | ||
| // Values of -1 and greater than number of characters | ||
| // in the buffer place the cursor at the end of the buffer. | ||
| // Ignore lower values | ||
| if (value <= -2) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to ignore this, or clamp it? What happens if the cursor position is larger than the document supports? We may want to just mimic that.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am relying on https://valadoc.org/gtk+-3.0/Gtk.TextBuffer.get_iter_at_offset.html which says that |
||
| return; | ||
| } | ||
|
|
||
| Gtk.TextIter iter; | ||
| buffer.get_iter_at_offset (out iter, value); | ||
| buffer.place_cursor (iter); //Assume invalid offset handled correctly for now | ||
| buffer.place_cursor (iter); | ||
| Idle.add (() => { | ||
| scroll_to_iter (iter, 0.25, false, 0, 0); | ||
| return Source.REMOVE; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did this change to -2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
-1and0are valid values, sending the cursor to the start or end of the document. So I chose-2or anything more negative to meanleave the cursor where it is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that's interesting. Can you document that? I didn't find that choice immediately obvious
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I learnt this from
https://valadoc.org/gtk+-3.0/Gtk.TextBuffer.get_iter_at_offset.htmlwhich is what the sourceview uses to place the cursor. The interpretation of a position <= -2 is mine as it could be useful to not specify a cursor position. I'll make the comment clearer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at it afresh it looks like I set the cursor position twice. I'll fix that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually the "-2" value of the parameter is only used internally when a range override present.