* Builds the editor context from the provided text editor or from the active text editor. * The context includes file path, effective selected text, and any diagnostics that intersect with the effective range. * * @param editor - (Optional) A specific text editor instance. If not provided, the
(editor?: vscode.TextEditor)
| 173 | * @returns An EditorContext object if successful; otherwise, null. |
| 174 | */ |
| 175 | static getEditorContext(editor?: vscode.TextEditor): EditorContext | null { |
| 176 | try { |
| 177 | if (!editor) { |
| 178 | editor = vscode.window.activeTextEditor |
| 179 | } |
| 180 | if (!editor) { |
| 181 | return null |
| 182 | } |
| 183 | |
| 184 | const document = editor.document |
| 185 | const selection = editor.selection |
| 186 | const effectiveRange = this.getEffectiveRange(document, selection) |
| 187 | |
| 188 | if (!effectiveRange) { |
| 189 | return null |
| 190 | } |
| 191 | |
| 192 | const filePath = this.getFilePath(document) |
| 193 | const diagnostics = vscode.languages |
| 194 | .getDiagnostics(document.uri) |
| 195 | .filter((d) => this.hasIntersectingRange(effectiveRange.range, d.range)) |
| 196 | .map(this.createDiagnosticData) |
| 197 | |
| 198 | return { |
| 199 | filePath, |
| 200 | selectedText: effectiveRange.text, |
| 201 | startLine: effectiveRange.range.start.line + 1, // Convert to 1-based line numbers |
| 202 | endLine: effectiveRange.range.end.line + 1, // Convert to 1-based line numbers |
| 203 | ...(diagnostics.length > 0 ? { diagnostics } : {}), |
| 204 | } |
| 205 | } catch (error) { |
| 206 | console.error("Error getting editor context:", error) |
| 207 | return null |
| 208 | } |
| 209 | } |
| 210 | } |
no test coverage detected