| 1 | /* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | import { EXTENSION_NAME } from "core/control-plane/env"; |
| 3 | // @ts-ignore |
| 4 | import svgBuilder from "svg-builder"; |
| 5 | import * as vscode from "vscode"; |
| 6 | |
| 7 | import { getTheme } from "../util/getTheme"; |
| 8 | import { getMetaKeyLabel, getMetaKeyName } from "../util/util"; |
| 9 | |
| 10 | const SVG_CONFIG = { |
| 11 | stroke: "#999998", |
| 12 | strokeWidth: 1, |
| 13 | shortcutColor: "#999998", |
| 14 | filter: "drop-shadow(0 2px 2px rgba(0,0,0,0.2))", |
| 15 | radius: 3, |
| 16 | leftMargin: 40, |
| 17 | debounceDelay: 500, |
| 18 | chatLabel: "Chat", |
| 19 | chatShortcut: `${getMetaKeyLabel()}+L`, |
| 20 | editLabel: "Edit", |
| 21 | editShortcut: `${getMetaKeyLabel()}+I`, |
| 22 | |
| 23 | get fontSize() { |
| 24 | return Math.ceil( |
| 25 | (vscode.workspace.getConfiguration("editor").get<number>("fontSize") ?? |
| 26 | 14) * 0.8, |
| 27 | ); |
| 28 | }, |
| 29 | get fontFamily() { |
| 30 | return ( |
| 31 | vscode.workspace.getConfiguration("editor").get<string>("fontFamily") || |
| 32 | "helvetica" |
| 33 | ); |
| 34 | }, |
| 35 | get paddingX() { |
| 36 | return Math.ceil(this.getEstimatedTextWidth(" ")); |
| 37 | }, |
| 38 | get gap() { |
| 39 | return this.fontSize * 0.5; |
| 40 | }, |
| 41 | get tipWidth() { |
| 42 | return ( |
| 43 | this.editShortcutX + |
| 44 | this.getEstimatedTextWidth(this.editShortcut) + |
| 45 | this.paddingX |
| 46 | ); |
| 47 | }, |
| 48 | get tipHeight() { |
| 49 | return this.fontSize; |
| 50 | }, |
| 51 | get textY() { |
| 52 | return (this.tipHeight + this.fontSize) / 2; |
| 53 | }, |
| 54 | get chatLabelX() { |
| 55 | return this.paddingX; |
| 56 | }, |
| 57 | get chatShortcutX() { |
| 58 | return this.chatLabelX + this.getEstimatedTextWidth(this.chatLabel + " "); |
| 59 | }, |
| 60 | get editLabelX() { |
| 61 | return ( |
| 62 | this.chatShortcutX + |
| 63 | this.getEstimatedTextWidth(this.chatShortcut) + |
| 64 | this.gap |
| 65 | ); |
| 66 | }, |
| 67 | get editShortcutX() { |
| 68 | return this.editLabelX + this.getEstimatedTextWidth(this.editLabel + " "); |
| 69 | }, |
| 70 | getEstimatedTextWidth(text: string): number { |
| 71 | return text.length * this.fontSize * 0.6; |
| 72 | }, |
| 73 | } as const; |
| 74 | |
| 75 | export class InlineTipManager { |
| 76 | private static instance: InlineTipManager; |
| 77 | |
| 78 | private readonly excludedURIPrefixes = ["output:", "vscode://inline-chat"]; |
| 79 | private readonly hideCommand = "continue.hideInlineTip"; |
| 80 | private svgTooltip: vscode.Uri | undefined = undefined; |
| 81 | |
| 82 | private debounceTimer: NodeJS.Timeout | undefined; |
| 83 | private lastActiveEditor?: vscode.TextEditor; |
| 84 | private theme = getTheme(); |
| 85 | private svgTooltipDecoration = this.createSvgTooltipDecoration(); |
| 86 | private emptyFileTooltipDecoration = this.createEmptyFileTooltipDecoration(); |
| 87 | |
| 88 | public static getInstance(): InlineTipManager { |
| 89 | if (!InlineTipManager.instance) { |
| 90 | InlineTipManager.instance = new InlineTipManager(); |
| 91 | } |
| 92 | return InlineTipManager.instance; |
| 93 | } |
| 94 | |
| 95 | private constructor() { |
| 96 | this.createSvgTooltip(); |
| 97 | this.setupSvgTipListeners(); |
| 98 | } |
| 99 | |
| 100 | public setupInlineTips(context: vscode.ExtensionContext) { |
| 101 | context.subscriptions.push( |
| 102 | vscode.window.onDidChangeTextEditorSelection((e) => { |
| 103 | this.handleSelectionChange(e); |
| 104 | }), |
| 105 | ); |
| 106 | |
| 107 | this.setupEmptyFileTips(context); |
| 108 | |
| 109 | context.subscriptions.push(this); |
| 110 | } |
| 111 | |
| 112 | public handleSelectionChange(e: vscode.TextEditorSelectionChangeEvent) { |
| 113 | const selection = e.selections[0]; |
| 114 | const editor = e.textEditor; |
| 115 | |
| 116 | if (selection.isEmpty || !this.shouldRenderTip(editor.document.uri)) { |
| 117 | editor.setDecorations(this.svgTooltipDecoration, []); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | this.debouncedSelectionChange(editor, selection); |
| 122 | } |
| 123 | |
| 124 | public dispose() { |
| 125 | this.svgTooltipDecoration.dispose(); |
| 126 | this.emptyFileTooltipDecoration.dispose(); |
| 127 | |
| 128 | if (this.debounceTimer) { |
| 129 | clearTimeout(this.debounceTimer); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | private debouncedSelectionChange( |
| 134 | editor: vscode.TextEditor, |
| 135 | selection: vscode.Selection, |
| 136 | ) { |
| 137 | if (this.debounceTimer) { |
| 138 | clearTimeout(this.debounceTimer); |
| 139 | } |
| 140 | |
| 141 | this.debounceTimer = setTimeout(() => { |
| 142 | // Clear decoration from previous editor |
| 143 | if (this.lastActiveEditor && this.lastActiveEditor !== editor) { |
| 144 | this.lastActiveEditor.setDecorations(this.svgTooltipDecoration, []); |
| 145 | } |
| 146 | |
| 147 | this.lastActiveEditor = editor; |
| 148 | |
| 149 | this.updateTooltipPosition(editor, selection); |
| 150 | }, SVG_CONFIG.debounceDelay); |
| 151 | } |
| 152 | |
| 153 | private setupSvgTipListeners() { |
| 154 | vscode.workspace.onDidChangeConfiguration((e) => { |
| 155 | if (e.affectsConfiguration("workbench.colorTheme")) { |
| 156 | this.theme = getTheme(); |
| 157 | this.createSvgTooltip(); |
| 158 | } |
| 159 | }); |
| 160 | |
| 161 | vscode.workspace.onDidChangeConfiguration((e) => { |
| 162 | if (e.affectsConfiguration("editor.fontSize")) { |
| 163 | this.createSvgTooltip(); |
| 164 | } |
| 165 | }); |
| 166 | } |
| 167 | |
| 168 | private shouldRenderTip(uri: vscode.Uri): boolean { |
| 169 | const isAllowedUri = |
| 170 | !this.excludedURIPrefixes.some((prefix) => |
| 171 | uri.toString().startsWith(prefix), |
| 172 | ) && uri.scheme !== "comment"; |
| 173 | |
| 174 | const isEnabled = |
| 175 | !!vscode.workspace |
| 176 | .getConfiguration(EXTENSION_NAME) |
| 177 | .get<boolean>("showInlineTip") === true; |
| 178 | |
| 179 | return isAllowedUri && isEnabled; |
| 180 | } |
| 181 | |
| 182 | private setupEmptyFileTips(context: vscode.ExtensionContext) { |
| 183 | context.subscriptions.push( |
| 184 | vscode.window.onDidChangeActiveTextEditor((editor) => { |
| 185 | if ( |
| 186 | editor?.document.getText() === "" && |
| 187 | this.shouldRenderTip(editor.document.uri) |
| 188 | ) { |
| 189 | editor.setDecorations(this.emptyFileTooltipDecoration, [ |
| 190 | { |
| 191 | range: new vscode.Range( |
| 192 | new vscode.Position(0, Number.MAX_VALUE), |
| 193 | new vscode.Position(0, Number.MAX_VALUE), |
| 194 | ), |
| 195 | }, |
| 196 | ]); |
| 197 | } |
| 198 | }), |
| 199 | ); |
| 200 | |
| 201 | context.subscriptions.push( |
| 202 | vscode.workspace.onDidChangeTextDocument((e) => { |
| 203 | if ( |
| 204 | e.document.getText() === "" && |
| 205 | this.shouldRenderTip(e.document.uri) |
| 206 | ) { |
| 207 | vscode.window.visibleTextEditors.forEach((editor) => { |
| 208 | editor.setDecorations(this.emptyFileTooltipDecoration, [ |
| 209 | { |
| 210 | range: new vscode.Range( |
| 211 | new vscode.Position(0, Number.MAX_VALUE), |
| 212 | new vscode.Position(0, Number.MAX_VALUE), |
| 213 | ), |
| 214 | }, |
| 215 | ]); |
| 216 | }); |
| 217 | } else { |
| 218 | vscode.window.visibleTextEditors.forEach((editor) => { |
| 219 | editor.setDecorations(this.emptyFileTooltipDecoration, []); |
| 220 | }); |
| 221 | } |
| 222 | }), |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | private createEmptyFileTooltipDecoration() { |
| 227 | return vscode.window.createTextEditorDecorationType({ |
| 228 | after: { |
| 229 | contentText: `Use ${getMetaKeyName()} + I to generate code`, |
| 230 | color: "#888", |
| 231 | margin: "2em 0 0 0", |
| 232 | fontStyle: "italic", |
| 233 | }, |
| 234 | }); |
| 235 | } |
| 236 | |
| 237 | private createSvgTooltipDecoration() { |
| 238 | var backgroundColour = "#333333"; |
| 239 | if (this.theme) { |
| 240 | backgroundColour = this.theme.colors["editor.background"]; |
| 241 | } |
| 242 | return vscode.window.createTextEditorDecorationType({ |
| 243 | after: { |
| 244 | contentIconPath: this.svgTooltip, |
| 245 | border: `;box-shadow: inset 0 0 0 ${SVG_CONFIG.strokeWidth}px ${SVG_CONFIG.stroke}, inset 0 0 0 ${SVG_CONFIG.tipHeight}px ${backgroundColour}; |
| 246 | border-radius: ${SVG_CONFIG.radius}px; |
| 247 | filter: ${SVG_CONFIG.filter}`, |
| 248 | margin: `0 0 0 ${SVG_CONFIG.leftMargin}px`, |
| 249 | width: `${SVG_CONFIG.tipWidth}px`, |
| 250 | }, |
| 251 | }); |
| 252 | } |
| 253 | |
| 254 | private createSvgTooltip() { |
| 255 | const baseTextConfig = { |
| 256 | y: SVG_CONFIG.textY, |
| 257 | "font-family": SVG_CONFIG.fontFamily, |
| 258 | "font-size": SVG_CONFIG.fontSize, |
| 259 | }; |
| 260 | |
| 261 | // if (!this.theme) { |
| 262 | // return; |
| 263 | // } |
| 264 | |
| 265 | try { |
| 266 | const builder = svgBuilder.newInstance |
| 267 | ? svgBuilder.newInstance() |
| 268 | : svgBuilder; |
| 269 | const svgContent = builder |
| 270 | .width(SVG_CONFIG.tipWidth) |
| 271 | .height(SVG_CONFIG.tipHeight) |
| 272 | // Chat |
| 273 | .text( |
| 274 | { |
| 275 | ...baseTextConfig, |
| 276 | x: SVG_CONFIG.chatLabelX, |
| 277 | fill: this.theme?.colors["editor.foreground"] ?? SVG_CONFIG.stroke, |
| 278 | }, |
| 279 | SVG_CONFIG.chatLabel, |
| 280 | ) |
| 281 | .text( |
| 282 | { |
| 283 | ...baseTextConfig, |
| 284 | x: SVG_CONFIG.chatShortcutX, |
| 285 | fill: SVG_CONFIG.shortcutColor, |
| 286 | }, |
| 287 | SVG_CONFIG.chatShortcut, |
| 288 | ) |
| 289 | // Edit |
| 290 | .text( |
| 291 | { |
| 292 | ...baseTextConfig, |
| 293 | x: SVG_CONFIG.editLabelX, |
| 294 | fill: this.theme?.colors["editor.foreground"] ?? SVG_CONFIG.stroke, |
| 295 | }, |
| 296 | SVG_CONFIG.editLabel, |
| 297 | ) |
| 298 | .text( |
| 299 | { |
| 300 | ...baseTextConfig, |
| 301 | x: SVG_CONFIG.editShortcutX, |
| 302 | fill: SVG_CONFIG.shortcutColor, |
| 303 | }, |
| 304 | SVG_CONFIG.editShortcut, |
| 305 | ) |
| 306 | .render(); |
| 307 | |
| 308 | const dataUri = `data:image/svg+xml;base64,${Buffer.from(svgContent).toString("base64")}`; |
| 309 | |
| 310 | this.svgTooltip = vscode.Uri.parse(dataUri); |
| 311 | this.svgTooltipDecoration.dispose(); |
| 312 | this.svgTooltipDecoration = this.createSvgTooltipDecoration(); |
| 313 | } catch (error) { |
| 314 | console.error("Error creating SVG for inline tip:", error); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | private buildHideTooltipHoverMsg() { |
| 319 | const hoverMarkdown = new vscode.MarkdownString( |
| 320 | `[Disable](command:${this.hideCommand})`, |
| 321 | ); |
| 322 | |
| 323 | hoverMarkdown.isTrusted = true; |
| 324 | hoverMarkdown.supportHtml = true; |
| 325 | return hoverMarkdown; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Calculates tooltip position using these rules: |
| 330 | * 1. For single-line selection: Place after the line's content |
| 331 | * 2. For multi-line selection: Place after the longer line between: |
| 332 | * - The first non-empty selected line |
| 333 | * - The line above the selection |
| 334 | * Returns null if selection is empty or contains only empty lines |
| 335 | */ |
| 336 | private calculateTooltipPosition( |
| 337 | editor: vscode.TextEditor, |
| 338 | selection: vscode.Selection, |
| 339 | ): vscode.Position | null { |
| 340 | const document = editor.document; |
| 341 | |
| 342 | // Get selection info |
| 343 | const startLine = selection.start.line; |
| 344 | const endLine = selection.end.line; |
| 345 | const isFullLineSelection = |
| 346 | selection.start.character === 0 && |
| 347 | (selection.end.line > selection.start.line |
| 348 | ? selection.end.character === 0 |
| 349 | : selection.end.character === |
| 350 | document.lineAt(selection.end.line).text.length); |
| 351 | |
| 352 | // Helper functions |
| 353 | const isLineEmpty = (lineNumber: number): boolean => { |
| 354 | return document.lineAt(lineNumber).text.trim().length === 0; |
| 355 | }; |
| 356 | |
| 357 | const getLineEndChar = (lineNumber: number): number => { |
| 358 | return document.lineAt(lineNumber).text.trimEnd().length; |
| 359 | }; |
| 360 | |
| 361 | // If single empty line selected and not full line selection, return null |
| 362 | if ( |
| 363 | startLine === endLine && |
| 364 | isLineEmpty(startLine) && |
| 365 | !isFullLineSelection |
| 366 | ) { |
| 367 | return null; |
| 368 | } |
| 369 | |
| 370 | // Find topmost non-empty line |
| 371 | let topNonEmptyLine = startLine; |
| 372 | while (topNonEmptyLine <= endLine && isLineEmpty(topNonEmptyLine)) { |
| 373 | topNonEmptyLine++; |
| 374 | } |
| 375 | |
| 376 | // If all lines empty, return null |
| 377 | if (topNonEmptyLine > endLine) { |
| 378 | return null; |
| 379 | } |
| 380 | |
| 381 | const OFFSET = 4; // Characters to offset from end of line |
| 382 | |
| 383 | // Single line or full line selection |
| 384 | if (isFullLineSelection || startLine === endLine) { |
| 385 | return new vscode.Position( |
| 386 | topNonEmptyLine, |
| 387 | getLineEndChar(topNonEmptyLine) + OFFSET, |
| 388 | ); |
| 389 | } |
| 390 | |
| 391 | // Check line above selection |
| 392 | const lineAboveSelection = Math.max(0, startLine - 1); |
| 393 | |
| 394 | // Get end positions |
| 395 | const topNonEmptyEndChar = getLineEndChar(topNonEmptyLine); |
| 396 | const lineAboveEndChar = getLineEndChar(lineAboveSelection); |
| 397 | |
| 398 | const baseEndChar = Math.max(topNonEmptyEndChar, lineAboveEndChar); |
| 399 | |
| 400 | return new vscode.Position(topNonEmptyLine, baseEndChar + OFFSET); |
| 401 | } |
| 402 | |
| 403 | private updateTooltipPosition( |
| 404 | editor: vscode.TextEditor, |
| 405 | selection: vscode.Selection, |
| 406 | ) { |
| 407 | const position = this.calculateTooltipPosition(editor, selection); |
| 408 | |
| 409 | if (!position) { |
| 410 | editor.setDecorations(this.svgTooltipDecoration, []); |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | editor.setDecorations(this.svgTooltipDecoration, [ |
| 415 | { |
| 416 | range: new vscode.Range(position, position), |
| 417 | hoverMessage: [this.buildHideTooltipHoverMsg()], |
| 418 | }, |
| 419 | ]); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | export default function setupInlineTips(context: vscode.ExtensionContext) { |
| 424 | InlineTipManager.getInstance().setupInlineTips(context); |
| 425 | } |