Skip to content

Commit dca4735

Browse files
authored
Set context keys for document language (#608)
* Set context keys for cell language when background is painted * Update changelog * Do not set context keys along with painting background * Add new `activateContextKeySetter()` * Context keys only apply to *active* documents * We don't need `onDidOpenTextDocument()` because we have `onDidChangeActiveTextEditor()`
1 parent 772c650 commit dca4735

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

apps/vscode/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## 1.118.0 (unreleased)
44

55
- Provide F1 help at cursor in Positron (<https://github.com/quarto-dev/quarto/pull/599>)
6-
- Expose new context keys for the language of a specific cell (<https://github.com/quarto-dev/quarto/pull/607>)
6+
- Expose new context keys for the main language of a document (<https://github.com/quarto-dev/quarto/pull/608>)
77
- No longer send all snippet suggestions to the bottom of the completion list (<https://github.com/quarto-dev/quarto/pull/609>).
88

99
## 1.117.0 (Release on 2024-11-07)

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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
import * as vscode from "vscode";
17+
import debounce from "lodash.debounce";
18+
19+
import { isQuartoDoc } from "../core/doc";
20+
import { MarkdownEngine } from "../markdown/engine";
21+
import { mainLanguage } from "../vdoc/vdoc";
22+
23+
const debounceOnDidChangeDocumentMs = 250;
24+
25+
export function activateContextKeySetter(
26+
context: vscode.ExtensionContext,
27+
engine: MarkdownEngine
28+
) {
29+
30+
// set context keys when active text editor changes
31+
vscode.window.onDidChangeActiveTextEditor(
32+
(editor) => {
33+
if (editor) {
34+
setContextKeys(editor, engine);
35+
}
36+
},
37+
null,
38+
context.subscriptions
39+
);
40+
41+
// set context keys on changes to the document (if it's active)
42+
vscode.workspace.onDidChangeTextDocument(
43+
(event) => {
44+
const activeEditor = vscode.window.activeTextEditor;
45+
if (activeEditor) {
46+
debounce(
47+
() => setContextKeys(activeEditor, engine),
48+
debounceOnDidChangeDocumentMs
49+
)();
50+
}
51+
},
52+
null,
53+
context.subscriptions
54+
);
55+
}
56+
57+
function setContextKeys(editor: vscode.TextEditor, engine: MarkdownEngine) {
58+
if (!editor || !isQuartoDoc(editor.document)) {
59+
return;
60+
}
61+
62+
// expose main language for use in keybindings, etc
63+
const tokens = engine.parse(editor.document);
64+
const language = mainLanguage(tokens);
65+
vscode.commands.executeCommand(
66+
'setContext',
67+
'quarto.document.languageId',
68+
language?.ids[0]);
69+
}

apps/vscode/src/vdoc/vdoc.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*/
1515

16-
import { Position, TextDocument, Uri, Range, commands } from "vscode";
16+
import { Position, TextDocument, Uri, Range } from "vscode";
1717
import { Token, isExecutableLanguageBlock, languageBlockAtPosition, languageNameFromBlock } from "quarto-core";
1818

1919
import { isQuartoDoc } from "../core/doc";
@@ -157,12 +157,8 @@ export async function virtualDocUri(
157157
export function languageAtPosition(tokens: Token[], position: Position) {
158158
const block = languageBlockAtPosition(tokens, position);
159159
if (block) {
160-
const language = languageFromBlock(block);
161-
// expose cell language for use in keybindings, etc
162-
commands.executeCommand('setContext', 'quarto.cellLangId', language?.ids[0]);
163-
return language;
160+
return languageFromBlock(block);
164161
} else {
165-
commands.executeCommand('setContext', 'quarto.cellLangId', undefined);
166162
return undefined;
167163
}
168164
}

0 commit comments

Comments
 (0)