Skip to content

Commit 3fc179b

Browse files
committed
Better to use virtualDocForLanguage() for this provider
1 parent 347d8f9 commit 3fc179b

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

apps/vscode/src/providers/semantic-tokens.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ import {
2626
import { DocumentSemanticsTokensSignature } from "vscode-languageclient";
2727
import { MarkdownEngine } from "../markdown/engine";
2828
import { isQuartoDoc } from "../core/doc";
29-
import { unadjustedSemanticTokens, virtualDoc, withVirtualDocUri } from "../vdoc/vdoc";
29+
import {
30+
unadjustedSemanticTokens,
31+
virtualDocForLanguage,
32+
withVirtualDocUri,
33+
languageAtPosition,
34+
mainLanguage
35+
} from "../vdoc/vdoc";
36+
import { EmbeddedLanguage } from "../vdoc/languages";
3037
import { QUARTO_SEMANTIC_TOKEN_LEGEND } from "quarto-utils";
3138

3239
/**
@@ -185,21 +192,33 @@ export function embeddedSemanticTokensProvider(engine: MarkdownEngine) {
185192
return await next(document, token);
186193
}
187194

195+
// Ensure we are dealing with the active document
188196
const editor = window.activeTextEditor;
189197
const activeDocument = editor?.document;
190198
if (!editor || activeDocument?.uri.toString() !== document.uri.toString()) {
191199
// Not the active document, delegate to default
192200
return await next(document, token);
193201
}
194202

203+
// Parse the document to get all tokens
204+
const tokens = engine.parse(document);
205+
206+
// Try to find language at cursor position, otherwise use main language
195207
const line = editor.selection.active.line;
196208
const position = new Position(line, 0);
197-
const vdoc = await virtualDoc(document, position, engine);
209+
let language = languageAtPosition(tokens, position);
210+
if (!language) {
211+
language = mainLanguage(tokens);
212+
}
198213

199-
if (!vdoc) {
214+
if (!language) {
215+
// No language found, delegate to default
200216
return await next(document, token);
201217
}
202218

219+
// Create virtual doc for all blocks of this language
220+
const vdoc = virtualDocForLanguage(document, tokens, language);
221+
203222
return await withVirtualDocUri(vdoc, document.uri, "semanticTokens", async (uri: Uri) => {
204223
try {
205224
// Get the legend from the embedded language provider

0 commit comments

Comments
 (0)