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/ ”上提交错误。
- 在kotlin中为Gradle脚本配置IntelliJ自动完成
- 使用yeoman生成器Gradle + Kotlin无法生成新项目
- Kotlin android扩展,软件包不能被导入
- Gradle kotlin脚本,>找不到id为`kotlin`的插件
- 为什么Netbeans抱怨模糊,而Intellij却没有
- IntelliJ(使用gradle):即使已经安装,也找不到Kotlin插件
- 如何获得IntelliJ IDEA代码完成作为Kotlin顶级函数导入静态Java方法?
- Intellij IDEAexception构建过程终止(XMx700m)
- IntelliJ:Kotlin错误导致鼠标闪烁和代码中的错误