From efa0b3f37ca2920235a251cacabf9a7758ad455e Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sun, 3 May 2026 16:26:39 +0100 Subject: [PATCH 1/2] SourceView: Save and restore selection when focus out and in --- src/Services/Document.vala | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Services/Document.vala b/src/Services/Document.vala index cc32ecf76..fcb3e34a5 100644 --- a/src/Services/Document.vala +++ b/src/Services/Document.vala @@ -175,6 +175,7 @@ namespace Scratch.Services { private string last_save_content = ""; public bool saved = true; private bool completion_shown = false; + private SelectionRange last_selection; private Gtk.ScrolledWindow scroll; private Gtk.SourceMap source_map; @@ -264,6 +265,8 @@ namespace Scratch.Services { check_file_status.begin (); } + restore_selection (); + return false; }); @@ -273,6 +276,8 @@ namespace Scratch.Services { save_with_hold.begin (); } + save_selection (); + return false; }); @@ -1364,5 +1369,33 @@ namespace Scratch.Services { outline.parse_symbols (); } } + + // Only call when document is focusing out + private void save_selection () { + Gtk.TextIter start,end; + var source_buffer = (Gtk.SourceBuffer)source_view.buffer; + if (source_buffer.has_selection) { + source_buffer.get_selection_bounds (out start, out end); + last_selection = SelectionRange () { + start_line = start.get_line (), + start_column = start.get_line_offset (), + end_line = end.get_line (), + end_column = end.get_line_offset () + }; + } else { + last_selection = SelectionRange.EMPTY; + } + } + + // We assume the selection cannot change while the document is focused out + private void restore_selection () { + if (last_selection != SelectionRange.EMPTY) { + var source_buffer = (Gtk.SourceBuffer)source_view.buffer; + Gtk.TextIter start, end; + source_buffer.get_iter_at_line_offset (out start, last_selection.start_line, last_selection.start_column); + source_buffer.get_iter_at_line_offset (out end, last_selection.end_line, last_selection.end_column); + source_buffer.select_range (start, end); + } + } } } From 995288dc5591a6ef7040a9f48f1e541fd79165f6 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 4 May 2026 10:42:51 +0100 Subject: [PATCH 2/2] Whitespace fix --- src/Services/LocationJumpManager.vala | 6 +++--- src/Services/RestoreOverride.vala | 6 +++--- src/Structs/SelectionRange.vala | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Services/LocationJumpManager.vala b/src/Services/LocationJumpManager.vala index d4423210f..4b4814472 100644 --- a/src/Services/LocationJumpManager.vala +++ b/src/Services/LocationJumpManager.vala @@ -1,6 +1,6 @@ -/* - * SPDX-License-Identifier: GPL-3.0-or-later - * SPDX-FileCopyrightText: 2023 elementary, Inc. +/* + * SPDX-License-Identifier: GPL-3.0-or-later + * SPDX-FileCopyrightText: 2023 elementary, Inc. * * Authored by: Colin Kiama */ diff --git a/src/Services/RestoreOverride.vala b/src/Services/RestoreOverride.vala index cd1f21ad4..08887df62 100644 --- a/src/Services/RestoreOverride.vala +++ b/src/Services/RestoreOverride.vala @@ -1,6 +1,6 @@ -/* - * SPDX-License-Identifier: GPL-3.0-or-later - * SPDX-FileCopyrightText: 2023 elementary, Inc. +/* + * SPDX-License-Identifier: GPL-3.0-or-later + * SPDX-FileCopyrightText: 2023 elementary, Inc. * * Authored by: Colin Kiama */ diff --git a/src/Structs/SelectionRange.vala b/src/Structs/SelectionRange.vala index 81e33cddd..1a9c6cc16 100644 --- a/src/Structs/SelectionRange.vala +++ b/src/Structs/SelectionRange.vala @@ -1,6 +1,6 @@ -/* - * SPDX-License-Identifier: GPL-3.0-or-later - * SPDX-FileCopyrightText: 2023 elementary, Inc. +/* + * SPDX-License-Identifier: GPL-3.0-or-later + * SPDX-FileCopyrightText: 2023 elementary, Inc. * * Authored by: Colin Kiama */