-
-
Notifications
You must be signed in to change notification settings - Fork 113
Add inline console.log display feature #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4142c7b
Initial plan
Copilot 0c122a9
Add inline console.log display feature with configuration option
Copilot 5ade4e7
Fix type checking and linting issues
Copilot e07ec67
Add documentation for inline console.log feature
Copilot 2b61db7
Fix stack trace parsing and enable feature by default
Copilot e52b90e
Use parseErrorStacktrace from @vitest/utils/source-map in worker threads
Copilot bbe6f87
Fix console log issues: enable printConsoleTrace, fix accumulation, s…
Copilot 57619e2
Use stripVTControlCharacters and fix multi-editor console log display
Copilot 9876e66
Clear console logs when file is edited to prevent stale line numbers
Copilot 9abe38b
Track document changes and adjust console log line numbers dynamically
Copilot ff25649
Use VSCode's built-in location API for inline console logs
Copilot 73c40e8
fix: support browser logs
sheremet-va ce6da25
Remove showConsoleLogInline configuration option
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| import type { ExtensionUserConsoleLog } from 'vitest-vscode-shared' | ||
| import { stripVTControlCharacters } from 'node:util' | ||
| import * as vscode from 'vscode' | ||
| import { getConfig } from './config' | ||
|
|
||
| interface ConsoleLogEntry { | ||
| content: string | ||
| time: number | ||
| } | ||
|
|
||
| export class InlineConsoleLogManager extends vscode.Disposable { | ||
| private decorationType: vscode.TextEditorDecorationType | ||
| private consoleLogsByFile = new Map<string, Map<number, ConsoleLogEntry[]>>() | ||
| private disposables: vscode.Disposable[] = [] | ||
|
|
||
| constructor() { | ||
| super(() => { | ||
| this.decorationType.dispose() | ||
| this.disposables.forEach(d => d.dispose()) | ||
| this.disposables = [] | ||
| }) | ||
|
|
||
| this.decorationType = vscode.window.createTextEditorDecorationType({ | ||
| after: { | ||
| margin: '0 0 0 3em', | ||
| textDecoration: 'none', | ||
| }, | ||
| rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen, | ||
| }) | ||
|
|
||
| // Update decorations when active editor changes | ||
| this.disposables.push( | ||
| vscode.window.onDidChangeActiveTextEditor((editor) => { | ||
| if (editor) { | ||
| this.updateDecorations(editor) | ||
| } | ||
| }), | ||
| ) | ||
|
|
||
| // Update decorations when document changes | ||
| this.disposables.push( | ||
| vscode.workspace.onDidChangeTextDocument((event) => { | ||
| const file = event.document.uri.fsPath | ||
| const fileMap = this.consoleLogsByFile.get(file) | ||
|
|
||
| if (!fileMap || fileMap.size === 0) { | ||
| return | ||
| } | ||
|
|
||
| // Adjust line numbers based on document changes | ||
| for (const change of event.contentChanges) { | ||
| const startLine = change.range.start.line | ||
| const endLine = change.range.end.line | ||
| const newLineCount = change.text.split('\n').length - 1 | ||
| const oldLineCount = endLine - startLine | ||
|
|
||
| // Calculate the net change in line numbers | ||
| const lineDelta = newLineCount - oldLineCount | ||
|
|
||
| if (lineDelta !== 0) { | ||
| // Create a new map with adjusted line numbers | ||
| const newFileMap = new Map<number, ConsoleLogEntry[]>() | ||
|
|
||
| fileMap.forEach((entries, line) => { | ||
| let newLine = line | ||
|
|
||
| // If the console log is after the change, adjust its line number | ||
| if (line > endLine) { | ||
| newLine = line + lineDelta | ||
| } | ||
| // If the console log is within the changed range, keep it at the start of the change | ||
| else if (line >= startLine && line <= endLine) { | ||
| newLine = startLine + newLineCount | ||
| } | ||
|
|
||
| // Ensure line number is valid | ||
| if (newLine >= 0) { | ||
| if (!newFileMap.has(newLine)) { | ||
| newFileMap.set(newLine, []) | ||
| } | ||
| newFileMap.get(newLine)!.push(...entries) | ||
| } | ||
| }) | ||
|
|
||
| this.consoleLogsByFile.set(file, newFileMap) | ||
| } | ||
| } | ||
|
|
||
| // Update all visible editors showing this file | ||
| vscode.window.visibleTextEditors.forEach((editor) => { | ||
| if (editor.document === event.document) { | ||
| this.updateDecorations(editor) | ||
| } | ||
| }) | ||
| }), | ||
| ) | ||
|
|
||
| // Update decorations when configuration changes | ||
| this.disposables.push( | ||
| vscode.workspace.onDidChangeConfiguration((event) => { | ||
| if (event.affectsConfiguration('vitest.showConsoleLogInline')) { | ||
| this.refresh() | ||
| } | ||
| }), | ||
| ) | ||
| } | ||
|
|
||
| addConsoleLog(consoleLog: ExtensionUserConsoleLog): void { | ||
| const config = getConfig() | ||
| if (!config.showConsoleLogInline) { | ||
| return | ||
| } | ||
|
|
||
| // Use pre-parsed location from worker | ||
| if (!consoleLog.parsedLocation) { | ||
| return | ||
| } | ||
|
|
||
| const { file, line } = consoleLog.parsedLocation | ||
|
|
||
| // Store console log entry | ||
| if (!this.consoleLogsByFile.has(file)) { | ||
| this.consoleLogsByFile.set(file, new Map()) | ||
| } | ||
|
|
||
| const fileMap = this.consoleLogsByFile.get(file)! | ||
| if (!fileMap.has(line)) { | ||
| fileMap.set(line, []) | ||
| } | ||
|
|
||
| fileMap.get(line)!.push({ | ||
| content: consoleLog.content, | ||
| time: consoleLog.time, | ||
| }) | ||
|
|
||
| // Update decorations for all visible editors showing this file | ||
| vscode.window.visibleTextEditors.forEach((editor) => { | ||
| if (editor.document.uri.fsPath === file) { | ||
| this.updateDecorations(editor) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| clear(): void { | ||
| this.consoleLogsByFile.clear() | ||
| // Update all visible editors | ||
| vscode.window.visibleTextEditors.forEach(editor => this.updateDecorations(editor)) | ||
| } | ||
|
|
||
| clearFile(file: string): void { | ||
| this.consoleLogsByFile.delete(file) | ||
| // Update all visible editors showing this file | ||
| vscode.window.visibleTextEditors.forEach((editor) => { | ||
| if (editor.document.uri.fsPath === file) { | ||
| this.updateDecorations(editor) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| private updateDecorations(editor: vscode.TextEditor): void { | ||
| const config = getConfig() | ||
| if (!config.showConsoleLogInline) { | ||
| editor.setDecorations(this.decorationType, []) | ||
| return | ||
| } | ||
|
|
||
| const file = editor.document.uri.fsPath | ||
| const fileMap = this.consoleLogsByFile.get(file) | ||
|
|
||
| if (!fileMap || fileMap.size === 0) { | ||
| editor.setDecorations(this.decorationType, []) | ||
| return | ||
| } | ||
|
|
||
| const decorations: vscode.DecorationOptions[] = [] | ||
|
|
||
| fileMap.forEach((entries, line) => { | ||
| // Skip if line is out of range | ||
| if (line >= editor.document.lineCount) { | ||
| return | ||
| } | ||
|
|
||
| // Combine multiple console logs on the same line | ||
| const content = entries.map(e => this.formatContent(e.content)).join(' ') | ||
|
|
||
| const lineRange = editor.document.lineAt(line).range | ||
| const decoration: vscode.DecorationOptions = { | ||
| range: lineRange, | ||
| renderOptions: { | ||
| after: { | ||
| contentText: content, | ||
| color: new vscode.ThemeColor('editorCodeLens.foreground'), | ||
| fontStyle: 'italic', | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| decorations.push(decoration) | ||
| }) | ||
|
|
||
| editor.setDecorations(this.decorationType, decorations) | ||
| } | ||
|
|
||
| private formatContent(content: string): string { | ||
| // Strip ANSI control characters using Node.js util | ||
| const stripped = stripVTControlCharacters(content) | ||
| // Remove trailing newlines and limit length | ||
| const cleaned = stripped.trim().replace(/\n/g, ' ') | ||
| const maxLength = 100 | ||
| if (cleaned.length > maxLength) { | ||
| return `${cleaned.substring(0, maxLength)}...` | ||
| } | ||
| return cleaned | ||
| } | ||
|
|
||
| private refresh(): void { | ||
| // Update all visible editors | ||
| vscode.window.visibleTextEditors.forEach(editor => this.updateDecorations(editor)) | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot
Actually, vscode already supports showing logs. If you provide a vscode.Location here, everything will be handled automatically and you can remove your class and simplify the implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit ${git_hash}. Removed the custom
InlineConsoleLogManagerclass and simplified the implementation by using VSCode's built-in location support. Now passingvscode.LocationtotestRun.appendOutput()and VSCode handles the inline display automatically. This reduced the code by ~200 lines while providing the same functionality.