<action id="EditorAction"
class="com.anguomob.anguo.actions.editor.EditorAction"
text="编辑器"
description="编辑器"
>
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
</action>
package com.anguomob.anguo.actions.editor
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Document
import com.intellij.psi.*
class EditorAction : AnAction() {
override fun actionPerformed(event: AnActionEvent) {
val project = event.getProject()
val editor = event.getData(CommonDataKeys.EDITOR)
// Set visibility only in the case of
// existing project editor, and selection
event.presentation.isEnabledAndVisible = (project != null
&& editor != null && editor.selectionModel.hasSelection());
val document: Document = editor!!.document
// Work off of the primary caret to get the selection info
// Work off of the primary caret to get the selection info
val primaryCaret: Caret = editor.caretModel.primaryCaret
val start: Int = primaryCaret.selectionStart
val end: Int = primaryCaret.selectionEnd
// Replace the selection with a fixed string.
// Must do this document change in a write action context.
// Replace the selection with a fixed string.
// Must do this document change in a write action context.
WriteCommandAction.runWriteCommandAction(
project
) {
document.replaceString(start, end, "editor_basics")
}
// De-select the text range that was just replaced
// De-select the text range that was just replaced
primaryCaret.removeSelection()
}
}