group comment / literal occurrences, first version

This commit is contained in:
Maxim.Mossienko
2014-07-31 13:40:22 +02:00
parent 7a141520c0
commit 1d0664abd7
5 changed files with 80 additions and 37 deletions
@@ -64,6 +64,7 @@ import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.ui.LightweightHint;
import com.intellij.ui.ReplacePromptDialog;
import com.intellij.usages.ChunkExtractor;
import com.intellij.usages.UsageViewManager;
import com.intellij.util.Consumer;
import com.intellij.util.containers.ContainerUtil;
@@ -572,8 +573,8 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo
final TextAttributesKey[] keys = activeSyntaxHighlighter.getTokenHighlights(tokenType);
if (tokens.contains(tokenType) ||
(model.isInStringLiteralsOnly() && isHighlightedAsString(keys)) ||
(model.isInCommentsOnly() && isHighlightedAsDocComment(keys))
(model.isInStringLiteralsOnly() && ChunkExtractor.isHighlightedAsString(keys)) ||
(model.isInCommentsOnly() && ChunkExtractor.isHighlightedAsComment(keys))
) {
int start = lexer.getTokenStart();
int end = lexer.getTokenEnd();
@@ -646,32 +647,6 @@ public class FindManagerImpl extends FindManager implements PersistentStateCompo
return prevFindResult;
}
private static boolean isHighlightedAsDocComment(TextAttributesKey... keys) {
for (TextAttributesKey key : keys) {
if (key == DefaultLanguageHighlighterColors.DOC_COMMENT || key == SyntaxHighlighterColors.DOC_COMMENT) {
return true;
}
final TextAttributesKey fallbackAttributeKey = key.getFallbackAttributeKey();
if (fallbackAttributeKey != null && isHighlightedAsDocComment(fallbackAttributeKey)) {
return true;
}
}
return false;
}
private static boolean isHighlightedAsString(TextAttributesKey... keys) {
for (TextAttributesKey key : keys) {
if (key == DefaultLanguageHighlighterColors.STRING || key == SyntaxHighlighterColors.STRING) {
return true;
}
final TextAttributesKey fallbackAttributeKey = key.getFallbackAttributeKey();
if (fallbackAttributeKey != null && isHighlightedAsString(fallbackAttributeKey)) {
return true;
}
}
return false;
}
private static TokenSet addTokenTypesForLanguage(FindModel model, Language lang, TokenSet tokensOfInterest) {
ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(lang);
if (definition != null) {
@@ -19,9 +19,7 @@ import com.intellij.injected.editor.DocumentWindow;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.lexer.Lexer;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.HighlighterColors;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.editor.markup.TextAttributes;
@@ -39,11 +37,13 @@ import com.intellij.psi.tree.IElementType;
import com.intellij.reference.SoftReference;
import com.intellij.usageView.UsageTreeColors;
import com.intellij.usageView.UsageTreeColorsScheme;
import com.intellij.usages.impl.rules.UsageType;
import com.intellij.util.Processor;
import com.intellij.util.containers.FactoryMap;
import com.intellij.util.text.CharArrayUtil;
import com.intellij.util.text.StringFactory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.*;
import java.lang.ref.WeakReference;
@@ -234,7 +234,7 @@ public class ChunkExtractor {
@NotNull final CharSequence chars,
int hiStart,
final int hiEnd,
@NotNull TextAttributesKey[] tokenHighlights,
@NotNull final TextAttributesKey[] tokenHighlights,
final boolean selectUsageWithBold,
@NotNull final List<TextChunk> result) {
final TextAttributes originalAttrs = convertAttributes(tokenHighlights);
@@ -249,8 +249,12 @@ public class ChunkExtractor {
int usageStart = segment.getStartOffset();
int usageEnd = segment.getEndOffset();
if (rangeIntersect(lastOffset[0], hiEnd, usageStart, usageEnd)) {
addChunk(chars, lastOffset[0], Math.max(lastOffset[0], usageStart), originalAttrs, false, result);
addChunk(chars, Math.max(lastOffset[0], usageStart), Math.min(hiEnd, usageEnd), originalAttrs, selectUsageWithBold, result);
addChunk(chars, lastOffset[0], Math.max(lastOffset[0], usageStart), originalAttrs, false, null, result);
UsageType usageType = isHighlightedAsString(tokenHighlights)
? UsageType.LITERAL_USAGE
: isHighlightedAsComment(tokenHighlights) ? UsageType.COMMENT_USAGE : null;
addChunk(chars, Math.max(lastOffset[0], usageStart), Math.min(hiEnd, usageEnd), originalAttrs, selectUsageWithBold, usageType, result);
lastOffset[0] = usageEnd;
if (usageEnd > hiEnd) {
return false;
@@ -260,22 +264,55 @@ public class ChunkExtractor {
}
});
if (lastOffset[0] < hiEnd) {
addChunk(chars, lastOffset[0], hiEnd, originalAttrs, false, result);
addChunk(chars, lastOffset[0], hiEnd, originalAttrs, false, null, result);
}
}
public static boolean isHighlightedAsComment(TextAttributesKey... keys) {
for (TextAttributesKey key : keys) {
if (key == DefaultLanguageHighlighterColors.DOC_COMMENT ||
key == SyntaxHighlighterColors.DOC_COMMENT ||
key == DefaultLanguageHighlighterColors.LINE_COMMENT ||
key == SyntaxHighlighterColors.LINE_COMMENT ||
key == DefaultLanguageHighlighterColors.BLOCK_COMMENT ||
key == SyntaxHighlighterColors.JAVA_BLOCK_COMMENT
) {
return true;
}
final TextAttributesKey fallbackAttributeKey = key.getFallbackAttributeKey();
if (fallbackAttributeKey != null && isHighlightedAsComment(fallbackAttributeKey)) {
return true;
}
}
return false;
}
public static boolean isHighlightedAsString(TextAttributesKey... keys) {
for (TextAttributesKey key : keys) {
if (key == DefaultLanguageHighlighterColors.STRING || key == SyntaxHighlighterColors.STRING) {
return true;
}
final TextAttributesKey fallbackAttributeKey = key.getFallbackAttributeKey();
if (fallbackAttributeKey != null && isHighlightedAsString(fallbackAttributeKey)) {
return true;
}
}
return false;
}
private static void addChunk(@NotNull CharSequence chars,
int start,
int end,
@NotNull TextAttributes originalAttrs,
boolean bold,
@Nullable UsageType usageType,
@NotNull List<TextChunk> result) {
if (start >= end) return;
TextAttributes attrs = bold
? TextAttributes.merge(originalAttrs, new TextAttributes(null, null, null, null, Font.BOLD))
: originalAttrs;
result.add(new TextChunk(attrs, StringFactory.createShared(CharArrayUtil.fromSequence(chars, start, end))));
result.add(new TextChunk(attrs, StringFactory.createShared(CharArrayUtil.fromSequence(chars, start, end)), usageType));
}
private static boolean rangeIntersect(int s1, int e1, int s2, int e2) {
@@ -18,17 +18,25 @@ package com.intellij.usages;
import com.intellij.openapi.editor.markup.AttributesFlyweight;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.usages.impl.rules.UsageType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class TextChunk {
public static final TextChunk[] EMPTY_ARRAY = new TextChunk[0];
private final AttributesFlyweight myAttributes;
private final String myText;
private final UsageType myType;
public TextChunk(@NotNull TextAttributes attributes, @NotNull String text) {
this(attributes, text, null);
}
public TextChunk(@NotNull TextAttributes attributes, @NotNull String text, @Nullable UsageType type) {
myAttributes = attributes.getFlyweight();
myText = text;
myType = type;
}
@NotNull
@@ -45,6 +53,10 @@ public class TextChunk {
return getText();
}
public @Nullable UsageType getType() {
return myType;
}
@NotNull
public SimpleTextAttributes getSimpleAttributesIgnoreBackground() {
SimpleTextAttributes simples = SimpleTextAttributes.fromTextAttributes(getAttributes());
@@ -38,6 +38,7 @@ import com.intellij.reference.SoftReference;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.usageView.UsageInfo;
import com.intellij.usageView.UsageViewBundle;
import com.intellij.usages.impl.rules.UsageType;
import com.intellij.usages.rules.*;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
@@ -493,4 +494,15 @@ public class UsageInfo2UsageAdapter implements UsageInModule,
public String getTooltipText() {
return myUsageInfo.getTooltipText();
}
public @Nullable UsageType getUsageType() {
TextChunk[] chunks = getText();
for(TextChunk chunk:chunks) {
UsageType type = chunk.getType();
if (type != null) {
return type;
}
}
return null;
}
}
@@ -19,6 +19,7 @@ import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.vcs.FileStatus;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.usages.*;
import com.intellij.usages.rules.PsiElementUsage;
@@ -42,7 +43,13 @@ public class UsageTypeGroupingRule implements UsageGroupingRuleEx {
if (usage instanceof PsiElementUsage) {
PsiElementUsage elementUsage = (PsiElementUsage)usage;
UsageType usageType = getUsageType(elementUsage.getElement(), targets);
PsiElement element = elementUsage.getElement();
UsageType usageType = getUsageType(element, targets);
if (usageType == null && element instanceof PsiFile && elementUsage instanceof UsageInfo2UsageAdapter) {
usageType = ((UsageInfo2UsageAdapter)elementUsage).getUsageType();
}
if (usageType != null) return new UsageTypeGroup(usageType);
if (usage instanceof ReadWriteAccessUsage) {