Search in sources :

Example 1 with SearchResult

use of org.dartlang.analysis.server.protocol.SearchResult in project intellij-plugins by JetBrains.

the class DartServerFindUsagesHandler method processElementUsages.

@Override
public boolean processElementUsages(@NotNull final PsiElement elementToSearch, @NotNull final Processor<UsageInfo> processor, @NotNull final FindUsagesOptions options) {
    final SearchScope scope = options.searchScope;
    final Project project = ReadAction.compute(this::getProject);
    final DartAnalysisServerService service = DartAnalysisServerService.getInstance(project);
    final ReadActionConsumer<SearchResult> searchResultProcessor = new ReadActionConsumer<SearchResult>() {

        @Override
        public void consumeInReadAction(SearchResult result) {
            if (result.getKind().equals(SearchResultKind.DECLARATION))
                return;
            final Location location = result.getLocation();
            final VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(location.getFile()));
            if (vFile == null)
                return;
            if (!scope.contains(vFile))
                return;
            final PsiFile psiFile = elementToSearch.getManager().findFile(vFile);
            if (psiFile == null)
                return;
            final int offset = service.getConvertedOffset(vFile, location.getOffset());
            final int length = service.getConvertedOffset(vFile, location.getOffset() + location.getLength()) - offset;
            final TextRange range = TextRange.create(offset, offset + length);
            final boolean potentialUsage = result.isPotential();
            final PsiElement usageElement = getUsagePsiElement(psiFile, range);
            final UsageInfo usageInfo = usageElement == null ? null : getUsageInfo(usageElement, range, potentialUsage);
            if (usageInfo != null && usageInfo.getElement() != null && (!(scope instanceof LocalSearchScope) || PsiSearchScopeUtil.isInScope((LocalSearchScope) scope, usageInfo.getElement()))) {
                processor.process(usageInfo);
            }
        }
    };
    final VirtualFile file = ReadAction.compute(() -> elementToSearch.getContainingFile().getVirtualFile());
    final int offset = elementToSearch.getTextRange().getStartOffset();
    service.search_findElementReferences(file, offset, searchResultProcessor);
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) SearchResult(org.dartlang.analysis.server.protocol.SearchResult) TextRange(com.intellij.openapi.util.TextRange) Project(com.intellij.openapi.project.Project) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiFile(com.intellij.psi.PsiFile) DartAnalysisServerService(com.jetbrains.lang.dart.analyzer.DartAnalysisServerService) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo) Location(org.dartlang.analysis.server.protocol.Location)

Aggregations

Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 SearchScope (com.intellij.psi.search.SearchScope)1 UsageInfo (com.intellij.usageView.UsageInfo)1 DartAnalysisServerService (com.jetbrains.lang.dart.analyzer.DartAnalysisServerService)1 Location (org.dartlang.analysis.server.protocol.Location)1 SearchResult (org.dartlang.analysis.server.protocol.SearchResult)1