JavaCodeStyleManager.getInstance(project).shortenClassReferences()不起作用
我不能使用JavaCodeStyleManager.getInstance(project).shortenClassReferences();
使其添加导入语句。 在sourceText
有完整的资格类名字符串像这样..
“@ com.google.gson.annotations.Expose \ n@com.google.gson.annotations.SerializedName(\”“+ jsonKey +”\“)”
public void generateFiles(PsiDirectory directory, List 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 = "")
这是因为Kotlin尚未提供com.intellij.psi.codeStyle.ReferenceAdjuster
的实现。 您可以在KT项目的“ https://youtrack.jetbrains.com/ ”上提交错误。