Search in sources :

Example 1 with UsageType

use of com.intellij.usages.impl.rules.UsageType in project smali by JesusFreke.

the class UsageTypeTest method doTest.

protected void doTest(@NotNull String fileName, @NotNull String text, @NotNull Object... expectedUsageTypes) throws Exception {
    Assert.assertTrue(expectedUsageTypes.length % 2 == 0);
    Map<Integer, UsageType> expectedUsageTypesMap = Maps.newHashMap();
    for (int i = 0; i < expectedUsageTypes.length; i += 2) {
        expectedUsageTypesMap.put((Integer) expectedUsageTypes[i], (UsageType) expectedUsageTypes[i + 1]);
    }
    PsiFile psiFile = createFile(fileName, REF_PATTERN.matcher(text).replaceAll(""));
    Map<Integer, Integer> refIndexMap = getRefIndexes(text);
    for (Map.Entry<Integer, Integer> entry : refIndexMap.entrySet()) {
        int refId = entry.getKey();
        int index = entry.getValue();
        PsiReference reference = psiFile.getFirstChild().findReferenceAt(index);
        Assert.assertNotNull(reference);
        if (reference instanceof PsiMultiReference) {
            // If there are multiple reference parents, the default seems to be the last one,
            // i.e. the highest parent. We actually want the lowest one here.
            reference = ((PsiMultiReference) reference).getReferences()[0];
        }
        UsageType usageType = usageTypeProvider.getUsageType(reference.getElement());
        Assert.assertNotNull(usageType);
        Assert.assertSame(expectedUsageTypesMap.get(refId), usageType);
        expectedUsageTypesMap.remove(refId);
    }
    Assert.assertTrue(expectedUsageTypesMap.isEmpty());
}
Also used : PsiReference(com.intellij.psi.PsiReference) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) PsiFile(com.intellij.psi.PsiFile) UsageType(com.intellij.usages.impl.rules.UsageType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with UsageType

use of com.intellij.usages.impl.rules.UsageType in project intellij-community by JetBrains.

the class JavaUsageTypeProviderTest method assertUsageType.

private void assertUsageType(UsageType expected, PsiClass target) {
    UsageTarget[] targets = { new PsiElement2UsageTargetAdapter(target) };
    PsiElement element = myFixture.getReferenceAtCaretPositionWithAssertion().getElement();
    UsageType usageType = new JavaUsageTypeProvider().getUsageType(element, targets);
    assertEquals(expected, usageType);
}
Also used : JavaUsageTypeProvider(com.intellij.usages.impl.rules.JavaUsageTypeProvider) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) UsageType(com.intellij.usages.impl.rules.UsageType) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget)

Example 3 with UsageType

use of com.intellij.usages.impl.rules.UsageType in project intellij-community by JetBrains.

the class ChunkExtractor method processIntersectingRange.

private void processIntersectingRange(@NotNull UsageInfo2UsageAdapter usageInfo2UsageAdapter, @NotNull final CharSequence chars, int hiStart, final int hiEnd, @NotNull final TextAttributesKey[] tokenHighlights, final boolean selectUsageWithBold, @NotNull final List<TextChunk> result) {
    final TextAttributes originalAttrs = convertAttributes(tokenHighlights);
    if (selectUsageWithBold) {
        originalAttrs.setFontType(Font.PLAIN);
    }
    final int[] lastOffset = { hiStart };
    usageInfo2UsageAdapter.processRangeMarkers(segment -> {
        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, 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;
            }
        }
        return true;
    });
    if (lastOffset[0] < hiEnd) {
        addChunk(chars, lastOffset[0], hiEnd, originalAttrs, false, null, result);
    }
}
Also used : TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) UsageType(com.intellij.usages.impl.rules.UsageType)

Example 4 with UsageType

use of com.intellij.usages.impl.rules.UsageType in project intellij-community by JetBrains.

the class UsageInfo2UsageAdapter method getUsageType.

@Nullable
public UsageType getUsageType() {
    UsageType usageType = myUsageType;
    if (usageType == null) {
        usageType = UsageType.UNCLASSIFIED;
        PsiFile file = getPsiFile();
        if (file != null) {
            Segment segment = getFirstSegment();
            if (segment != null) {
                Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file);
                if (document != null) {
                    ChunkExtractor extractor = ChunkExtractor.getExtractor(file);
                    SmartList<TextChunk> chunks = new SmartList<>();
                    extractor.createTextChunks(this, document.getCharsSequence(), segment.getStartOffset(), segment.getEndOffset(), false, chunks);
                    for (TextChunk chunk : chunks) {
                        UsageType chunkUsageType = chunk.getType();
                        if (chunkUsageType != null) {
                            usageType = chunkUsageType;
                            break;
                        }
                    }
                }
            }
        }
        myUsageType = usageType;
    }
    return usageType;
}
Also used : Document(com.intellij.openapi.editor.Document) UsageType(com.intellij.usages.impl.rules.UsageType) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

UsageType (com.intellij.usages.impl.rules.UsageType)4 PsiElement2UsageTargetAdapter (com.intellij.find.findUsages.PsiElement2UsageTargetAdapter)1 Document (com.intellij.openapi.editor.Document)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 PsiMultiReference (com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)1 UsageTarget (com.intellij.usages.UsageTarget)1 JavaUsageTypeProvider (com.intellij.usages.impl.rules.JavaUsageTypeProvider)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Nullable (org.jetbrains.annotations.Nullable)1