* Computes the effective range of text from the given document based on the user's selection. * If the selection is non-empty, returns that directly. * Otherwise, if the current line is non-empty, expands the range to include the adjacent lines. * * @param document - The text document to ext
( document: vscode.TextDocument, range: vscode.Range | vscode.Selection, )
| 63 | * @returns An EffectiveRange object containing the effective range and its text, or null if no valid text is found. |
| 64 | */ |
| 65 | static getEffectiveRange( |
| 66 | document: vscode.TextDocument, |
| 67 | range: vscode.Range | vscode.Selection, |
| 68 | ): EffectiveRange | null { |
| 69 | try { |
| 70 | const selectedText = document.getText(range) |
| 71 | if (selectedText) { |
| 72 | return { range, text: selectedText } |
| 73 | } |
| 74 | |
| 75 | const currentLine = document.lineAt(range.start.line) |
| 76 | if (!currentLine.text.trim()) { |
| 77 | return null |
| 78 | } |
| 79 | |
| 80 | const startLineIndex = Math.max(0, currentLine.lineNumber - 1) |
| 81 | const endLineIndex = Math.min(document.lineCount - 1, currentLine.lineNumber + 1) |
| 82 | |
| 83 | const effectiveRange = new vscode.Range( |
| 84 | new vscode.Position(startLineIndex, 0), |
| 85 | new vscode.Position(endLineIndex, document.lineAt(endLineIndex).text.length), |
| 86 | ) |
| 87 | |
| 88 | return { |
| 89 | range: effectiveRange, |
| 90 | text: document.getText(effectiveRange), |
| 91 | } |
| 92 | } catch (error) { |
| 93 | console.error("Error getting effective range:", error) |
| 94 | return null |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Retrieves the file path of a given text document. |
no test coverage detected