From b23eefaa1aed9812387e962662fd71721e3bf680 Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Sun, 15 Aug 2021 14:20:58 -0700 Subject: [PATCH] Improve handling of vanishing selection If you select a note or long comment and delete it, the selection is lost, because the selected item no longer exists. This is inconvenient if you're working with the keyboard, because it moves the keyboard position to the top of the file. If the previous selection was non-empty, and the new selection is empty, we want to select the line that would have appeared below the item that was deleted. Making this work exactly right is a bunch of work, but we can make it work mostly right by selecting the first line associated with the offset of the previous selection. --- SourceGen/LineListGen.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/SourceGen/LineListGen.cs b/SourceGen/LineListGen.cs index 0b3d93f..7fd99d3 100644 --- a/SourceGen/LineListGen.cs +++ b/SourceGen/LineListGen.cs @@ -404,6 +404,26 @@ namespace SourceGen { // to the top of the list. topIndex = 0; } + + // If the selected item was deleted, we can't restore the selection. This is + // annoying when you're using the keyboard, because if you hit Del to delete a + // note and then hit down-arrow you jump to the top of the file. If we detect + // this situation, we want to select the thing that appeared below it. We can + // approximate this by just selecting the first thing at the offset (which is + // not quite right if you have both a long comment and a note, but it'll do). + // + // This doesn't help for some of the header stuff, e.g. if you select the last + // platform/project equate and then remove one symbol, the selection gets lost. + if (sel.Count == 0 && mSelectionTags.Count != 0) { + Debug.WriteLine("Lost selection"); + int tryOffset = mSelectionTags[0].mOffset; + int tryIndex = LineListGen.FindLineByOffset(lineList, tryOffset); + if (tryIndex >= 0) { + Debug.WriteLine(" Setting to offset +" + tryOffset.ToString("x6") + + " (line " + tryIndex + ")"); + sel[tryIndex] = true; + } + } return sel; } @@ -569,6 +589,7 @@ namespace SourceGen { return -1; } + // Start with a binary search. int low = 0; int high = lineList.Count - 1; int mid = -1;