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()
}
}
成都创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为余庆企业提供专业的网站设计、成都网站设计,余庆网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网页题目:AndroidStudio插件开发8、替换文本-创新互联
本文路径:http://ybzwz.com/article/dcoshj.html