Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,20 @@ public void testURICreationUnix() {
Assert.assertEquals("file:///test%20with%20space", LSPEclipseUtils.toUri(new File("/test with space")).toString());
}

@Test
public void testToUri_WindowsDriveLetter() {
Assume.assumeTrue(Platform.OS_WIN32.equals(Platform.getOS()));
// Use a synthetic drive path (doesn't need to exist)
File drivePath = new File("C:\\Temp\\with space");
URI uri = LSPEclipseUtils.toUri(drivePath);
// Should be file:///C:/... and percent-encode spaces
Assert.assertTrue(uri.toString().startsWith("file:///C:/Temp/"));
Assert.assertFalse(uri.toString().contains(" "));
Assert.assertTrue(uri.toString().contains("with%20space"));
// Should not contain quadruple slashes
Assert.assertFalse(uri.toString().startsWith("file:////"));
}

@Test
public void testUNCwindowsURI() {
Assume.assumeTrue(Platform.OS_WIN32.equals(Platform.getOS()));
Expand All @@ -308,6 +322,22 @@ public void testUNCwindowsURI() {
Assert.assertEquals(file1, file2);
}

@Test
public void testToUri_WindowsUNC() {
Assume.assumeTrue(Platform.OS_WIN32.equals(Platform.getOS()));
File unc = new File("\\\\localhost\\c$\\Windows");
URI uri = LSPEclipseUtils.toUri(unc);
System.err.println(uri.toString());
Assert.assertTrue(uri.toString().startsWith("file://localhost/c$/Windows"));

File uncWithSpaces = new File("\\\\server-name\\shared folder\\dir with space");
URI uriWithSpaces = LSPEclipseUtils.toUri(uncWithSpaces);
Assert.assertTrue(uriWithSpaces.toString().startsWith("file://server-name/shared%20folder/dir%20with%20space"));

// Ensure there is an authority and no malformed quadruple slashes
Assert.assertFalse(uriWithSpaces.toString().startsWith("file:////"));
}

@Test
public void testToWorkspaceFolder() {
WorkspaceFolder folder = LSPEclipseUtils.toWorkspaceFolder(project);
Expand Down
10 changes: 9 additions & 1 deletion org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,15 @@ public static URI toUri(IPath absolutePath) {
public static URI toUri(File file) {
// URI scheme specified by language server protocol and LSP
try {
return new URI("file", "", file.getAbsoluteFile().toURI().getPath(), null); //$NON-NLS-1$ //$NON-NLS-2$
final var path = file.getAbsoluteFile().toURI().getPath();
if (path.startsWith("//")) { // UNC path like //localhost/c$/Windows/ //$NON-NLS-1$
// split: authority = "localhost", absPath = "/c$/Windows/"
final int slash = path.indexOf('/', 2);
final String authority = slash > 2 ? path.substring(2, slash) : path.substring(2);
final String absPath = slash > 2 ? path.substring(slash) : "/"; //$NON-NLS-1$
return new URI(FILE_SCHEME, authority, absPath, null);
}
return new URI(FILE_SCHEME, "", path, null); //$NON-NLS-1$
} catch (URISyntaxException e) {
LanguageServerPlugin.logError(e);
return file.getAbsoluteFile().toURI();
Expand Down