Open read files in editor instead of code accordian

This commit is contained in:
Saoud Rizwan
2024-09-11 16:38:33 -04:00
parent dd01fc7fc4
commit 0522a26fd9
7 changed files with 103 additions and 26 deletions

View File

@@ -1138,7 +1138,7 @@ export class ClaudeDev {
const message = JSON.stringify({
tool: "readFile",
path: this.getReadablePath(relPath),
content,
content: absolutePath,
} as ClaudeSayTool)
if (this.alwaysAllowReadOnly) {
await this.say("tool", message)

View File

@@ -10,7 +10,7 @@ import fs from "fs/promises"
import { HistoryItem } from "../shared/HistoryItem"
import axios from "axios"
import { getTheme } from "../utils/getTheme"
import { openImage } from "../utils/open-image"
import { openFile, openImage } from "../utils/open-file"
/*
https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
@@ -402,6 +402,9 @@ export class ClaudeDevProvider implements vscode.WebviewViewProvider {
case "openImage":
openImage(message.text!)
break
case "openFile":
openFile(message.text!)
break
// Add more switch case statements here as more webview message commands
// are created within the webview context (i.e. inside media/main.js)
}

View File

@@ -18,6 +18,7 @@ export interface WebviewMessage {
| "resetState"
| "requestOllamaModels"
| "openImage"
| "openFile"
text?: string
askResponse?: ClaudeAskResponse
apiConfiguration?: ApiConfiguration

50
src/utils/open-file.ts Normal file
View File

@@ -0,0 +1,50 @@
import * as path from "path"
import * as os from "os"
import * as vscode from "vscode"
export async function openImage(dataUri: string) {
const matches = dataUri.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/)
if (!matches) {
vscode.window.showErrorMessage("Invalid data URI format")
return
}
const [, format, base64Data] = matches
const imageBuffer = Buffer.from(base64Data, "base64")
const tempFilePath = path.join(os.tmpdir(), `temp_image_${Date.now()}.${format}`)
try {
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFilePath), imageBuffer)
await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(tempFilePath))
} catch (error) {
vscode.window.showErrorMessage(`Error opening image: ${error}`)
}
}
export async function openFile(absolutePath: string) {
try {
const uri = vscode.Uri.file(absolutePath)
// Check if the document is already open in a tab group that's not in the active editor's column. If it is, then close it (if not dirty) so that we don't duplicate tabs
try {
for (const group of vscode.window.tabGroups.all) {
const existingTab = group.tabs.find(
(tab) => tab.input instanceof vscode.TabInputText && tab.input.uri.fsPath === uri.fsPath
)
if (existingTab) {
const activeColumn = vscode.window.activeTextEditor?.viewColumn
const tabColumn = vscode.window.tabGroups.all.find((group) =>
group.tabs.includes(existingTab)
)?.viewColumn
if (activeColumn && activeColumn !== tabColumn && !existingTab.isDirty) {
await vscode.window.tabGroups.close(existingTab)
}
break
}
}
} catch {} // not essential, sometimes tab operations fail
const document = await vscode.workspace.openTextDocument(uri)
await vscode.window.showTextDocument(document, { preview: false })
} catch (error) {
vscode.window.showErrorMessage(`Could not open file!`)
}
}

View File

@@ -1,20 +0,0 @@
import * as path from "path"
import * as os from "os"
import * as vscode from "vscode"
export async function openImage(dataUri: string) {
const matches = dataUri.match(/^data:image\/([a-zA-Z]+);base64,(.+)$/)
if (!matches) {
vscode.window.showErrorMessage("Invalid data URI format")
return
}
const [, format, base64Data] = matches
const imageBuffer = Buffer.from(base64Data, "base64")
const tempFilePath = path.join(os.tmpdir(), `temp_image_${Date.now()}.${format}`)
try {
await vscode.workspace.fs.writeFile(vscode.Uri.file(tempFilePath), imageBuffer)
await vscode.commands.executeCommand("vscode.open", vscode.Uri.file(tempFilePath))
} catch (error) {
vscode.window.showErrorMessage(`Error opening image: ${error}`)
}
}