|
| 1 | +import type { CodingContext } from './types' |
| 2 | + |
| 3 | +import { useLogger } from '@guiiai/logg' |
| 4 | + |
| 5 | +import * as vscode from 'vscode' |
| 6 | + |
| 7 | +/** |
| 8 | + * Collector for coding context in VSCode |
| 9 | + */ |
| 10 | +export class ContextCollector { |
| 11 | + constructor( |
| 12 | + private readonly contextLines: number = 5, |
| 13 | + ) {} |
| 14 | + |
| 15 | + /** |
| 16 | + * Collect context from the current active editor |
| 17 | + */ |
| 18 | + async collect(editor: vscode.TextEditor): Promise<CodingContext | null> { |
| 19 | + try { |
| 20 | + const document = editor.document |
| 21 | + const position = editor.selection.active |
| 22 | + |
| 23 | + // File information |
| 24 | + const file = { |
| 25 | + path: document.uri.fsPath, |
| 26 | + languageId: document.languageId, |
| 27 | + fileName: document.fileName, |
| 28 | + workspaceFolder: this.getWorkspaceFolder(document.uri), |
| 29 | + } |
| 30 | + |
| 31 | + // Cursor position |
| 32 | + const cursor = { |
| 33 | + line: position.line, |
| 34 | + character: position.character, |
| 35 | + } |
| 36 | + |
| 37 | + // Selected text |
| 38 | + const selection = editor.selection.isEmpty |
| 39 | + ? undefined |
| 40 | + : { |
| 41 | + text: document.getText(editor.selection), |
| 42 | + start: { |
| 43 | + line: editor.selection.start.line, |
| 44 | + character: editor.selection.start.character, |
| 45 | + }, |
| 46 | + end: { |
| 47 | + line: editor.selection.end.line, |
| 48 | + character: editor.selection.end.character, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + // Current line |
| 53 | + const currentLine = { |
| 54 | + lineNumber: position.line, |
| 55 | + text: document.lineAt(position.line).text, |
| 56 | + } |
| 57 | + |
| 58 | + // Context (N lines before and after) |
| 59 | + const context = this.getContext(document, position.line) |
| 60 | + |
| 61 | + // Git information (simplified, can be extended later) |
| 62 | + const git = await this.getGitInfo(document.uri) |
| 63 | + |
| 64 | + return { |
| 65 | + file, |
| 66 | + cursor, |
| 67 | + selection, |
| 68 | + currentLine, |
| 69 | + context, |
| 70 | + git, |
| 71 | + timestamp: Date.now(), |
| 72 | + } |
| 73 | + } |
| 74 | + catch (error) { |
| 75 | + useLogger().errorWithError('Failed to collect context:', error) |
| 76 | + return null |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get context before and after the current line |
| 82 | + */ |
| 83 | + private getContext(document: vscode.TextDocument, currentLine: number) { |
| 84 | + const before: string[] = [] |
| 85 | + const after: string[] = [] |
| 86 | + |
| 87 | + // Get preceding lines |
| 88 | + const startLine = Math.max(0, currentLine - this.contextLines) |
| 89 | + for (let i = startLine; i < currentLine; i++) { |
| 90 | + before.push(document.lineAt(i).text) |
| 91 | + } |
| 92 | + |
| 93 | + // Get following lines |
| 94 | + const endLine = Math.min(document.lineCount - 1, currentLine + this.contextLines) |
| 95 | + for (let i = currentLine + 1; i <= endLine; i++) { |
| 96 | + after.push(document.lineAt(i).text) |
| 97 | + } |
| 98 | + |
| 99 | + return { before, after } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get workspace folder path |
| 104 | + */ |
| 105 | + private getWorkspaceFolder(uri: vscode.Uri): string | undefined { |
| 106 | + const folder = vscode.workspace.getWorkspaceFolder(uri) |
| 107 | + return folder?.uri.fsPath |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get Git information (simplified) |
| 112 | + */ |
| 113 | + private async getGitInfo(uri: vscode.Uri): Promise<{ branch: string, isDirty: boolean } | undefined> { |
| 114 | + try { |
| 115 | + const gitExtension = vscode.extensions.getExtension('vscode.git')?.exports |
| 116 | + if (!gitExtension) |
| 117 | + return undefined |
| 118 | + |
| 119 | + const git = gitExtension.getAPI(1) |
| 120 | + const repo = git.getRepository(uri) |
| 121 | + if (!repo) |
| 122 | + return undefined |
| 123 | + |
| 124 | + return { |
| 125 | + branch: repo.state.HEAD?.name ?? 'unknown', |
| 126 | + isDirty: repo.state.workingTreeChanges.length > 0, |
| 127 | + } |
| 128 | + } |
| 129 | + catch { |
| 130 | + return undefined |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments