From d8b6bd5cb335e97747134432f945324b8ab7273b Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 18 Aug 2017 12:52:41 +0200 Subject: [PATCH] Decouple idea-analysis from Android Introduce API for extending syntax highlighting; use it in android component --- idea/idea-analysis/idea-analysis.iml | 1 - .../FunctionsHighlightingVisitor.kt | 2 ++ .../idea/highlighter/HighlighterExtension.kt | 30 +++++++++++++++++ .../idea/highlighter/HighlightingVisitor.kt | 14 ++++++++ .../PropertiesHighlightingVisitor.kt | 6 ++-- .../TypeKindHighlightingVisitor.kt | 1 + idea/idea-android/idea-android.iml | 1 + .../android/AndroidHighlighterExtension.kt | 32 +++++++++++++++++++ idea/src/META-INF/android.xml | 2 ++ idea/src/META-INF/extensions/ide.xml | 3 ++ 10 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/HighlighterExtension.kt create mode 100644 idea/idea-android/src/org/jetbrains/kotlin/android/AndroidHighlighterExtension.kt diff --git a/idea/idea-analysis/idea-analysis.iml b/idea/idea-analysis/idea-analysis.iml index 3f37b84dcf1..35adaa45849 100644 --- a/idea/idea-analysis/idea-analysis.iml +++ b/idea/idea-analysis/idea-analysis.iml @@ -33,7 +33,6 @@ - \ No newline at end of file diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.kt index e7d6232584d..b7aa18fb973 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/FunctionsHighlightingVisitor.kt @@ -73,6 +73,8 @@ internal class FunctionsHighlightingVisitor(holder: AnnotationHolder, bindingCon private fun highlightCall(callee: PsiElement, resolvedCall: ResolvedCall) { val calleeDescriptor = resolvedCall.resultingDescriptor + if (applyHighlighterExtensions(callee, calleeDescriptor)) return + if (calleeDescriptor.isDynamic()) { highlightName(callee, DYNAMIC_FUNCTION_CALL) } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/HighlighterExtension.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/HighlighterExtension.kt new file mode 100644 index 00000000000..4b4eb777299 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/HighlighterExtension.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.highlighter + +import com.intellij.openapi.editor.colors.TextAttributesKey +import com.intellij.openapi.extensions.ExtensionPointName +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor + +abstract class HighlighterExtension { + abstract fun highlightReference(elementToHighlight: PsiElement, descriptor: DeclarationDescriptor): TextAttributesKey? + + companion object { + val EP_NAME = ExtensionPointName.create("org.jetbrains.kotlin.highlighterExtension") + } +} \ No newline at end of file diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/HighlightingVisitor.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/HighlightingVisitor.kt index 42cc1dcb512..29a8c1c59bc 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/HighlightingVisitor.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/HighlightingVisitor.kt @@ -19,8 +19,10 @@ package org.jetbrains.kotlin.idea.highlighter import com.intellij.lang.annotation.Annotation import com.intellij.lang.annotation.AnnotationHolder import com.intellij.openapi.editor.colors.TextAttributesKey +import com.intellij.openapi.extensions.Extensions import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.psi.KtVisitorVoid abstract class HighlightingVisitor protected constructor( @@ -44,4 +46,16 @@ abstract class HighlightingVisitor protected constructor( createInfoAnnotation(textRange, message).textAttributes = attributesKey } } + + protected fun applyHighlighterExtensions(element: PsiElement, descriptor: DeclarationDescriptor): Boolean { + if (!NameHighlighter.namesHighlightingEnabled) return false + for (extension in Extensions.getExtensions(HighlighterExtension.EP_NAME)) { + val key = extension.highlightReference(element, descriptor) + if (key != null) { + highlightName(element, key) + return true + } + } + return false + } } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/PropertiesHighlightingVisitor.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/PropertiesHighlightingVisitor.kt index ccef67de56f..b8221df6005 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/PropertiesHighlightingVisitor.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/PropertiesHighlightingVisitor.kt @@ -18,7 +18,6 @@ package org.jetbrains.kotlin.idea.highlighter import com.intellij.lang.annotation.AnnotationHolder import com.intellij.psi.PsiElement -import org.jetbrains.kotlin.android.synthetic.res.AndroidSyntheticProperty import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.descriptors.impl.SyntheticFieldDescriptor import org.jetbrains.kotlin.idea.highlighter.KotlinHighlightingColors.* @@ -73,13 +72,12 @@ internal class PropertiesHighlightingVisitor(holder: AnnotationHolder, bindingCo elementToHighlight: PsiElement, descriptor: PropertyDescriptor) { + if (applyHighlighterExtensions(elementToHighlight, descriptor)) return + val attributesKey = when { descriptor.isDynamic() -> DYNAMIC_PROPERTY_CALL - descriptor is AndroidSyntheticProperty -> - ANDROID_EXTENSIONS_PROPERTY_CALL - descriptor.extensionReceiverParameter != null -> EXTENSION_PROPERTY diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.kt index defcc96fa72..333be893293 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/highlighter/TypeKindHighlightingVisitor.kt @@ -86,6 +86,7 @@ internal class TypeKindHighlightingVisitor(holder: AnnotationHolder, bindingCont val identifier = classOrObject.nameIdentifier val classDescriptor = bindingContext.get(BindingContext.CLASS, classOrObject) if (identifier != null && classDescriptor != null) { + if (applyHighlighterExtensions(identifier, classDescriptor)) return highlightName(identifier, textAttributesKeyForClass(classDescriptor)) } super.visitClassOrObject(classOrObject) diff --git a/idea/idea-android/idea-android.iml b/idea/idea-android/idea-android.iml index 6e7da04e0d4..45650c979ad 100644 --- a/idea/idea-android/idea-android.iml +++ b/idea/idea-android/idea-android.iml @@ -26,5 +26,6 @@ + \ No newline at end of file diff --git a/idea/idea-android/src/org/jetbrains/kotlin/android/AndroidHighlighterExtension.kt b/idea/idea-android/src/org/jetbrains/kotlin/android/AndroidHighlighterExtension.kt new file mode 100644 index 00000000000..09b5bbbda68 --- /dev/null +++ b/idea/idea-android/src/org/jetbrains/kotlin/android/AndroidHighlighterExtension.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.android + +import com.intellij.openapi.editor.colors.TextAttributesKey +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.android.synthetic.res.AndroidSyntheticProperty +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.idea.highlighter.HighlighterExtension +import org.jetbrains.kotlin.idea.highlighter.KotlinHighlightingColors + +class AndroidHighlighterExtension : HighlighterExtension() { + override fun highlightReference(elementToHighlight: PsiElement, descriptor: DeclarationDescriptor): TextAttributesKey? = + if (descriptor is AndroidSyntheticProperty) + KotlinHighlightingColors.ANDROID_EXTENSIONS_PROPERTY_CALL + else + null +} diff --git a/idea/src/META-INF/android.xml b/idea/src/META-INF/android.xml index 01e6be4bf4f..3e04ed2fa49 100644 --- a/idea/src/META-INF/android.xml +++ b/idea/src/META-INF/android.xml @@ -90,6 +90,8 @@ + + diff --git a/idea/src/META-INF/extensions/ide.xml b/idea/src/META-INF/extensions/ide.xml index 50f583e5d6c..f027e3a5ceb 100644 --- a/idea/src/META-INF/extensions/ide.xml +++ b/idea/src/META-INF/extensions/ide.xml @@ -13,6 +13,9 @@ + +