IDEA插件杂记
官方资料
https://plugins.jetbrains.com/docs/intellij/welcome.html?from=jetbrains.org
官方文档很糙,很多东西需要自己啃,蛛丝马迹自己找。
插入代码格式化
通过插件自动插入代码到当前IDE开发环境中,对插入的代码基于当前编码风格格式化:
// 获取PsiDocumentManger,以便在后面可以进行格式化操作
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(mProject);
documentManager.commitDocument(mDocument);
CodeStyleManager styleManager = CodeStyleManager.getInstance(mProject);
PsiElement psiFile = mEvent.getData(LangDataKeys.PSI_FILE);
if (psiFile != null) {
// 选中插入的代码
mEditor.getSelectionModel().setSelection(startOffset, endOffset);
// 格式化插入的代码
styleManager.reformatRange(psiFile, startOffset, endOffset); // startOffset和endOffset:插入代码的起始和结束偏移
}
自动补全
随着代码的敲入,实时推荐菜单项并自动补全代码
plugin.xml配置:
<extensions defaultExtensionNs="com.intellij">
<completion.contributor language="any" implementationClass="com.plugin.demo.TestCompletionContributor" order="first"/>
</extensions>
TestCompletionContributor类实现:
// 继承 CompletionContributor
public class TestCompletionContributor extends CompletionContributor {
// 构造方法,仅执行一次。可在此处添加推荐菜单项
public TestCompletionContributor() {
extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider<CompletionParameters>() {
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
result.addElement(LookupElementBuilder.create("Hello"));
result.addElement(LookupElementBuilder.create("Hao"));
result.addElement(LookupElementBuilder.create("Test"));
}
});
}
// 可在此处添加推荐菜单项,IDEA中每敲入一个字符,则会触发一次此方法
@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
super.fillCompletionVariants(parameters, result); // 此result为通过extend添加的菜单项
final PsiElement element = parameters.getOriginalPosition();
// 简单用法
result.addElement(LookupElementBuilder.create("Demo"));
// 复杂用法
result.addElement(LookupElementBuilder.create("匹配本文,最终回车插入内容")
.withTypeText("最右侧提示文本")
.withLookupString("额外的匹配文本1")
.withLookupString("额外的匹配文本2")
.withPresentableText("一级提示文本")
.withCaseSensitivity(true)// 大小写不敏感
.appendTailText("二级提示文本", true)
.withItemTextForeground(Color.BLUE)// 一级提示文本颜色
.withStrikeoutness(true)// 添加表示废弃的删除线
.withInsertHandler(new InsertHandler<LookupElement>() { // 二次处理插入内容
@Override
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement item) {
Document document = context.getDocument();
int pos = context.getSelectionEndOffset();
// 插入补全代码
document.insertString(pos, "补全代码");
// 移动光标位置
pos += content.length();
context.getEditor().getCaretModel().getCurrentCaret().moveToOffset(pos);
}
}));
}
}
Gradle添加依赖
可在gradle中正常添加三方sdk,如Retrofit、RxJava、okhttp3等:
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.squareup.retrofit2:adapter-rxjava3:2.9.0")
implementation("io.reactivex.rxjava3:rxjava:3.1.4")
implementation("com.squareup.okhttp3:okhttp:4.9.3")
implementation("com.squareup.okhttp3:logging-interceptor:4.9.3")
implementation("org.xerial:sqlite-jdbc:3.36.0.3")
implementation("io.github.java-diff-utils:java-diff-utils:4.11")
implementation("com.github.javaparser:javaparser-core:3.24.2")
implementation("com.github.javaparser:javaparser-symbol-solver-core:3.24.2")
implementation("com.github.javaparser:javaparser-core-serialization:3.24.2")
implementation("org.glassfish:javax.json:1.1.4")
implementation("com.fifesoft:rsyntaxtextarea:3.2.0")
}
添加依赖后,通过 buildPlugin 编译生成的zip包位于:build --> distributions目录下。
说明:此zip包包含了gradle添加依赖的jar包,安装时需使用此zip包,避免gradle添加的依赖找不到,无法运行。
代码混淆
使用JavaParser,实现源码级别的代码混淆,工具类文件下载:CodeParser工具类下载
基于javaparser实现的java源码AST解析和混淆,而不必基于字节码。一行代码即可引用:CodeParser.codeParser(“Java代码”)。通过AST解析后,混淆代码的方法名、参数、变量名,可全部替换为空格或a ~ z、A ~ Z字母。也可用于检测语法错误。