Refactor: add InternalSelectionsTracker class (followup to #9644) #9655
+322
−133
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Followup to #9644
What this PR does / why we need it
This PR replaces the
ModeHandler.selectionsChanged
object with an instance of a new classInternalSelectionsTracker
, which encapsulates logic that was previously scattered across multiple files:selectionsChanged.ignoreIntermediateSelections
InternalSelectionsTracker.shouldIgnoreIntermediateSelections
and helper methodsInternalSelectionsTracker.startIgnoringIntermediateSelections
andInternalSelectionsTracker.stopIgnoringIntermediateSelections
willTriggerChange
logic at the bottom ofupdateView
InternalSelectionsTracker.maybeTrackSelectionsUpdateToIgnore
methodif (e.kind !== vscode.TextEditorSelectionChangeKind.Mouse)
branch invscode.window.onDidChangeTextEditorSelection
listener registrationInternalSelectionsTracker.maybeIgnoreInternalSelectionChangeEvent
methodThe benefit is reduced mental overhead, and hopefully better maintainability, now that the various pieces of logic related to handing internally-triggered selection change events are encapsulated together in a single class. As well as some reduced clutter in functions like
updateView
and thevscode.window.onDidChangeTextEditorSelection
listener registration.Which issue(s) this PR fixes
Not sure it's worth filing an issue for this but happy to if you'd prefer that!
Special notes for your reviewer
This is a pure refactor with no functionality change. I also added a good amount more explanation around details, pitfalls, and tradeoffs through class/method/property docstrings. Maybe even too much explanation, although with behavior this complex and connected, and knowing I had to figure out certain things by diving deep into old PRs and debugging with logs, I did try to err on more details than fewer.
My biggest uncertainty was in replacing and extending @berknam 's earlier (2020) comments and logs about what he called "slipped selections", i.e. the fallback case when the event's selections aren't found in our tracked internal updates, and we're not currently ignoring intermediate selections, but we still have some internal selections updates tracked. I did my best to understand and document the logic there, but I'm not currently sure about any concrete examples where an intermediate selection has actually slipped through past the end of an action and would corrupt state if we didn't ignore it. So I've mentioned in
InternalSelectionsTracker.shouldIgnoreAsIntermediateSelection
that we may want to revisit the tradeoff in the future, to help the documentation not come across as more certain than we (or at least I) actually am about the assumption we're operating on there. But if you have a better grasp of things there let me know and I can adjust the docstring as needed.