|
| 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