Skip to content

Commit

Permalink
feat(intellij): add chat panel actions. (#3316)
Browse files Browse the repository at this point in the history
  • Loading branch information
icycodes authored Oct 25, 2024
1 parent df8ce08 commit 7c5a13d
Show file tree
Hide file tree
Showing 19 changed files with 448 additions and 113 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class ToggleInlineCompletionTriggerMode : AnAction() {

override fun update(e: AnActionEvent) {
if (settings.completionTriggerMode == SettingsState.TriggerMode.AUTOMATIC) {
e.presentation.text = "Switch to Manual Mode"
e.presentation.description = "Manual trigger inline completion suggestions on demand."
e.presentation.text = "Disable Auto Inline Completion"
e.presentation.description = "You can trigger inline completion manually."
} else {
e.presentation.text = "Switch to Automatic Mode"
e.presentation.text = "Enable Auto Inline Completion"
e.presentation.description = "Show inline completion suggestions automatically."
}
}
Expand Down
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)
}
})
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() }
}
})
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)
}
})
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
}
}
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() }
}
})
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() }
}
})
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() }
}
})
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() }
}
})
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()
}
}
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
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.tabbyml.intellijtabby.actions.inlineCompletion

import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.tabbyml.intellijtabby.completion.InlineCompletionService
Expand All @@ -17,9 +16,4 @@ class TabAccept : InlineCompletionAction(object : InlineCompletionActionHandler
): Boolean {
return !inlineCompletionService.isInlineCompletionStartWithIndentation()
}
}) {
override fun update(e: AnActionEvent) {
super.update(e)
e.presentation.isVisible = false
}
}
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tabbyml.intellijtabby.actions
package com.tabbyml.intellijtabby.actions.inlineCompletion

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
Expand All @@ -8,8 +8,7 @@ import com.intellij.openapi.components.serviceOrNull
import com.tabbyml.intellijtabby.actionPromoter.HasPriority
import com.tabbyml.intellijtabby.completion.InlineCompletionService


class TriggerInlineCompletion : AnAction(), HasPriority {
class Trigger : AnAction(), HasPriority {
override fun actionPerformed(e: AnActionEvent) {
val inlineCompletionService =
e.getRequiredData(CommonDataKeys.PROJECT).serviceOrNull<InlineCompletionService>() ?: return
Expand Down
Loading

0 comments on commit 7c5a13d

Please sign in to comment.