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);