AndroidStudio插件开发8、替换文本-创新互联
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()
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网站栏目:AndroidStudio插件开发8、替换文本-创新互联
网页链接:http://tyjierui.cn/article/dcoshj.html