Skip to content

Commit 7d8b8b4

Browse files
committed
Fix watched files registration to work with paths outside workspace.
- Changes notifications for certain files (eg. `java.settings.url`) were not triggered when they existed outside the workspace Signed-off-by: Roland Grunberg <[email protected]>
1 parent 15ad5f6 commit 7d8b8b4

File tree

5 files changed

+45
-35
lines changed

5 files changed

+45
-35
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/ResourceUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import org.eclipse.core.runtime.URIUtil;
4848
import org.eclipse.jdt.core.IJavaModelMarker;
4949
import org.eclipse.jdt.core.compiler.IProblem;
50+
import org.eclipse.lsp4j.RelativePattern;
51+
import org.eclipse.lsp4j.jsonrpc.messages.Either;
5052

5153
import com.google.common.io.CharStreams;
5254

@@ -288,7 +290,7 @@ public String lookup(String key) {
288290
* the path to convert
289291
* @return a glob pattern prefixed with the path
290292
*/
291-
public static String toGlobPattern(IPath path) {
293+
public static Either<String, RelativePattern> toGlobPattern(IPath path) {
292294
if (path == null) {
293295
return null;
294296
}
@@ -306,11 +308,15 @@ public static String toGlobPattern(IPath path) {
306308
* whether to end the glob with "/**"
307309
* @return a glob pattern prefixed with the path
308310
*/
309-
public static String toGlobPattern(IPath path, boolean recursive) {
311+
public static Either<String, RelativePattern> toGlobPattern(IPath path, boolean recursive) {
310312
if (path == null) {
311313
return null;
312314
}
313315

316+
if (path.isAbsolute() && !recursive) {
317+
return Either.forRight(new RelativePattern(Either.forRight(path.removeLastSegments(1).toPortableString()), path.lastSegment()));
318+
}
319+
314320
String globPattern = path.toPortableString();
315321
if (path.getDevice() != null) {
316322
//This seems pretty hack-ish: need to remove device as it seems to break
@@ -328,7 +334,7 @@ public static String toGlobPattern(IPath path, boolean recursive) {
328334
}
329335
}
330336

331-
return globPattern;
337+
return Either.forLeft(globPattern);
332338
}
333339

334340
public static String dos2Unix(String str) {

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/StandardProjectsManager.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
import org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions;
100100
import org.eclipse.lsp4j.FileSystemWatcher;
101101
import org.eclipse.lsp4j.MessageType;
102+
import org.eclipse.lsp4j.RelativePattern;
102103
import org.eclipse.lsp4j.TextDocumentIdentifier;
103104
import org.eclipse.lsp4j.WatchKind;
104105
import org.eclipse.lsp4j.jsonrpc.messages.Either;
@@ -107,7 +108,7 @@
107108
public class StandardProjectsManager extends ProjectsManager {
108109
private final static String FORMATTER_OPTION_PREFIX = JavaCore.PLUGIN_ID + ".formatter"; //$NON-NLS-1$
109110
protected static final String BUILD_SUPPORT_EXTENSION_POINT_ID = "buildSupport";
110-
private static final Set<String> watchers = new LinkedHashSet<>();
111+
private static final Set<Either<String, RelativePattern>> watchers = new LinkedHashSet<>();
111112
private PreferenceManager preferenceManager;
112113
private boolean buildFinished;
113114
private boolean shouldUpdateProjects;
@@ -123,12 +124,12 @@ public void setShouldUpdateProjects(boolean shouldUpdateProjects) {
123124
}
124125

125126
//@formatter:off
126-
private static final List<String> basicWatchers = Arrays.asList(
127-
"**/*.java",
128-
"**/.project",
129-
"**/.classpath",
130-
"**/.settings/*.prefs",
131-
"**/src/**"
127+
private static final List<Either<String, RelativePattern>> basicWatchers = Arrays.asList(
128+
Either.forLeft("**/*.java"),
129+
Either.forLeft("**/.project"),
130+
Either.forLeft("**/.classpath"),
131+
Either.forLeft("**/.settings/*.prefs"),
132+
Either.forLeft("**/src/**")
132133
);
133134
//@formatter:on
134135

@@ -465,8 +466,8 @@ public void registerWatchers(boolean runInJob) {
465466
public List<FileSystemWatcher> registerWatchers() {
466467
logInfo(">> registerWatchers'");
467468
if (preferenceManager.getClientPreferences().isWorkspaceChangeWatchedFilesDynamicRegistered()) {
468-
Set<String> patterns = new LinkedHashSet<>(basicWatchers);
469-
buildSupports().forEach(e -> e.getWatchPatterns().forEach(patterns::add));
469+
Set<Either<String, RelativePattern>> patterns = new LinkedHashSet<>(basicWatchers);
470+
buildSupports().forEach(e -> e.getWatchPatterns().forEach(p -> patterns.add(Either.forLeft(p))));
470471
Set<IPath> sources = new HashSet<>();
471472
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
472473
try {
@@ -519,9 +520,9 @@ public List<FileSystemWatcher> registerWatchers() {
519520
IPath projectFolder = ProjectUtils.getProjectRealFolder(project);
520521
Set<String> libraries = preferenceManager.getPreferences().getReferencedLibraries().getInclude();
521522
for (String pattern: libraries) {
522-
patterns.add(ProjectUtils.resolveGlobPath(projectFolder, pattern).toPortableString());
523+
patterns.add(Either.forLeft(ProjectUtils.resolveGlobPath(projectFolder, pattern).toPortableString()));
523524
}
524-
patterns.add("**/.settings");
525+
patterns.add(Either.forLeft("**/.settings"));
525526
}
526527
}
527528
}
@@ -550,14 +551,14 @@ public List<FileSystemWatcher> registerWatchers() {
550551
addWatcher(settings, sources);
551552
}
552553
patterns.addAll(sources.stream().map(p -> ResourceUtils.toGlobPattern(p, false)).collect(Collectors.toList()));
553-
for (String pattern : patterns) {
554-
FileSystemWatcher watcher = new FileSystemWatcher(Either.forLeft(pattern));
554+
for (Either<String, RelativePattern> pattern : patterns) {
555+
FileSystemWatcher watcher = new FileSystemWatcher(pattern);
555556
fileWatchers.add(watcher);
556557
}
557558
// Watch on project root folders.
558559
for (IProject project : projects) {
559560
if (ProjectUtils.isVisibleProject(project) && project.exists()) {
560-
FileSystemWatcher watcher = new FileSystemWatcher(Either.forLeft(ResourceUtils.toGlobPattern(project.getLocation(), false)), WatchKind.Delete);
561+
FileSystemWatcher watcher = new FileSystemWatcher(ResourceUtils.toGlobPattern(project.getLocation(), false), WatchKind.Delete);
561562
fileWatchers.add(watcher);
562563
}
563564
}

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/syntaxserver/SyntaxProjectsManager.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@
5454
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
5555
import org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions;
5656
import org.eclipse.lsp4j.FileSystemWatcher;
57+
import org.eclipse.lsp4j.RelativePattern;
5758
import org.eclipse.lsp4j.jsonrpc.messages.Either;
5859

5960
public class SyntaxProjectsManager extends ProjectsManager {
6061
//@formatter:off
61-
private static final List<String> basicWatchers = Arrays.asList(
62-
"**/*.java"
62+
private static final List<Either<String, RelativePattern>> basicWatchers = Arrays.asList(
63+
Either.forLeft("**/*.java")
6364
// "**/src/**"
6465
);
6566
//@formatter:on
6667

67-
private final Set<String> watchers = new LinkedHashSet<>();
68+
private final Set<Either<String, RelativePattern>> watchers = new LinkedHashSet<>();
6869

6970
private Job registerWatcherJob = new Job("Register Watchers") {
7071

@@ -137,14 +138,14 @@ public List<FileSystemWatcher> registerWatchers() {
137138
JavaLanguageServerPlugin.logException(e.getMessage(), e);
138139
}
139140
List<FileSystemWatcher> fileWatchers = new ArrayList<>();
140-
Set<String> patterns = new LinkedHashSet<>(basicWatchers);
141+
Set<Either<String, RelativePattern>> patterns = new LinkedHashSet<>(basicWatchers);
141142
patterns.addAll(Stream.of(sources).map(ResourceUtils::toGlobPattern).collect(Collectors.toList()));
142143

143-
for (String pattern : patterns) {
144-
FileSystemWatcher watcher = new FileSystemWatcher(Either.forLeft(pattern));
144+
for (Either<String, RelativePattern> pattern : patterns) {
145+
FileSystemWatcher watcher = new FileSystemWatcher(pattern);
145146
fileWatchers.add(watcher);
146147
}
147-
148+
148149
if (!patterns.equals(watchers)) {
149150
JavaLanguageServerPlugin.logInfo(">> registerFeature 'workspace/didChangeWatchedFiles'");
150151
DidChangeWatchedFilesRegistrationOptions didChangeWatchedFilesRegistrationOptions = new DidChangeWatchedFilesRegistrationOptions(fileWatchers);

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/ResourceUtilsTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@
2121
import org.eclipse.core.runtime.IPath;
2222
import org.eclipse.core.runtime.Path;
2323
import org.eclipse.core.runtime.Platform;
24+
import org.eclipse.lsp4j.RelativePattern;
25+
import org.eclipse.lsp4j.jsonrpc.messages.Either;
2426
import org.junit.Test;
2527

2628
public class ResourceUtilsTest {
2729

2830
@Test
2931
public void testToGlobPattern() {
3032
assertNull(toGlobPattern(null));
31-
assertEquals("/foo/bar/**", toGlobPattern(Path.forPosix("/foo/bar")));
32-
assertEquals("/foo/bar/**", toGlobPattern(Path.forPosix("/foo/bar/")));
33-
assertEquals("**/foo/bar/**", toGlobPattern(Path.forWindows("c:/foo/bar/")));
34-
assertEquals("**/foo/bar/**", toGlobPattern(Path.forWindows("c:\\foo\\bar")));
35-
assertEquals("/foo/bar/foo.jar", toGlobPattern(Path.forPosix("/foo/bar/foo.jar")));
33+
assertEquals(Either.forLeft("/foo/bar/**"), toGlobPattern(Path.forPosix("/foo/bar")));
34+
assertEquals(Either.forLeft("/foo/bar/**"), toGlobPattern(Path.forPosix("/foo/bar/")));
35+
assertEquals(Either.forLeft("**/foo/bar/**"), toGlobPattern(Path.forWindows("c:/foo/bar/")));
36+
assertEquals(Either.forLeft("**/foo/bar/**"), toGlobPattern(Path.forWindows("c:\\foo\\bar")));
37+
assertEquals(Either.forRight(new RelativePattern(Either.forRight("/foo/bar"), "foo.jar")), toGlobPattern(Path.forPosix("/foo/bar/foo.jar")));
3638
}
3739

3840
@Test

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,10 @@ public void testWatchers() throws Exception {
326326
// 8 basic + 3 project roots
327327
assertEquals("Unexpected watchers:\n" + toString(watchers), 12, watchers.size());
328328
List<FileSystemWatcher> projectWatchers = watchers.subList(9, 12);
329-
assertTrue(projectWatchers.get(0).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("/TestProject"));
329+
assertTrue(projectWatchers.get(0).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("TestProject"));
330330
assertTrue(WatchKind.Delete == projectWatchers.get(0).getKind());
331-
assertTrue(projectWatchers.get(1).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("/maven/salut"));
332-
assertTrue(projectWatchers.get(2).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("/gradle/simple-gradle"));
331+
assertTrue(projectWatchers.get(1).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("salut"));
332+
assertTrue(projectWatchers.get(2).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("simple-gradle"));
333333

334334
watchers = watchers.subList(0, 9);
335335
Collections.sort(watchers, Comparator.comparing(fileSystemWatcher -> fileSystemWatcher.getGlobPattern().map(Function.identity(), RelativePattern::getPattern)));
@@ -373,9 +373,9 @@ public void testWatchers() throws Exception {
373373
verify(client, times(1)).registerCapability(any());
374374
assertEquals("Unexpected watchers:\n" + toString(watchers), 12, newWatchers.size());
375375
projectWatchers = newWatchers.subList(9, 12);
376-
assertTrue(projectWatchers.get(0).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("/TestProject"));
377-
assertTrue(projectWatchers.get(1).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("/maven/salut"));
378-
assertTrue(projectWatchers.get(2).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("/gradle/simple-gradle"));
376+
assertTrue(projectWatchers.get(0).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("TestProject"));
377+
assertTrue(projectWatchers.get(1).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("salut"));
378+
assertTrue(projectWatchers.get(2).getGlobPattern().map(Function.identity(), RelativePattern::getPattern).endsWith("simple-gradle"));
379379

380380
newWatchers = watchers.subList(0, 9);
381381
Collections.sort(newWatchers, Comparator.comparing(fileSystemWatcher -> fileSystemWatcher.getGlobPattern().map(Function.identity(), RelativePattern::getPattern)));

0 commit comments

Comments
 (0)