diff --git a/src/Services/Document.vala b/src/Services/Document.vala index cc32ecf769..fcb3e34a5f 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); + } + } } } diff --git a/src/Services/LocationJumpManager.vala b/src/Services/LocationJumpManager.vala index d4423210fe..4b4814472b 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 cd1f21ad48..08887df62d 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 81e33cdddf..1a9c6cc163 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 */