onDidChangeActiveTextEditor always reports active selection of 0,0 · Issue #136618 · microsoft/vscode (original) (raw)
Does this issue occur when all extensions are disabled?: Bug is about extension API
- VS Code Version: 1.61
- OS Version: Ubuntu 20.04.3 LTS
When a user opens a new file at any position other than (0,0)
(for example, by using Go to definition
), onDidChangeActiveTextEditor
will report (0,0)
until the onDidChangeTextEditorSelection
reaches the extension host. An extension that wants to act on the active selection has to implement a waiting logic similar to
onDidChangeActiveTextEditor(document => {
// Wait a short period until an optional selection update reaches the extension host
setTimeout(() => {
// Execute logic here
}, 500);
}
Needless to say that this is a brittle workaround, but I cannot think of any else, because the onDidChangeTextEditorSelection
is not triggered if the document is opened at (0,0)
.
Out of curiosity, I dug around the source code to understand where the problem is coming from. Here is my understanding: In textFileEditors.ts
, the onDidChangeActiveTextEditor
event is triggered by updating the text model:
textEditor.setModel(textFileModel.textEditorModel);
The selection is updated shortly afterwards:
applyTextEditorOptions(options, textEditor, ScrollType.Immediate);
Flipping these statements around is not enough, because the selection is internally transported by an event and will not reach the code that generates the event that is sent to the extension host in time.