Skip to content

Commit

Permalink
Implement new shortcut Shift+Cmd+L for opening chat (#66)
Browse files Browse the repository at this point in the history
* Implement new shortcut for opening chat

* formatting

* Doc/function rename

---------

Co-authored-by: Duke Pan <59063950+Fryingpannn@users.noreply.github.com>
  • Loading branch information
2 people authored and Nathan A committed Oct 3, 2024
1 parent 26a7bb9 commit 6a4ab22
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const outgoingMessageSchema = zod.discriminatedUnion("type", [
zod.object({
type: zod.literal("startChat"),
}),
zod.object({
type: zod.literal("openChat"),
}),
zod.object({
type: zod.literal("enterOpenAIApiKey"),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ export class ChatController {
await this.chatPanel.update(this.chatModel);
}

private async addAndShowConversation<T extends Conversation>(
/**
* Opens and displays the specified conversation.
* @param conversation The conversation to show.
* @returns The specified conversation.
*/
private async showConversation<T extends Conversation>(
conversation: T
): Promise<T> {
this.chatModel.addAndSelectConversation(conversation);

this.chatModel.selectConversation(conversation);
await this.showChatPanel();
await this.updateChatPanel();

Expand All @@ -75,8 +79,13 @@ export class ChatController {
break;
}
case "clickCollapsedConversation": {
this.chatModel.selectedConversationId = message.data.id;
await this.updateChatPanel();
const conversation = this.chatModel.getConversationById(
message.data.id
);
if (conversation) {
this.chatModel.selectConversation(conversation);
await this.updateChatPanel();
}
break;
}
case "sendMessage": {
Expand All @@ -89,6 +98,10 @@ export class ChatController {
await this.createConversation(this.basicChatTemplateId);
break;
}
case "openChat": {
await this.showLastSelectedConversationOrCreateNew();
break;
}
case "deleteConversation": {
this.chatModel.deleteConversation(message.data.id);
await this.updateChatPanel();
Expand Down Expand Up @@ -162,7 +175,8 @@ export class ChatController {
return;
}

await this.addAndShowConversation(result.conversation);
await this.chatModel.addAndSelectConversation(result.conversation);
await this.showConversation(result.conversation);

if (result.shouldImmediatelyAnswer) {
await result.conversation.answer();
Expand All @@ -173,4 +187,21 @@ export class ChatController {
await vscode.window.showErrorMessage(error?.message ?? error);
}
}

private getLastSelectedConversation() {
return this.chatModel.getLastSelectedConversation();
}

/**
* Opens and displays the last selected conversation, if one exists.
* If there is no last selected conversation, creates a new one first.
*/
async showLastSelectedConversationOrCreateNew() {
const lastSelectedConversation = this.getLastSelectedConversation();
if (!lastSelectedConversation) {
await this.createConversation("chat-en");
return;
}
await this.showConversation(lastSelectedConversation);
}
}
16 changes: 15 additions & 1 deletion extensions/pearai-extension/lib/extension/src/chat/ChatModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@ export class ChatModel {
conversations: Array<Conversation> = [];
selectedConversationId: string | undefined;

selectConversation(conversation: Conversation) {
this.selectedConversationId = conversation.id;
}

addAndSelectConversation(conversation: Conversation) {
this.conversations.push(conversation);
this.selectedConversationId = conversation.id;
this.selectConversation(conversation);
}

getConversationById(id: string): Conversation | undefined {
return this.conversations.find((conversation) => conversation.id === id);
}

/**
* @returns The last selected conversation, if it exists.
*/
getLastSelectedConversation(): Conversation | undefined {
if (!this.selectedConversationId) {
return;
}
return this.getConversationById(this.selectedConversationId);
}

deleteConversation(id: string) {
this.conversations = this.conversations.filter(
(conversation) => conversation.id !== id
Expand Down
3 changes: 3 additions & 0 deletions extensions/pearai-extension/lib/extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export const activate = async (context: vscode.ExtensionContext) => {
vscode.commands.registerCommand("pearai.startChat", () => {
chatController.createConversation("chat-en");
}),
vscode.commands.registerCommand("pearai.openChat", () => {
chatController.showLastSelectedConversationOrCreateNew();
}),
vscode.commands.registerCommand("pearai.editCode", () => {
chatController.createConversation("edit-code");
}),
Expand Down
19 changes: 17 additions & 2 deletions extensions/pearai-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
"category": "PearAI",
"icon": "$(comment-discussion)"
},
{
"command": "pearai.openChat",
"title": "Open Chat",
"category": "PearAI"
},
{
"command": "pearai.editCode",
"title": "Edit Code 🖊️",
Expand Down Expand Up @@ -243,11 +248,21 @@
{
"command": "pearai.startChat",
"when": "isMac",
"key": "Cmd+l"
"key": "Shift+Cmd+l"
},
{
"command": "pearai.startChat",
"when": "!isMac",
"key": "Shift+Alt+l"
},
{
"command": "pearai.openChat",
"when": "isMac",
"key": "Cmd+l"
},
{
"command": "pearai.openChat",
"when": "!isMac",
"key": "Alt+l"
},
{
Expand Down Expand Up @@ -436,4 +451,4 @@
"typescript": "4.9.5",
"vitest": "0.28.3"
}
}
}

0 comments on commit 6a4ab22

Please sign in to comment.