Search in sources :

Example 1 with Location

use of org.dartlang.analysis.server.protocol.Location 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)

Example 2 with Location

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

the class DartServerGotoSuperHandler method addSuperComponent.

private static void addSuperComponent(@NotNull Project project, List<DartComponent> supers, boolean isInClass, TypeHierarchyItem item) {
    // prepare Element for the current item
    final Element itemElement = isInClass ? item.getClassElement() : item.getMemberElement();
    if (itemElement == null) {
        return;
    }
    // ignore Object
    if (ElementKind.CLASS.equals(itemElement.getKind()) && "Object".equals(itemElement.getName())) {
        return;
    }
    // find the DartComponent
    final Location itemLocation = itemElement.getLocation();
    final DartComponent itemComponent = DartHierarchyUtil.findDartComponent(project, itemLocation);
    if (itemComponent != null) {
        supers.add(itemComponent);
    }
}
Also used : DartComponent(com.jetbrains.lang.dart.psi.DartComponent) PsiElement(com.intellij.psi.PsiElement) Element(org.dartlang.analysis.server.protocol.Element) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) Location(org.dartlang.analysis.server.protocol.Location)

Example 3 with Location

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

the class DartInheritorsSearcher method addSubClasses.

private static void addSubClasses(@NotNull final Project project, @NotNull final SearchScope scope, @NotNull final Set<TypeHierarchyItem> visited, @NotNull final List<TypeHierarchyItem> hierarchyItems, @NotNull final List<DartComponent> components, @NotNull final TypeHierarchyItem currentItem, final boolean addItem) {
    if (!visited.add(currentItem)) {
        return;
    }
    if (addItem) {
        final Element element = currentItem.getClassElement();
        final Location location = element.getLocation();
        final DartComponent component = DartHierarchyUtil.findDartComponent(project, location);
        if (component != null && isInScope(scope, component)) {
            components.add(component);
        }
    }
    for (int subIndex : currentItem.getSubclasses()) {
        final TypeHierarchyItem subItem = hierarchyItems.get(subIndex);
        addSubClasses(project, scope, visited, hierarchyItems, components, subItem, true);
    }
}
Also used : DartComponent(com.jetbrains.lang.dart.psi.DartComponent) PsiElement(com.intellij.psi.PsiElement) Element(org.dartlang.analysis.server.protocol.Element) TypeHierarchyItem(org.dartlang.analysis.server.protocol.TypeHierarchyItem) Location(org.dartlang.analysis.server.protocol.Location)

Example 4 with Location

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

the class DartHierarchyUtil method findDartClass.

@Nullable
public static DartClass findDartClass(@NotNull final Project project, @NotNull final TypeHierarchyItem item) {
    final Element classElement = item.getClassElement();
    final Location location = classElement.getLocation();
    final DartComponent component = findDartComponent(project, location);
    return component instanceof DartClass ? (DartClass) component : null;
}
Also used : Element(org.dartlang.analysis.server.protocol.Element) Location(org.dartlang.analysis.server.protocol.Location) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with Location

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

the class RequestUtilities method buildJsonElement.

@VisibleForTesting
public static JsonElement buildJsonElement(Object object) {
    if (object instanceof Boolean) {
        return new JsonPrimitive((Boolean) object);
    } else if (object instanceof Number) {
        return new JsonPrimitive((Number) object);
    } else if (object instanceof String) {
        return new JsonPrimitive((String) object);
    } else if (object instanceof List<?>) {
        List<?> list = (List<?>) object;
        JsonArray jsonArray = new JsonArray();
        for (Object item : list) {
            JsonElement jsonItem = buildJsonElement(item);
            jsonArray.add(jsonItem);
        }
        return jsonArray;
    } else if (object instanceof Map<?, ?>) {
        Map<?, ?> map = (Map<?, ?>) object;
        JsonObject jsonObject = new JsonObject();
        for (Entry<?, ?> entry : map.entrySet()) {
            Object key = entry.getKey();
            // prepare string key
            String keyString;
            if (key instanceof String) {
                keyString = (String) key;
            } else {
                throw new IllegalArgumentException("Unable to convert to string: " + getClassName(key));
            }
            // prepare JsonElement value
            Object value = entry.getValue();
            JsonElement valueJson = buildJsonElement(value);
            // put a property into the JSON object
            if (keyString != null && valueJson != null) {
                jsonObject.add(keyString, valueJson);
            }
        }
        return jsonObject;
    } else if (object instanceof AnalysisError) {
        return buildJsonObjectAnalysisError((AnalysisError) object);
    } else if (object instanceof AddContentOverlay) {
        return ((AddContentOverlay) object).toJson();
    } else if (object instanceof ChangeContentOverlay) {
        return ((ChangeContentOverlay) object).toJson();
    } else if (object instanceof RemoveContentOverlay) {
        return ((RemoveContentOverlay) object).toJson();
    } else if (object instanceof AnalysisOptions) {
        return ((AnalysisOptions) object).toJson();
    } else if (object instanceof Location) {
        return buildJsonObjectLocation((Location) object);
    }
    throw new IllegalArgumentException("Unable to convert to JSON: " + object);
}
Also used : AddContentOverlay(org.dartlang.analysis.server.protocol.AddContentOverlay) JsonPrimitive(com.google.gson.JsonPrimitive) AnalysisOptions(org.dartlang.analysis.server.protocol.AnalysisOptions) AnalysisError(org.dartlang.analysis.server.protocol.AnalysisError) ChangeContentOverlay(org.dartlang.analysis.server.protocol.ChangeContentOverlay) JsonObject(com.google.gson.JsonObject) JsonArray(com.google.gson.JsonArray) RemoveContentOverlay(org.dartlang.analysis.server.protocol.RemoveContentOverlay) JsonElement(com.google.gson.JsonElement) List(java.util.List) JsonObject(com.google.gson.JsonObject) Map(java.util.Map) Location(org.dartlang.analysis.server.protocol.Location) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Location (org.dartlang.analysis.server.protocol.Location)6 PsiElement (com.intellij.psi.PsiElement)4 Element (org.dartlang.analysis.server.protocol.Element)4 DartComponent (com.jetbrains.lang.dart.psi.DartComponent)3 TypeHierarchyItem (org.dartlang.analysis.server.protocol.TypeHierarchyItem)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 NavigatablePsiElement (com.intellij.psi.NavigatablePsiElement)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 List (java.util.List)1