Skip to content

Cleanup: Simplify open document - #1773

Merged
jeremypw merged 23 commits into
masterfrom
jeremypw/open-document/simplify
Aug 24, 2026
Merged

Cleanup: Simplify open document#1773
jeremypw merged 23 commits into
masterfrom
jeremypw/open-document/simplify

Conversation

@jeremypw

@jeremypw jeremypw commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1703

  • Move creation of Document objects into DocumentView and change first parameter to string (path)
  • Only create new objects when needed
  • Update calls to this function appropriately
  • Lose unneeded intermediate functions in MainWindow and call the Document View function directly
  • Simplify the open_document function by DRYing and treating all documents the same regarding focus, cursor_position and range, whether newly created or existing .

The foldermanagerview member of window is temporarily made public but after #1778 this will not be necessary as that function will be moved to GitManager

@jeremypw
jeremypw marked this pull request as ready for review July 22, 2026 17:08
@danirabbit

Copy link
Copy Markdown
Member

@jeremypw can you resolve conflicts please

@jeremypw
jeremypw marked this pull request as draft July 22, 2026 17:49
@jeremypw

Copy link
Copy Markdown
Collaborator Author

Converting back to draft for testing after tricky merge

@jeremypw
jeremypw marked this pull request as ready for review July 22, 2026 18:13
@jeremypw
jeremypw requested a review from danirabbit July 22, 2026 18:18
// 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)

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because -1 and 0 are valid values, sending the cursor to the start or end of the document. So I chose -2 or anything more negative to mean leave the cursor where it is.

Copy link
Copy Markdown
Member

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

@jeremypw jeremypw Aug 10, 2026

Copy link
Copy Markdown
Collaborator Author

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.html which 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.

Copy link
Copy Markdown
Collaborator Author

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.

Copy link
Copy Markdown
Collaborator Author

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.

Comment thread src/Widgets/SourceView.vala Outdated
@jeremypw
jeremypw requested a review from danirabbit July 23, 2026 10:21
@jeremypw

Copy link
Copy Markdown
Collaborator Author

Probably to much to review - I'll try and split up.

@jeremypw
jeremypw marked this pull request as draft August 16, 2026 16:43
@zeebok

zeebok commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Probably to much to review - I'll try and split up.

@jeremypw I don't think that is case. I was about to review it, but if you really want to break it up go ahead.

@jeremypw
jeremypw marked this pull request as ready for review August 16, 2026 19:57
@jeremypw

Copy link
Copy Markdown
Collaborator Author

@zeebok Sorry, I have reset it as "ready". Thanks for having a look at this.

@jeremypw jeremypw added this to the 8.3.x milestone Aug 23, 2026

@zeebok zeebok left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple thoughts but it seems to work well with no found regressions!

for (int n = 0; n < docs.length (); n++) {
doc = docs.nth_data (n);
//TODO Is this check necessary?
if (doc == null || doc.file == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are probably right about doc; not so sure about doc.file - there is nothing to stop this being set to null by an external call as it is public. This will be the subject of future cleanup PRs. We should not need to keep a separate list of docs - we can already iterate over the tabpage children if needed. We should make doc.file private and stop it becoming null then the check can be removed. I'll leave this for now as outside scope.

Thanks for the review!

// 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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

@jeremypw jeremypw Aug 24, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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 TextBuffer.get_iter_at_offset (out iter, int char_offset) already handles any offset from -1 upwards (even if beyond the end of the buffer). The value of -2 is our own special case for "leave the cursor alone" used for a range override.

@jeremypw
jeremypw merged commit c5c2a48 into master Aug 24, 2026
6 checks passed
@jeremypw
jeremypw deleted the jeremypw/open-document/simplify branch August 24, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document opened with range does not scroll to show selected range

3 participants