Skip to content

Commit

Permalink
report all errors when generating dynamic semantic tokens (#2747)
Browse files Browse the repository at this point in the history
* report all errors when generating dynamic semantic tokens

* report when no tokens were produced
  • Loading branch information
brettfo authored Feb 16, 2023
1 parent 05d6872 commit a8b18e9
Showing 1 changed file with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as metadataUtilities from './metadataUtilities';
import { DynamicGrammarSemanticTokenProvider, VSCodeExtensionLike } from './dynamicGrammarSemanticTokenProvider';
import * as constants from './constants';
import * as vscodeNotebookManagement from './vscodeNotebookManagement';
import { Logger } from './polyglot-notebooks';

// https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide#standard-token-types-and-modifiers
const defaultTokenTypes = [
Expand Down Expand Up @@ -98,34 +99,43 @@ export class DocumentSemanticTokensProvider implements vscode.DocumentSemanticTo
}

async provideDocumentSemanticTokens(document: vscode.TextDocument, _cancellationToken: vscode.CancellationToken): Promise<vscode.SemanticTokens> {
const tokensBuilder = new vscode.SemanticTokensBuilder(this.semanticTokensLegend);
const notebookDocument = vscode.workspace.notebookDocuments.find(notebook => notebook.getCells().some(cell => cell.document === document));
if (notebookDocument) {
const notebookMetadata = metadataUtilities.getNotebookDocumentMetadataFromNotebookDocument(notebookDocument);
const text = document.getText();
const cell = notebookDocument.getCells().find(cell => cell.document === document);
if (cell) {
const cellMetadata = metadataUtilities.getNotebookCellMetadataFromNotebookCellElement(cell);
const cellKernelName = cellMetadata.kernelName ?? notebookMetadata.kernelInfo.defaultKernelName;
const tokens = await this._dynamicTokenProvider.getTokens(notebookDocument.uri, cellKernelName, text);
for (const token of tokens) {
try {
try {
let tokenCount = 0;
const tokensBuilder = new vscode.SemanticTokensBuilder(this.semanticTokensLegend);
const notebookDocument = vscode.workspace.notebookDocuments.find(notebook => notebook.getCells().some(cell => cell.document === document));
if (notebookDocument) {
const notebookMetadata = metadataUtilities.getNotebookDocumentMetadataFromNotebookDocument(notebookDocument);
const text = document.getText();
const cell = notebookDocument.getCells().find(cell => cell.document === document);
if (cell) {
const cellMetadata = metadataUtilities.getNotebookCellMetadataFromNotebookCellElement(cell);
const cellKernelName = cellMetadata.kernelName ?? notebookMetadata.kernelInfo.defaultKernelName;
const tokens = await this._dynamicTokenProvider.getTokens(notebookDocument.uri, cellKernelName, text);
for (const token of tokens) {
tokenCount++;
tokensBuilder.push(
new vscode.Range(
new vscode.Position(token.line, token.startColumn),
new vscode.Position(token.line, token.endColumn)
),
token.tokenType,
token.tokenModifiers);
} catch (e) {
const x = e;
}

if (tokenCount === 0 && text !== '') {
// there was text, but nothing was produced
Logger.default.info(`No tokens were produced for cell ${cell.index} of notebook ${notebookDocument.uri.toString()} with text: ${text}`);
}
}
}
}

// TODO: agument with real semantic tokens?
// TODO: agument with real semantic tokens?

return tokensBuilder.build();
const tokens = tokensBuilder.build();
return tokens;
} catch (ex) {
Logger.default.warn(`Error generating dynamic semantic tokens: ${JSON.stringify(ex)}`);
return new vscode.SemanticTokens(new Uint32Array(0));
}
}
}

0 comments on commit a8b18e9

Please sign in to comment.