|
1 | 1 | import * as vscode from "vscode";
|
| 2 | +import { genMarkdownString, getColorTokenValue } from "./utils"; |
2 | 3 |
|
3 |
| -export default function setupChangeEvent() { |
| 4 | +export default function setupChangeEvent( |
| 5 | + context: vscode.ExtensionContext, |
| 6 | + fullToken: any |
| 7 | +) { |
| 8 | + let timeout: NodeJS.Timer | undefined = undefined; |
4 | 9 | let activeEditor = vscode.window.activeTextEditor;
|
| 10 | + const fullTokenKeys = Object.keys(fullToken); |
| 11 | + const decorationSet = new Set<vscode.TextEditorDecorationType>(); |
| 12 | + |
| 13 | + if (activeEditor) { |
| 14 | + triggerUpdateDecorations(); |
| 15 | + } |
5 | 16 |
|
6 | 17 | vscode.workspace.onDidChangeTextDocument(
|
7 | 18 | (event) => {
|
8 |
| - console.log(event); |
9 |
| - // if (activeEditor && event.document === activeEditor.document) { |
10 |
| - // triggerUpdateDecorations(true); |
11 |
| - // } |
| 19 | + if (activeEditor && event.document === activeEditor.document) { |
| 20 | + triggerUpdateDecorations(true); |
| 21 | + } |
| 22 | + }, |
| 23 | + null, |
| 24 | + context.subscriptions |
| 25 | + ); |
| 26 | + |
| 27 | + vscode.window.onDidChangeActiveTextEditor( |
| 28 | + (editor) => { |
| 29 | + activeEditor = editor; |
| 30 | + if (editor) { |
| 31 | + triggerUpdateDecorations(); |
| 32 | + } |
12 | 33 | },
|
13 |
| - null |
14 |
| - // context.subscriptions |
| 34 | + null, |
| 35 | + context.subscriptions |
15 | 36 | );
|
| 37 | + |
| 38 | + function triggerUpdateDecorations(throttle = false) { |
| 39 | + if (timeout) { |
| 40 | + clearTimeout(timeout); |
| 41 | + timeout = undefined; |
| 42 | + } |
| 43 | + if (throttle) { |
| 44 | + timeout = setTimeout(updateDecorations, 500); |
| 45 | + } else { |
| 46 | + updateDecorations(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + function updateDecorations() { |
| 51 | + if (activeEditor) { |
| 52 | + const text = activeEditor.document.getText(); |
| 53 | + decorationSet.forEach((value) => { |
| 54 | + value.dispose(); |
| 55 | + }); |
| 56 | + |
| 57 | + fullTokenKeys.forEach((key: string) => { |
| 58 | + if (!activeEditor) { |
| 59 | + return; |
| 60 | + } |
| 61 | + const regEx = new RegExp(`\\b(${key})\\b(?!-)`, "g"); |
| 62 | + |
| 63 | + let match; |
| 64 | + while ((match = regEx.exec(text))) { |
| 65 | + const valueDecorations: vscode.DecorationOptions[] = []; |
| 66 | + let decorationType: vscode.TextEditorDecorationType; |
| 67 | + |
| 68 | + const startPos = activeEditor.document.positionAt(match.index); |
| 69 | + const endPos = activeEditor.document.positionAt( |
| 70 | + match.index + match[0].length |
| 71 | + ); |
| 72 | + |
| 73 | + const value = String(fullToken[key]); |
| 74 | + const colorSpan = genMarkdownString(value); |
| 75 | + const markDownString = new vscode.MarkdownString( |
| 76 | + `<h3>antd design token: ${match[0]}</h3>${colorSpan}<code>${value}</code><br></br>` |
| 77 | + ); |
| 78 | + markDownString.supportHtml = true; |
| 79 | + markDownString.isTrusted = true; |
| 80 | + const decoration = { |
| 81 | + range: new vscode.Range(startPos, endPos), |
| 82 | + hoverMessage: markDownString, |
| 83 | + }; |
| 84 | + |
| 85 | + const colorValue = getColorTokenValue(fullToken[key]); |
| 86 | + valueDecorations.push(decoration); |
| 87 | + |
| 88 | + decorationType = vscode.window.createTextEditorDecorationType({ |
| 89 | + after: { |
| 90 | + contentText: colorValue ? `**` : `(${String(fullToken[key])})`, |
| 91 | + backgroundColor: colorValue || "", |
| 92 | + margin: "0 0 0 4px;", |
| 93 | + color: colorValue || "#1890ff", |
| 94 | + fontWeight: "bolder", |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + decorationSet.add(decorationType); |
| 99 | + |
| 100 | + activeEditor.setDecorations(decorationType, valueDecorations); |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
16 | 105 | }
|
0 commit comments