Skip to content
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 7 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## 1.118.0 (unreleased)

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

## 1.117.0 (Release on 2024-11-07)
Expand Down
4 changes: 4 additions & 0 deletions apps/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { activateDiagram } from "./providers/diagram/diagram";
import { activateOptionEnterProvider } from "./providers/option";
import { textFormattingCommands } from "./providers/text-format";
import { activateCodeFormatting } from "./providers/format";
import { activateContextKeySetter } from "./providers/context-keys";
import { ExtensionHost } from "./host";

export function activateCommon(
Expand All @@ -37,6 +38,9 @@ export function activateCommon(
// background highlighter
activateBackgroundHighlighter(context, engine);

// context setter
activateContextKeySetter(context, engine);

// diagramming
const diagramCommands = activateDiagram(context, host, engine);

Expand Down
93 changes: 93 additions & 0 deletions apps/vscode/src/providers/context-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 docs are opened
vscode.workspace.onDidOpenTextDocument(
(doc) => {
if (doc === vscode.window.activeTextEditor?.document) {
if (isQuartoDoc(doc)) {
setContextKeys(vscode.window.activeTextEditor, engine);
}
}
},
null,
context.subscriptions
);

// set context keys when visible text editors change
vscode.window.onDidChangeVisibleTextEditors(
(_editors) => {
triggerUpdateContextKeys(engine);
},
null,
context.subscriptions
);

// set context keys on changes to the document (if its visible)
vscode.workspace.onDidChangeTextDocument(
(event) => {
const visibleEditor = vscode.window.visibleTextEditors.find(editor => {
return editor.document.uri.toString() === event.document.uri.toString();
});
if (visibleEditor) {
debounce(
() => setContextKeys(visibleEditor, engine),
debounceOnDidChangeDocumentMs
)();
}
},
null,
context.subscriptions
);

// set context keys at activation time
triggerUpdateContextKeys(engine);

}

function triggerUpdateContextKeys(engine: MarkdownEngine) {
for (const editor of vscode.window.visibleTextEditors) {
setContextKeys(editor, engine);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is looking great!

My main comment is about this, and any other usage of visibleTextEditors

IIUC, the context key is "global". i.e. there is not a context key per document, there is exactly 1 context key set globally.

If that is the case, then the context key should probably only be set for the active text editor, not for all visible text editors (because only 1 wins!).

For example, if I open 2 qmds side by side in split panes, one that is mostly R and one that is mostly Python, then I'm not entirely sure which is going to win here because both are "visible". However, only one at a time is ever "active", and I feel like that is probably what we should be targeting with this context key.

Is it possible all we really need to do is having a single hook for onDidChangeActiveTextEditor()?

Does that make any sense?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes total sense, yep. Changed in 8d00a51.

Copy link
Collaborator Author

@juliasilge juliasilge Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the hook for onDidChangeTextDocument() (with some debounce) for the case where someone changes their document from being Python to R or vice versa. That is certainly rare, but that case does feel fairly broken if we don't include it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct that we don't need onDidOpenTextDocument() if we have onDidChangeActiveTextEditor(). 👍

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the hook for onDidChangeTextDocument()

Makes sense because you also dont have a "main" language at the start of a document until you actually start typing and adding some chunks (or maybe there is a default, but it likely isnt right)


PR looks great nice job!


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]);
}
8 changes: 2 additions & 6 deletions apps/vscode/src/vdoc/vdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
*/

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

import { isQuartoDoc } from "../core/doc";
Expand Down Expand Up @@ -157,12 +157,8 @@ export async function virtualDocUri(
export function languageAtPosition(tokens: Token[], position: Position) {
const block = languageBlockAtPosition(tokens, position);
if (block) {
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

const language = languageFromBlock(block);
// expose cell language for use in keybindings, etc
commands.executeCommand('setContext', 'quarto.cellLangId', language?.ids[0]);
return language;
return languageFromBlock(block);
} else {
commands.executeCommand('setContext', 'quarto.cellLangId', undefined);
return undefined;
}
}
Expand Down