Skip to content

Commit b21faac

Browse files
Merge pull request #357 from ThemiraChathumina/hoverRendering
Improve the hover content rendering
2 parents 5bb26de + 8991756 commit b21faac

File tree

1 file changed

+63
-9
lines changed

1 file changed

+63
-9
lines changed

src/main/java/org/wso2/lsp4intellij/utils/GUIUtils.java

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@
3838
import org.wso2.lsp4intellij.extensions.LSPExtensionManager;
3939
import org.wso2.lsp4intellij.contributors.label.LSPLabelProvider;
4040

41-
import javax.swing.*;
41+
import javax.swing.JTextPane;
4242
import javax.swing.event.HyperlinkEvent;
43+
import javax.swing.text.StyleConstants;
44+
import javax.swing.text.html.HTMLDocument;
4345
import javax.swing.text.html.HTMLEditorKit;
44-
import java.awt.*;
46+
import javax.swing.text.html.StyleSheet;
47+
48+
import java.awt.Color;
49+
import java.awt.Point;
4550
import java.net.URISyntaxException;
4651
import java.util.Objects;
4752
import java.util.Optional;
@@ -67,6 +72,56 @@ public static Hint createAndShowEditorHint(Editor editor, String string, Point p
6772
return createAndShowEditorHint(editor, string, point, HintManager.ABOVE, flags);
6873
}
6974

75+
private static StyleSheet getStyleSheet(JTextPane textPane) {
76+
HTMLDocument doc = (HTMLDocument) textPane.getDocument();
77+
return doc.getStyleSheet();
78+
}
79+
80+
private static void configureTextStyles(JTextPane textPane) {
81+
String fontFamily = "Arial";
82+
int fontSize = 10;
83+
84+
StyleSheet styleSheet = getStyleSheet(textPane);
85+
86+
styleSheet.addRule("p { font-family: Segoe UI Semibold; font-size: " + fontSize + "px; }");
87+
styleSheet.addRule("li { font-family: Segoe UI Semibold; font-size: " + fontSize + "px; }");
88+
styleSheet.addRule(
89+
"code { font-family: " + fontFamily + "; font-weight: bold; font-size: " + fontSize + "px; }");
90+
91+
for (int i = 1; i <= 6; i++) {
92+
styleSheet.addRule("h" + i + " { font-family: " + fontFamily + "; font-weight: bold; }");
93+
}
94+
}
95+
96+
private static void adjustWidth(JTextPane textPane) {
97+
int width = textPane.getPreferredSize().width;
98+
if (width > 600) {
99+
getStyleSheet(textPane).addRule("p { width: 600px; }");
100+
}
101+
}
102+
103+
private static void addPaddingToCodeBlocks(JTextPane textPane, String text) {
104+
text = text.replace("<code>", "<code>&nbsp;")
105+
.replaceAll("(?<!\\n)</code>", "&nbsp;</code>");
106+
107+
textPane.setText(text);
108+
}
109+
110+
private static void setCodeBlockBackgroundColor(JTextPane textPane) {
111+
StyleSheet styleSheet = getStyleSheet(textPane);
112+
113+
Color bodyFontColor = styleSheet.getStyle("body").getAttribute(StyleConstants.Foreground) instanceof Color
114+
? (Color) styleSheet.getStyle("body").getAttribute(StyleConstants.Foreground) : Color.BLACK;
115+
116+
Color inverseColor = new Color(255 - bodyFontColor.getRed(),
117+
255 - bodyFontColor.getGreen(),
118+
255 - bodyFontColor.getBlue()).darker();
119+
120+
String hexColor = String.format("#%02x%02x%02x", inverseColor.getRed(), inverseColor.getGreen(), inverseColor.getBlue());
121+
122+
styleSheet.addRule("code { background-color: " + hexColor + "; }");
123+
}
124+
70125
/**
71126
* Shows a hint in the editor
72127
*
@@ -80,12 +135,12 @@ public static Hint createAndShowEditorHint(Editor editor, String string, Point p
80135
public static Hint createAndShowEditorHint(Editor editor, String string, Point point, short constraint, int flags) {
81136
JTextPane textPane = new JTextPane();
82137
textPane.setEditorKit(new HTMLEditorKit());
83-
textPane.setText(string);
84-
int width = textPane.getPreferredSize().width;
85-
if (width > 600) {
86-
// max-width does not seem to be supported, so use this rather ugly hack...
87-
textPane.setText(string.replace("<style>", "<style>p {width: 600px}\n"));
88-
}
138+
139+
addPaddingToCodeBlocks(textPane, string);
140+
configureTextStyles(textPane);
141+
setCodeBlockBackgroundColor(textPane);
142+
adjustWidth(textPane);
143+
89144
textPane.setEditable(false);
90145
textPane.addHyperlinkListener(e -> {
91146
if ((e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
@@ -138,5 +193,4 @@ public static LSPLabelProvider getLabelProviderFor(LanguageServerDefinition serv
138193
return IntellijLanguageClient.getExtensionManagerForDefinition(serverDefinition)
139194
.map(LSPExtensionManager::getLabelProvider).orElse(DEFAULT_LABEL_PROVIDER);
140195
}
141-
142196
}

0 commit comments

Comments
 (0)