Android Studio 插件开发8、替换文本
2023-09-27 14:27:38 时间
<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()
}
}
相关文章
- Android Studio “懒人”必备插件android layout id converter
- Android VectorDrawable svg 命令详细综述
- Android 6.0 Changes
- Android Studio 使用Log
- Android基础-系统架构分析,环境搭建,下载Android Studio,AndroidDevTools,Git使用教程,Github入门,界面设计介绍
- Android Studio使用命令窗口(terminal终端)
- 【Android Studio】 资源下载
- 【HMS Core】【SDK集成】Android Studio中Gradle Version7.1+以上版本如何集成agcp插件?
- 《Android游戏开发详解》一2.16 区分类和对象
- Android 自定义注解(Annotation)
- 带你彻底明白 Android Studio 打包混淆
- Android Matrix详解
- Android的BUG(四) - Android app的卡死问题
- Android JNI入门第四篇——Android.mk文件分析
- Android Studio 小技巧/快捷键 合集
- android退出登陆后,清空之前所有的activity,进入登陆主界面
- 拿走不谢!22 个 Android Studio 优秀插件汇总
- SVN文件夹图标不正常显示解决方案(win10)android studio
- android UI跨线程操作
- Android Studio 工具之 Json 工具 Gson 的源码和各个版本 jar 下载地址整理
- Android Studio工程结构介绍
- Android控件开发——ListView
- Android Studio 统计代码行数插件 — Statistic