use of org.eclipse.jdt.core.search.MethodNameMatchRequestor 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;
}
Aggregations