Skip to content

Commit 72788a8

Browse files
committed
Add java.diagnostic.filter to exclude files from diagnostic publishing.
- When diagnostic filter is updated : - only update (clear) documents that are being newly filtered - documents no longer being filtered are updated upon modification - Add testcase Signed-off-by: Roland Grunberg <[email protected]>
1 parent 6c14f9b commit 72788a8

File tree

5 files changed

+102
-7
lines changed

5 files changed

+102
-7
lines changed

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

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.ls.core.internal.handlers;
1515

16+
import java.io.File;
1617
import java.lang.reflect.Field;
18+
import java.net.URI;
1719
import java.util.ArrayList;
1820
import java.util.Arrays;
1921
import java.util.Collections;
22+
import java.util.HashSet;
2023
import java.util.List;
24+
import java.util.Set;
2125

2226
import org.eclipse.core.resources.IMarker;
27+
import org.eclipse.core.resources.IProject;
2328
import org.eclipse.core.resources.IResource;
2429
import org.eclipse.core.runtime.CoreException;
2530
import org.eclipse.jdt.core.IBuffer;
@@ -33,6 +38,7 @@
3338
import org.eclipse.jdt.ls.core.internal.JDTUtils;
3439
import org.eclipse.jdt.ls.core.internal.JavaClientConnection;
3540
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
41+
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
3642
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
3743
import org.eclipse.jface.text.IDocument;
3844
import org.eclipse.lsp4j.Diagnostic;
@@ -129,12 +135,48 @@ public void beginReporting() {
129135

130136
@Override
131137
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($);
138+
if (!matchesDiagnosticFilter(uri, JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getDiagnosticFilter())) {
139+
JavaLanguageServerPlugin.logInfo(problems.size() + " problems reported for " + this.uri.substring(this.uri.lastIndexOf('/')));
140+
boolean isDiagnosticTagSupported = JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isDiagnosticTagSupported();
141+
List<Diagnostic> diagnostics = toDiagnosticsArray(this.cu, problems, isDiagnosticTagSupported);
142+
collectNonJavaProblems(diagnostics, isDiagnosticTagSupported);
143+
PublishDiagnosticsParams $ = new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), diagnostics);
144+
this.connection.publishDiagnostics($);
145+
}
146+
}
147+
148+
/**
149+
* @param uri the String URI to test
150+
* @param diagnosticFilters a list of patterns to test against
151+
* @return true if the URI matches any of the given patterns.
152+
*/
153+
public static boolean matchesDiagnosticFilter(String uri, List<String> diagnosticFilters) {
154+
return JDTUtils.isExcludedFile(diagnosticFilters, uri);
155+
}
156+
157+
/**
158+
* @param diagnosticFilter a list of patterns to test against
159+
* @return a set of document URI that match any of the given patterns.
160+
*/
161+
public static Set<String> getDocumentsMatchingFilter(List<String> diagnosticFilter) {
162+
Set<String> uris = new HashSet<>();
163+
for (IProject project : ProjectUtils.getAllProjects()) {
164+
try {
165+
IMarker[] markers = project.findMarkers(null, true, IResource.DEPTH_INFINITE);
166+
for (IMarker marker : markers) {
167+
URI locationURI = marker.getResource().getLocationURI();
168+
if (locationURI != null && !new File(locationURI).isDirectory()) {
169+
String uriString = locationURI.toString();
170+
if (BaseDiagnosticsHandler.matchesDiagnosticFilter(uriString, diagnosticFilter)) {
171+
uris.add(uriString);
172+
}
173+
}
174+
}
175+
} catch (CoreException e) {
176+
// continue
177+
}
178+
}
179+
return uris;
138180
}
139181

140182
/**

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.matchesDiagnosticFilter(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.matchesDiagnosticFilter(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.matchesDiagnosticFilter(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/PreferenceManager.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,24 @@
1212
*******************************************************************************/
1313
package org.eclipse.jdt.ls.core.internal.preferences;
1414

15+
import java.io.File;
1516
import java.io.IOException;
1617
import java.io.StringWriter;
1718
import java.io.Writer;
19+
import java.net.URI;
20+
import java.util.Collections;
21+
import java.util.HashSet;
1822
import java.util.Hashtable;
1923
import java.util.LinkedHashMap;
2024
import java.util.List;
2125
import java.util.Map;
2226
import java.util.Objects;
27+
import java.util.Set;
2328
import java.util.stream.Collectors;
2429

2530
import org.apache.commons.lang3.StringUtils;
31+
import org.eclipse.core.resources.IMarker;
32+
import org.eclipse.core.resources.IProject;
2633
import org.eclipse.core.resources.IResource;
2734
import org.eclipse.core.runtime.CoreException;
2835
import org.eclipse.core.runtime.ISafeRunnable;
@@ -47,11 +54,15 @@
4754
import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
4855
import org.eclipse.jdt.ls.core.internal.IConstants;
4956
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
57+
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
58+
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
5059
import org.eclipse.jdt.ls.core.internal.StatusFactory;
60+
import org.eclipse.jdt.ls.core.internal.handlers.BaseDiagnosticsHandler;
5161
import org.eclipse.jdt.ls.core.internal.handlers.FormatterHandler;
5262
import org.eclipse.jface.text.templates.Template;
5363
import org.eclipse.jface.text.templates.TemplateContextType;
5464
import org.eclipse.lsp4j.ClientCapabilities;
65+
import org.eclipse.lsp4j.PublishDiagnosticsParams;
5566
import org.eclipse.text.templates.ContextTypeRegistry;
5667
import org.eclipse.text.templates.TemplatePersistenceData;
5768
import org.eclipse.text.templates.TemplateReaderWriter;
@@ -237,6 +248,16 @@ public void update(Preferences preferences) {
237248
// add the resourceFilters preference; the org.eclipse.jdt.ls.filesystem plugin uses it
238249
eclipsePreferences.put(Preferences.JAVA_RESOURCE_FILTERS, String.join("::", resourceFilters));
239250
// TODO serialize preferences
251+
252+
if (!oldPreferences.getDiagnosticFilter().equals(preferences.getDiagnosticFilter())) {
253+
Set<String> oldMatchingURI = BaseDiagnosticsHandler.getDocumentsMatchingFilter(oldPreferences.getDiagnosticFilter());
254+
Set<String> matchingURI = BaseDiagnosticsHandler.getDocumentsMatchingFilter(preferences.getDiagnosticFilter());
255+
matchingURI.removeAll(oldMatchingURI);
256+
for (String uri : matchingURI) {
257+
PublishDiagnosticsParams diagnostics = new PublishDiagnosticsParams(ResourceUtils.toClientUri(uri), Collections.emptyList());
258+
JavaLanguageServerPlugin.getInstance().getClientConnection().publishDiagnostics(diagnostics);
259+
}
260+
}
240261
}
241262

242263
private void preferencesChanged(Preferences oldPreferences, Preferences newPreferences) {

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(param.getUri() + " should have been excluded from diagnostics.", !param.getUri().contains("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)