use of org.eclipse.lsp4j.DocumentSymbol in project xtext-core by eclipse.
the class HierarchicalDocumentSymbolService method getSymbols.
public List<Either<SymbolInformation, DocumentSymbol>> getSymbols(XtextResource resource, CancelIndicator cancelIndicator) {
HashMap<EObject, DocumentSymbol> allSymbols = new HashMap<>();
ArrayList<DocumentSymbol> rootSymbols = new ArrayList<>();
Iterator<Object> itr = getAllContents(resource);
while (itr.hasNext()) {
operationCanceledManager.checkCanceled(cancelIndicator);
Optional<EObject> next = toEObject(itr.next());
if (next.isPresent()) {
EObject object = next.get();
DocumentSymbol symbol = symbolMapper.toDocumentSymbol(object);
if (isValid(symbol)) {
allSymbols.put(object, symbol);
EObject parent = object.eContainer();
if (parent == null) {
rootSymbols.add(symbol);
} else {
DocumentSymbol parentSymbol = allSymbols.get(parent);
while (parentSymbol == null && parent != null) {
parent = parent.eContainer();
parentSymbol = allSymbols.get(parent);
}
if (parentSymbol == null) {
rootSymbols.add(symbol);
} else {
parentSymbol.getChildren().add(symbol);
}
}
}
}
}
return rootSymbols.stream().map(symbol -> Either.<SymbolInformation, DocumentSymbol>forRight(symbol)).collect(Collectors.toList());
}
use of org.eclipse.lsp4j.DocumentSymbol in project eclipse.jdt.ls by eclipse.
the class DocumentSymbolHandlerTest method internalGetHierarchicalSymbols.
private List<? extends DocumentSymbol> internalGetHierarchicalSymbols(IProject project, IProgressMonitor monitor, String className) throws JavaModelException, UnsupportedEncodingException, InterruptedException, ExecutionException {
String uri = ClassFileUtil.getURI(project, className);
TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri);
DocumentSymbolParams params = new DocumentSymbolParams();
params.setTextDocument(identifier);
when(preferenceManager.getClientPreferences().isHierarchicalDocumentSymbolSupported()).thenReturn(true);
// @formatter:off
List<DocumentSymbol> symbols = new DocumentSymbolHandler(preferenceManager).documentSymbol(params, monitor).stream().map(Either::getRight).collect(toList());
// @formatter:on
assertTrue(symbols.size() > 0);
return symbols;
}
use of org.eclipse.lsp4j.DocumentSymbol in project eclipse.jdt.ls by eclipse.
the class DocumentSymbolHandler method toDocumentSymbol.
private DocumentSymbol toDocumentSymbol(IJavaElement unit, IProgressMonitor monitor) {
int type = unit.getElementType();
if (type != TYPE && type != FIELD && type != METHOD && type != PACKAGE_DECLARATION && type != COMPILATION_UNIT) {
return null;
}
if (monitor.isCanceled()) {
throw new OperationCanceledException("User abort");
}
DocumentSymbol symbol = new DocumentSymbol();
try {
String name = getName(unit);
symbol.setName(name);
symbol.setRange(getRange(unit));
symbol.setSelectionRange(getSelectionRange(unit));
symbol.setKind(mapKind(unit));
if (JDTUtils.isDeprecated(unit)) {
if (preferenceManager.getClientPreferences().isSymbolTagSupported()) {
symbol.setTags(List.of(SymbolTag.Deprecated));
} else {
symbol.setDeprecated(true);
}
}
symbol.setDetail(getDetail(unit, name));
if (unit instanceof IParent) {
// @formatter:off
IJavaElement[] children = filter(((IParent) unit).getChildren());
symbol.setChildren(Stream.of(children).map(child -> toDocumentSymbol(child, monitor)).filter(Objects::nonNull).collect(Collectors.toList()));
// @formatter:off
}
} catch (JavaModelException e) {
Exceptions.sneakyThrow(e);
}
return symbol;
}
use of org.eclipse.lsp4j.DocumentSymbol in project xtext-core by eclipse.
the class DocumentSymbolMapper method toDocumentSymbol.
/**
* Converts the {@code EObject} argument into a {@link DocumentSymbol document symbol} without the
* {@link DocumentSymbol#children children} information filled in.
*/
public DocumentSymbol toDocumentSymbol(EObject object) {
DocumentSymbol documentSymbol = new DocumentSymbol();
String objectName = nameProvider.getName(object);
if (objectName != null) {
documentSymbol.setName(objectName);
}
SymbolKind objectKind = kindProvider.getSymbolKind(object);
if (objectKind != null) {
documentSymbol.setKind(objectKind);
}
Range objectRange = rangeProvider.getRange(object);
if (objectRange != null) {
documentSymbol.setRange(objectRange);
}
Range objectSelectionRange = rangeProvider.getSelectionRange(object);
if (objectSelectionRange != null) {
documentSymbol.setSelectionRange(objectSelectionRange);
}
documentSymbol.setDetail(detailsProvider.getDetails(object));
documentSymbol.setDeprecated(deprecationInfoProvider.isDeprecated(object));
documentSymbol.setChildren(new ArrayList<>());
return documentSymbol;
}
use of org.eclipse.lsp4j.DocumentSymbol in project xtext-core by eclipse.
the class DocumentSymbolService method getSymbols.
public List<Either<SymbolInformation, DocumentSymbol>> getSymbols(XtextResource resource, CancelIndicator cancelIndicator) {
String uri = uriExtensions.toUriString(resource.getURI());
ArrayList<SymbolInformation> infos = new ArrayList<>();
List<DocumentSymbol> rootSymbols = Lists.transform(hierarchicalDocumentSymbolService.getSymbols(resource, cancelIndicator), Either::getRight);
for (DocumentSymbol rootSymbol : rootSymbols) {
Iterable<DocumentSymbol> symbols = Traverser.forTree(DocumentSymbol::getChildren).depthFirstPreOrder(rootSymbol);
Function1<? super DocumentSymbol, ? extends String> containerNameProvider = (DocumentSymbol symbol) -> {
DocumentSymbol firstSymbol = IterableExtensions.findFirst(symbols, (DocumentSymbol it) -> {
return it != symbol && !IterableExtensions.isNullOrEmpty(it.getChildren()) && it.getChildren().contains(symbol);
});
if (firstSymbol != null) {
return firstSymbol.getName();
}
return null;
};
for (DocumentSymbol s : symbols) {
infos.add(createSymbol(uri, s, containerNameProvider));
}
}
return Lists.transform(infos, Either::forLeft);
}
Aggregations