Skip to content

Commit b2f851e

Browse files
committed
fix: Avoid Windows path parsing issue
- Add test case for reproducing a windows path parsing issue - Move helper method to utility class - Fix path parsing: Path.of(uri.getPath()) => Path.of(uri) - Bump test plug-in version
1 parent bbcafd7 commit b2f851e

5 files changed

Lines changed: 31 additions & 10 deletions

File tree

org.eclipse.lsp4e.test/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: Tests for language server bundle (Incubation)
44
Bundle-SymbolicName: org.eclipse.lsp4e.test;singleton:=true
5-
Bundle-Version: 0.16.10.qualifier
5+
Bundle-Version: 0.16.11.qualifier
66
Fragment-Host: org.eclipse.lsp4e
77
Bundle-Vendor: Eclipse LSP4E
88
Bundle-RequiredExecutionEnvironment: JavaSE-21

org.eclipse.lsp4e.test/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</parent>
99
<artifactId>org.eclipse.lsp4e.test</artifactId>
1010
<packaging>eclipse-test-plugin</packaging>
11-
<version>0.16.10-SNAPSHOT</version>
11+
<version>0.16.11-SNAPSHOT</version>
1212

1313
<properties>
1414
<os-jvm-flags /> <!-- for the default case -->

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.lsp4e.test.edit;
1515

16+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1617
import static org.junit.jupiter.api.Assertions.assertEquals;
1718
import static org.junit.jupiter.api.Assertions.assertFalse;
1819
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -81,6 +82,7 @@
8182
import org.junit.jupiter.api.io.TempDir;
8283
import org.junit.jupiter.params.ParameterizedTest;
8384
import org.junit.jupiter.params.provider.Arguments;
85+
import org.junit.jupiter.params.provider.CsvSource;
8486
import org.junit.jupiter.params.provider.MethodSource;
8587

8688
public class LSPEclipseUtilsTest extends AbstractTestWithProject {
@@ -637,5 +639,18 @@ public static Stream<Arguments> getHtmlDocString() {
637639
void getHtmlDocString(Either<@Nullable String, MarkupContent> arg, String expected) throws Exception {
638640
assertEquals(expected, LSPEclipseUtils.getHtmlDocString(arg));
639641
}
642+
643+
@ParameterizedTest
644+
@CsvSource({
645+
"file:///C:/Users/username/path/to/SomeDir/SomeType.hpp, SomeType.hpp",
646+
"file:///home/username/path/to/SomeDir/SomeType.hpp, SomeType.hpp",
647+
"file:///,"
648+
})
649+
void testReadingFileNameFromUri(String uriText, String expectedFileName) {
650+
URI uri = URI.create(uriText);
651+
652+
String actualFileName = assertDoesNotThrow(() -> LSPEclipseUtils.getFileName(uri));
653+
assertEquals(expectedFileName, actualFileName);
654+
}
640655

641656
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,15 @@ public static CallHierarchyPrepareParams toCallHierarchyPrepareParams(int offset
431431
return toPath(toBuffer(document));
432432
}
433433

434+
public static @Nullable String getFileName(URI uri) {
435+
try {
436+
return java.nio.file.Path.of(uri).getFileName().toString();
437+
} catch (Exception e) {
438+
LanguageServerPlugin.logWarning("Failed to parse file name from URI " + uri, e); //$NON-NLS-1$
439+
}
440+
return null;
441+
}
442+
434443
public static int toEclipseMarkerSeverity(@Nullable DiagnosticSeverity lspSeverity) {
435444
if (lspSeverity == null) {
436445
// if severity is empty it is up to the client to interpret diagnostics

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/symbols/internal/SymbolIconProviderRegistry.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
package org.eclipse.lsp4e.operations.symbols.internal;
1313

1414
import java.net.URI;
15-
import java.nio.file.Path;
1615
import java.util.HashMap;
1716
import java.util.Map;
1817

@@ -22,6 +21,7 @@
2221
import org.eclipse.core.runtime.Platform;
2322
import org.eclipse.core.runtime.content.IContentType;
2423
import org.eclipse.jdt.annotation.Nullable;
24+
import org.eclipse.lsp4e.LSPEclipseUtils;
2525
import org.eclipse.lsp4e.LanguageServerPlugin;
2626
import org.eclipse.lsp4e.outline.SymbolsModel.DocumentSymbolWithURI;
2727
import org.eclipse.lsp4e.ui.SymbolIconProvider;
@@ -106,11 +106,8 @@ private SymbolIconProvider getIconProvider(Object symbol) {
106106
return defaultIconProvider;
107107
}
108108

109-
String fileName = null;
110-
try {
111-
fileName = Path.of(uri.getPath()).getFileName().toString();
112-
} catch (Exception e) {
113-
LanguageServerPlugin.logWarning("Failed to parse file name from URI " + uri, e); //$NON-NLS-1$
109+
String fileName = LSPEclipseUtils.getFileName(uri);
110+
if (fileName == null) {
114111
return defaultIconProvider;
115112
}
116113

@@ -146,8 +143,8 @@ private SymbolIconProvider getIconProvider(Object symbol) {
146143
}
147144

148145
try {
149-
return URI.create(uri);
150-
} catch (IllegalArgumentException e) {
146+
return LSPEclipseUtils.toUri(uri);
147+
} catch (Exception e) {
151148
LanguageServerPlugin.logWarning("Failed to parse URI " + uri, e); //$NON-NLS-1$
152149
return null;
153150
}

0 commit comments

Comments
 (0)