generated from JetBrains/intellij-platform-plugin-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gui): add auto dev insert to code action
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
Showing
5 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
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
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
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/cc/unitmesh/devti/gui/snippet/AutoDevInsertToCodeAction.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,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 | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.