Skip to content

Commit 4b53a04

Browse files
committed
feat(lsp): syntax-highlight code in the code-hints documentation popup
Run completion documentation through highlight.js (same path as the hover popup) so fenced code blocks render as colored code instead of flat monospace. Extract the theme-aware hljs token colours into a shared .lsp-hover-quickview/.lsp-hint-doc-popup rule used by both popups.
1 parent 960ddda commit 4b53a04

2 files changed

Lines changed: 52 additions & 23 deletions

File tree

src/languageTools/DefaultProviders.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ define(function (require, exports, module) {
5757
}
5858
}
5959

60+
// Syntax-highlight fenced code blocks (the signature/examples in completion docs) with the
61+
// globally available highlight.js, so they read like code instead of flat monospace. Theme-aware
62+
// token colours live in src/styles/brackets.less (.lsp-hint-doc-popup, shared with the hover).
63+
function _highlightCode(html) {
64+
var hljs = (typeof Phoenix !== "undefined") && Phoenix.libs && Phoenix.libs.hljs;
65+
if (!hljs) {
66+
return html;
67+
}
68+
var $wrap = $("<div>").html(html);
69+
$wrap.find("pre > code").each(function () {
70+
var $code = $(this);
71+
var match = ($code.attr("class") || "").match(/language-([\w-]+)/);
72+
var lang = match && match[1];
73+
if (lang && hljs.getLanguage(lang)) {
74+
try {
75+
$code.html(hljs.highlight($code.text(), { language: lang }).value).addClass("hljs");
76+
} catch (e) {
77+
// leave the block unhighlighted on any hljs error
78+
}
79+
}
80+
});
81+
return $wrap.html();
82+
}
83+
6084
function _docToHtml(documentation) {
6185
if (!documentation) {
6286
return "";
@@ -66,7 +90,7 @@ define(function (require, exports, module) {
6690
return "";
6791
}
6892
try {
69-
return marked.parse(md);
93+
return _highlightCode(marked.parse(md));
7094
} catch (e) {
7195
return _.escape(md);
7296
}

src/styles/brackets.less

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2269,28 +2269,8 @@ a, img {
22692269
}
22702270
}
22712271

2272-
// highlight.js token colours for the signature block. The app's global hljs theme is
2273-
// github-dark (locked, for the always-dark sidebar); override it within the hover so the
2274-
// signature reads well on the light theme too. The chip background follows the editor theme,
2275-
// so we use a GitHub-light palette in light mode and a GitHub-dark palette in dark mode.
2276-
pre code.hljs { background: none; }
2277-
.hljs-keyword, .hljs-literal, .hljs-doctag, .hljs-meta .hljs-keyword { color: #cf222e; }
2278-
.hljs-string, .hljs-regexp, .hljs-meta-string { color: #0a3069; }
2279-
.hljs-title, .hljs-title.function_, .hljs-title.class_, .hljs-section { color: #6639ba; }
2280-
.hljs-built_in, .hljs-type, .hljs-class .hljs-title { color: #953800; }
2281-
.hljs-number, .hljs-symbol, .hljs-bullet { color: #0550ae; }
2282-
.hljs-attr, .hljs-attribute, .hljs-property, .hljs-variable, .hljs-params { color: @bc-text-emphasized; }
2283-
.hljs-comment, .hljs-quote, .hljs-meta { color: #6e7781; }
2284-
2285-
.dark & {
2286-
.hljs-keyword, .hljs-literal, .hljs-doctag, .hljs-meta .hljs-keyword { color: #ff7b72; }
2287-
.hljs-string, .hljs-regexp, .hljs-meta-string { color: #a5d6ff; }
2288-
.hljs-title, .hljs-title.function_, .hljs-title.class_, .hljs-section { color: #d2a8ff; }
2289-
.hljs-built_in, .hljs-type, .hljs-class .hljs-title { color: #ffa657; }
2290-
.hljs-number, .hljs-symbol, .hljs-bullet { color: #79c0ff; }
2291-
.hljs-attr, .hljs-attribute, .hljs-property, .hljs-variable, .hljs-params { color: @dark-bc-text-emphasized; }
2292-
.hljs-comment, .hljs-quote, .hljs-meta { color: #8b949e; }
2293-
}
2272+
// hljs token colours for the signature block are shared with the code-hint doc popup -
2273+
// see the `.lsp-hover-quickview, .lsp-hint-doc-popup` rule below.
22942274

22952275
// Inline code: parameter names, types, identifiers.
22962276
code {
@@ -2433,6 +2413,31 @@ a, img {
24332413
}
24342414

24352415
// LSP code-hint documentation popup (shown beside the code hint list)
2416+
// Shared highlight.js token colours for both LSP popups (hover signature + code-hint docs). The
2417+
// app's global hljs theme is github-dark (locked, for the always-dark sidebar); these override it
2418+
// so highlighted code reads well on the light theme too - GitHub-light palette in light mode,
2419+
// GitHub-dark in dark mode. `&` is the popup selector, so `.dark &` scopes both correctly.
2420+
.lsp-hover-quickview, .lsp-hint-doc-popup {
2421+
pre code.hljs { background: none; }
2422+
.hljs-keyword, .hljs-literal, .hljs-doctag, .hljs-meta .hljs-keyword { color: #cf222e; }
2423+
.hljs-string, .hljs-regexp, .hljs-meta-string { color: #0a3069; }
2424+
.hljs-title, .hljs-title.function_, .hljs-title.class_, .hljs-section { color: #6639ba; }
2425+
.hljs-built_in, .hljs-type, .hljs-class .hljs-title { color: #953800; }
2426+
.hljs-number, .hljs-symbol, .hljs-bullet { color: #0550ae; }
2427+
.hljs-attr, .hljs-attribute, .hljs-property, .hljs-variable, .hljs-params { color: @bc-text-emphasized; }
2428+
.hljs-comment, .hljs-quote, .hljs-meta { color: #6e7781; }
2429+
2430+
.dark & {
2431+
.hljs-keyword, .hljs-literal, .hljs-doctag, .hljs-meta .hljs-keyword { color: #ff7b72; }
2432+
.hljs-string, .hljs-regexp, .hljs-meta-string { color: #a5d6ff; }
2433+
.hljs-title, .hljs-title.function_, .hljs-title.class_, .hljs-section { color: #d2a8ff; }
2434+
.hljs-built_in, .hljs-type, .hljs-class .hljs-title { color: #ffa657; }
2435+
.hljs-number, .hljs-symbol, .hljs-bullet { color: #79c0ff; }
2436+
.hljs-attr, .hljs-attribute, .hljs-property, .hljs-variable, .hljs-params { color: @dark-bc-text-emphasized; }
2437+
.hljs-comment, .hljs-quote, .hljs-meta { color: #8b949e; }
2438+
}
2439+
}
2440+
24362441
.lsp-hint-doc-popup {
24372442
display: none;
24382443
position: fixed;

0 commit comments

Comments
 (0)