IDEA插件笔记

1. Setter生成源码:

https://github.com/donnie0915/GenerateAllSetter

 

2. 代码示例

PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
// final Document document = editor.getDocument();
//获取光标所在位置
final int offset = editor.getCaretModel().getOffset();
PsiElement element = psiFile.findElementAt(offset);

 

// final int lineEndOffset = document.getLineEndOffset(document.getLineNumber(offset) + 1);
// document.insertString(lineEndOffset, ";");

 

3. IDEA插件如果想修改代码,IDEA为了不影响主线程,要求使用子线程进行操作

WriteCommandAction.runWriteCommandAction(project, () -> {

  //todo 编写更改操作的代码

}

 

4. 格式化代码

CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
codeStyleManager.reformat(psiJavaFile);

 

5. 获取字段类型全名称

field.getType().getInternalCanonicalText();
 
6. 获取字段完整类名
PsiClassReferenceType psiClassReferenceType = (PsiClassReferenceType) field.getType();
PsiClass psiClass = psiClassReferenceType.resolve();
psiClass.getQualifiedName()
 
7. 根据全量限定名查找PsiClass
Project project = e.getData(LangDataKeys.PROJECT);
JavaPsiFacade javaPsiFacade = JavaPsiFacade.getInstance(project);
javaPsiFacade.findClass("com.hujiapeng.Person.PersonInner", GlobalSearchScope.allScope(project));

 

posted @ 2022-06-19 22:37  jiapeng  阅读(621)  评论(0编辑  收藏  举报