-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(intellij): add chat panel actions. (#3316)
- Loading branch information
Showing
19 changed files
with
448 additions
and
113 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/OpenChatToolWindow.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/AddFileToChat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.tabbyml.intellijtabby.chat.ChatBrowser | ||
|
||
class AddFileToChat : ChatAction(object : ChatActionHandler { | ||
override fun doExecute(editor: Editor, chatBrowser: ChatBrowser) { | ||
chatBrowser.addActiveEditorAsContext(false) | ||
} | ||
}) |
14 changes: 14 additions & 0 deletions
14
...nts/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/AddSelectionToChat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.tabbyml.intellijtabby.chat.ChatBrowser | ||
|
||
class AddSelectionToChat : ChatAction(object : ChatActionHandler { | ||
override fun doExecute(editor: Editor, chatBrowser: ChatBrowser) { | ||
chatBrowser.addActiveEditorAsContext(true) | ||
} | ||
|
||
override fun isEnabled(editor: Editor, chatBrowser: ChatBrowser?): Boolean { | ||
return editor.selectionModel.let { it.hasSelection() && !it.selectedText.isNullOrBlank() } | ||
} | ||
}) |
43 changes: 43 additions & 0 deletions
43
clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/ChatAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.actionSystem.DataContext | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.editor.Caret | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.editor.actionSystem.EditorAction | ||
import com.intellij.openapi.editor.actionSystem.EditorActionHandler | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.wm.ToolWindowManager | ||
import com.tabbyml.intellijtabby.chat.ChatBrowser | ||
import com.tabbyml.intellijtabby.chat.ChatBrowserFactory | ||
import com.tabbyml.intellijtabby.widgets.ChatToolWindowFactory | ||
|
||
abstract class ChatAction(private val chatActionHandler: ChatActionHandler) : | ||
EditorAction(object : EditorActionHandler() { | ||
private fun openChatToolWindow(project: Project, runnable: Runnable?) { | ||
val toolWindowManager = ToolWindowManager.getInstance(project) | ||
val toolWindow = toolWindowManager.getToolWindow(ChatToolWindowFactory.TOOL_WINDOW_ID) ?: return | ||
toolWindow.show(runnable) | ||
} | ||
|
||
private fun findActiveChatBrowser(editor: Editor): ChatBrowser? { | ||
val project = editor.project ?: return null | ||
val toolWindowManager = ToolWindowManager.getInstance(project) | ||
val toolWindow = toolWindowManager.getToolWindow(ChatToolWindowFactory.TOOL_WINDOW_ID) ?: return null | ||
val chatBrowserFactory = project.service<ChatBrowserFactory>() | ||
return chatBrowserFactory.getChatBrowser(toolWindow) | ||
} | ||
|
||
override fun doExecute(editor: Editor, caret: Caret?, dataContext: DataContext?) { | ||
val project = editor.project ?: return | ||
openChatToolWindow(project) { | ||
val chatBrowser = findActiveChatBrowser(editor) ?: return@openChatToolWindow | ||
chatActionHandler.doExecute(editor, chatBrowser) | ||
} | ||
} | ||
|
||
override fun isEnabledForCaret(editor: Editor, caret: Caret, dataContext: DataContext?): Boolean { | ||
val chatBrowser = findActiveChatBrowser(editor) | ||
return chatActionHandler.isEnabled(editor, chatBrowser) | ||
} | ||
}) |
11 changes: 11 additions & 0 deletions
11
clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/ChatActionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.tabbyml.intellijtabby.chat.ChatBrowser | ||
|
||
interface ChatActionHandler { | ||
fun doExecute(editor: Editor, chatBrowser: ChatBrowser) | ||
fun isEnabled(editor: Editor, chatBrowser: ChatBrowser?): Boolean { | ||
return true | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/Explain.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.tabbyml.intellijtabby.chat.ChatBrowser | ||
|
||
class Explain : ChatAction(object : ChatActionHandler { | ||
override fun doExecute(editor: Editor, chatBrowser: ChatBrowser) { | ||
chatBrowser.explainSelectedText() | ||
} | ||
|
||
override fun isEnabled(editor: Editor, chatBrowser: ChatBrowser?): Boolean { | ||
return editor.selectionModel.let { it.hasSelection() && !it.selectedText.isNullOrBlank() } | ||
} | ||
}) |
14 changes: 14 additions & 0 deletions
14
clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/Fix.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.tabbyml.intellijtabby.chat.ChatBrowser | ||
|
||
class Fix : ChatAction(object : ChatActionHandler { | ||
override fun doExecute(editor: Editor, chatBrowser: ChatBrowser) { | ||
chatBrowser.fixSelectedText() | ||
} | ||
|
||
override fun isEnabled(editor: Editor, chatBrowser: ChatBrowser?): Boolean { | ||
return editor.selectionModel.let { it.hasSelection() && !it.selectedText.isNullOrBlank() } | ||
} | ||
}) |
14 changes: 14 additions & 0 deletions
14
clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/GenerateDocs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.tabbyml.intellijtabby.chat.ChatBrowser | ||
|
||
class GenerateDocs : ChatAction(object : ChatActionHandler { | ||
override fun doExecute(editor: Editor, chatBrowser: ChatBrowser) { | ||
chatBrowser.generateDocsForSelectedText() | ||
} | ||
|
||
override fun isEnabled(editor: Editor, chatBrowser: ChatBrowser?): Boolean { | ||
return editor.selectionModel.let { it.hasSelection() && !it.selectedText.isNullOrBlank() } | ||
} | ||
}) |
14 changes: 14 additions & 0 deletions
14
clients/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/GenerateTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.tabbyml.intellijtabby.chat.ChatBrowser | ||
|
||
class GenerateTests : ChatAction(object : ChatActionHandler { | ||
override fun doExecute(editor: Editor, chatBrowser: ChatBrowser) { | ||
chatBrowser.generateTestsForSelectedText() | ||
} | ||
|
||
override fun isEnabled(editor: Editor, chatBrowser: ChatBrowser?): Boolean { | ||
return editor.selectionModel.let { it.hasSelection() && !it.selectedText.isNullOrBlank() } | ||
} | ||
}) |
14 changes: 14 additions & 0 deletions
14
...nts/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/OpenChatToolWindow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.wm.ToolWindowManager | ||
import com.tabbyml.intellijtabby.widgets.ChatToolWindowFactory | ||
|
||
class OpenChatToolWindow : AnAction() { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
val toolWindowManager = e.project?.let { ToolWindowManager.getInstance(it) } ?: return | ||
val toolWindow = toolWindowManager.getToolWindow(ChatToolWindowFactory.TOOL_WINDOW_ID) ?: return | ||
toolWindow.show() | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...s/intellij/src/main/kotlin/com/tabbyml/intellijtabby/actions/chat/ToggleChatToolWindow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.tabbyml.intellijtabby.actions.chat | ||
|
||
import com.intellij.openapi.actionSystem.AnAction | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.fileEditor.FileEditorManager | ||
import com.intellij.openapi.wm.IdeFocusManager | ||
import com.intellij.openapi.wm.ToolWindowManager | ||
import com.tabbyml.intellijtabby.actionPromoter.HasPriority | ||
import com.tabbyml.intellijtabby.chat.ChatBrowserFactory | ||
import com.tabbyml.intellijtabby.widgets.ChatToolWindowFactory | ||
|
||
class ToggleChatToolWindow : AnAction(), HasPriority { | ||
override fun actionPerformed(e: AnActionEvent) { | ||
val project = e.project ?: return | ||
val toolWindowManager = ToolWindowManager.getInstance(project) | ||
val toolWindow = toolWindowManager.getToolWindow(ChatToolWindowFactory.TOOL_WINDOW_ID) ?: return | ||
|
||
val editor = FileEditorManager.getInstance(project).selectedTextEditor | ||
val chatBrowserFactory = project.service<ChatBrowserFactory>() | ||
val chatBrowser = chatBrowserFactory.getChatBrowser(toolWindow) | ||
if (toolWindow.isActive) { | ||
if (editor != null) { | ||
IdeFocusManager.getInstance(project).requestFocus(editor.contentComponent, true) | ||
} | ||
} else { | ||
toolWindow.show { | ||
toolWindow.activate { | ||
if (editor != null && chatBrowser != null && editor.selectionModel.let { it.hasSelection() && !it.selectedText.isNullOrBlank() }) { | ||
chatBrowser.addActiveEditorAsContext(true) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override val priority: Int = 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.