From f0ef04a6a8035e0c72989da4b6a88a9fe204ae2e Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Tue, 22 Mar 2016 18:34:59 +0300 Subject: [PATCH] Changed highlighting range for unresolved annotation name to not include '@' #KT-11529 Fixed --- .../diagnostics/PositioningStrategies.kt | 7 +++++-- .../jetbrains/kotlin/psi/psiUtil/ktPsiUtil.kt | 17 ----------------- .../diagnostics/tests/PackageInTypePosition.kt | 2 +- .../AmbigiousAnnotationConstructor.kt | 2 +- .../tests/annotations/AnnotationForObject.kt | 2 +- .../tests/annotations/DanglingMixed.kt | 18 +++++++++--------- .../tests/annotations/MultiDeclaration.kt | 2 +- .../UnresolvedAnnotationOnObject.kt | 2 +- .../forParameterAnnotationResolve.kt | 2 +- .../tests/annotations/kt1860-negative.kt | 8 ++++---- .../tests/annotations/onInitializer.kt | 4 ++-- .../annotations/unresolvedReferenceRange.kt | 6 +++--- .../inner/selfAnnotationForClassObject.kt | 2 +- .../tests/modifiers/IllegalModifiers.kt | 2 +- .../diagnostics/tests/regressions/ea65509.kt | 2 +- .../TypeKindHighlightingVisitor.java | 18 +++++++++++++++--- ...AnnotationOnNamedParameterOfFunctionType.kt | 2 +- .../FunctionLiteralInsideAnnotation.kt | 2 +- idea/testData/highlighter/Annotations.kt | 4 ++-- 19 files changed, 51 insertions(+), 53 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt index 7fd3e67123b..6a35e852f10 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt @@ -24,7 +24,10 @@ import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.lexer.KtModifierKeywordToken import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.* +import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType +import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.utils.sure object PositioningStrategies { @@ -219,7 +222,7 @@ object PositioningStrategies { return ranges } } - return listOf(element.getHighlightingRange()) + return listOf(element.textRange) } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/ktPsiUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/ktPsiUtil.kt index ea6ba046d9e..4ad0b18d3fe 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/ktPsiUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/psiUtil/ktPsiUtil.kt @@ -150,23 +150,6 @@ fun KtExpression.getQualifiedExpressionForReceiverOrThis(): KtExpression { fun KtExpression.isDotReceiver(): Boolean = (parent as? KtDotQualifiedExpression)?.getReceiverExpression() == this -fun KtReferenceExpression.getHighlightingRange(): TextRange { - // include '@' symbol if the reference is the first segment of KtAnnotationEntry - // if "Deprecated" is highlighted then '@' should be highlighted too in "@Deprecated" - val annotationEntry = - PsiTreeUtil.getParentOfType( - this, KtAnnotationEntry::class.java, /* strict = */false, KtValueArgumentList::class.java - ) - - val atSymbol = annotationEntry?.atSymbol - return if (atSymbol != null && atSymbol.endOffset == this.startOffset) { - TextRange(atSymbol.startOffset, this.endOffset) - } - else { - this.textRange - } -} - // ---------- Block expression ------------------------------------------------------------------------------------------------------------- fun KtElement.blockExpressionsOrSingle(): Sequence = diff --git a/compiler/testData/diagnostics/tests/PackageInTypePosition.kt b/compiler/testData/diagnostics/tests/PackageInTypePosition.kt index 41dc371632b..8a6e37b341e 100644 --- a/compiler/testData/diagnostics/tests/PackageInTypePosition.kt +++ b/compiler/testData/diagnostics/tests/PackageInTypePosition.kt @@ -6,4 +6,4 @@ package foo // FILE: b.kt -@foo fun bar(p: foo): foo = null!! \ No newline at end of file +@foo fun bar(p: foo): foo = null!! \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/AmbigiousAnnotationConstructor.kt b/compiler/testData/diagnostics/tests/annotations/AmbigiousAnnotationConstructor.kt index c5e43c40d1c..8f8271728a3 100644 --- a/compiler/testData/diagnostics/tests/annotations/AmbigiousAnnotationConstructor.kt +++ b/compiler/testData/diagnostics/tests/annotations/AmbigiousAnnotationConstructor.kt @@ -1,5 +1,5 @@ import java.util.ArrayList @ArrayList(1, 1) fun b() {} -@Xoo(x) fun c() {} +@Xoo(x) fun c() {} @java.lang.Deprecated(x) fun a() {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/AnnotationForObject.kt b/compiler/testData/diagnostics/tests/annotations/AnnotationForObject.kt index 1c983e84865..890cd6b12c0 100644 --- a/compiler/testData/diagnostics/tests/annotations/AnnotationForObject.kt +++ b/compiler/testData/diagnostics/tests/annotations/AnnotationForObject.kt @@ -1,3 +1,3 @@ // Check that there won't be "Rewrite at slice ANNOTATION key" exception - EA-36935 -@someErrorAnnotation object Test { +@someErrorAnnotation object Test { } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/DanglingMixed.kt b/compiler/testData/diagnostics/tests/annotations/DanglingMixed.kt index f3d759215d4..62a023634f4 100644 --- a/compiler/testData/diagnostics/tests/annotations/DanglingMixed.kt +++ b/compiler/testData/diagnostics/tests/annotations/DanglingMixed.kt @@ -4,19 +4,19 @@ annotation class Ann2 class C { fun foo() { class Local { - @Ann0 - @Ann @Ann3 + @Ann0 + @Ann @Ann3 @Ann2(1) - @Ann4 + @Ann4 } } - @Ann0 - @Ann @Ann3 + @Ann0 + @Ann @Ann3 @Ann2(1) - @Ann4 + @Ann4 } -@Ann0 -@Ann @Ann3 +@Ann0 +@Ann @Ann3 @Ann2(1) -@Ann4 \ No newline at end of file +@Ann4 \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/MultiDeclaration.kt b/compiler/testData/diagnostics/tests/annotations/MultiDeclaration.kt index 6609488aad8..65b9584f3e5 100644 --- a/compiler/testData/diagnostics/tests/annotations/MultiDeclaration.kt +++ b/compiler/testData/diagnostics/tests/annotations/MultiDeclaration.kt @@ -4,6 +4,6 @@ data class Pair(val x: Int, val y: Int) fun foo(): Int { @Ann val (a, b) = Pair(12, 34) - @Err val (c, d) = Pair(56, 78) + @Err val (c, d) = Pair(56, 78) return a + b + c + d } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/UnresolvedAnnotationOnObject.kt b/compiler/testData/diagnostics/tests/annotations/UnresolvedAnnotationOnObject.kt index 85f6f537376..57fdbcf9af6 100644 --- a/compiler/testData/diagnostics/tests/annotations/UnresolvedAnnotationOnObject.kt +++ b/compiler/testData/diagnostics/tests/annotations/UnresolvedAnnotationOnObject.kt @@ -3,7 +3,7 @@ package test // Checks that there is no rewrite error at ANNOTATION slice because of resolving annotations for object in lazy resolve and resolving // object as property (method tries to resolve annotations too). -@BadAnnotation +@BadAnnotation object SomeObject val some = SomeObject \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/forParameterAnnotationResolve.kt b/compiler/testData/diagnostics/tests/annotations/forParameterAnnotationResolve.kt index aad4e21f9a1..8878679890e 100644 --- a/compiler/testData/diagnostics/tests/annotations/forParameterAnnotationResolve.kt +++ b/compiler/testData/diagnostics/tests/annotations/forParameterAnnotationResolve.kt @@ -10,5 +10,5 @@ fun foo() { for (@Ann(3) (x, @Ann(4) y) in bar()) {} - for (@Err() (x,y) in bar()) {} + for (@Err() (x,y) in bar()) {} } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/kt1860-negative.kt b/compiler/testData/diagnostics/tests/annotations/kt1860-negative.kt index dcff8916ab7..c01858c41b6 100644 --- a/compiler/testData/diagnostics/tests/annotations/kt1860-negative.kt +++ b/compiler/testData/diagnostics/tests/annotations/kt1860-negative.kt @@ -1,9 +1,9 @@ -fun foo(@varargs f : Int) {} +fun foo(@varargs f : Int) {} var bar : Int = 1 - set(@varargs v) {} + set(@varargs v) {} -val x : (Int) -> Int = {@varargs x : Int -> x} +val x : (Int) -> Int = {@varargs x : Int -> x} -class Hello(@varargs args: Any) { +class Hello(@varargs args: Any) { } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/onInitializer.kt b/compiler/testData/diagnostics/tests/annotations/onInitializer.kt index 9ea6fc65bb1..9da6d3d8d8a 100644 --- a/compiler/testData/diagnostics/tests/annotations/onInitializer.kt +++ b/compiler/testData/diagnostics/tests/annotations/onInitializer.kt @@ -1,11 +1,11 @@ class A { @ann init {} - @aaa init {} + @aaa init {} } interface T { @ann init {} - @aaa init {} + @aaa init {} } annotation class ann \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/annotations/unresolvedReferenceRange.kt b/compiler/testData/diagnostics/tests/annotations/unresolvedReferenceRange.kt index a6cadb8f310..c478ee26d62 100644 --- a/compiler/testData/diagnostics/tests/annotations/unresolvedReferenceRange.kt +++ b/compiler/testData/diagnostics/tests/annotations/unresolvedReferenceRange.kt @@ -1,5 +1,5 @@ -@Ann class A -@Ann class B -@Ann(1) class C +@Ann class A +@Ann class B +@Ann(1) class C @kotlin.Ann(1) class D @kotlin.annotation.Ann(1) class E \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inner/selfAnnotationForClassObject.kt b/compiler/testData/diagnostics/tests/inner/selfAnnotationForClassObject.kt index 892648677a2..653e6181323 100644 --- a/compiler/testData/diagnostics/tests/inner/selfAnnotationForClassObject.kt +++ b/compiler/testData/diagnostics/tests/inner/selfAnnotationForClassObject.kt @@ -1,5 +1,5 @@ class Test { - @ClassObjectAnnotation + @ClassObjectAnnotation @NestedAnnotation companion object { annotation class ClassObjectAnnotation diff --git a/compiler/testData/diagnostics/tests/modifiers/IllegalModifiers.kt b/compiler/testData/diagnostics/tests/modifiers/IllegalModifiers.kt index 19425d8c030..4ba647ee99c 100644 --- a/compiler/testData/diagnostics/tests/modifiers/IllegalModifiers.kt +++ b/compiler/testData/diagnostics/tests/modifiers/IllegalModifiers.kt @@ -1,4 +1,4 @@ -@myAnnotation public package illegal_modifiers +@myAnnotation public package illegal_modifiers abstract class A() { abstract final fun f() diff --git a/compiler/testData/diagnostics/tests/regressions/ea65509.kt b/compiler/testData/diagnostics/tests/regressions/ea65509.kt index 8c919315f19..8cc546cc126 100644 --- a/compiler/testData/diagnostics/tests/regressions/ea65509.kt +++ b/compiler/testData/diagnostics/tests/regressions/ea65509.kt @@ -1,5 +1,5 @@ // !DIAGNOSTICS: -FUNCTION_DECLARATION_WITH_NO_NAME class ClassB() { - private inner class ClassC: super.@ClassA() { + private inner class ClassC: super.@ClassA() { } } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.java b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.java index 759472bb59a..1c345b513d2 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.java +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.java @@ -20,10 +20,10 @@ import com.intellij.lang.annotation.AnnotationHolder; import com.intellij.openapi.editor.colors.TextAttributesKey; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; +import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.psi.*; -import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt; import org.jetbrains.kotlin.resolve.BindingContext; class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor { @@ -61,8 +61,20 @@ class TypeKindHighlightingVisitor extends AfterAnalysisHighlightingVisitor { } private void highlightAnnotation(@NotNull KtSimpleNameExpression expression) { - TextRange toHighlight = KtPsiUtilKt.getHighlightingRange(expression); - NameHighlighter.highlightName(holder, toHighlight, KotlinHighlightingColors.ANNOTATION); + TextRange range = expression.getTextRange(); + + // include '@' symbol if the reference is the first segment of KtAnnotationEntry + // if "Deprecated" is highlighted then '@' should be highlighted too in "@Deprecated" + KtAnnotationEntry annotationEntry = PsiTreeUtil.getParentOfType( + expression, KtAnnotationEntry.class, /* strict = */false, KtValueArgumentList.class); + if (annotationEntry != null) { + PsiElement atSymbol = annotationEntry.getAtSymbol(); + if (atSymbol != null) { + range = new TextRange(atSymbol.getTextRange().getStartOffset(), expression.getTextRange().getEndOffset()); + } + } + + NameHighlighter.highlightName(holder, range, KotlinHighlightingColors.ANNOTATION); } @Override diff --git a/idea/testData/checker/regression/AnnotationOnNamedParameterOfFunctionType.kt b/idea/testData/checker/regression/AnnotationOnNamedParameterOfFunctionType.kt index 17dade5cb78..c7b2e017825 100644 --- a/idea/testData/checker/regression/AnnotationOnNamedParameterOfFunctionType.kt +++ b/idea/testData/checker/regression/AnnotationOnNamedParameterOfFunctionType.kt @@ -1,3 +1,3 @@ -fun intercept(block: (@A key: K, (K) -> V) -> V) { +fun intercept(block: (@A key: K, (K) -> V) -> V) { } \ No newline at end of file diff --git a/idea/testData/checker/regression/FunctionLiteralInsideAnnotation.kt b/idea/testData/checker/regression/FunctionLiteralInsideAnnotation.kt index 57115c44b99..fecf4612689 100644 --- a/idea/testData/checker/regression/FunctionLiteralInsideAnnotation.kt +++ b/idea/testData/checker/regression/FunctionLiteralInsideAnnotation.kt @@ -1,4 +1,4 @@ package foo -@Anno ({ val x: Int }) +@Anno ({ val x: Int }) fun f() {} \ No newline at end of file diff --git a/idea/testData/highlighter/Annotations.kt b/idea/testData/highlighter/Annotations.kt index 264d1948577..9a9062a8028 100644 --- a/idea/testData/highlighter/Annotations.kt +++ b/idea/testData/highlighter/Annotations.kt @@ -13,11 +13,11 @@ fun foo() { bar @Ann { 1 } - @Err + @Err 5 } -@Err class Err1 +@Err class Err1 class NotAnn @NotAnn