Search in sources :

Example 1 with ActionableNotification

use of org.eclipse.jdt.ls.core.internal.ActionableNotification in project eclipse.jdt.ls by eclipse.

the class StandardProjectsManager method fileChanged.

@Override
public void fileChanged(String uriString, CHANGE_TYPE changeType) {
    if (uriString == null) {
        return;
    }
    boolean configureNeeded = false;
    String formatterUrl = preferenceManager.getPreferences().getFormatterUrl();
    if (formatterUrl != null && JavaLanguageServerPlugin.getInstance().getProtocol() != null) {
        URI uri = JDTUtils.toURI(uriString);
        List<URI> uris = getURIs(formatterUrl);
        boolean changed = false;
        for (URI formatterUri : uris) {
            if (URIUtil.sameURI(formatterUri, uri)) {
                changed = true;
                break;
            }
        }
        if (changed) {
            if (changeType == CHANGE_TYPE.DELETED || changeType == CHANGE_TYPE.CREATED) {
                registerWatchers();
            }
            configureNeeded = true;
        }
    }
    String settingsUrl = preferenceManager.getPreferences().getSettingsUrl();
    if (settingsUrl != null && JavaLanguageServerPlugin.getInstance().getProtocol() != null) {
        URI uri = JDTUtils.toURI(uriString);
        List<URI> uris = getURIs(settingsUrl);
        boolean changed = false;
        for (URI settingsURI : uris) {
            if (URIUtil.sameURI(settingsURI, uri)) {
                changed = true;
                break;
            }
        }
        if (changed) {
            if (changeType == CHANGE_TYPE.DELETED || changeType == CHANGE_TYPE.CREATED) {
                registerWatchers();
            }
            configureNeeded = true;
        }
    }
    if (configureNeeded) {
        configureSettings(preferenceManager.getPreferences());
    }
    IResource resource = JDTUtils.getFileOrFolder(uriString);
    if (resource == null) {
        return;
    }
    try {
        Optional<IBuildSupport> bs = getBuildSupport(resource.getProject());
        if (bs.isPresent()) {
            IBuildSupport buildSupport = bs.get();
            if (JDTUtils.isExcludedFile(buildSupport.getExcludedFilePatterns(), uriString)) {
                return;
            }
            boolean requireConfigurationUpdate = buildSupport.fileChanged(resource, changeType, new NullProgressMonitor());
            if (requireConfigurationUpdate) {
                FeatureStatus status = preferenceManager.getPreferences().getUpdateBuildConfigurationStatus();
                switch(status) {
                    case automatic:
                        // do not force the build, because it's not started by user and should be done only if build file has changed
                        updateProject(resource.getProject(), false);
                        break;
                    case disabled:
                        break;
                    default:
                        if (client != null) {
                            String cmd = "java.projectConfiguration.status";
                            TextDocumentIdentifier uri = new TextDocumentIdentifier(uriString);
                            ActionableNotification updateProjectConfigurationNotification = new ActionableNotification().withSeverity(MessageType.Info).withMessage("A build file was modified. Do you want to synchronize the Java classpath/configuration?").withCommands(asList(new Command("Never", cmd, asList(uri, FeatureStatus.disabled)), new Command("Now", cmd, asList(uri, FeatureStatus.interactive)), new Command("Always", cmd, asList(uri, FeatureStatus.automatic))));
                            client.sendActionableNotification(updateProjectConfigurationNotification);
                        }
                }
            }
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Problem refreshing workspace", e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) FeatureStatus(org.eclipse.jdt.ls.core.internal.preferences.Preferences.FeatureStatus) ActionableNotification(org.eclipse.jdt.ls.core.internal.ActionableNotification) URI(java.net.URI) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command) IResource(org.eclipse.core.resources.IResource)

Example 2 with ActionableNotification

use of org.eclipse.jdt.ls.core.internal.ActionableNotification in project eclipse.jdt.ls by eclipse.

the class DocumentLifeCycleHandler method handleOpen.

public void handleOpen(DidOpenTextDocumentParams params) {
    String uri = params.getTextDocument().getUri();
    ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
    if (unit == null || unit.getResource() == null) {
        return;
    }
    try {
        // checks if the underlying resource exists and refreshes to sync the newly created file.
        if (!unit.getResource().isAccessible()) {
            try {
                unit.getResource().refreshLocal(IResource.DEPTH_ONE, new NullProgressMonitor());
                if (unit.getResource().exists()) {
                    IJavaElement parent = unit.getParent();
                    if (parent instanceof IPackageFragment) {
                        IPackageFragment pkg = (IPackageFragment) parent;
                        unit = pkg.createCompilationUnit(unit.getElementName(), unit.getSource(), true, new NullProgressMonitor());
                    }
                }
            } catch (CoreException e) {
            // ignored
            }
        }
        IProject project = unit.getResource().getProject();
        // Resources belonging to the default project can only report syntax errors, because the project classpath is incomplete
        boolean isDefaultProject = project.equals(JavaLanguageServerPlugin.getProjectsManager().getDefaultProject());
        if (isDefaultProject || !JDTUtils.isOnClassPath(unit)) {
            Severity severity = preferenceManager.getPreferences(project).getIncompleteClasspathSeverity();
            String msg;
            if (isDefaultProject) {
                msg = "Classpath is incomplete. Only syntax errors will be reported";
            } else {
                msg = unit.getElementName() + " isn't on the classpath. Only syntax errors will be reported";
            }
            JavaLanguageServerPlugin.logInfo(msg + " for " + uri);
            if (severity.compareTo(Preferences.Severity.ignore) > 0) {
                ActionableNotification ignoreIncompleteClasspath = new ActionableNotification().withSeverity(severity.toMessageType()).withMessage(msg).withCommands(Arrays.asList(new Command("More Information", "java.ignoreIncompleteClasspath.help", null), new Command("Don't Show Again", "java.ignoreIncompleteClasspath", null)));
                connection.sendActionableNotification(ignoreIncompleteClasspath);
            }
        }
        // DiagnosticsHandler problemRequestor = new DiagnosticsHandler(connection, unit.getResource(), reportOnlySyntaxErrors);
        unit.becomeWorkingCopy(new NullProgressMonitor());
        IBuffer buffer = unit.getBuffer();
        String newContent = params.getTextDocument().getText();
        if (buffer != null && !buffer.getContents().equals(newContent)) {
            buffer.setContents(newContent);
        }
        triggerValidation(unit);
        // see https://github.com/redhat-developer/vscode-java/issues/274
        checkPackageDeclaration(uri, unit);
    } catch (JavaModelException e) {
        JavaLanguageServerPlugin.logException("Error while opening document", e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) ActionableNotification(org.eclipse.jdt.ls.core.internal.ActionableNotification) Severity(org.eclipse.jdt.ls.core.internal.preferences.Preferences.Severity) IProject(org.eclipse.core.resources.IProject) IBuffer(org.eclipse.jdt.core.IBuffer) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command)

Example 3 with ActionableNotification

use of org.eclipse.jdt.ls.core.internal.ActionableNotification in project eclipse.jdt.ls by eclipse.

the class ProjectsManager method fileChanged.

public void fileChanged(String uriString, CHANGE_TYPE changeType) {
    if (uriString == null) {
        return;
    }
    IResource resource = JDTUtils.findFile(uriString);
    if (resource == null) {
        return;
    }
    try {
        if (changeType == CHANGE_TYPE.DELETED) {
            resource = resource.getParent();
        }
        if (resource != null) {
            resource.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
        }
        if (isBuildFile(resource)) {
            FeatureStatus status = preferenceManager.getPreferences().getUpdateBuildConfigurationStatus();
            switch(status) {
                case automatic:
                    updateProject(resource.getProject());
                    break;
                case disabled:
                    break;
                default:
                    if (client != null) {
                        String cmd = "java.projectConfiguration.status";
                        TextDocumentIdentifier uri = new TextDocumentIdentifier(uriString);
                        ActionableNotification updateProjectConfigurationNotification = new ActionableNotification().withSeverity(MessageType.Info).withMessage("A build file was modified. Do you want to synchronize the Java classpath/configuration?").withCommands(asList(new Command("Never", cmd, asList(uri, FeatureStatus.disabled)), new Command("Now", cmd, asList(uri, FeatureStatus.interactive)), new Command("Always", cmd, asList(uri, FeatureStatus.automatic))));
                        client.sendActionableNotification(updateProjectConfigurationNotification);
                    }
            }
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Problem refreshing workspace", e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CoreException(org.eclipse.core.runtime.CoreException) FeatureStatus(org.eclipse.jdt.ls.core.internal.preferences.Preferences.FeatureStatus) Command(org.eclipse.lsp4j.Command) ActionableNotification(org.eclipse.jdt.ls.core.internal.ActionableNotification) IResource(org.eclipse.core.resources.IResource)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 ActionableNotification (org.eclipse.jdt.ls.core.internal.ActionableNotification)3 Command (org.eclipse.lsp4j.Command)3 IResource (org.eclipse.core.resources.IResource)2 FeatureStatus (org.eclipse.jdt.ls.core.internal.preferences.Preferences.FeatureStatus)2 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)2 URI (java.net.URI)1 IProject (org.eclipse.core.resources.IProject)1 IBuffer (org.eclipse.jdt.core.IBuffer)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 Severity (org.eclipse.jdt.ls.core.internal.preferences.Preferences.Severity)1