Skip to content

Commit

Permalink
feat(icon): add support for auto dev insert file action
Browse files Browse the repository at this point in the history
This commit modifies the `AutoDevInsertToCodeAction.kt` file in order to add support for the auto dev insert file action in the GUI. It imports `PlatformDataKeys` to access editor data and performs a command action to insert selected text or the entire text into the editor.
  • Loading branch information
phodal committed Feb 28, 2024
1 parent f56d7de commit 66c2e96
Showing 1 changed file with 4 additions and 6 deletions.
Expand Up @@ -2,24 +2,22 @@ package cc.unitmesh.devti.gui.snippet

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
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 project = e.project ?: return
val editor = e.getData(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> {
WriteCommandAction.writeCommandAction(project).compute<Any, RuntimeException> {
val offset = selectionModel?.selectionStart ?: selectedTextEditor?.document?.textLength ?: return@compute null
selectedTextEditor?.document?.insertString(offset, textToPaste)
TextRange.from(offset, textToPaste.length)
}
}

Expand Down

0 comments on commit 66c2e96

Please sign in to comment.