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()));
}
}
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();
}
}
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();
}
}
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);
}
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);
}
Aggregations