IDEA插件杂记

16 篇文章 0 订阅
6 篇文章 0 订阅

官方资料

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字母。也可用于检测语法错误。

淘宝花钱买的最新版!需要的拿去! This asset obfuscates your code to make it harder for bad guys to reverse engineer your projects. Specifically designed for Unity, it seamlessly links in with its build process. The top priority of this package is to work straight out of the box with no extra steps required. While other obfuscators can stop a game from working, Beebyte's obfuscator looks for specific Unity related code that must be protected. The contents of your source files are unchanged, the obfuscation targets the compiled assembly. Features: - Supports IL2CPP - Supports Assembly Definition Files (Unity 2017.3+) - Removes Namespaces without any conflicts - Recognises Unity related code that must not be changed - Renames Classes (including MonoBehaviours) - Renames Methods - Renames Parameters - Renames Fields - Renames Properties - Renames Events - String literal obfuscation - Adds fake methods - Easy and extensive customisation using the Unity inspector window - Consistent name translations are possible across multiple builds and developers - Semantically secure cryptographic naming convention for renamed members The asset works for both Unity Free and Unity Pro version 4.2.0 onwards (including Unity 5 & 2017 & 2018). Build targets include Standalone, Android, iOS, WebGL, UWP. Other platforms are not guaranteed or supported but may become supported at a future date. IL2CPP builds are much harder to reverse engineer but strings and member information (class, method names etc) are visible in the global-metadata.dat file. Obfuscation will apply to this file adding further security. Why not complement your security with the Anti-Cheat Toolkit - a great third party asset. For more information about the Obfuscator, please see the FAQ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值