Skip to content

Commit

Permalink
Chat view should respond to editor selection changes
Browse files Browse the repository at this point in the history
  • Loading branch information
unixzii authored and ktiays committed Mar 27, 2023
1 parent ebc9dba commit d8a357e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/common/chatService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ export const CHAT_SERVICE_NAME = "chat";
export interface IChatService extends IService {
confirmPrompt(prompt: string): Promise<number>;
}

export const CHAT_VIEW_SERVICE_NAME = "chat_view";

export interface IChatViewService extends IService {
setHasSelection(hasSelection: boolean): Promise<void>;
}
17 changes: 17 additions & 0 deletions src/extension/chat/chatPanelProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import * as vscode from "vscode";
import { getNonce } from "../utils";
import { sharedChatServiceImpl } from "./chatServiceImpl";
import { ExtensionHostServiceManager } from "../../common/ipc/extensionHost";
import {
IChatViewService,
CHAT_VIEW_SERVICE_NAME,
} from "../../common/chatService";

export class ChatPanelProvider implements vscode.WebviewViewProvider {
static readonly viewType = "chat";
Expand Down Expand Up @@ -35,7 +39,20 @@ export class ChatPanelProvider implements vscode.WebviewViewProvider {

const serviceManager = new ExtensionHostServiceManager(webview);
serviceManager.registerService(sharedChatServiceImpl());

const eventDisposable = vscode.window.onDidChangeTextEditorSelection(
async (e) => {
const hasSelection = !e.selections[0].isEmpty;
const chatViewService =
await serviceManager.getService<IChatViewService>(
CHAT_VIEW_SERVICE_NAME
);
await chatViewService.setHasSelection(hasSelection);
}
);

webviewView.onDidDispose(() => {
eventDisposable.dispose();
serviceManager.dispose();
});
}
Expand Down
16 changes: 16 additions & 0 deletions src/webview/chat/chatViewServiceImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
IChatViewService,
CHAT_VIEW_SERVICE_NAME,
} from "../../common/chatService";

export class ChatViewServiceImpl implements IChatViewService {
setHasSelectionAction: ((hasSelection: boolean) => void) | null = null;

get name(): string {
return CHAT_VIEW_SERVICE_NAME;
}

async setHasSelection(hasSelection: boolean): Promise<void> {
this.setHasSelectionAction?.call(null, hasSelection);
}
}
10 changes: 9 additions & 1 deletion src/webview/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { VSCodeButton, VSCodeTextArea } from "@vscode/webview-ui-toolkit/react";

import "./style.css";
import { MessageItem, MessageItemModel } from "./MessageItem";
import { ChatViewServiceImpl } from "./chatViewServiceImpl";
import { getServiceManager } from "../../common/ipc/webview";
import { IChatService, CHAT_SERVICE_NAME } from "../../common/chatService";

export function ChatPage() {
const [messages, setMessages] = useState([] as MessageItemModel[]);
const [hasSelection, setHasSelection] = useState(false);
const [prompt, setPrompt] = useState("");
useEffect(() => {
setMessages(
Expand All @@ -23,6 +25,10 @@ export function ChatPage() {
};
})
);

const viewServiceImpl = new ChatViewServiceImpl();
viewServiceImpl.setHasSelectionAction = setHasSelection;
getServiceManager().registerService(viewServiceImpl);
}, []);

const handleAskAction = useCallback(async () => {
Expand Down Expand Up @@ -55,7 +61,9 @@ export function ChatPage() {
<VSCodeTextArea
style={{ width: "100%" }}
rows={3}
placeholder="Talk about the whole document..."
placeholder={`Talk about the ${
hasSelection ? "selected contents" : "whole document"
}...`}
value={prompt}
onInput={(e: React.ChangeEvent<HTMLTextAreaElement>) => {
setPrompt(e.target.value);
Expand Down

0 comments on commit d8a357e

Please sign in to comment.