Skip to content

Commit 6b2d12a

Browse files
authored
fix: decode file URIs in document hyperlink labels (#1422)
1 parent 56ff5fd commit 6b2d12a

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/documentLink/DocumentLinkTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ public void testDocumentLinkExternalFile() throws Exception {
7878
assertEquals("file://test0", hyperlinks[0].getHyperlinkText());
7979
}
8080

81+
@Test
82+
public void testDocumentLinkWithEncodedUri() throws Exception {
83+
final var links = new ArrayList<DocumentLink>();
84+
links.add(new DocumentLink(new Range(new Position(0, 9), new Position(0, 15)), "file:///tmp/fi%C3%A9le.ts"));
85+
MockLanguageServer.INSTANCE.setDocumentLinks(links);
86+
87+
IFile file = TestUtils.createUniqueTestFile(project, "not_link <link>");
88+
ITextViewer viewer = TestUtils.openTextViewer(file);
89+
90+
IHyperlink[] hyperlinks = documentLinkDetector.detectHyperlinks(viewer, new Region(13, 0), true);
91+
assertEquals(1, hyperlinks.length);
92+
assertEquals("/tmp/fiéle.ts", hyperlinks[0].getHyperlinkText());
93+
}
94+
8195
@Test
8296
public void testDocumentLinkWrongRegion() throws Exception {
8397
final var links = new ArrayList<DocumentLink>();

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/documentLink/DocumentLinkDetector.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public class DocumentLinkDetector extends AbstractHyperlinkDetector {
3939
public static class DocumentHyperlink implements IHyperlink {
4040

4141
private final String uri;
42+
private final String label;
4243
private final IRegion highlightRegion;
4344

4445
public DocumentHyperlink(String uri, IRegion highlightRegion) {
4546
this.uri = uri;
47+
this.label = toLabel(uri);
4648
this.highlightRegion = highlightRegion;
4749
}
4850

@@ -53,19 +55,35 @@ public IRegion getHyperlinkRegion() {
5355

5456
@Override
5557
public String getTypeLabel() {
56-
return uri;
58+
return label;
5759
}
5860

5961
@Override
6062
public String getHyperlinkText() {
61-
return uri;
63+
return label;
6264
}
6365

6466
@Override
6567
public void open() {
6668
LSPEclipseUtils.open(uri, null, true);
6769
}
6870

71+
private static String toLabel(final String uri) {
72+
if (uri.isEmpty())
73+
return uri;
74+
75+
if (uri.startsWith(LSPEclipseUtils.FILE_URI)) {
76+
try {
77+
final URI fileUri = URI.create(uri);
78+
final String path = fileUri.getPath();
79+
if (path != null && !path.isEmpty())
80+
return path;
81+
} catch (final IllegalArgumentException ex) {
82+
LanguageServerPlugin.logError(ex);
83+
}
84+
}
85+
return uri;
86+
}
6987
}
7088

7189
@Override

0 commit comments

Comments
 (0)