zoukankan      html  css  js  c++  java
  • IDEA插件(Android Studio插件)开发示例代码及bug解决

    IDEA插件(Android Studio插件)开发示例代码及bug解决

    代码在actionPerformed方法中,有个AnActionEvent e
    插件开发就是要求我们复写上述的这个方法即可,在此方法中写上我们需要处理的相关逻辑
    所有的对象都是PsiElement

    获得Project

    Project mProject = e.getData(PlatformDataKeys.PROJECT); 
    

    判断扩展名隐藏我们的Action

     //在Action显示之前,根据选中文件扩展名判定是否显示此Action 
      String extension = getFileExtension(event.getDataContext()); 
      this.getTemplatePresentation().setEnabled(extension != null && "jar".equals(extension)); 
      
    	public static String getFileExtension(DataContext dataContext) { 
    	  VirtualFile file = DataKeys.VIRTUAL_FILE.getData(dataContext); 
    	  return file == null ? null : file.getExtension(); 
    	} 
    

    创建不同的PsiElement

    参考链接

    创建对话框

    Messages.showMessageDialog("message", "title", Messages.getInformationIcon());
    

    获得class文件

    		PsiFile psiFile = e.getData(DataKeys.PSI_FILE);//获得文件
            GlobalSearchScope globalSearchScope = GlobalSearchScope.fileScope(psiFile);
            String fullName = psiFile.getName();
            String className = fullName.split("\.")[0];
            PsiClass psiClass = PsiShortNamesCache.getInstance(psiFile.getProject()).getClassesByName(className, globalSearchScope)[0];//获得class文件
    

    通关方法名找到对应java文件中的方法

    PsiMethod method = psiClass.findMethodsByName("initView", false)[0];//通过方法名找到方法
    

    获得方法中的代码

    		PsiMethod method = psiClass.findMethodsByName("onCreate", false)[0];//通过方法名找到方法
            PsiCodeBlock body = method.getBody();
            PsiStatement[] statements = body.getStatements();//这里的PsiStatement数组就是方法中的一行行代码,通过遍历就可以找到对应的代码
    		for (PsiStatement statement : statements) {
    			String s = statement.getText();//获得代码内容
    		}
    

    写文件

    WriteCommandAction.runWriteCommandAction(psiClass.getProject(), new Runnable() {
                        @Override
                        public void run() {
                           //写文件操作
                           //delete,replace,add...
                        }
                    });
    

    获得编辑器

    Editor editor = e.getData(PlatformDataKeys.EDITOR);
    

    参考

    官方开发文档

    开发后续使用出现的bug

    1、Plugin 'Plugin display name here' is incompatible with this installation

    写好插件后,导出插件到本地,然后使用Android Studio安装插件,提示“Plugin 'Plugin display name here' is incompatible with this installation”,意思是版本不兼容;
    解决办法是打开插件工程的plugin.xml文件,修改版本号

    <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
    <!--这句话的意思是最低支持的ide版本,查看当前的Android Studio版本号修改-->
    <idea-version since-build="171.0"/>
    

    IDEA的插件支持的版本最低为173,而我的Android Studio的内置IDEA的版本号为171,所以安装的时候出现了错误提示,也就是不兼容。

    所以,我们查看Android Studio的版本号(help->about)

    修改插件中的plugin.xml中的版本号即可解决问题

    2.使用插件没有效果

    原因:原本有个插件出问题了,把自己插件的错误提示给覆盖了,所以无法知道自己的插件出现了什么问题
    刚开始使用插件的时候,发现没有效果,右下角出现了个错误提示,说我当前的某个插件有错误(不是我开发的那个插件)
    然后我就把那个插件更新了,再次使用我的插件,右下角出现的错误提示终于是我的了

    3、java.lang.NullPointerException

    使用插件功能的时候,发现右下角出现错误,
    java.lang.NullPointerException at com.intellij.ide.SystemHealthMonitor.getActionName

    写好插件,导出插件到本地,然后使用Android Studio安装插件,提示“java.lang.NullPointerException at com.intellij.ide.SystemHealthMonitor.getActionName”
    从参考链接可知,具体原因不明
    但解决办法是,将所有的Action及其他Java文件都放到包下,而不是直接放在src目录下,也就是在src在新建个包,把所有的java文件放入即可解决问题

  • 相关阅读:
    STL
    STL
    视觉里程计- 位姿
    opencv
    C++ 智能指针auto_ptr、shared_ptr、unique_ptr《三》-----智能指针的选择
    C++ 智能指针auto_ptr、shared_ptr、unique_ptr《二》-----为何unique_ptr优于auto_ptr
    C++ 智能指针auto_ptr、shared_ptr、unique_ptr《一》-----智能指针初识
    DBow中,TF-IDF了解
    网络爬虫(CrawlSpider)
    python3 获取cookie
  • 原文地址:https://www.cnblogs.com/stars-one/p/10473936.html
Copyright © 2011-2022 走看看