|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { genMarkdownString, getColorTokenValue } from "./utils"; |
| 3 | + |
| 4 | +export default class DecorationManager { |
| 5 | + private activeEditor: vscode.TextEditor | undefined; |
| 6 | + private fullToken: any; |
| 7 | + private fileDecorationMap: Map< |
| 8 | + string, |
| 9 | + Map<number, vscode.TextEditorDecorationType[]> |
| 10 | + >; |
| 11 | + private timeout: NodeJS.Timer | undefined = undefined; |
| 12 | + private fileName: string = ""; |
| 13 | + |
| 14 | + constructor($activeEditor: vscode.TextEditor | undefined, $fullToken: any) { |
| 15 | + this.activeEditor = $activeEditor; |
| 16 | + this.fullToken = $fullToken; |
| 17 | + this.fileDecorationMap = new Map(); |
| 18 | + } |
| 19 | + |
| 20 | + setActiveEditor(editor: vscode.TextEditor) { |
| 21 | + this.activeEditor = editor; |
| 22 | + this.fileName = editor.document.fileName; |
| 23 | + } |
| 24 | + |
| 25 | + triggerUpdateDecorations( |
| 26 | + throttle: boolean = false, |
| 27 | + isEdit: boolean = false, |
| 28 | + diffLine: number = 0, |
| 29 | + startLine?: number, |
| 30 | + endLine?: number, |
| 31 | + originalStartLine?: number, |
| 32 | + originalEndLine?: number |
| 33 | + ) { |
| 34 | + /** |
| 35 | + * Active editor changed event |
| 36 | + */ |
| 37 | + if (!isEdit) { |
| 38 | + if (this.fileDecorationMap.has(this.fileName)) { |
| 39 | + this.clearCurrentFileDecoration(); |
| 40 | + this.fileDecorationMap.set(this.fileName, new Map()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + if (this.timeout) { |
| 45 | + clearTimeout(this.timeout); |
| 46 | + this.timeout = undefined; |
| 47 | + } |
| 48 | + |
| 49 | + if (throttle) { |
| 50 | + this.timeout = setTimeout(() => { |
| 51 | + this.setupDecorations( |
| 52 | + diffLine, |
| 53 | + startLine, |
| 54 | + endLine, |
| 55 | + originalStartLine, |
| 56 | + originalEndLine |
| 57 | + ); |
| 58 | + }, 500); |
| 59 | + } else { |
| 60 | + this.setupDecorations( |
| 61 | + diffLine, |
| 62 | + startLine, |
| 63 | + endLine, |
| 64 | + originalStartLine, |
| 65 | + originalEndLine |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + setupDecorations( |
| 71 | + diffLine: number, |
| 72 | + startLine?: number, |
| 73 | + endLine?: number, |
| 74 | + originalStartLine?: number, |
| 75 | + originalEndLine?: number |
| 76 | + ) { |
| 77 | + if (startLine && endLine) { |
| 78 | + const lines = this.getLines(startLine, endLine); |
| 79 | + |
| 80 | + if (diffLine < 0) { |
| 81 | + this.clearCurrentFileDecoration(lines); |
| 82 | + this.updateFileDecorationMap(diffLine, startLine); |
| 83 | + } |
| 84 | + |
| 85 | + if (diffLine === 0) { |
| 86 | + this.clearCurrentFileDecoration([startLine]); |
| 87 | + this.setDecorations([startLine]); |
| 88 | + } |
| 89 | + |
| 90 | + if (diffLine > 0) { |
| 91 | + /** |
| 92 | + * paste override copyed lines |
| 93 | + */ |
| 94 | + if (originalStartLine && originalEndLine) { |
| 95 | + const originalLines = this.getLines( |
| 96 | + originalStartLine, |
| 97 | + originalEndLine |
| 98 | + ); |
| 99 | + this.clearCurrentFileDecoration(originalLines); |
| 100 | + } |
| 101 | + |
| 102 | + this.updateFileDecorationMap(diffLine, startLine); |
| 103 | + this.setDecorations(lines); |
| 104 | + } |
| 105 | + } else { |
| 106 | + this.setDecorations(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + setDecorations(sepecificLines?: number[]) { |
| 111 | + const text = this.activeEditor!.document.getText(); |
| 112 | + const fullTokenKeys = Object.keys(this.fullToken); |
| 113 | + const lineDecorationMap: Map<number, vscode.TextEditorDecorationType[]> = |
| 114 | + this.fileDecorationMap.get(this.fileName) || new Map(); |
| 115 | + |
| 116 | + if (!this.fileDecorationMap.has(this.fileName)) { |
| 117 | + this.fileDecorationMap.set(this.fileName, new Map()); |
| 118 | + } |
| 119 | + |
| 120 | + fullTokenKeys.forEach((key: string) => { |
| 121 | + const regEx = new RegExp(`\\b(${key})\\b(?!-)`, "g"); |
| 122 | + let match; |
| 123 | + |
| 124 | + while ((match = regEx.exec(text))) { |
| 125 | + const currentLine = this.activeEditor!.document.positionAt( |
| 126 | + match.index |
| 127 | + ).line; |
| 128 | + |
| 129 | + if ( |
| 130 | + !sepecificLines || |
| 131 | + (sepecificLines && sepecificLines.includes(currentLine)) |
| 132 | + ) { |
| 133 | + this.setDecoration(match, key, lineDecorationMap); |
| 134 | + } |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + this.fileDecorationMap.set(this.fileName, lineDecorationMap); |
| 139 | + } |
| 140 | + |
| 141 | + clearCurrentFileDecoration(lines?: number[]) { |
| 142 | + const lineDecorationMapItem = this.fileDecorationMap.get(this.fileName); |
| 143 | + if (lineDecorationMapItem?.size) { |
| 144 | + if (lines) { |
| 145 | + lines.forEach((line) => { |
| 146 | + if (lineDecorationMapItem.has(line)) { |
| 147 | + lineDecorationMapItem.get(line)?.forEach(this.dispose); |
| 148 | + lineDecorationMapItem.delete(line); |
| 149 | + } |
| 150 | + }); |
| 151 | + } else { |
| 152 | + lineDecorationMapItem.forEach((value) => { |
| 153 | + value.forEach(this.dispose); |
| 154 | + }); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + dispose(disposable: vscode.TextEditorDecorationType) { |
| 160 | + disposable.dispose(); |
| 161 | + } |
| 162 | + |
| 163 | + getLines(start: number, end: number): number[] { |
| 164 | + const result: number[] = []; |
| 165 | + |
| 166 | + if (start > end) { |
| 167 | + return []; |
| 168 | + } |
| 169 | + |
| 170 | + for (let i = start; i <= end; i++) { |
| 171 | + result.push(i); |
| 172 | + } |
| 173 | + |
| 174 | + return result; |
| 175 | + } |
| 176 | + |
| 177 | + setDecoration( |
| 178 | + match: RegExpExecArray, |
| 179 | + key: string, |
| 180 | + lineDecorationMap: Map<number, vscode.TextEditorDecorationType[]> |
| 181 | + ) { |
| 182 | + const valueDecorations: vscode.DecorationOptions[] = []; |
| 183 | + const startPos = this.activeEditor!.document.positionAt(match.index); |
| 184 | + const endPos = this.activeEditor!.document.positionAt( |
| 185 | + match.index + match[0].length |
| 186 | + ); |
| 187 | + const currentLine = startPos.line; |
| 188 | + const value = String(this.fullToken[key]); |
| 189 | + const colorSpan = genMarkdownString(value); |
| 190 | + const markDownString = new vscode.MarkdownString( |
| 191 | + `<h3>antd design token: ${match[0]}</h3>${colorSpan}<code>${value}</code><br></br>` |
| 192 | + ); |
| 193 | + markDownString.supportHtml = true; |
| 194 | + markDownString.isTrusted = true; |
| 195 | + |
| 196 | + const decoration = { |
| 197 | + range: new vscode.Range(startPos, endPos), |
| 198 | + hoverMessage: markDownString, |
| 199 | + }; |
| 200 | + |
| 201 | + const colorValue = getColorTokenValue(this.fullToken[key]); |
| 202 | + valueDecorations.push(decoration); |
| 203 | + |
| 204 | + const decorationType = vscode.window.createTextEditorDecorationType({ |
| 205 | + after: { |
| 206 | + contentText: colorValue ? `**` : `${String(this.fullToken[key])}`, |
| 207 | + backgroundColor: colorValue || "", |
| 208 | + margin: "0 0 0 5px;", |
| 209 | + color: colorValue || "#b37feb", |
| 210 | + fontWeight: "bolder", |
| 211 | + }, |
| 212 | + }); |
| 213 | + |
| 214 | + const lineValue = lineDecorationMap.get(currentLine); |
| 215 | + lineDecorationMap.set( |
| 216 | + currentLine, |
| 217 | + lineValue ? lineValue.concat([decorationType]) : [decorationType] |
| 218 | + ); |
| 219 | + this.activeEditor!.setDecorations(decorationType, valueDecorations); |
| 220 | + } |
| 221 | + |
| 222 | + updateFileDecorationMap(diffLine: number, startLine: number) { |
| 223 | + const lineDecorationMapItem = this.fileDecorationMap.get(this.fileName); |
| 224 | + const newMap: Map<number, vscode.TextEditorDecorationType[]> = new Map(); |
| 225 | + |
| 226 | + if (lineDecorationMapItem?.size) { |
| 227 | + lineDecorationMapItem.forEach((value, key) => { |
| 228 | + if (key >= startLine) { |
| 229 | + newMap.set( |
| 230 | + diffLine > 0 ? key + diffLine : key - Math.abs(diffLine), |
| 231 | + lineDecorationMapItem.get(key)! |
| 232 | + ); |
| 233 | + } else { |
| 234 | + newMap.set(key, lineDecorationMapItem.get(key)!); |
| 235 | + } |
| 236 | + }); |
| 237 | + } |
| 238 | + |
| 239 | + this.fileDecorationMap.set(this.fileName, newMap); |
| 240 | + } |
| 241 | +} |
0 commit comments