Created
August 22, 2024 13:49
-
-
Save tanaka-geek/8add62fe98c7e4379f97be5dc7d7121d to your computer and use it in GitHub Desktop.
VSCode-test-project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "desktop-tester", | |
"displayName": "Desktop-tester", | |
"description": "", | |
"version": "0.0.1", | |
"engines": { | |
"vscode": "^1.92.0" | |
}, | |
"categories": [ | |
"Other" | |
], | |
"activationEvents": [ | |
"onLanguage:plaintext" | |
], | |
"main": "./out/extension.js", | |
"contributes": { | |
"commands": [ | |
{ | |
"command": "desktop-tester.helloWorld", | |
"title": "Hello World" | |
} | |
], | |
"configuration": { | |
"type": "object", | |
"title": "Desktop Tester Settings", | |
"properties": { | |
"desktopTester.wordsToHighlight": { | |
"type": "array", | |
"default": ["example"], | |
"description": "List of words to highlight in the editor." | |
} | |
} | |
} | |
}, | |
"scripts": { | |
"vscode:prepublish": "npm run compile", | |
"compile": "tsc -p ./", | |
"watch": "tsc -watch -p ./", | |
"pretest": "npm run compile && npm run lint", | |
"lint": "eslint src --ext ts", | |
"test": "vscode-test" | |
}, | |
"devDependencies": { | |
"@types/vscode": "^1.92.0", | |
"@types/mocha": "^10.0.7", | |
"@types/node": "20.x", | |
"@typescript-eslint/eslint-plugin": "^7.14.1", | |
"@typescript-eslint/parser": "^7.11.0", | |
"eslint": "^8.57.0", | |
"typescript": "^5.4.5", | |
"@vscode/test-cli": "^0.0.9", | |
"@vscode/test-electron": "^2.4.0" | |
} | |
} | |
import * as vscode from "vscode"; | |
export function activate(context: vscode.ExtensionContext) { | |
let decorationType = vscode.window.createTextEditorDecorationType({ | |
backgroundColor: "yellow", | |
}); | |
const wordsToHighlight = ["example", "test", "highlight"]; // Hardcoded words to highlight | |
const regex = new RegExp(`\\b(${wordsToHighlight.join("|")})\\b`, "gi"); | |
const updateDecorations = () => { | |
const editor = vscode.window.activeTextEditor; | |
if (!editor) return; | |
const text = editor.document.getText(); | |
const decorations: vscode.DecorationOptions[] = []; | |
let match; | |
while ((match = regex.exec(text))) { | |
const startPos = editor.document.positionAt(match.index); | |
const endPos = editor.document.positionAt(match.index + match[0].length); | |
const decoration = { range: new vscode.Range(startPos, endPos) }; | |
decorations.push(decoration); | |
} | |
editor.setDecorations(decorationType, decorations); | |
}; | |
context.subscriptions.push( | |
vscode.window.onDidChangeActiveTextEditor(updateDecorations), | |
vscode.workspace.onDidChangeTextDocument((event) => { | |
if (vscode.window.activeTextEditor && event.document === vscode.window.activeTextEditor.document) { | |
updateDecorations(); | |
} | |
}) | |
); | |
updateDecorations(); // Initial call to update decorations on activation | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment