38
38
import org .wso2 .lsp4intellij .extensions .LSPExtensionManager ;
39
39
import org .wso2 .lsp4intellij .contributors .label .LSPLabelProvider ;
40
40
41
- import javax .swing .* ;
41
+ import javax .swing .JTextPane ;
42
42
import javax .swing .event .HyperlinkEvent ;
43
+ import javax .swing .text .StyleConstants ;
44
+ import javax .swing .text .html .HTMLDocument ;
43
45
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 ;
45
50
import java .net .URISyntaxException ;
46
51
import java .util .Objects ;
47
52
import java .util .Optional ;
@@ -67,6 +72,56 @@ public static Hint createAndShowEditorHint(Editor editor, String string, Point p
67
72
return createAndShowEditorHint (editor , string , point , HintManager .ABOVE , flags );
68
73
}
69
74
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> " )
105
+ .replaceAll ("(?<!\\ n)</code>" , " </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
+
70
125
/**
71
126
* Shows a hint in the editor
72
127
*
@@ -80,12 +135,12 @@ public static Hint createAndShowEditorHint(Editor editor, String string, Point p
80
135
public static Hint createAndShowEditorHint (Editor editor , String string , Point point , short constraint , int flags ) {
81
136
JTextPane textPane = new JTextPane ();
82
137
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
+
89
144
textPane .setEditable (false );
90
145
textPane .addHyperlinkListener (e -> {
91
146
if ((e .getEventType () == HyperlinkEvent .EventType .ACTIVATED )
@@ -138,5 +193,4 @@ public static LSPLabelProvider getLabelProviderFor(LanguageServerDefinition serv
138
193
return IntellijLanguageClient .getExtensionManagerForDefinition (serverDefinition )
139
194
.map (LSPExtensionManager ::getLabelProvider ).orElse (DEFAULT_LABEL_PROVIDER );
140
195
}
141
-
142
196
}
0 commit comments