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;
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations