highlight names: do not highlight in huge chunks because there maybe whitespaces... appcode/163.3118 clion/163.3126
authorAlexey Kudravtsev <cdr@intellij.com>
Tue, 23 Aug 2016 11:54:58 +0000 (14:54 +0300)
committerAlexey Kudravtsev <cdr@intellij.com>
Tue, 23 Aug 2016 11:54:58 +0000 (14:54 +0300)
java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightNamesUtil.java
java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/NamesHighlighting.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/scopeBased/x/Shared.java
java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlighting/scopeBased/x/X.java

index 4e00cea6c5c99ae48908ebaee7831c55f02edbe2..4ba8a25730f3125511a6c1b4a3208f69c10f6faf 100644 (file)
@@ -111,21 +111,12 @@ public class HighlightNamesUtil {
 
   @NotNull
   static HighlightInfo highlightClassName(@Nullable PsiClass aClass, @NotNull PsiElement elementToHighlight, @NotNull TextAttributesScheme colorsScheme) {
-    HighlightInfoType type = getClassNameHighlightType(aClass, elementToHighlight);
-    TextAttributes attributes = mergeWithScopeAttributes(aClass, type, colorsScheme);
     TextRange range = elementToHighlight.getTextRange();
     if (elementToHighlight instanceof PsiJavaCodeReferenceElement) {
       final PsiJavaCodeReferenceElement referenceElement = (PsiJavaCodeReferenceElement)elementToHighlight;
-      PsiReferenceParameterList parameterList = referenceElement.getParameterList();
-      if (parameterList != null) {
-        TextRange paramListRange = parameterList.getTextRange();
-        PsiElement identifier = PsiTreeUtil.skipSiblingsBackward(parameterList, PsiWhiteSpace.class, PsiComment.class); // e.g. " List    <String>";
-        if (identifier != null) {
-          paramListRange = new TextRange(identifier.getTextRange().getEndOffset(), paramListRange.getEndOffset());
-        }
-        if (!paramListRange.isEmpty()) {
-          range = new TextRange(range.getStartOffset(), paramListRange.getStartOffset());
-        }
+      PsiElement identifier = referenceElement.getReferenceNameElement();
+      if (identifier != null) {
+        range = identifier.getTextRange();
       }
     }
 
@@ -136,12 +127,13 @@ public class HighlightNamesUtil {
       range = new TextRange(psiAnnotation.getTextRange().getStartOffset(), range.getEndOffset());
     }
 
+    HighlightInfoType type = getClassNameHighlightType(aClass, elementToHighlight);
+    TextAttributes attributes = mergeWithScopeAttributes(aClass, type, colorsScheme);
     HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(range);
     if (attributes != null) {
       builder.textAttributes(attributes);
     }
-    HighlightInfo info = builder.createUnconditionally();
-    return info;
+    return builder.createUnconditionally();
   }
 
   @Nullable
@@ -316,4 +308,32 @@ public class HighlightNamesUtil {
     if (node != null) return node.getTextRange().getStartOffset();
     return textRange.getStartOffset();
   }
+
+  static HighlightInfo highlightPackage(@NotNull PsiElement resolved, @NotNull PsiJavaCodeReferenceElement elementToHighlight, @NotNull TextAttributesScheme scheme) {
+    PsiElement referenceNameElement = elementToHighlight.getReferenceNameElement();
+    TextRange range;
+    if (referenceNameElement == null) {
+      range = elementToHighlight.getTextRange();
+    }
+    else {
+      PsiElement nextSibling = PsiTreeUtil.nextLeaf(referenceNameElement);
+      if (nextSibling != null && nextSibling.getTextRange().isEmpty()) {
+        // empty PsiReferenceParameterList
+        nextSibling = PsiTreeUtil.nextLeaf(nextSibling);
+      }
+      if (nextSibling instanceof PsiJavaToken && ((PsiJavaToken)nextSibling).getTokenType() == JavaTokenType.DOT) {
+        range = new TextRange(referenceNameElement.getTextRange().getStartOffset(), nextSibling.getTextRange().getEndOffset());
+      }
+      else {
+        range = referenceNameElement.getTextRange();
+      }
+    }
+    HighlightInfoType type = JavaHighlightInfoTypes.CLASS_NAME;
+    TextAttributes attributes = mergeWithScopeAttributes(resolved, type, scheme);
+    HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(type).range(range);
+    if (attributes != null) {
+      builder.textAttributes(attributes);
+    }
+    return builder.createUnconditionally();
+  }
 }
index 25426f6e326a7ffa6fe94a601e897a15fbde615f..59d7ac4958db3d26748656bf3253d12cd9929160 100644 (file)
@@ -53,7 +53,10 @@ import gnu.trove.TObjectIntHashMap;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
-import java.util.*;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 public class HighlightVisitorImpl extends JavaElementVisitor implements HighlightVisitor {
   @NotNull
@@ -819,9 +822,6 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
 
   private void highlightReferencedMethodOrClassName(@NotNull PsiJavaCodeReferenceElement element, PsiElement resolved) {
     PsiElement parent = element.getParent();
-    if (parent instanceof PsiReferenceExpression || parent instanceof PsiJavaCodeReferenceElement) {
-      return;
-    }
     final TextAttributesScheme colorsScheme = myHolder.getColorsScheme();
     if (parent instanceof PsiMethodCallExpression) {
       PsiMethod method = ((PsiMethodCallExpression)parent).resolveMethod();
@@ -839,33 +839,19 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
           final PsiElement referenceNameElement = element.getReferenceNameElement();
           if(referenceNameElement != null) {
             // exclude type parameters from the highlighted text range
-            TextRange range = new TextRange(element.getTextRange().getStartOffset(), referenceNameElement.getTextRange().getEndOffset());
+            TextRange range = referenceNameElement.getTextRange();
             myHolder.add(HighlightNamesUtil.highlightMethodName(methodOrClass, referenceNameElement, range, colorsScheme, false));
           }
         }
       }
       catch (IndexNotReadyException ignored) { }
     }
-    else if (parent instanceof PsiImportStatement && ((PsiImportStatement)parent).isOnDemand()) {
-      // highlight on demand import as class
-      myHolder.add(HighlightNamesUtil.highlightClassName(null, element, colorsScheme));
+    else if (resolved instanceof PsiPackage) {
+      // highlight package (and following dot) as a class
+      myHolder.add(HighlightNamesUtil.highlightPackage(resolved, element, colorsScheme));
     }
     else if (resolved instanceof PsiClass) {
-      final PsiElement qualifier = element.getQualifier();
-      boolean highlightByItem = qualifier instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement)qualifier).getTypeParameters().length > 0 ||
-                                PsiTreeUtil.findChildOfType(element, PsiComment.class) != null;
-      final PsiElement referenceNameElement = element.getReferenceNameElement();
-      final List<PsiElement> toHighlight = new ArrayList<>();
-      if (highlightByItem && referenceNameElement != null) {
-        toHighlight.add(referenceNameElement);
-        toHighlight.addAll(PsiTreeUtil.findChildrenOfType(element, PsiJavaCodeReferenceElement.class));
-      }
-      else {
-        toHighlight.add(element);
-      }
-      for (PsiElement psiElement : toHighlight) {
-        myHolder.add(HighlightNamesUtil.highlightClassName((PsiClass)resolved, psiElement, colorsScheme));
-      }
+      myHolder.add(HighlightNamesUtil.highlightClassName((PsiClass)resolved, element, colorsScheme));
     }
   }
 
index fdad77d08dfbdf8493cc371bcd049465d03e84eb..eb3076ba890235324460b45ec63ff12447d4d516 100644 (file)
@@ -1,15 +1,15 @@
-import <symbolName descr="null" type="CLASS_NAME">java.io</symbolName>.*; // highlight on demand import as class name
-import <symbolName descr="null" type="CLASS_NAME">java.util</symbolName>.*; // highlight on demand import as class name
-import <symbolName descr="null" type="CLASS_NAME"><symbolName descr="null" type="CLASS_NAME">java</symbolName>.lang</symbolName>/*comment*/.<symbolName descr="null" type="CLASS_NAME">String</symbolName>;
-import static <symbolName descr="null" type="CLASS_NAME">java.io.File</symbolName>.<symbolName descr="null" type="STATIC_FINAL_FIELD">pathSeparator</symbolName>;
-import static <symbolName descr="null" type="CLASS_NAME">java.io.File</symbolName>.*;
+import <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">io.</symbolName>*; // highlight on demand import as class name
+import <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">util.</symbolName>*; // highlight on demand import as class name
+import <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">lang</symbolName>/*comment*/.<symbolName descr="null" type="CLASS_NAME">String</symbolName>;
+import static <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">io.</symbolName><symbolName descr="null" type="CLASS_NAME">File</symbolName>.<symbolName descr="null" type="STATIC_FINAL_FIELD">pathSeparator</symbolName>;
+import static <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">io.</symbolName><symbolName descr="null" type="CLASS_NAME">File</symbolName>.*;
 
 class <symbolName descr="null" type="CLASS_NAME">a</symbolName> {
   void <symbolName descr="null" type="METHOD_DECLARATION">method</symbolName>() {
     <symbolName descr="null" type="METHOD_CALL">method</symbolName>();
 
     new <symbolName descr="null" type="CONSTRUCTOR_CALL">Exception</symbolName>();
-    new <symbolName descr="null" type="CONSTRUCTOR_CALL">java.lang.Exception</symbolName>();
+    new <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">lang.</symbolName><symbolName descr="null" type="CONSTRUCTOR_CALL">Exception</symbolName>();
   }
 
   <symbolName descr="null" type="CONSTRUCTOR_DECLARATION">a</symbolName>() {
@@ -21,7 +21,7 @@ class <symbolName descr="null" type="CLASS_NAME">a</symbolName> {
    */
   static void <symbolName descr="null" type="METHOD_DECLARATION">f</symbolName>() {
     <symbolName descr="null" type="CLASS_NAME">Integer</symbolName>.<symbolName descr="null" type="STATIC_METHOD">parseInt</symbolName>("");
-    <symbolName descr="null" type="CLASS_NAME">java.lang.Integer</symbolName>.<symbolName descr="null" type="STATIC_METHOD">parseInt</symbolName>("");
+    <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">lang.</symbolName><symbolName descr="null" type="CLASS_NAME">Integer</symbolName>.<symbolName descr="null" type="STATIC_METHOD">parseInt</symbolName>("");
     <symbolName descr="null" type="STATIC_METHOD">f</symbolName>();
   }
 
@@ -36,13 +36,13 @@ class <symbolName descr="null" type="CLASS_NAME">a</symbolName> {
 
   void <symbolName descr="null" type="METHOD_DECLARATION">ff</symbolName>(<symbolName descr="null" type="INTERFACE_NAME">Runnable</symbolName> <symbolName descr="null" type="PARAMETER">r</symbolName>) {
      <symbolName descr="null" type="METHOD_CALL">ff</symbolName>(
-       new <symbolName descr="null" type="ANONYMOUS_CLASS_NAME">java.lang.Runnable</symbolName>() {
+       new <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">lang.</symbolName><symbolName descr="null" type="ANONYMOUS_CLASS_NAME">Runnable</symbolName>() {
          public void <symbolName descr="null" type="METHOD_DECLARATION">run</symbolName>() {}
          int <symbolName descr="null" type="INSTANCE_FIELD">instance</symbolName> = 0;
        }
      );
 
-    int <symbolName descr="null" type="LOCAL_VARIABLE">i</symbolName> = <symbolName descr="null" type="CLASS_NAME">java.lang.Integer</symbolName>.<symbolName descr="null" type="STATIC_FINAL_FIELD">MIN_VALUE</symbolName>;
+    int <symbolName descr="null" type="LOCAL_VARIABLE">i</symbolName> = <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">lang.</symbolName><symbolName descr="null" type="CLASS_NAME">Integer</symbolName>.<symbolName descr="null" type="STATIC_FINAL_FIELD">MIN_VALUE</symbolName>;
     int <symbolName descr="null" type="LOCAL_VARIABLE">j</symbolName> = <symbolName descr="null" type="INTERFACE_NAME">itf</symbolName>.<symbolName descr="null" type="STATIC_FINAL_FIELD">CONST</symbolName>;
   }
 }
@@ -74,7 +74,7 @@ class <symbolName descr="null" type="CLASS_NAME">InheritedSymbolNames</symbolNam
 
   private static class <symbolName descr="null" type="CLASS_NAME">B</symbolName> extends <symbolName descr="null" type="CLASS_NAME">A</symbolName> {
       {
-          new <symbolName descr="null" type="ANONYMOUS_CLASS_NAME">java.lang.Runnable</symbolName>() {
+          new <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">lang.</symbolName><symbolName descr="null" type="ANONYMOUS_CLASS_NAME">Runnable</symbolName>() {
               public void <symbolName descr="null" type="METHOD_DECLARATION">run</symbolName>() {
                   <symbolName descr="null" type="INHERITED_METHOD">getName</symbolName>();
               }
@@ -85,7 +85,7 @@ class <symbolName descr="null" type="CLASS_NAME">InheritedSymbolNames</symbolNam
 
   private static class <symbolName descr="null" type="CLASS_NAME">C</symbolName> extends <symbolName descr="null" type="CLASS_NAME">A</symbolName> {
       {
-          new <symbolName descr="null" type="ANONYMOUS_CLASS_NAME">java.lang.Runnable</symbolName>() {
+          new <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">lang.</symbolName><symbolName descr="null" type="ANONYMOUS_CLASS_NAME">Runnable</symbolName>() {
               public void <symbolName descr="null" type="METHOD_DECLARATION">run</symbolName>() {
                   <symbolName descr="null" type="METHOD_CALL">getName</symbolName>();
               }
index a6f8d4d04ac388d63a296760a38bc283ee56f3e5..0d1c392ac4cd015da0783117e8fa49cf1f083ad4 100644 (file)
@@ -1,9 +1,9 @@
-package x;
+package <symbolName descr="null" type="CLASS_NAME">x</symbolName>;
 class <symbolName descr="null" type="CLASS_NAME" foreground="0x000000" background="0x404040" effectcolor="0x0000ff" effecttype="BOXED" fonttype="2">Shared</symbolName> {
   <symbolName descr="null" type="CLASS_NAME" foreground="0x000000" background="0x404040" effectcolor="0x0000ff" effecttype="BOXED" fonttype="2">Shared</symbolName> <symbolName descr="null" type="INSTANCE_FIELD">x</symbolName> = new <symbolName descr="null" type="CONSTRUCTOR_CALL" foreground="0x000000" background="0x404040" effectcolor="0x0000ff" effecttype="BOXED" fonttype="2">Shared</symbolName>();
-  <symbolName descr="null" type="INTERFACE_NAME" foreground="0x000000" background="0xff00ff" effectcolor="0xffc800" effecttype="STRIKEOUT" fonttype="1">java.util.List</symbolName>
+  <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">util.</symbolName><symbolName descr="null" type="INTERFACE_NAME" foreground="0x000000" background="0xff00ff" effectcolor="0xffc800" effecttype="STRIKEOUT" fonttype="1">List</symbolName>
   <symbolName descr="null" type="METHOD_DECLARATION">list</symbolName>(
-    <symbolName descr="null" type="INTERFACE_NAME" foreground="0x000000" background="0xff00ff" effectcolor="0xffc800" effecttype="STRIKEOUT" fonttype="1">java.util.Map</symbolName> <symbolName descr="null" type="PARAMETER">map</symbolName>
+  <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">util.</symbolName><symbolName descr="null" type="INTERFACE_NAME" foreground="0x000000" background="0xff00ff" effectcolor="0xffc800" effecttype="STRIKEOUT" fonttype="1">Map</symbolName> <symbolName descr="null" type="PARAMETER">map</symbolName>
   ) {
    return null;
   }
index 475f7d784ba5176d034029e757030d9784f61761..c4e41be2dbc2d5c3040024a93b13af67f6aa5895 100644 (file)
@@ -1,9 +1,9 @@
-package x;
+package <symbolName descr="null" type="CLASS_NAME">x</symbolName>;
 class <symbolName descr="null" type="CLASS_NAME" foreground="0x000000" background="0x404040" effectcolor="0x0000ff" effecttype="BOXED" fonttype="2">X</symbolName> {
   <symbolName descr="null" type="CLASS_NAME" foreground="0x000000" background="0x404040" effectcolor="0x0000ff" effecttype="BOXED" fonttype="2">X</symbolName> <symbolName descr="null" type="INSTANCE_FIELD">x</symbolName> = new <symbolName descr="null" type="CONSTRUCTOR_CALL" foreground="0x000000" background="0x404040" effectcolor="0x0000ff" effecttype="BOXED" fonttype="2">X</symbolName>();
-  <symbolName descr="null" type="INTERFACE_NAME" foreground="0x000000" background="0xff00ff" effectcolor="0xffc800" effecttype="STRIKEOUT" fonttype="1">java.util.List</symbolName>
+  <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">util.</symbolName><symbolName descr="null" type="INTERFACE_NAME" foreground="0x000000" background="0xff00ff" effectcolor="0xffc800" effecttype="STRIKEOUT" fonttype="1">List</symbolName>
   <symbolName descr="null" type="METHOD_DECLARATION">list</symbolName>(
-    <symbolName descr="null" type="INTERFACE_NAME" foreground="0x000000" background="0xff00ff" effectcolor="0xffc800" effecttype="STRIKEOUT" fonttype="1">java.util.Map</symbolName> <symbolName descr="null" type="PARAMETER">map</symbolName>
+  <symbolName descr="null" type="CLASS_NAME">java.</symbolName><symbolName descr="null" type="CLASS_NAME">util.</symbolName><symbolName descr="null" type="INTERFACE_NAME" foreground="0x000000" background="0xff00ff" effectcolor="0xffc800" effecttype="STRIKEOUT" fonttype="1">Map</symbolName> <symbolName descr="null" type="PARAMETER">map</symbolName>
   ) {
    return null;
   }