-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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()`
- Loading branch information
1 parent
772c650
commit dca4735
Showing
4 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* context-keys.ts | ||
* | ||
* Copyright (C) 2024 by Posit Software, PBC | ||
* | ||
* Unless you have received this program directly from Posit Software pursuant | ||
* to the terms of a commercial license agreement with Posit Software, then | ||
* this program is licensed to you under the terms of version 3 of the | ||
* GNU Affero General Public License. This program is distributed WITHOUT | ||
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, | ||
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the | ||
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. | ||
* | ||
*/ | ||
|
||
import * as vscode from "vscode"; | ||
import debounce from "lodash.debounce"; | ||
|
||
import { isQuartoDoc } from "../core/doc"; | ||
import { MarkdownEngine } from "../markdown/engine"; | ||
import { mainLanguage } from "../vdoc/vdoc"; | ||
|
||
const debounceOnDidChangeDocumentMs = 250; | ||
|
||
export function activateContextKeySetter( | ||
context: vscode.ExtensionContext, | ||
engine: MarkdownEngine | ||
) { | ||
|
||
// 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 it's active) | ||
vscode.workspace.onDidChangeTextDocument( | ||
(event) => { | ||
const activeEditor = vscode.window.activeTextEditor; | ||
if (activeEditor) { | ||
debounce( | ||
() => setContextKeys(activeEditor, engine), | ||
debounceOnDidChangeDocumentMs | ||
)(); | ||
} | ||
}, | ||
null, | ||
context.subscriptions | ||
); | ||
} | ||
|
||
function setContextKeys(editor: vscode.TextEditor, engine: MarkdownEngine) { | ||
if (!editor || !isQuartoDoc(editor.document)) { | ||
return; | ||
} | ||
|
||
// expose main language for use in keybindings, etc | ||
const tokens = engine.parse(editor.document); | ||
const language = mainLanguage(tokens); | ||
vscode.commands.executeCommand( | ||
'setContext', | ||
'quarto.document.languageId', | ||
language?.ids[0]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters