Skip to content

Commit eb52e53

Browse files
committed
Add java.diagnostic.filter to exclude files from diagnostic publishing.
- Add testcase Signed-off-by: Roland Grunberg <[email protected]>
1 parent 6c14f9b commit eb52e53

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BaseDiagnosticsHandler.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,18 @@ public void beginReporting() {
129129

130130
@Override
131131
public void endReporting() {
132-
JavaLanguageServerPlugin.logInfo(problems.size() + " problems reported for " + this.uri.substring(this.uri.lastIndexOf('/')));
133-
boolean isDiagnosticTagSupported = JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isDiagnosticTagSupported();
134-
List<Diagnostic> diagnostics = toDiagnosticsArray(this.cu, problems, isDiagnosticTagSupported);
135-
collectNonJavaProblems(diagnostics, isDiagnosticTagSupported);
136-
PublishDiagnosticsParams $ = new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), diagnostics);
137-
this.connection.publishDiagnostics($);
132+
if (!filterURIFromDiagnostics(uri, JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getDiagnosticFilter())) {
133+
JavaLanguageServerPlugin.logInfo(problems.size() + " problems reported for " + this.uri.substring(this.uri.lastIndexOf('/')));
134+
boolean isDiagnosticTagSupported = JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isDiagnosticTagSupported();
135+
List<Diagnostic> diagnostics = toDiagnosticsArray(this.cu, problems, isDiagnosticTagSupported);
136+
collectNonJavaProblems(diagnostics, isDiagnosticTagSupported);
137+
PublishDiagnosticsParams $ = new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), diagnostics);
138+
this.connection.publishDiagnostics($);
139+
}
140+
}
141+
142+
public static boolean filterURIFromDiagnostics(String uri, List<String> diagnosticFilter) {
143+
return JDTUtils.isExcludedFile(diagnosticFilter, uri);
138144
}
139145

140146
/**

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ else if (projectsManager.isBuildFile(file)) {
196196
}
197197
if (document != null) {
198198
String uri = JDTUtils.getFileURI(resource);
199-
this.connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), toDiagnosticsArray(document, markers, isDiagnosticTagSupported)));
199+
if (!BaseDiagnosticsHandler.filterURIFromDiagnostics(uri, JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getDiagnosticFilter())) {
200+
this.connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), toDiagnosticsArray(document, markers, isDiagnosticTagSupported)));
201+
}
200202
}
201203
return false;
202204
}
@@ -207,6 +209,9 @@ private void publishMarkers(IProject project, IMarker[] markers) throws CoreExce
207209
List<IMarker> projectMarkers = new ArrayList<>(markers.length);
208210

209211
String uri = JDTUtils.getFileURI(project);
212+
if (BaseDiagnosticsHandler.filterURIFromDiagnostics(uri, JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getDiagnosticFilter())) {
213+
return;
214+
}
210215
IFile pom = project.getFile("pom.xml");
211216
IFile gradleWrapperProperties = project.getFile(GradleProjectImporter.GRADLE_WRAPPER_PROPERTIES_DESCRIPTOR);
212217
List<IMarker> pomMarkers = new ArrayList<>();
@@ -316,6 +321,9 @@ private void publishDiagnostics(List<IMarker> markers) {
316321
}
317322
IDocument document = null;
318323
String uri = JDTUtils.getFileURI(file);
324+
if (BaseDiagnosticsHandler.filterURIFromDiagnostics(uri, JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getDiagnosticFilter())) {
325+
continue;
326+
}
319327
if (JavaCore.isJavaLikeFileName(file.getName())) {
320328
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
321329
//ignoring working copies, they're handled in the DocumentLifecycleHandler

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ public class Preferences {
487487
public static final String JAVA_TELEMETRY_ENABLED_KEY = "java.telemetry.enabled";
488488

489489
public static final String JAVA_EDIT_VALIDATE_ALL_OPEN_BUFFERS_ON_CHANGES = "java.edit.validateAllOpenBuffersOnChanges";
490+
public static final String JAVA_DIAGNOSTIC_FILER = "java.diagnostic.filter";
490491
/**
491492
* The preferences for generating toString method.
492493
*/
@@ -696,6 +697,7 @@ public class Preferences {
696697
private boolean telemetryEnabled;
697698
private boolean validateAllOpenBuffersOnChanges;
698699
private boolean chainCompletionEnabled;
700+
private List<String> diagnosticFilter;
699701

700702
static {
701703
JAVA_IMPORT_EXCLUSIONS_DEFAULT = new LinkedList<>();
@@ -932,6 +934,7 @@ public Preferences() {
932934
extractInterfaceReplaceEnabled = false;
933935
telemetryEnabled = false;
934936
validateAllOpenBuffersOnChanges = true;
937+
diagnosticFilter = new ArrayList<>();
935938
}
936939

937940
private static void initializeNullAnalysisClasspathStorage() {
@@ -1325,6 +1328,8 @@ public static Preferences createFrom(Map<String, Object> configuration) {
13251328
prefs.setValidateAllOpenBuffersOnChanges(validateAllOpenBuffers);
13261329
boolean chainCompletionEnabled = getBoolean(configuration, CHAIN_COMPLETION_KEY, false);
13271330
prefs.setChainCompletionEnabled(chainCompletionEnabled);
1331+
List<String> diagnosticFilter = getList(configuration, JAVA_DIAGNOSTIC_FILER, Collections.emptyList());
1332+
prefs.setDiagnosticFilter(diagnosticFilter);
13281333
return prefs;
13291334
}
13301335

@@ -2569,4 +2574,12 @@ public boolean isValidateAllOpenBuffersOnChanges() {
25692574
public void setValidateAllOpenBuffersOnChanges(boolean validateAllOpenBuffersOnChanges) {
25702575
this.validateAllOpenBuffersOnChanges = validateAllOpenBuffersOnChanges;
25712576
}
2577+
2578+
public List<String> getDiagnosticFilter() {
2579+
return this.diagnosticFilter;
2580+
}
2581+
2582+
public void setDiagnosticFilter(List<String> diagnosticFilter) {
2583+
this.diagnosticFilter = diagnosticFilter;
2584+
}
25722585
}

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/WorkspaceDiagnosticsHandlerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,17 @@ public void testEncoding() throws Exception {
651651
}
652652
}
653653

654+
@Test
655+
public void testDiagnosticFiltering() throws Exception {
656+
// preferences.setDiagnosticFilter(List.of("**/Foo*.java"));
657+
importProjects("eclipse/hello");
658+
ArgumentCaptor<PublishDiagnosticsParams> captor = ArgumentCaptor.forClass(PublishDiagnosticsParams.class);
659+
verify(connection, atLeastOnce()).publishDiagnostics(captor.capture());
660+
for (PublishDiagnosticsParams param : captor.getAllValues()) {
661+
assertTrue("Files containing 'Foo' have not been correctly filtered.", !param.getUri().matches("Foo"));
662+
}
663+
}
664+
654665
private IMarker createMarker(int severity, String msg, int line, int start, int end) {
655666
IMarker m = mock(IMarker.class);
656667
when(m.exists()).thenReturn(true);

0 commit comments

Comments
 (0)