* Retrieves the file path of a given text document. * Utilizes an internal cache to avoid redundant computations. * If the document belongs to a workspace, attempts to compute a relative path; otherwise, returns the absolute fsPath. * * @param document - The text document for which to retrie
(document: vscode.TextDocument)
| 104 | * @returns The file path as a string. |
| 105 | */ |
| 106 | static getFilePath(document: vscode.TextDocument): string { |
| 107 | let filePath = this.filePathCache.get(document) |
| 108 | if (filePath) { |
| 109 | return filePath |
| 110 | } |
| 111 | |
| 112 | try { |
| 113 | const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri) |
| 114 | if (!workspaceFolder) { |
| 115 | filePath = document.uri.fsPath |
| 116 | } else { |
| 117 | const relativePath = path.relative(workspaceFolder.uri.fsPath, document.uri.fsPath) |
| 118 | filePath = !relativePath || relativePath.startsWith("..") ? document.uri.fsPath : relativePath |
| 119 | } |
| 120 | |
| 121 | this.filePathCache.set(document, filePath) |
| 122 | return filePath |
| 123 | } catch (error) { |
| 124 | console.error("Error getting file path:", error) |
| 125 | return document.uri.fsPath |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Converts a VSCode Diagnostic object to a local DiagnosticData instance. |
no test coverage detected