Skip to content

Commit

Permalink
[feat] support file link in hover
Browse files Browse the repository at this point in the history
Fixes redhat-developer#376

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jun 20, 2024
1 parent 69f8a0c commit 1887ade
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.intellij.openapi.ui.popup.JBPopupListener;
import com.intellij.openapi.ui.popup.LightweightWindowEvent;
import com.redhat.devtools.lsp4ij.console.LSPConsoleToolWindowPanel;
import com.redhat.devtools.lsp4ij.features.documentation.MarkdownConverter;
import com.redhat.devtools.lsp4ij.features.documentation.markdown.MarkdownConverter;
import com.redhat.devtools.lsp4ij.internal.StringUtils;
import org.eclipse.lsp4j.*;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.redhat.devtools.lsp4ij.*;
import com.redhat.devtools.lsp4ij.features.documentation.MarkdownConverter;
import com.redhat.devtools.lsp4ij.features.documentation.markdown.MarkdownConverter;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.services.LanguageServer;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.redhat.devtools.lsp4ij.features.documentation.markdown.MarkdownConverter;
import com.redhat.devtools.lsp4ij.internal.StringUtils;
import org.eclipse.lsp4j.*;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.redhat.devtools.lsp4ij.features.documentation.markdown;

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);

html.attr("href", resolvedLink.getUrl());

// 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 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.lsp4ij.features.documentation;
package com.redhat.devtools.lsp4ij.features.documentation.markdown;

import com.intellij.lang.Language;
import com.intellij.openapi.project.Project;
Expand Down Expand Up @@ -103,16 +103,15 @@ public NodeRenderer apply(DataHolder options) {
* @param fileName the file name which must be used to retrieve TextMate (if non-null) for MarkDown code block which defines the language or indented blockquote.
* @return the given <code>markdown</code> to Html.
*/
public @NotNull String toHtml(@NotNull String markdown, @Nullable Language language, @Nullable String fileName) {
public @NotNull String toHtml(@NotNull String markdown,
@Nullable Language language,
@Nullable String fileName) {
var htmlRenderer = this.htmlRenderer;
if (language != null || fileName != null) {
htmlRenderer = HtmlRenderer.builder(options)
.nodeRendererFactory(new NodeRendererFactory() {
@Override
public NodeRenderer apply(DataHolder options) {
return new SyntaxColorationCodeBlockRenderer(project, language, fileName);
}
}).build();
.nodeRendererFactory(new CustomLinkRenderer.Factory())
.nodeRendererFactory(new SyntaxColorationCodeBlockRenderer.Factory(project, language, fileName))
.build();
}
return htmlRenderer.render(htmlParser.parse(markdown));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.lsp4ij.features.documentation;
package com.redhat.devtools.lsp4ij.features.documentation.markdown;

import com.intellij.lang.Language;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
Expand All @@ -17,15 +17,18 @@
import com.intellij.openapi.editor.richcopy.SyntaxInfoBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.psi.TokenType;
import com.redhat.devtools.lsp4ij.features.documentation.LightQuickDocHighlightingHelper;
import com.redhat.devtools.lsp4ij.internal.SimpleLanguageUtils;
import com.redhat.devtools.lsp4ij.internal.StringUtils;
import com.vladsch.flexmark.ast.FencedCodeBlock;
import com.vladsch.flexmark.ast.IndentedCodeBlock;
import com.vladsch.flexmark.html.HtmlWriter;
import com.vladsch.flexmark.html.renderer.NodeRenderer;
import com.vladsch.flexmark.html.renderer.NodeRendererContext;
import com.vladsch.flexmark.html.renderer.NodeRendererFactory;
import com.vladsch.flexmark.html.renderer.NodeRenderingHandler;
import com.vladsch.flexmark.util.ast.ContentNode;
import com.vladsch.flexmark.util.data.DataHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -208,5 +211,22 @@ private static boolean hasTextMateSupport() {
}
}

public static class Factory implements NodeRendererFactory {


private final Project project;
private final Language fileLanguage;
private final String fileName;

public Factory(Project project, Language fileLanguage, String fileName) {
this.project = project;
this.fileLanguage = fileLanguage;
this.fileName = fileName;
}
@Override
public NodeRenderer apply(DataHolder options) {
return new SyntaxColorationCodeBlockRenderer(project, fileLanguage, fileName);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.lsp4ij.features.documentation;
package com.redhat.devtools.lsp4ij.features.documentation.markdown;

import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
Expand All @@ -18,6 +18,7 @@
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.util.registry.Registry;
import com.redhat.devtools.lsp4ij.LanguageServersRegistry;
import com.redhat.devtools.lsp4ij.features.documentation.markdown.SyntaxColorationCodeBlockRenderer;
import com.redhat.devtools.lsp4ij.internal.StringUtils;
import com.vladsch.flexmark.html.HtmlWriter;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.intellij.profile.codeInspection.ui.DescriptionEditorPaneKt;
import com.intellij.ui.ScrollPaneFactory;
import com.redhat.devtools.lsp4ij.LanguageServerBundle;
import com.redhat.devtools.lsp4ij.features.documentation.MarkdownConverter;
import com.redhat.devtools.lsp4ij.features.documentation.markdown.MarkdownConverter;
import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
<projectService
serviceImplementation="com.redhat.devtools.lsp4ij.internal.telemetry.TelemetryManager"/>
<projectService
serviceImplementation="com.redhat.devtools.lsp4ij.features.documentation.MarkdownConverter"/>
serviceImplementation="com.redhat.devtools.lsp4ij.features.documentation.markdown.MarkdownConverter"/>

<!-- LSP textDocument/publishDiagnostics notification support -->
<externalAnnotator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.intellij.lang.Language;
import com.intellij.lang.xml.XMLLanguage;
import com.redhat.devtools.lsp4ij.features.documentation.markdown.MarkdownConverter;
import com.redhat.devtools.lsp4ij.fixtures.LSPCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down

0 comments on commit 1887ade

Please sign in to comment.