Skip to content

Commit

Permalink
to(vscode): request fit line range
Browse files Browse the repository at this point in the history
add ChatFeature to ChatViewProvider for smart apply functionality
  • Loading branch information
Sma1lboy committed Sep 11, 2024
1 parent 6241339 commit b2a370a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
20 changes: 19 additions & 1 deletion clients/vscode/src/chat/ChatViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type { AgentFeature as Agent } from "../lsp/AgentFeature";
import { createClient } from "./chatPanel";
import { GitProvider } from "../git/GitProvider";
import { getLogger } from "../logger";
import { ChatFeature } from "../lsp/ChatFeature";

export class ChatViewProvider implements WebviewViewProvider {
webview?: WebviewView;
Expand All @@ -39,6 +40,7 @@ export class ChatViewProvider implements WebviewViewProvider {
private readonly agent: Agent,
private readonly logger: LogOutputChannel,
private readonly gitProvider: GitProvider,
private readonly chat: ChatFeature,
) {}

static getFileContextFromSelection({
Expand Down Expand Up @@ -204,8 +206,24 @@ export class ChatViewProvider implements WebviewViewProvider {
});
}
},
onSmartApplyInEditor: (content: string) => {
onSmartApplyInEditor: async (languageId: string, content: string) => {
const editor = window.activeTextEditor;
if (!editor) {
window.showErrorMessage("No active editor found.");
return;
}
if (editor.document.languageId !== languageId) {
window.showErrorMessage("The active editor is not in the correct language.");
return;
}

getLogger("Tabby").info("Smart apply in editor is not implemented yet.", content);
const lineRangeRes = await this.chat.provideLineRange({
uri: editor.document.uri.toString(),
applyCode: content,
});
getLogger().info("asd ", lineRangeRes?.start, lineRangeRes?.end);
//TODO: inline edit to apply code into range
},
onLoaded: () => {
setTimeout(() => {
Expand Down
2 changes: 1 addition & 1 deletion clients/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function activate(context: ExtensionContext) {
});

// Register chat panel
const chatViewProvider = new ChatViewProvider(context, client.agent, logger, gitProvider);
const chatViewProvider = new ChatViewProvider(context, client.agent, logger, gitProvider, client.chat);
context.subscriptions.push(
window.registerWebviewViewProvider("tabby.chatView", chatViewProvider, {
webviewOptions: { retainContextWhenHidden: true },
Expand Down
16 changes: 16 additions & 0 deletions clients/vscode/src/lsp/ChatFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ import {
ChatEditResolveParams,
ApplyWorkspaceEditParams,
ApplyWorkspaceEditRequest,
ChatLineRangeSmartApplyParams,
ChatLineRangeSmartApplyRequest,
ChatLineRangeSmartApplyResult,
} from "tabby-agent";
import { diffLines } from "diff";
import { getLogger } from "../logger";

export class ChatFeature extends EventEmitter implements DynamicFeature<unknown> {
private registration: string | undefined = undefined;
Expand Down Expand Up @@ -127,6 +131,18 @@ export class ChatFeature extends EventEmitter implements DynamicFeature<unknown>
return this.client.sendRequest(ChatEditRequest.method, params, token);
}

async provideLineRange(
params: ChatLineRangeSmartApplyParams,
token?: CancellationToken,
): Promise<ChatLineRangeSmartApplyResult | null> {
if (!this.isAvailable) {
return null;
}
const res = await this.client.sendRequest(ChatLineRangeSmartApplyRequest.type, params, token);
getLogger().info("provideLineRange", res);
return res;
}

private async handleApplyWorkspaceEdit(params: ApplyWorkspaceEditParams): Promise<boolean> {
const { edit, options } = params;
const activeEditor = window.activeTextEditor;
Expand Down

0 comments on commit b2a370a

Please sign in to comment.