forked from redhat-developer/lsp4ij
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes redhat-developer#376 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
69f8a0c
commit 5196092
Showing
8 changed files
with
221 additions
and
47 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...n/java/com/redhat/devtools/lsp4ij/features/documentation/LSPDocumentationLinkHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.redhat.devtools.lsp4ij.features.documentation; | ||
|
||
import com.intellij.openapi.application.ApplicationManager; | ||
import com.intellij.platform.backend.documentation.DocumentationLinkHandler; | ||
import com.intellij.platform.backend.documentation.DocumentationTarget; | ||
import com.intellij.platform.backend.documentation.LinkResolveResult; | ||
import com.redhat.devtools.lsp4ij.hint.LSPNavigationLinkHandler; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class LSPDocumentationLinkHandler implements DocumentationLinkHandler { | ||
|
||
@Override | ||
public @Nullable LinkResolveResult resolveLink(@NotNull DocumentationTarget target, @NotNull String url) { | ||
if (target instanceof LSPDocumentationTarget lspTarget) { | ||
if (url.endsWith(LSPNavigationLinkHandler.PREFIX_OR_SUFFIX)) { | ||
ApplicationManager.getApplication() | ||
.invokeLater(() -> { | ||
String fileUrl = url.substring(0, url.length() - LSPNavigationLinkHandler.PREFIX_OR_SUFFIX.length()); | ||
var file = lspTarget.getFile(); | ||
LSPNavigationLinkHandler.handleLink(fileUrl, file, file.getProject()); | ||
}); | ||
return LinkResolveResult.resolvedTarget(target); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...n/java/com/redhat/devtools/lsp4ij/features/documentation/markdown/CustomLinkRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.redhat.devtools.lsp4ij.features.documentation.markdown; | ||
|
||
import com.redhat.devtools.lsp4ij.hint.LSPNavigationLinkHandler; | ||
import com.vladsch.flexmark.ast.Link; | ||
import com.vladsch.flexmark.html.HtmlWriter; | ||
import com.vladsch.flexmark.html.renderer.*; | ||
import com.vladsch.flexmark.util.ast.Node; | ||
import com.vladsch.flexmark.util.data.DataHolder; | ||
import com.vladsch.flexmark.util.misc.CharPredicate; | ||
import com.vladsch.flexmark.util.sequence.BasedSequence; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
public class CustomLinkRenderer implements NodeRenderer { | ||
|
||
@Override | ||
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() { | ||
return Collections.singleton(new NodeRenderingHandler<>(Link.class, this::render)); | ||
} | ||
|
||
private void render(Link node, NodeRendererContext context, HtmlWriter html) { | ||
if (context.isDoNotRenderLinks() /*|| isSuppressedLinkPrefix(node.getUrl(), context)*/) { | ||
context.renderChildren(node); | ||
} else { | ||
ResolvedLink resolvedLink = context.resolveLink(LinkType.LINK, node.getUrl().unescape(), null, null); | ||
|
||
String url = getUrl(resolvedLink.getUrl()); | ||
html.attr("href", url); | ||
|
||
// we have a title part, use that | ||
if (node.getTitle().isNotNull()) { | ||
resolvedLink = resolvedLink.withTitle(node.getTitle().unescape()); | ||
} | ||
|
||
html.attr(resolvedLink.getNonNullAttributes()); | ||
html.srcPos(node.getChars()).withAttr(resolvedLink).tag("a"); | ||
renderChildrenSourceLineWrapped(node, node.getText(), context, html); | ||
html.tag("/a"); | ||
} | ||
} | ||
|
||
private String getUrl(String url) { | ||
if (isFileUrl(url)) { | ||
return url + LSPNavigationLinkHandler.PREFIX_OR_SUFFIX; | ||
} | ||
return url; | ||
} | ||
|
||
private static boolean isFileUrl(String url) { | ||
int index = url.indexOf("://"); | ||
return (index == -1 || url.substring(0, index).equals("file")); | ||
} | ||
|
||
private void renderChildrenSourceLineWrapped( | ||
Node node, | ||
BasedSequence nodeChildText, | ||
NodeRendererContext context, | ||
HtmlWriter html | ||
) { | ||
// if have SOFT BREAK or HARD BREAK as child then we open our own span | ||
if (context.getHtmlOptions().sourcePositionParagraphLines && nodeChildText.indexOfAny(CharPredicate.ANY_EOL) >= 0) { | ||
// if (myNextLine > 0) { | ||
// myNextLine--; | ||
// } | ||
|
||
// outputSourceLineSpan(node, node, node, html); | ||
context.renderChildren(node); | ||
html.tag("/span"); | ||
} else { | ||
context.renderChildren(node); | ||
} | ||
} | ||
|
||
public static class Factory implements NodeRendererFactory { | ||
@Override | ||
public NodeRenderer apply(DataHolder options) { | ||
return new CustomLinkRenderer(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters