Android Studio 插件开发8替换文本

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio 插件开发8替换文本相关的知识,希望对你有一定的参考价值。

     <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 插件开发8替换文本的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 插件开发8显示插入位置

如何获取 android studio 组件(按钮、文本等)大小(宽度、高度或默认大小)

android studio 怎么整理

Android开发实战(十八):Android Studio 优秀插件:GsonFormat

如何使用Android Studio开发Gradle插件

Android?Studio?优秀插件汇总