use of org.eclipse.lsp4j.SymbolInformation in project eclipse.jdt.ls by eclipse.
the class WorkspaceSymbolHandlerTest method testWorkspaceSearch.
@Test
public void testWorkspaceSearch() {
String query = "Array";
List<SymbolInformation> results = WorkspaceSymbolHandler.search(query, monitor);
assertNotNull(results);
assertEquals("Unexpected results", 11, results.size());
Range defaultRange = JDTUtils.newRange();
for (SymbolInformation symbol : results) {
assertNotNull("Kind is missing", symbol.getKind());
assertNotNull("ContainerName is missing", symbol.getContainerName());
assertTrue(symbol.getName().startsWith(query));
Location location = symbol.getLocation();
assertEquals(defaultRange, location.getRange());
// No class in the workspace project starts with Array, so everything comes from the JDK
assertTrue("Unexpected uri " + location.getUri(), location.getUri().startsWith("jdt://"));
}
}
use of org.eclipse.lsp4j.SymbolInformation in project eclipse.jdt.ls by eclipse.
the class DocumentSymbolHandlerTest method testDeprecated_property.
@Test
public void testDeprecated_property() throws Exception {
when(preferenceManager.getClientPreferences().isSymbolTagSupported()).thenReturn(false);
String className = "org.sample.Bar";
List<? extends SymbolInformation> symbols = getSymbols(className);
SymbolInformation deprecated = symbols.stream().filter(symbol -> symbol.getName().equals("MyInterface")).findFirst().orElse(null);
assertNotNull(deprecated);
assertEquals(SymbolKind.Interface, deprecated.getKind());
assertNotNull(deprecated.getDeprecated());
assertTrue("Should be deprecated", deprecated.getDeprecated());
}
use of org.eclipse.lsp4j.SymbolInformation in project eclipse.jdt.ls by eclipse.
the class WorkspaceSymbolHandler method search.
public static List<SymbolInformation> search(String query, int maxResults, String projectName, boolean sourceOnly, IProgressMonitor monitor) {
ArrayList<SymbolInformation> symbols = new ArrayList<>();
if (StringUtils.isBlank(query)) {
return symbols;
}
try {
monitor.beginTask("Searching the types...", 100);
IJavaSearchScope searchScope = createSearchScope(projectName, sourceOnly);
int typeMatchRule = SearchPattern.R_CAMELCASE_MATCH;
if (query.contains("*") || query.contains("?")) {
typeMatchRule |= SearchPattern.R_PATTERN_MATCH;
}
PreferenceManager preferenceManager = JavaLanguageServerPlugin.getPreferencesManager();
new SearchEngine().searchAllTypeNames(null, SearchPattern.R_PATTERN_MATCH, query.trim().toCharArray(), typeMatchRule, IJavaSearchConstants.TYPE, searchScope, new TypeNameMatchRequestor() {
@Override
public void acceptTypeNameMatch(TypeNameMatch match) {
try {
if (maxResults > 0 && symbols.size() >= maxResults) {
return;
}
Location location = null;
try {
if (!sourceOnly && match.getType().isBinary()) {
location = JDTUtils.toLocation(match.getType().getClassFile());
} else if (!match.getType().isBinary()) {
location = JDTUtils.toLocation(match.getType());
}
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e);
return;
}
if (location != null && match.getSimpleTypeName() != null && !match.getSimpleTypeName().isEmpty()) {
SymbolInformation symbolInformation = new SymbolInformation();
symbolInformation.setContainerName(match.getTypeContainerName());
symbolInformation.setName(match.getSimpleTypeName());
symbolInformation.setKind(mapKind(match));
if (Flags.isDeprecated(match.getType().getFlags())) {
if (preferenceManager != null && preferenceManager.getClientPreferences().isSymbolTagSupported()) {
symbolInformation.setTags(List.of(SymbolTag.Deprecated));
} else {
symbolInformation.setDeprecated(true);
}
}
symbolInformation.setLocation(location);
symbols.add(symbolInformation);
if (maxResults > 0 && symbols.size() >= maxResults) {
monitor.setCanceled(true);
}
}
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e);
return;
}
}
private SymbolKind mapKind(TypeNameMatch match) {
int flags = match.getModifiers();
if (Flags.isInterface(flags)) {
return SymbolKind.Interface;
}
if (Flags.isAnnotation(flags)) {
return SymbolKind.Property;
}
if (Flags.isEnum(flags)) {
return SymbolKind.Enum;
}
return SymbolKind.Class;
}
}, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
if (preferenceManager != null && preferenceManager.getPreferences().isIncludeSourceMethodDeclarations()) {
monitor.beginTask("Searching methods...", 100);
IJavaSearchScope nonSourceSearchScope = createSearchScope(projectName, true);
new SearchEngine().searchAllMethodNames(null, SearchPattern.R_PATTERN_MATCH, query.trim().toCharArray(), typeMatchRule, nonSourceSearchScope, new MethodNameMatchRequestor() {
@Override
public void acceptMethodNameMatch(MethodNameMatch match) {
try {
if (maxResults > 0 && symbols.size() >= maxResults) {
return;
}
Location location = null;
try {
location = JDTUtils.toLocation(match.getMethod());
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getMethod().getElementName(), e);
return;
}
if (location != null && match.getMethod().getElementName() != null && !match.getMethod().getElementName().isEmpty()) {
SymbolInformation symbolInformation = new SymbolInformation();
symbolInformation.setContainerName(match.getMethod().getDeclaringType().getFullyQualifiedName());
symbolInformation.setName(match.getMethod().getElementName());
symbolInformation.setKind(SymbolKind.Method);
if (Flags.isDeprecated(match.getMethod().getFlags())) {
if (preferenceManager != null && preferenceManager.getClientPreferences().isSymbolTagSupported()) {
symbolInformation.setTags(List.of(SymbolTag.Deprecated));
} else {
symbolInformation.setDeprecated(true);
}
}
symbolInformation.setLocation(location);
symbols.add(symbolInformation);
if (maxResults > 0 && symbols.size() >= maxResults) {
monitor.setCanceled(true);
}
}
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getMethod().getElementName(), e);
return;
}
}
}, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
}
} catch (Exception e) {
if (e instanceof OperationCanceledException) {
// ignore.
} else {
JavaLanguageServerPlugin.logException("Problem getting search for" + query, e);
}
} finally {
monitor.done();
}
return symbols;
}
use of org.eclipse.lsp4j.SymbolInformation in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method definitionToSymbol.
private SymbolInformation definitionToSymbol(IDefinition definition) {
SymbolInformation symbol = new SymbolInformation();
if (definition instanceof IClassDefinition) {
symbol.setKind(SymbolKind.Class);
} else if (definition instanceof IInterfaceDefinition) {
symbol.setKind(SymbolKind.Interface);
} else if (definition instanceof IFunctionDefinition) {
IFunctionDefinition functionDefinition = (IFunctionDefinition) definition;
if (functionDefinition.isConstructor()) {
symbol.setKind(SymbolKind.Constructor);
} else {
symbol.setKind(SymbolKind.Function);
}
} else if (definition instanceof IFunctionDefinition) {
symbol.setKind(SymbolKind.Function);
} else if (definition instanceof IConstantDefinition) {
symbol.setKind(SymbolKind.Constant);
} else {
symbol.setKind(SymbolKind.Variable);
}
symbol.setName(definition.getQualifiedName());
Location location = new Location();
String sourcePath = definition.getSourcePath();
if (sourcePath == null) {
//I'm not sure why getSourcePath() can sometimes return null, but
//getContainingFilePath() seems to work as a fallback -JT
sourcePath = definition.getContainingFilePath();
}
Path definitionPath = Paths.get(sourcePath);
location.setUri(definitionPath.toUri().toString());
Position start = new Position();
Position end = new Position();
//getLine() and getColumn() may include things like metadata, so it
//makes more sense to jump to where the definition name starts
int line = definition.getNameLine();
int column = definition.getNameColumn();
if (line < 0 || column < 0) {
//this is not ideal, but MXML variable definitions may not have a
//node associated with them, so we need to figure this out from the
//offset instead of a pre-calculated line and column -JT
String code = sourceByPath.get(Paths.get(sourcePath));
offsetToLineAndCharacter(new StringReader(code), definition.getNameStart(), start);
end.setLine(start.getLine());
end.setCharacter(start.getCharacter());
} else {
start.setLine(line);
start.setCharacter(column);
end.setLine(line);
end.setCharacter(column);
}
Range range = new Range();
range.setStart(start);
range.setEnd(end);
location.setRange(range);
symbol.setLocation(location);
return symbol;
}
use of org.eclipse.lsp4j.SymbolInformation in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method scopeToSymbols.
private void scopeToSymbols(IASScope scope, List<SymbolInformation> result) {
Collection<IDefinition> definitions = scope.getAllLocalDefinitions();
for (IDefinition definition : definitions) {
if (definition instanceof IPackageDefinition) {
IPackageDefinition packageDefinition = (IPackageDefinition) definition;
IASScope packageScope = packageDefinition.getContainedScope();
scopeToSymbols(packageScope, result);
} else if (definition instanceof ITypeDefinition) {
ITypeDefinition typeDefinition = (ITypeDefinition) definition;
if (!definition.isImplicit()) {
SymbolInformation typeSymbol = definitionToSymbol(typeDefinition);
result.add(typeSymbol);
}
IASScope typeScope = typeDefinition.getContainedScope();
scopeToSymbols(typeScope, result);
} else if (definition instanceof IFunctionDefinition || definition instanceof IVariableDefinition) {
if (definition.isImplicit()) {
continue;
}
SymbolInformation localSymbol = definitionToSymbol(definition);
result.add(localSymbol);
}
}
}
Aggregations