Search in sources :

Example 1 with DidChangeWatchedFilesRegistrationOptions

use of org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions in project eclipse.jdt.ls by eclipse.

the class StandardProjectsManager method registerWatchers.

@Override
public List<FileSystemWatcher> registerWatchers() {
    logInfo(">> registerWatchers'");
    if (preferenceManager.getClientPreferences().isWorkspaceChangeWatchedFilesDynamicRegistered()) {
        Set<String> patterns = new LinkedHashSet<>(basicWatchers);
        buildSupports().forEach(e -> e.getWatchPatterns().forEach(patterns::add));
        Set<IPath> sources = new HashSet<>();
        IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
        try {
            for (IProject project : projects) {
                if (DEFAULT_PROJECT_NAME.equals(project.getName())) {
                    continue;
                }
                IJavaProject javaProject = JavaCore.create(project);
                if (javaProject != null && javaProject.exists()) {
                    IClasspathEntry[] classpath = javaProject.getRawClasspath();
                    for (IClasspathEntry entry : classpath) {
                        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                            IPath path = entry.getPath();
                            if (path != null && !path.toString().contains("/src/") && !path.toString().endsWith("/src")) {
                                IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
                                if (folder.exists() && !folder.isDerived()) {
                                    IPath location = folder.getLocation();
                                    if (location != null && !isContainedIn(location, sources)) {
                                        sources.add(location);
                                    }
                                }
                            }
                        }
                        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                            IPath path = entry.getPath();
                            IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
                            if (resource != null && !resource.isDerived()) {
                                IPath location = resource.getLocation();
                                if (location != null && !isContainedIn(location, sources)) {
                                    sources.add(location);
                                }
                            }
                        }
                    }
                    if (!ProjectUtils.isVisibleProject(project)) {
                        // Invisible project will watch referenced libraries' include patterns
                        IPath projectFolder = ProjectUtils.getProjectRealFolder(project);
                        Set<String> libraries = preferenceManager.getPreferences().getReferencedLibraries().getInclude();
                        for (String pattern : libraries) {
                            patterns.add(ProjectUtils.resolveGlobPath(projectFolder, pattern).toPortableString());
                        }
                        patterns.add("**/.settings");
                    }
                }
            }
        } catch (JavaModelException e) {
            JavaLanguageServerPlugin.logException(e.getMessage(), e);
        }
        List<FileSystemWatcher> fileWatchers = new ArrayList<>();
        patterns.addAll(sources.stream().map(ResourceUtils::toGlobPattern).collect(Collectors.toList()));
        sources.clear();
        URI formatter = preferenceManager.getPreferences().getFormatterAsURI();
        if (formatter == null && preferenceManager.getPreferences().getFormatterUrl() != null) {
            List<URI> uris = getURIs(preferenceManager.getPreferences().getFormatterUrl());
            for (URI uri : uris) {
                addWatcher(uri, sources);
            }
        } else {
            addWatcher(formatter, sources);
        }
        URI settings = preferenceManager.getPreferences().getSettingsAsURI();
        if (settings == null && preferenceManager.getPreferences().getSettingsUrl() != null) {
            List<URI> uris = getURIs(preferenceManager.getPreferences().getSettingsUrl());
            for (URI uri : uris) {
                addWatcher(uri, sources);
            }
        } else {
            addWatcher(settings, sources);
        }
        patterns.addAll(sources.stream().map(p -> ResourceUtils.toGlobPattern(p, false)).collect(Collectors.toList()));
        for (String pattern : patterns) {
            FileSystemWatcher watcher = new FileSystemWatcher(pattern);
            fileWatchers.add(watcher);
        }
        // Watch on project root folders.
        for (IProject project : projects) {
            if (ProjectUtils.isVisibleProject(project) && project.exists()) {
                FileSystemWatcher watcher = new FileSystemWatcher(ResourceUtils.toGlobPattern(project.getLocation(), false), WatchKind.Delete);
                fileWatchers.add(watcher);
            }
        }
        if (!patterns.equals(watchers)) {
            logInfo(">> registerFeature 'workspace/didChangeWatchedFiles'");
            DidChangeWatchedFilesRegistrationOptions didChangeWatchedFilesRegistrationOptions = new DidChangeWatchedFilesRegistrationOptions(fileWatchers);
            JavaLanguageServerPlugin.getInstance().unregisterCapability(Preferences.WORKSPACE_WATCHED_FILES_ID, Preferences.WORKSPACE_WATCHED_FILES);
            JavaLanguageServerPlugin.getInstance().registerCapability(Preferences.WORKSPACE_WATCHED_FILES_ID, Preferences.WORKSPACE_WATCHED_FILES, didChangeWatchedFilesRegistrationOptions);
            watchers.clear();
            watchers.addAll(patterns);
        }
        return fileWatchers;
    }
    return Collections.emptyList();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) URI(java.net.URI) IProject(org.eclipse.core.resources.IProject) DidChangeWatchedFilesRegistrationOptions(org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions) FileSystemWatcher(org.eclipse.lsp4j.FileSystemWatcher) IJavaProject(org.eclipse.jdt.core.IJavaProject) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with DidChangeWatchedFilesRegistrationOptions

use of org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions in project eclipse.jdt.ls by eclipse.

the class SyntaxProjectsManager method registerWatchers.

@Override
public List<FileSystemWatcher> registerWatchers() {
    logInfo(">> registerFeature 'workspace/didChangeWatchedFiles'");
    if (JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isWorkspaceChangeWatchedFilesDynamicRegistered()) {
        IPath[] sources = new IPath[0];
        try {
            sources = listAllSourcePaths();
        } catch (JavaModelException e) {
            JavaLanguageServerPlugin.logException(e.getMessage(), e);
        }
        List<FileSystemWatcher> fileWatchers = new ArrayList<>();
        Set<String> patterns = new LinkedHashSet<>(basicWatchers);
        patterns.addAll(Stream.of(sources).map(ResourceUtils::toGlobPattern).collect(Collectors.toList()));
        for (String pattern : patterns) {
            FileSystemWatcher watcher = new FileSystemWatcher(pattern);
            fileWatchers.add(watcher);
        }
        if (!patterns.equals(watchers)) {
            JavaLanguageServerPlugin.logInfo(">> registerFeature 'workspace/didChangeWatchedFiles'");
            DidChangeWatchedFilesRegistrationOptions didChangeWatchedFilesRegistrationOptions = new DidChangeWatchedFilesRegistrationOptions(fileWatchers);
            JavaLanguageServerPlugin.getInstance().unregisterCapability(Preferences.WORKSPACE_WATCHED_FILES_ID, Preferences.WORKSPACE_WATCHED_FILES);
            JavaLanguageServerPlugin.getInstance().registerCapability(Preferences.WORKSPACE_WATCHED_FILES_ID, Preferences.WORKSPACE_WATCHED_FILES, didChangeWatchedFilesRegistrationOptions);
            watchers.clear();
            watchers.addAll(patterns);
        }
        return fileWatchers;
    }
    return Collections.emptyList();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JavaModelException(org.eclipse.jdt.core.JavaModelException) FileSystemWatcher(org.eclipse.lsp4j.FileSystemWatcher) IPath(org.eclipse.core.runtime.IPath) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) ArrayList(java.util.ArrayList) DidChangeWatchedFilesRegistrationOptions(org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions)

Aggregations

ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 IPath (org.eclipse.core.runtime.IPath)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 ResourceUtils (org.eclipse.jdt.ls.core.internal.ResourceUtils)2 DidChangeWatchedFilesRegistrationOptions (org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions)2 FileSystemWatcher (org.eclipse.lsp4j.FileSystemWatcher)2 URI (java.net.URI)1 HashSet (java.util.HashSet)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IProject (org.eclipse.core.resources.IProject)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1