I am developing a vs code extension, in which I want to highlight a few lines of code for certain files:
export class SidebarProvider implements vscode.WebviewViewProvider {
_view?: vscode.WebviewView;
_doc?: vscode.TextDocument;
constructor(private readonly _extensionUri: vscode.Uri) {}
public resolveWebviewView(webviewView: vscode.WebviewView) {
this._view = webviewView;
webviewView.webview.options = {
// Allow scripts in the webview
enableScripts: true,
localResourceRoots: [this._extensionUri],
};
webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);
webviewView.webview.onDidReceiveMessage(async (data) => {
switch (data.type) {
case "fetchCode": {
..........
resolutionLength.forEach((async (lines, filePath) => {
const document = await vscode.workspace.openTextDocument(filePath);
const editor = await vscode.window.showTextDocument(document);
for(let i=0;i<lines.length;i++) {
this.highlight(editor, startPositions.get(filePath)?.at(i),
resolutionLength.get(filePath)?.at(i));
}
}));
............
}
}
});
private higlight(editor: vscode.TextEditor, startPosition: number | undefined, resolutionLength: number | undefined): void {
const decorations: vscode.DecorationOptions[] = [];
if(!startPosition|| !resolutionLength) {
throw new Error('Start position or length not defined.');
}
const range = new vscode.Range(startPosition, 0, startPosition+ resolutionLength, 0);
const decoration = { range, hoverMessage: 'Resolution line' };
decorations.push(decoration);
editor.setDecorations(conflictResolutionDecorationType, decorations);
};
};
const conflictResolutionDecorationType = vscode.window.createTextEditorDecorationType({
// Add your decoration style here
backgroundColor: 'rgba(255, 255, 0, 0.5)'
});
When I run the extension, after the above code gets executed, the highlight on the lines appears only for a second and then disappears. Now I understand that editor is not persisting the highlight decorations once it goes out of scope. But my question is: is there any way to make this highlight permanent. That is, whenever I open any of the files which have been highlighted, I should be able to see those highlights. Is that possible?