Skip to content

Commit

Permalink
Context keys only apply to *active* documents
Browse files Browse the repository at this point in the history
  • Loading branch information
juliasilge committed Nov 26, 2024
1 parent c6f28f8 commit 8d00a51
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions apps/vscode/src/providers/context-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*
*/


import * as vscode from "vscode";
import debounce from "lodash.debounce";

Expand Down Expand Up @@ -41,41 +40,31 @@ export function activateContextKeySetter(
context.subscriptions
);

// set context keys when visible text editors change
vscode.window.onDidChangeVisibleTextEditors(
(_editors) => {
triggerUpdateContextKeys(engine);
// set context keys when active text editor changes
vscode.window.onDidChangeActiveTextEditor(
(editor) => {
if (editor) {
setContextKeys(editor, engine);
}
},
null,
context.subscriptions
);

// set context keys on changes to the document (if its visible)
// set context keys on changes to the document (if it's active)
vscode.workspace.onDidChangeTextDocument(
(event) => {
const visibleEditor = vscode.window.visibleTextEditors.find(editor => {
return editor.document.uri.toString() === event.document.uri.toString();
});
if (visibleEditor) {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
debounce(
() => setContextKeys(visibleEditor, engine),
() => setContextKeys(activeEditor, engine),
debounceOnDidChangeDocumentMs
)();
}
},
null,
context.subscriptions
);

// set context keys at activation time
triggerUpdateContextKeys(engine);

}

function triggerUpdateContextKeys(engine: MarkdownEngine) {
for (const editor of vscode.window.visibleTextEditors) {
setContextKeys(editor, engine);
}
}

function setContextKeys(editor: vscode.TextEditor, engine: MarkdownEngine) {
Expand Down

0 comments on commit 8d00a51

Please sign in to comment.