| 362 | private static MAX_BYTES = 100000; |
| 363 | |
| 364 | async readFile(fileUri: string): Promise<string> { |
| 365 | try { |
| 366 | const uri = vscode.Uri.parse(fileUri); |
| 367 | |
| 368 | // First, check whether it's a notebook document |
| 369 | // Need to iterate over the cells to get full contents |
| 370 | const notebook = |
| 371 | vscode.workspace.notebookDocuments.find((doc) => |
| 372 | URI.equal(doc.uri.toString(), uri.toString()), |
| 373 | ) ?? |
| 374 | (uri.path.endsWith("ipynb") |
| 375 | ? await vscode.workspace.openNotebookDocument(uri) |
| 376 | : undefined); |
| 377 | if (notebook) { |
| 378 | return notebook |
| 379 | .getCells() |
| 380 | .map((cell) => cell.document.getText()) |
| 381 | .join("\n\n"); |
| 382 | } |
| 383 | |
| 384 | // Check whether it's an open document |
| 385 | const openTextDocument = vscode.workspace.textDocuments.find((doc) => |
| 386 | URI.equal(doc.uri.toString(), uri.toString()), |
| 387 | ); |
| 388 | if (openTextDocument !== undefined) { |
| 389 | return openTextDocument.getText(); |
| 390 | } |
| 391 | |
| 392 | const fileStats = await this.ideUtils.stat(uri); |
| 393 | if (fileStats === null || fileStats.size > 10 * VsCodeIde.MAX_BYTES) { |
| 394 | return ""; |
| 395 | } |
| 396 | |
| 397 | const bytes = await this.ideUtils.readFile(uri); |
| 398 | if (bytes === null) { |
| 399 | return ""; |
| 400 | } |
| 401 | |
| 402 | // Truncate the buffer to the first MAX_BYTES |
| 403 | const truncatedBytes = bytes.slice(0, VsCodeIde.MAX_BYTES); |
| 404 | const contents = new TextDecoder().decode(truncatedBytes); |
| 405 | return contents; |
| 406 | } catch (e) { |
| 407 | return ""; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | async openUrl(url: string): Promise<void> { |
| 412 | await vscode.env.openExternal(vscode.Uri.parse(url)); |