|
9 | 9 | (:import
|
10 | 10 | [com.intellij.openapi.util.text StringUtil]
|
11 | 11 | [com.intellij.openapi.util.text HtmlBuilder]
|
12 |
| - [com.intellij.psi PsiDocumentManager PsiElement PsiFile])) |
| 12 | + [com.intellij.psi PsiDocumentManager PsiElement PsiFile] |
| 13 | + [com.intellij.openapi.fileTypes SyntaxHighlighterFactory] |
| 14 | + [com.intellij.openapi.editor.colors EditorColorsManager] |
| 15 | + [com.github.clojure_lsp.intellij ClojureLanguage] |
| 16 | + [java.awt Color])) |
13 | 17 |
|
14 | 18 | (set! *warn-on-reflection* true)
|
15 | 19 |
|
| 20 | +(defn ^:private rgb-html-style [^Color color] |
| 21 | + (str "rgb(" (.getRed color) ", " (.getGreen color) ", " (.getBlue color) ")")) |
| 22 | + |
| 23 | +(defn ^:private html-style [prop value] |
| 24 | + (if (some? value) |
| 25 | + (str prop ":" value ";") |
| 26 | + "")) |
| 27 | + |
| 28 | +(defn ^:private highlight-html-text |
| 29 | + [^String text {:keys [foreground-color background-color font-type]}] |
| 30 | + (let [color-style (html-style "color" (when foreground-color |
| 31 | + (rgb-html-style foreground-color))) |
| 32 | + background-style (html-style "background-color" (when background-color |
| 33 | + (rgb-html-style foreground-color))) |
| 34 | + font-weight-style (html-style "font-weight" (case font-type |
| 35 | + :bold "bold" |
| 36 | + :bold-italic "bold" |
| 37 | + "normal")) |
| 38 | + font-style (html-style "font-style" (case font-type |
| 39 | + :italic "italic" |
| 40 | + :bold-italic "italic" |
| 41 | + "normal"))] |
| 42 | + (str "<span style=\"" color-style background-style font-weight-style font-style "\">" text "</span>"))) |
| 43 | + |
| 44 | +(defn ^:private highlight-html-code [^String code] |
| 45 | + (let [highlighter (SyntaxHighlighterFactory/getSyntaxHighlighter (ClojureLanguage/INSTANCE) nil nil) |
| 46 | + lexer (.getHighlightingLexer highlighter) |
| 47 | + color-scheme (.getGlobalScheme (EditorColorsManager/getInstance))] |
| 48 | + (loop [highlighted-code ""] |
| 49 | + (if (empty? highlighted-code) |
| 50 | + (.start lexer code) |
| 51 | + (.advance lexer)) |
| 52 | + (let [lexer-not-finished? (.getTokenType lexer)] |
| 53 | + (if lexer-not-finished? |
| 54 | + (let [text (.getTokenText lexer) |
| 55 | + highlight (first (.getTokenHighlights highlighter (.getTokenType lexer))) |
| 56 | + highlight-attrs (.getAttributes color-scheme highlight) |
| 57 | + foreground-color (some-> highlight-attrs .getForegroundColor) |
| 58 | + background-color (some-> highlight-attrs .getBackgroundColor) |
| 59 | + font-type (some-> highlight-attrs .getFontType (#(case (int %) |
| 60 | + 1 :bold |
| 61 | + 2 :italic |
| 62 | + 3 :bold-italic |
| 63 | + nil)))] |
| 64 | + (if (some some? [foreground-color background-color font-type]) |
| 65 | + (recur (str highlighted-code (highlight-html-text text {:foreground-color foreground-color |
| 66 | + :background-color background-color |
| 67 | + :font-type font-type}))) |
| 68 | + (recur (str highlighted-code text)))) |
| 69 | + highlighted-code))))) |
| 70 | + |
16 | 71 | (defn ^:private build-doc [^PsiElement element]
|
17 | 72 | (when-let [client (db/get-in (.getProject element) [:client])]
|
18 | 73 | (when-let [psi-file ^PsiFile (.getContainingFile element)]
|
|
25 | 80 | :position {:line (.line line-col)
|
26 | 81 | :character (.column line-col)}}])]
|
27 | 82 |
|
28 |
| - (when-let [raw-html (markdown/md-to-html-string (:value contents))] |
| 83 | + (when-let [html (markdown/md-to-html-string (:value contents) |
| 84 | + :codeblock-no-escape? true |
| 85 | + :codeblock-callback (fn [code language] |
| 86 | + (if (= language "clojure") |
| 87 | + (highlight-html-code code) |
| 88 | + code)))] |
29 | 89 | (-> (HtmlBuilder.)
|
30 |
| - (.appendRaw raw-html) |
| 90 | + (.appendRaw html) |
31 | 91 | (.toString)))))))
|
32 | 92 |
|
33 | 93 | (defn -getCustomDocumentationElement
|
|
0 commit comments