mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 13:02:30 +07:00
[Java. Code Formatting] Add check to TypeAnnotationUtil that language level is at least 8
IDEA-353192 GitOrigin-RevId: e8d2d8270dbc1c39300d5a2d55ef993e433ccdbc
This commit is contained in:
committed by
intellij-monorepo-bot
parent
7fe1e933e6
commit
41cf8f6fe3
@@ -2,7 +2,9 @@
|
||||
package com.intellij.psi.formatter.java
|
||||
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import com.intellij.pom.java.LanguageLevel
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.formatter.java.TypeAnnotationUtil.KNOWN_TYPE_ANNOTATIONS
|
||||
import com.intellij.psi.formatter.java.TypeAnnotationUtil.isTypeAnnotation
|
||||
@@ -32,10 +34,14 @@ internal object TypeAnnotationUtil {
|
||||
@JvmStatic
|
||||
fun isTypeAnnotation(annotation: ASTNode): Boolean {
|
||||
val node = annotation.psi as? PsiAnnotation ?: return false
|
||||
|
||||
val languageLevel = LanguageLevelProjectExtension.getInstance(node.project).languageLevel
|
||||
if (languageLevel.isLessThan(LanguageLevel.JDK_1_8)) return false
|
||||
|
||||
val next = PsiTreeUtil.skipSiblingsForward(node, PsiWhiteSpace::class.java, PsiAnnotation::class.java)
|
||||
if (next is PsiKeyword) return false
|
||||
|
||||
val psiReference: PsiJavaCodeReferenceElement = node.nameReferenceElement ?: return false
|
||||
|
||||
if (psiReference.isQualified) {
|
||||
return KNOWN_TYPE_ANNOTATIONS.contains(getCanonicalTextOfTheReference(psiReference))
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ public interface MyTestClass {
|
||||
}
|
||||
|
||||
public class MyRealTestClass implements MyTestClass {
|
||||
@NotNull String implementMe(String arg) {
|
||||
@NotNull
|
||||
String implementMe(String arg) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.example;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class Formatter {
|
||||
@NotNull @Nullable String breakLineBetweenAnnotations() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@NotNull String breakLineBetweenTypeAndAnnotations() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable <T> String breakLineBetweenTypeParameterAndAnnotation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@NotNull <T> String breakLineMixed() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.example;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class Formatter {
|
||||
@NotNull
|
||||
@Nullable
|
||||
String breakLineBetweenAnnotations() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@NotNull
|
||||
String breakLineBetweenTypeAndAnnotations() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
<T> String breakLineBetweenTypeParameterAndAnnotation() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@NotNull
|
||||
<T> String breakLineMixed() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,11 @@ import com.intellij.application.options.CodeStyle
|
||||
import com.intellij.lang.java.JavaLanguage
|
||||
import com.intellij.openapi.command.WriteCommandAction
|
||||
import com.intellij.openapi.roots.ModuleRootModificationUtil
|
||||
import com.intellij.pom.java.LanguageLevel
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings.WRAP_ALWAYS
|
||||
import com.intellij.testFramework.IdeaTestUtil
|
||||
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
|
||||
|
||||
@@ -22,6 +24,7 @@ class TypeAnnotationFormatterTest : LightJavaCodeInsightFixtureTestCase() {
|
||||
super.setUp()
|
||||
commonSettings.KEEP_LINE_BREAKS = false
|
||||
commonSettings.METHOD_ANNOTATION_WRAP = WRAP_ALWAYS
|
||||
IdeaTestUtil.setProjectLanguageLevel(myFixture.project, LanguageLevel.JDK_1_8)
|
||||
ModuleRootModificationUtil.updateModel(module, DefaultLightProjectDescriptor::addJetBrainsAnnotations)
|
||||
}
|
||||
|
||||
@@ -52,6 +55,11 @@ class TypeAnnotationFormatterTest : LightJavaCodeInsightFixtureTestCase() {
|
||||
doTest()
|
||||
}
|
||||
|
||||
fun testLowLanguageLevel() {
|
||||
IdeaTestUtil.setProjectLanguageLevel(myFixture.project, LanguageLevel.JDK_1_7)
|
||||
doTest()
|
||||
}
|
||||
|
||||
private fun doTest() {
|
||||
val testName = getTestName(false)
|
||||
myFixture.configureByFile("$testName.java")
|
||||
|
||||
@@ -2,5 +2,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
interface A {
|
||||
|
||||
@Nullable <T, U> U foo(U u, T t);
|
||||
@Nullable
|
||||
<T, U> U foo(U u, T t);
|
||||
}
|
||||
Reference in New Issue
Block a user