Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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>)

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

Expand Down
5 changes: 5 additions & 0 deletions apps/vscode/src/providers/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { isQuartoDoc, kQuartoDocSelector } from "../core/doc";
import { MarkdownEngine } from "../markdown/engine";
import { isExecutableLanguageBlock } from "quarto-core";
import { vscRange } from "../core/range";
import { mainLanguage } from "../vdoc/vdoc";

export function activateBackgroundHighlighter(
context: vscode.ExtensionContext,
Expand Down Expand Up @@ -162,6 +163,10 @@ async function setEditorHighlightDecorations(
blockRanges.push(vscRange(block.range));
Copy link
Collaborator

@DavisVaughan DavisVaughan Nov 26, 2024

Choose a reason for hiding this comment

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

What if highlightingConfig isn't enabled? See the if-block above.

(See broader comment about using a different approach)

}

// expose cell language for use in keybindings, etc
const language = mainLanguage(tokens);
vscode.commands.executeCommand('setContext', 'quartoLangId', language?.ids[0]);
Copy link
Collaborator

@DavisVaughan DavisVaughan Nov 26, 2024

Choose a reason for hiding this comment

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

I'd consider using a non-abbreviated and very descriptive name

quarto.document.mainLanguageId
quarto.documentMainLanguageId
quartoDocumentMainLanguageId

See "quarto.assistView.isEnabled" as a nice example

It's a little tricky because we have a mix of styles right now for context key naming. Sometimes we do quarto. sometimes we dont https://github.com/search?q=repo%3Aquarto-dev%2Fquarto%20setContext&type=code

I tend to like using the . as a way to namespace it to quarto

(also including main in the name is a way for us to guard against the possibility of us eventually actually getting a per-cell context key right)

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just quarto.document.languageId vs (eventually) something like quarto.cell.languageId?

document vs cell is an evocative distinction. main wants to contrast with secondary, and that's not what you're setting up to do in the future.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Works great for me!


Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not feel it is appropriate to set the context here

Currently this function is quite pure. It is just about setting highlight decorations and is very easy to understand.

If we take a look at why it works to put this here, its because this is called from

  • triggerUpdateAllEditorsDecorations(), which is called during the onDidChangeVisibleTextEditors() hook
  • triggerUpdateActiveEditorDecorations(), which is called during the onDidOpenTextDocument() hook

That hook registration is part of activateBackgroundHighlighter(), which is called in activateCommon().


I think what we really want is:

  • In activateCommon(),
  • Add a new activateLanguageContextSetter()
  • Which registers onDidOpenTextDocument() and onDidChangeVisibleTextEditors() hooks of its own, and those hooks set the quartoMainLanguageId of the main language for the document.

Yes you have to reparse the document, but I think that is worth it over muddying the waters about what setEditorHighlightDecorations() is about, plus it gives you finer control over when the context is updated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a great idea! Implemented in c6f28f8 and working really well in posit-dev/positron#5451.

// find inline executable code
for (let i = 0; i < editor.document.lineCount; i++) {
const line = editor.document.lineAt(i);
Expand Down
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