Skip to content

Commit

Permalink
feat(gui): add auto dev insert to code action
Browse files Browse the repository at this point in the history
Added a new file `AutoDevInsertToCodeAction.kt` in the `cc.unitmesh.devti.gui.snippet` package. This action allows users to insert selected or all text into the editor. It handles both selection and non-selection scenarios and inserts text at the correct offset. The action is enabled and visible only if the selected text editor is writable.
  • Loading branch information
phodal committed Feb 28, 2024
1 parent 062a352 commit f56d7de
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
Expand Up @@ -5,6 +5,7 @@ import cc.unitmesh.devti.provider.context.ChatContextProvider
import cc.unitmesh.devti.provider.context.ChatCreationContext
import cc.unitmesh.idea.detectLanguageLevel
import com.intellij.lang.java.JavaLanguage
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.project.Project
Expand Down
3 changes: 3 additions & 0 deletions src/233/main/resources/META-INF/autodev-core.xml
Expand Up @@ -291,6 +291,9 @@
<action id="AutoDev.ToolWindow.Snippet.CopyToClipboard"
icon="AllIcons.Actions.Copy"
class="cc.unitmesh.devti.gui.snippet.AutoDevCopyToClipboardAction"/>
<action id="AutoDev.ToolWindow.Snippet.InsertFile"
icon="cc.unitmesh.devti.AutoDevIcons.InsertCode"
class="cc.unitmesh.devti.gui.snippet.AutoDevInsertFileAction"/>
</group>

<group id="AutoDev.ToolWindow.Chat.TitleActions">
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/AutoDevIcons.kt
Expand Up @@ -37,4 +37,7 @@ object AutoDevIcons {

@JvmField
val Disliked: Icon = IconLoader.getIcon("/icons/disliked.svg", AutoDevIcons::class.java)

@JvmField
val InsertCode: Icon = IconLoader.getIcon("/icons/insert-code.svg", AutoDevIcons::class.java)
}
@@ -0,0 +1,42 @@
package cc.unitmesh.devti.gui.snippet

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.util.TextRange

class AutoDevInsertFileAction : DumbAwareAction() {
override fun actionPerformed(e: AnActionEvent) {
val editor = e.getData(com.intellij.openapi.actionSystem.PlatformDataKeys.EDITOR) ?: return
val selectionModel = if (editor.selectionModel.hasSelection()) editor.selectionModel else null
val textToPaste = selectionModel?.selectedText ?: editor.document.text.trimEnd()

val project = e.project ?: return

val selectedTextEditor = FileEditorManager.getInstance(project).selectedTextEditor
WriteCommandAction.writeCommandAction(project).compute<TextRange, RuntimeException> {
val offset = selectionModel?.selectionStart ?: selectedTextEditor?.document?.textLength ?: return@compute null
selectedTextEditor?.document?.insertString(offset, textToPaste)
TextRange.from(offset, textToPaste.length)
}
}

override fun update(e: AnActionEvent) {
val project = e.project
if (project == null) {
e.presentation.isEnabled = false
return
}

val selectedTextEditor = FileEditorManager.getInstance(project).selectedTextEditor
if (selectedTextEditor == null || !selectedTextEditor.document.isWritable) {
e.presentation.isEnabled = false
} else {
e.presentation.isEnabledAndVisible = true
}
}

override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT
}
13 changes: 13 additions & 0 deletions src/main/resources/icons/insert-code.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f56d7de

Please sign in to comment.