Search in sources :

Example 1 with DiagnosticsHandler

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

the class InvisibleProjectPreferenceChangeListener method preferencesChange.

@Override
public void preferencesChange(Preferences oldPreferences, Preferences newPreferences) {
    try {
        if (!Objects.equals(oldPreferences.getInvisibleProjectSourcePaths(), newPreferences.getInvisibleProjectSourcePaths())) {
            for (IJavaProject javaProject : ProjectUtils.getJavaProjects()) {
                IProject project = javaProject.getProject();
                if (ProjectUtils.isVisibleProject(project)) {
                    continue;
                }
                if (project.equals(ProjectsManager.getDefaultProject())) {
                    continue;
                }
                IFolder workspaceLinkFolder = javaProject.getProject().getFolder(ProjectUtils.WORKSPACE_LINK);
                IPath rootPath = ProjectUtils.findBelongedWorkspaceRoot(workspaceLinkFolder.getLocation());
                if (rootPath == null) {
                    continue;
                }
                List<IPath> sourcePaths = InvisibleProjectImporter.getSourcePaths(newPreferences.getInvisibleProjectSourcePaths(), workspaceLinkFolder);
                List<IPath> excludingPaths = InvisibleProjectImporter.getExcludingPath(javaProject, rootPath, workspaceLinkFolder);
                IPath outputPath = InvisibleProjectImporter.getOutputPath(javaProject, newPreferences.getInvisibleProjectOutputPath(), true);
                IClasspathEntry[] classpathEntries = InvisibleProjectImporter.resolveClassPathEntries(javaProject, sourcePaths, excludingPaths, outputPath);
                javaProject.setRawClasspath(classpathEntries, outputPath, new NullProgressMonitor());
                if (JavaLanguageServerPlugin.getInstance().getProtocol() != null && JavaLanguageServerPlugin.getInstance().getProtocol().getClientConnection() != null) {
                    for (ICompilationUnit unit : JavaCore.getWorkingCopies(null)) {
                        IPath path = unit.getPath();
                        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
                        if (file.exists()) {
                            String contents = null;
                            try {
                                if (unit.hasUnsavedChanges()) {
                                    contents = unit.getSource();
                                }
                            } catch (Exception e) {
                                JavaLanguageServerPlugin.logException(e.getMessage(), e);
                            }
                            unit.discardWorkingCopy();
                            if (unit.equals(CoreASTProvider.getInstance().getActiveJavaElement())) {
                                CoreASTProvider.getInstance().disposeAST();
                            }
                            unit = JavaCore.createCompilationUnitFrom(file);
                            unit.becomeWorkingCopy(null);
                            if (contents != null) {
                                unit.getBuffer().setContents(contents);
                            }
                        }
                        DiagnosticsHandler diagnosticHandler = new DiagnosticsHandler(JavaLanguageServerPlugin.getInstance().getProtocol().getClientConnection(), unit);
                        diagnosticHandler.clearDiagnostics();
                        DiagnosticsCommand.refreshDiagnostics(JDTUtils.toURI(unit), "thisFile", JDTUtils.isDefaultProject(unit) || !JDTUtils.isOnClassPath(unit));
                    }
                }
            }
        } else if (!Objects.equals(oldPreferences.getInvisibleProjectOutputPath(), newPreferences.getInvisibleProjectOutputPath())) {
            for (IJavaProject javaProject : ProjectUtils.getJavaProjects()) {
                IProject project = javaProject.getProject();
                if (ProjectUtils.isVisibleProject(project)) {
                    continue;
                }
                if (project.equals(ProjectsManager.getDefaultProject())) {
                    continue;
                }
                IPath outputPath = InvisibleProjectImporter.getOutputPath(javaProject, newPreferences.getInvisibleProjectOutputPath(), true);
                javaProject.setOutputLocation(outputPath, new NullProgressMonitor());
            }
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.getProjectsManager().getConnection().showMessage(new MessageParams(MessageType.Error, e.getMessage()));
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) MessageParams(org.eclipse.lsp4j.MessageParams) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) DiagnosticsHandler(org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with DiagnosticsHandler

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

the class NonProjectFixTest method testReportSyntaxErrorsFixForNonProjectFile.

@Test
public void testReportSyntaxErrorsFixForNonProjectFile() throws Exception {
    JavaLanguageServerPlugin.getNonProjectDiagnosticsState().setGlobalErrorLevel(false);
    IJavaProject javaProject = newDefaultProject();
    IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
    IPackageFragment pack1 = sourceFolder.createPackageFragment("java", false, null);
    // @formatter:off
    String standaloneFileContent = "package java;\n" + "public class Foo extends UnknownType {\n" + "	public void method1(){\n" + "		super.whatever()\n" + "	}\n" + "}";
    // @formatter:on
    ICompilationUnit cu1 = pack1.createCompilationUnit("Foo.java", standaloneFileContent, false, null);
    final DiagnosticsHandler handler = new DiagnosticsHandler(javaClient, cu1);
    WorkingCopyOwner wcOwner = createWorkingCopyOwner(cu1, handler);
    cu1.becomeWorkingCopy(null);
    try {
        cu1.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
        List<IProblem> problems = handler.getProblems();
        assertFalse(problems.isEmpty());
        List<Either<Command, CodeAction>> actions = getCodeActions(cu1, problems.get(0));
        assertEquals(2, actions.size());
        CodeAction action = actions.get(0).getRight();
        assertEquals(CodeActionKind.QuickFix, action.getKind());
        assertEquals(ActionMessages.ReportSyntaxErrorsForThisFile, action.getCommand().getTitle());
        assertEquals(3, action.getCommand().getArguments().size());
        assertEquals("thisFile", action.getCommand().getArguments().get(1));
        assertEquals(true, action.getCommand().getArguments().get(2));
        action = actions.get(1).getRight();
        assertEquals(CodeActionKind.QuickFix, action.getKind());
        assertEquals(ActionMessages.ReportSyntaxErrorsForAnyNonProjectFile, action.getCommand().getTitle());
        assertEquals(3, action.getCommand().getArguments().size());
        assertEquals("anyNonProjectFile", action.getCommand().getArguments().get(1));
        assertEquals(true, action.getCommand().getArguments().get(2));
    } finally {
        cu1.discardWorkingCopy();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) DiagnosticsHandler(org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler) IProblem(org.eclipse.jdt.core.compiler.IProblem) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 3 with DiagnosticsHandler

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

the class NonProjectFixTest method testReportAllErrorsFixForNonProjectFile.

@Test
public void testReportAllErrorsFixForNonProjectFile() throws Exception {
    IJavaProject javaProject = newDefaultProject();
    IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(javaProject.getProject().getFolder("src"));
    IPackageFragment pack1 = sourceFolder.createPackageFragment("java", false, null);
    // @formatter:off
    String standaloneFileContent = "package java;\n" + "public class Foo extends UnknownType {\n" + "	public void method1(){\n" + "		super.whatever()\n" + "	}\n" + "}";
    // @formatter:on
    ICompilationUnit cu1 = pack1.createCompilationUnit("Foo.java", standaloneFileContent, false, null);
    final DiagnosticsHandler handler = new DiagnosticsHandler(javaClient, cu1);
    WorkingCopyOwner wcOwner = createWorkingCopyOwner(cu1, handler);
    cu1.becomeWorkingCopy(null);
    try {
        cu1.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
        List<IProblem> problems = handler.getProblems();
        assertFalse(problems.isEmpty());
        List<Either<Command, CodeAction>> actions = getCodeActions(cu1, problems.get(0));
        assertEquals(2, actions.size());
        CodeAction action = actions.get(0).getRight();
        assertEquals(CodeActionKind.QuickFix, action.getKind());
        assertEquals(ActionMessages.ReportAllErrorsForThisFile, action.getCommand().getTitle());
        assertEquals(3, action.getCommand().getArguments().size());
        assertEquals("thisFile", action.getCommand().getArguments().get(1));
        assertEquals(false, action.getCommand().getArguments().get(2));
        action = actions.get(1).getRight();
        assertEquals(CodeActionKind.QuickFix, action.getKind());
        assertEquals(ActionMessages.ReportAllErrorsForAnyNonProjectFile, action.getCommand().getTitle());
        assertEquals(3, action.getCommand().getArguments().size());
        assertEquals("anyNonProjectFile", action.getCommand().getArguments().get(1));
        assertEquals(false, action.getCommand().getArguments().get(2));
    } finally {
        cu1.discardWorkingCopy();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) DiagnosticsHandler(org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler) IProblem(org.eclipse.jdt.core.compiler.IProblem) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 4 with DiagnosticsHandler

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

the class MavenClasspathTest method typemismatchTest.

@Test
public void typemismatchTest() throws Exception {
    IProject project = importMavenProject("typemismatch");
    IJavaProject javaProject = JavaCore.create(project);
    IType type = javaProject.findType("test.Test");
    ICompilationUnit cu = type.getCompilationUnit();
    openDocument(cu, cu.getSource(), 1);
    final DiagnosticsHandler handler = new DiagnosticsHandler(javaClient, cu);
    WorkingCopyOwner wcOwner = getWorkingCopy(handler);
    cu.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
    long size = handler.getProblems().stream().filter(p -> p.isError()).count();
    assertTrue("There is an error", size == 0);
    String source = cu.getSource();
    source = source.replace("Test {", "Test { ");
    cu.getBuffer().setContents(source);
    cu.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
    size = handler.getProblems().stream().filter(p -> p.isError()).count();
    assertTrue("There is an error", size == 0);
    source = source.replace("Test { ", "Test {  ");
    cu.getBuffer().setContents(source);
    cu.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
    size = handler.getProblems().stream().filter(p -> p.isError()).count();
    assertTrue("There is an error", size == 0);
    source = source.replace("Test {  ", "Test {   ");
    cu.getBuffer().setContents(source);
    cu.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
    size = handler.getProblems().stream().filter(p -> p.isError()).count();
    assertTrue("There is an error", size == 0);
    source = source.replace("Test {   ", "Test {");
    cu.getBuffer().setContents(source);
    cu.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
    size = handler.getProblems().stream().filter(p -> p.isError()).count();
    assertTrue("There is an error", size == 0);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticsHandler(org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaCore(org.eclipse.jdt.core.JavaCore) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IType(org.eclipse.jdt.core.IType) TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) IProject(org.eclipse.core.resources.IProject) DidOpenTextDocumentParams(org.eclipse.lsp4j.DidOpenTextDocumentParams) IProblemRequestor(org.eclipse.jdt.core.IProblemRequestor) After(org.junit.After) DocumentLifeCycleHandler(org.eclipse.jdt.ls.core.internal.handlers.DocumentLifeCycleHandler) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) Before(org.junit.Before) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) DiagnosticsHandler(org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler) IProject(org.eclipse.core.resources.IProject) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test)

Example 5 with DiagnosticsHandler

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

the class MavenClasspathTest method testMain.

@Test
public void testMain() throws Exception {
    IProject project = importMavenProject("classpathtest");
    IJavaProject javaProject = JavaCore.create(project);
    IType type = javaProject.findType("main.App");
    ICompilationUnit cu = type.getCompilationUnit();
    openDocument(cu, cu.getSource(), 1);
    final DiagnosticsHandler handler = new DiagnosticsHandler(javaClient, cu);
    WorkingCopyOwner wcOwner = getWorkingCopy(handler);
    cu.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null);
    assertTrue("There aren't any problems", handler.getProblems().size() == 1);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) DiagnosticsHandler(org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler) IProject(org.eclipse.core.resources.IProject) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 DiagnosticsHandler (org.eclipse.jdt.ls.core.internal.handlers.DiagnosticsHandler)7 IJavaProject (org.eclipse.jdt.core.IJavaProject)6 WorkingCopyOwner (org.eclipse.jdt.core.WorkingCopyOwner)6 Test (org.junit.Test)5 IProject (org.eclipse.core.resources.IProject)4 IType (org.eclipse.jdt.core.IType)3 IFile (org.eclipse.core.resources.IFile)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)2 IProblem (org.eclipse.jdt.core.compiler.IProblem)2 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)2 CodeAction (org.eclipse.lsp4j.CodeAction)2 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)2 IFolder (org.eclipse.core.resources.IFolder)1 IResource (org.eclipse.core.resources.IResource)1 CoreException (org.eclipse.core.runtime.CoreException)1 IPath (org.eclipse.core.runtime.IPath)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1