JavaCodeStyleManager.getInstance(project).shortenClassReferences()不起作用

时间:2018-02-03 05:42:46

标签: intellij-idea intellij-plugin

我无法使用JavaCodeStyleManager.getInstance(project).shortenClassReferences();来添加import语句。在sourceText中有完整的限定类名字符串,如此..

  

" @ com.google.gson.annotations.Expose \ n@com.google.gson.annotations.SerializedName(\""   + jsonKey +" \")"

public void generateFiles(PsiDirectory directory, List<ClassModel> classDataList) {
    final Project project = directory.getProject();

    WriteCommandAction.runWriteCommandAction(directory.getProject(), (Runnable) () -> {
        PsiFileFactory factory = PsiFileFactory.getInstance(project);
        PsiDirectoryFactory directoryFactory = PsiDirectoryFactory.getInstance(directory.getProject());
        JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(project);
        for (ClassModel classData : classDataList) {
            classData.packageName = directoryFactory.getQualifiedName(directory, true);
            String sourceText = generateFileContentForClass(classData);
            PsiFile classFile = factory.createFileFromText(getFileName(classData.name), KotlinFileType.INSTANCE, sourceText);
            manager.shortenClassReferences(classFile);

            directory.add(classFile);
        }
    });
}

生成后的当前结果

package com.xxx.yyy


data class Aaa(@com.google.gson.annotations.Expose
                @com.google.gson.annotations.SerializedName("code")
                val code: Int = 0,
                @com.google.gson.annotations.Expose
                @com.google.gson.annotations.SerializedName("data")
                val data: Data? = null,
                @com.google.gson.annotations.Expose
                @com.google.gson.annotations.SerializedName("about")
                val about: About? = null,
                @com.google.gson.annotations.Expose
                @com.google.gson.annotations.SerializedName("token_session")
                val tokenSession: String = "")

预期

package com.xxx.yyy

import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName


data class Aaa(@Expose
               @SerializedName("code")
               val code: Int = 0,
               @Expose
               @SerializedName("data")
               val data: Data? = null,
               @Expose
               @SerializedName("about")
               val about: About? = null,
               @Expose
               @SerializedName("token_session")
               val tokenSession: String = "")

1 个答案:

答案 0 :(得分:0)

This is because Kotlin doesn't provide an implementation of com.intellij.psi.codeStyle.ReferenceAdjuster yet. You can file a bug at "https://youtrack.jetbrains.com/" in KT project.

相关问题