Search in sources :

Example 1 with MethodNameMatchRequestor

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;
}
Also used : MethodNameMatch(org.eclipse.jdt.core.search.MethodNameMatch) SymbolKind(org.eclipse.lsp4j.SymbolKind) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) JavaModelException(org.eclipse.jdt.core.JavaModelException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) MethodNameMatchRequestor(org.eclipse.jdt.core.search.MethodNameMatchRequestor) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) Location(org.eclipse.lsp4j.Location)

Aggregations

ArrayList (java.util.ArrayList)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)1 MethodNameMatch (org.eclipse.jdt.core.search.MethodNameMatch)1 MethodNameMatchRequestor (org.eclipse.jdt.core.search.MethodNameMatchRequestor)1 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)1 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)1 TypeNameMatchRequestor (org.eclipse.jdt.core.search.TypeNameMatchRequestor)1 PreferenceManager (org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager)1 Location (org.eclipse.lsp4j.Location)1 SymbolInformation (org.eclipse.lsp4j.SymbolInformation)1 SymbolKind (org.eclipse.lsp4j.SymbolKind)1