-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set context keys for document language #608
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
11d1830
Set context keys for cell language when background is painted
juliasilge 35ad78a
Update changelog
juliasilge f1aa3fb
Merge branch 'main' into revamp-quarto-lang-context-keys
juliasilge 4a34c1f
Do not set context keys along with painting background
juliasilge c6f28f8
Add new `activateContextKeySetter()`
juliasilge 8d00a51
Context keys only apply to *active* documents
juliasilge a3535e0
We don't need `onDidOpenTextDocument()` because we have `onDidChangeA…
juliasilge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, previously it attempted to update the context key dynamically on each cell "activation", right? That does seem hard to get 100% right.
And now it tries to update the context key 1 time for whatever the main language in the document is.
I suppose that could be mildly confusing. If you have 1 python cell in a mostly R document, then clicking in the python cell and running a keybinding will run the R version. That's kinda confusing, but I'm not sure what we can do better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did hope for us to get this working really nicely for reticulate Quarto files (per cell), but when I tried to use the approach with
languageAtPosition()
in practice, it felt quite awful. So yep, I think for now this approach with the "main language" is the best option.