Skip to content

Commit c6f28f8

Browse files
committed
Add new activateContextKeySetter()
1 parent 4a34c1f commit c6f28f8

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

apps/vscode/src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { activateDiagram } from "./providers/diagram/diagram";
2323
import { activateOptionEnterProvider } from "./providers/option";
2424
import { textFormattingCommands } from "./providers/text-format";
2525
import { activateCodeFormatting } from "./providers/format";
26+
import { activateContextKeySetter } from "./providers/context-keys";
2627
import { ExtensionHost } from "./host";
2728

2829
export function activateCommon(
@@ -37,6 +38,9 @@ export function activateCommon(
3738
// background highlighter
3839
activateBackgroundHighlighter(context, engine);
3940

41+
// context setter
42+
activateContextKeySetter(context, engine);
43+
4044
// diagramming
4145
const diagramCommands = activateDiagram(context, host, engine);
4246

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* context-keys.ts
3+
*
4+
* Copyright (C) 2024 by Posit Software, PBC
5+
*
6+
* Unless you have received this program directly from Posit Software pursuant
7+
* to the terms of a commercial license agreement with Posit Software, then
8+
* this program is licensed to you under the terms of version 3 of the
9+
* GNU Affero General Public License. This program is distributed WITHOUT
10+
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11+
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12+
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13+
*
14+
*/
15+
16+
17+
import * as vscode from "vscode";
18+
import debounce from "lodash.debounce";
19+
20+
import { isQuartoDoc } from "../core/doc";
21+
import { MarkdownEngine } from "../markdown/engine";
22+
import { mainLanguage } from "../vdoc/vdoc";
23+
24+
const debounceOnDidChangeDocumentMs = 250;
25+
26+
export function activateContextKeySetter(
27+
context: vscode.ExtensionContext,
28+
engine: MarkdownEngine
29+
) {
30+
31+
// set context keys when docs are opened
32+
vscode.workspace.onDidOpenTextDocument(
33+
(doc) => {
34+
if (doc === vscode.window.activeTextEditor?.document) {
35+
if (isQuartoDoc(doc)) {
36+
setContextKeys(vscode.window.activeTextEditor, engine);
37+
}
38+
}
39+
},
40+
null,
41+
context.subscriptions
42+
);
43+
44+
// set context keys when visible text editors change
45+
vscode.window.onDidChangeVisibleTextEditors(
46+
(_editors) => {
47+
triggerUpdateContextKeys(engine);
48+
},
49+
null,
50+
context.subscriptions
51+
);
52+
53+
// set context keys on changes to the document (if its visible)
54+
vscode.workspace.onDidChangeTextDocument(
55+
(event) => {
56+
const visibleEditor = vscode.window.visibleTextEditors.find(editor => {
57+
return editor.document.uri.toString() === event.document.uri.toString();
58+
});
59+
if (visibleEditor) {
60+
debounce(
61+
() => setContextKeys(visibleEditor, engine),
62+
debounceOnDidChangeDocumentMs
63+
)();
64+
}
65+
},
66+
null,
67+
context.subscriptions
68+
);
69+
70+
// set context keys at activation time
71+
triggerUpdateContextKeys(engine);
72+
73+
}
74+
75+
function triggerUpdateContextKeys(engine: MarkdownEngine) {
76+
for (const editor of vscode.window.visibleTextEditors) {
77+
setContextKeys(editor, engine);
78+
}
79+
}
80+
81+
function setContextKeys(editor: vscode.TextEditor, engine: MarkdownEngine) {
82+
if (!editor || !isQuartoDoc(editor.document)) {
83+
return;
84+
}
85+
86+
// expose main language for use in keybindings, etc
87+
const tokens = engine.parse(editor.document);
88+
const language = mainLanguage(tokens);
89+
vscode.commands.executeCommand(
90+
'setContext',
91+
'quarto.document.languageId',
92+
language?.ids[0]);
93+
}

0 commit comments

Comments
 (0)