Skip to content

Commit

Permalink
chore(vscode): add relevant context menu in context menu (#2916)
Browse files Browse the repository at this point in the history
* chore(vscode): add relevant context menu in context menu

* Update clients/vscode/package.json

* chore(vscode): support add file context (#2918)

* chore(vscode): support add file context

* Update clients/vscode/package.json

Co-authored-by: Zhiming Ma <[email protected]>

---------

Co-authored-by: Zhiming Ma <[email protected]>

---------

Co-authored-by: Zhiming Ma <[email protected]>
  • Loading branch information
wsxiaoys and icycodes authored Aug 20, 2024
1 parent fc9f6c1 commit bbf4519
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
29 changes: 29 additions & 0 deletions clients/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"title": "Add Selection to Chat",
"category": "Tabby"
},
{
"command": "tabby.chat.addFileContext",
"title": "Add File to Chat",
"category": "Tabby"
},
{
"command": "tabby.chat.explainCodeBlock",
"title": "Explain This",
Expand Down Expand Up @@ -130,7 +135,25 @@
"category": "Tabby"
}
],
"submenus": [
{
"id": "tabby.submenu",
"label": "Tabby"
}
],
"menus": {
"tabby.submenu": [
{
"command": "tabby.chat.addRelevantContext",
"when": "editorHasSelection && tabby.chatEnabled",
"group": "tabby.submenu@1"
},
{
"command": "tabby.chat.addFileContext",
"when": "tabby.chatEnabled",
"group": "tabby.submenu@2"
}
],
"commandPalette": [
{
"command": "tabby.inlineCompletion.trigger",
Expand Down Expand Up @@ -176,6 +199,12 @@
"command": "tabby.chat.edit.discard",
"when": "false"
}
],
"editor/context": [
{
"submenu": "tabby.submenu",
"group": "2_tabby"
}
]
},
"walkthroughs": [
Expand Down
11 changes: 11 additions & 0 deletions clients/vscode/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ export class Commands {
"chat.addRelevantContext": async () => {
this.addRelevantContext();
},
"chat.addFileContext": () => {
const editor = window.activeTextEditor;
if (editor) {
commands.executeCommand("tabby.chatView.focus").then(() => {
const fileContext = ChatViewProvider.getFileContextFromEditor({ editor, gitProvider: this.gitProvider });
this.chatViewProvider.addRelevantContext(fileContext);
});
} else {
window.showInformationMessage("No active editor");
}
},
"chat.fixCodeBlock": async () => {
this.sendMessageToChatPanel("Identify and fix potential bugs in the selected code:");
},
Expand Down
48 changes: 37 additions & 11 deletions clients/vscode/src/chat/ChatViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,7 @@ export class ChatViewProvider implements WebviewViewProvider {
const text = editor.document.getText(editor.selection);
if (!text) return null;

const workspaceFolder = workspace.getWorkspaceFolder(uri);
const repo = gitProvider.getRepository(uri);
const remoteUrl = repo ? gitProvider.getDefaultRemoteUrl(repo) : undefined;
let filePath = uri.toString(true);
if (repo) {
filePath = filePath.replace(repo.rootUri.toString(true), "");
} else if (workspaceFolder) {
filePath = filePath.replace(workspaceFolder.uri.toString(true), "");
}
const { filepath, git_url } = resolveFilePathAndGitUrl(uri, gitProvider);

return {
kind: "file",
Expand All @@ -81,8 +73,25 @@ export class ChatViewProvider implements WebviewViewProvider {
start: editor.selection.start.line + 1,
end: editor.selection.end.line + 1,
},
filepath: filePath.startsWith("/") ? filePath.substring(1) : filePath,
git_url: remoteUrl ?? "",
filepath,
git_url,
};
}

static getFileContextFromEditor({ editor, gitProvider }: { editor: TextEditor; gitProvider: GitProvider }): Context {
const content = editor.document.getText();
const lineCount = editor.document.lineCount;
const uri = editor.document.uri;
const { filepath, git_url } = resolveFilePathAndGitUrl(uri, gitProvider);
return {
kind: "file",
content,
range: {
start: 1,
end: lineCount,
},
filepath,
git_url,
};
}

Expand Down Expand Up @@ -482,3 +491,20 @@ export class ChatViewProvider implements WebviewViewProvider {
.join("-");
}
}

function resolveFilePathAndGitUrl(uri: Uri, gitProvider: GitProvider): { filepath: string; git_url: string } {
const workspaceFolder = workspace.getWorkspaceFolder(uri);
const repo = gitProvider.getRepository(uri);
const remoteUrl = repo ? gitProvider.getDefaultRemoteUrl(repo) : undefined;
let filePath = uri.toString(true);
if (repo) {
filePath = filePath.replace(repo.rootUri.toString(true), "");
} else if (workspaceFolder) {
filePath = filePath.replace(workspaceFolder.uri.toString(true), "");
}

return {
filepath: filePath.startsWith("/") ? filePath.substring(1) : filePath,
git_url: remoteUrl ?? "",
};
}

0 comments on commit bbf4519

Please sign in to comment.