Skip to content

Commit e64901a

Browse files
committed
Fix: Error when using paths with non-ASCII characters for file watching on Unix/MacOS
1 parent 4072712 commit e64901a

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,14 @@ public void testToUri_WindowsUNC() {
342342
// Ensure there is an authority and no malformed quadruple slashes
343343
assertFalse(uriWithSpaces.toString().startsWith("file:////"));
344344
}
345+
346+
@Test
347+
void testFileURIWithNonAsciiPath() throws Exception {
348+
// File name contains a German Eszett and a Japanese Kana
349+
String fileName = "foo ßア";
350+
IFile targetFile = project.getFile(fileName);
351+
assertEquals(fileName, Paths.get(LSPEclipseUtils.toUri(targetFile)).getFileName().toString());
352+
}
345353

346354
@Test
347355
public void testToWorkspaceFolder() {

org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,15 +1353,20 @@ public static URI toUri(File file) {
13531353
final int slash = path.indexOf('/', 2);
13541354
final String authority = slash > 2 ? path.substring(2, slash) : path.substring(2);
13551355
final String absPath = slash > 2 ? path.substring(slash) : "/"; //$NON-NLS-1$
1356-
return new URI(FILE_SCHEME, authority, absPath, null);
1356+
return ensureAscii(new URI(FILE_SCHEME, authority, absPath, null));
13571357
}
1358-
return new URI(FILE_SCHEME, "", path, null); //$NON-NLS-1$
1358+
return ensureAscii(new URI(FILE_SCHEME, "", path, null)); //$NON-NLS-1$
13591359
} catch (URISyntaxException e) {
13601360
LanguageServerPlugin.logError(e);
13611361
return file.getAbsoluteFile().toURI();
13621362
}
13631363
}
13641364

1365+
private static URI ensureAscii(URI uri) {
1366+
// Perform one round-trip to make all non-ASCII characters are properly encoded.
1367+
return URI.create(uri.toASCIIString());
1368+
}
1369+
13651370
public static @Nullable IFile getFile(@Nullable IDocument document) {
13661371
IPath path = toPath(document);
13671372
return getFile(path);

0 commit comments

Comments
 (0)