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 @@ -78,6 +78,20 @@ public void testDocumentLinkExternalFile() throws Exception {
assertEquals("file://test0", hyperlinks[0].getHyperlinkText());
}

@Test
public void testDocumentLinkWithEncodedUri() throws Exception {
final var links = new ArrayList<DocumentLink>();
links.add(new DocumentLink(new Range(new Position(0, 9), new Position(0, 15)), "file:///tmp/fi%C3%A9le.ts"));
MockLanguageServer.INSTANCE.setDocumentLinks(links);

IFile file = TestUtils.createUniqueTestFile(project, "not_link <link>");
ITextViewer viewer = TestUtils.openTextViewer(file);

IHyperlink[] hyperlinks = documentLinkDetector.detectHyperlinks(viewer, new Region(13, 0), true);
assertEquals(1, hyperlinks.length);
assertEquals("/tmp/fiéle.ts", hyperlinks[0].getHyperlinkText());
}

@Test
public void testDocumentLinkWrongRegion() throws Exception {
final var links = new ArrayList<DocumentLink>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ public class DocumentLinkDetector extends AbstractHyperlinkDetector {
public static class DocumentHyperlink implements IHyperlink {

private final String uri;
private final String label;
private final IRegion highlightRegion;

public DocumentHyperlink(String uri, IRegion highlightRegion) {
this.uri = uri;
this.label = toLabel(uri);
this.highlightRegion = highlightRegion;
}

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

@Override
public String getTypeLabel() {
return uri;
return label;
}

@Override
public String getHyperlinkText() {
return uri;
return label;
}

@Override
public void open() {
LSPEclipseUtils.open(uri, null, true);
}

private static String toLabel(final String uri) {
if (uri.isEmpty())
return uri;

if (uri.startsWith(LSPEclipseUtils.FILE_URI)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about decoding other URIs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decode them to what? Here we turn file URLs in readable file paths.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant something like #1426.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that still a valid URL? here we convert from valid but barely readable URL to readable and valid local file path.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by valid? It is the same one, just decoded. Do you mean by valid that you can copy it and pasten into a "browser", then I think not, but the same would be valid for the path. Since this is meant to be shown to the user, not as an input for a browser, I think this should be better that showing the encoded URL, shouldn't it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we should only decode if the decoded form is directly usable by the end user which in case of local files seems obvious. But I don't have a strong preference if we should or should not do the same for other types of URLs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we should only decode if the decoded form is directly usable by the end user which in case of local files seems obvious. But I don't have a strong preference if we should or should not do the same for other types of URLs.

try {
final URI fileUri = URI.create(uri);
final String path = fileUri.getPath();
if (path != null && !path.isEmpty())
return path;
} catch (final IllegalArgumentException ex) {
LanguageServerPlugin.logError(ex);
}
}
return uri;
}
}

@Override
Expand Down