@stacksjs/components 0.2.863 → 0.2.864

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -964,6 +964,130 @@ class LineHighlighter {
964
964
  }
965
965
  }
966
966
 
967
+ // src/ui/code-editor/core/hints.ts
968
+ var WORD_CHARACTER2 = /[\p{L}\p{N}_$]/u;
969
+ function wordAt(text, column, inclusiveEnd = false) {
970
+ if (column < 0 || column > text.length)
971
+ return null;
972
+ let at = column;
973
+ if (at >= text.length || !WORD_CHARACTER2.test(text.charAt(at))) {
974
+ if (!inclusiveEnd || at === 0 || !WORD_CHARACTER2.test(text.charAt(at - 1)))
975
+ return null;
976
+ at--;
977
+ }
978
+ let from = at;
979
+ while (from > 0 && WORD_CHARACTER2.test(text.charAt(from - 1)))
980
+ from--;
981
+ let to = at + 1;
982
+ while (to < text.length && WORD_CHARACTER2.test(text.charAt(to)))
983
+ to++;
984
+ return { from, to, word: text.slice(from, to) };
985
+ }
986
+ var FENCE = /^(`{3,}|~{3,})[ \t]*([\w+#.-]*)[^\n]*\n([\s\S]*?)\n?^\1[ \t]*$/gm;
987
+ function parseHoverContent(content) {
988
+ const source = content.replace(/\r\n?/g, `
989
+ `);
990
+ const segments = [];
991
+ const text = (value) => {
992
+ const trimmed = value.replace(/^\n+|\s+$/g, "");
993
+ if (trimmed !== "")
994
+ segments.push({ kind: "text", text: trimmed, language: "" });
995
+ };
996
+ let last = 0;
997
+ FENCE.lastIndex = 0;
998
+ for (let match = FENCE.exec(source);match; match = FENCE.exec(source)) {
999
+ text(source.slice(last, match.index));
1000
+ if (match[3] !== "")
1001
+ segments.push({ kind: "code", text: match[3], language: match[2] ?? "" });
1002
+ last = match.index + match[0].length;
1003
+ }
1004
+ text(source.slice(last));
1005
+ return segments;
1006
+ }
1007
+ function hoverPlainText(content) {
1008
+ return parseHoverContent(content).map((segment) => segment.text.replace(/\s+/g, " ").trim()).filter(Boolean).join(". ");
1009
+ }
1010
+ var CALL_SCAN_LIMIT = 4000;
1011
+ var CLOSERS = { ")": "(", "]": "[", "}": "{" };
1012
+ function callContext(text) {
1013
+ const start = Math.max(0, text.length - CALL_SCAN_LIMIT);
1014
+ const stack = [];
1015
+ let quote = "";
1016
+ for (let index = start;index < text.length; index++) {
1017
+ const character = text.charAt(index);
1018
+ if (quote) {
1019
+ if (character === "\\")
1020
+ index++;
1021
+ else if (character === quote || character === `
1022
+ ` && quote !== "`")
1023
+ quote = "";
1024
+ continue;
1025
+ }
1026
+ if (character === '"' || character === "'" || character === "`") {
1027
+ quote = character;
1028
+ } else if (character === "/" && text.charAt(index + 1) === "/") {
1029
+ const end = text.indexOf(`
1030
+ `, index);
1031
+ index = end === -1 ? text.length : end;
1032
+ } else if (character === "/" && text.charAt(index + 1) === "*") {
1033
+ const end = text.indexOf("*/", index + 2);
1034
+ index = end === -1 ? text.length : end + 1;
1035
+ } else if (character === "(" || character === "[" || character === "{") {
1036
+ stack.push({ bracket: character, index, commas: 0 });
1037
+ } else if (character in CLOSERS) {
1038
+ if (stack.length > 0 && stack[stack.length - 1].bracket === CLOSERS[character])
1039
+ stack.pop();
1040
+ } else if (character === "," && stack.length > 0) {
1041
+ stack[stack.length - 1].commas++;
1042
+ }
1043
+ }
1044
+ if (quote)
1045
+ return null;
1046
+ for (let depth = stack.length - 1;depth >= 0; depth--) {
1047
+ const frame = stack[depth];
1048
+ if (frame.bracket === "(")
1049
+ return { open: frame.index, active: frame.commas };
1050
+ if (frame.bracket === "{" && !/(?:[(,=:]|=>)\s*$/.test(text.slice(Math.max(start, frame.index - 40), frame.index)))
1051
+ return null;
1052
+ }
1053
+ return null;
1054
+ }
1055
+ function signatureParts(help) {
1056
+ const { label } = help;
1057
+ const parameters = help.parameters ?? [];
1058
+ const active = help.active ?? 0;
1059
+ const none = { before: label, active: "", after: "", parameterDocumentation: "" };
1060
+ let cursor = 0;
1061
+ for (let index = 0;index < parameters.length; index++) {
1062
+ const parameter = parameters[index];
1063
+ const spec = typeof parameter === "object" && !Array.isArray(parameter) ? parameter.label : parameter;
1064
+ let from;
1065
+ let to;
1066
+ if (Array.isArray(spec)) {
1067
+ from = Math.max(0, Math.min(label.length, spec[0]));
1068
+ to = Math.max(from, Math.min(label.length, spec[1]));
1069
+ } else {
1070
+ from = spec === "" ? -1 : label.indexOf(spec, cursor);
1071
+ if (from === -1) {
1072
+ if (index === active)
1073
+ return none;
1074
+ continue;
1075
+ }
1076
+ to = from + spec.length;
1077
+ }
1078
+ cursor = to;
1079
+ if (index === active) {
1080
+ return {
1081
+ before: label.slice(0, from),
1082
+ active: label.slice(from, to),
1083
+ after: label.slice(to),
1084
+ parameterDocumentation: typeof parameter === "object" && !Array.isArray(parameter) ? parameter.documentation ?? "" : ""
1085
+ };
1086
+ }
1087
+ }
1088
+ return none;
1089
+ }
1090
+
967
1091
  // src/ui/code-editor/core/keymap.ts
968
1092
  var defaultKeymap = {
969
1093
  Enter: "insertNewlineAndIndent",
@@ -976,7 +1100,9 @@ var defaultKeymap = {
976
1100
  "Mod-y": "redo",
977
1101
  "Mod-Enter": "run",
978
1102
  "Mod-Shift-Enter": "runSelection",
979
- "Ctrl-Space": "triggerCompletion"
1103
+ "Ctrl-Space": "triggerCompletion",
1104
+ "Mod-i": "showHover",
1105
+ "Mod-Shift-Space": "triggerSignature"
980
1106
  };
981
1107
  var MODIFIER_ORDER = ["Ctrl", "Alt", "Shift", "Meta"];
982
1108
  function isApplePlatform(platform) {
@@ -1079,6 +1205,8 @@ async function createLanguageTokenizer(language) {
1079
1205
  // src/ui/code-editor/view.ts
1080
1206
  var OVERSCAN = 12;
1081
1207
  var SYNC_BUDGET = 200;
1208
+ var HOVER_DELAY = 300;
1209
+ var HINT_ROOM = 120;
1082
1210
  var IDLE_BUDGET = 600;
1083
1211
  var SEVERITY_RANK = { error: 3, warning: 2, info: 1, hint: 0 };
1084
1212
  var PRINTABLE_ASCII = /^[\x20-\x7E]*$/;
@@ -1087,14 +1215,29 @@ function escapeHtml(text) {
1087
1215
  return text;
1088
1216
  return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1089
1217
  }
1090
- function renderLineHtml(line, text) {
1091
- if (!line || line.text !== text)
1092
- return escapeHtml(text);
1218
+ function renderTokensHtml(tokens) {
1093
1219
  let html = "";
1094
- for (const token of line.tokens)
1220
+ for (const token of tokens)
1095
1221
  html += token.type === "" ? escapeHtml(token.content) : `<span class="tok-${token.type}">${escapeHtml(token.content)}</span>`;
1096
1222
  return html;
1097
1223
  }
1224
+ function renderLineHtml(line, text) {
1225
+ if (!line || line.text !== text)
1226
+ return escapeHtml(text);
1227
+ return renderTokensHtml(line.tokens);
1228
+ }
1229
+ function renderCodeHtml(tokenizer, code) {
1230
+ let state = tokenizer.initialState();
1231
+ const lines = [];
1232
+ for (const text of code.split(`
1233
+ `)) {
1234
+ const result = tokenizer.tokenizeLine(text, state);
1235
+ state = result.endState;
1236
+ lines.push(renderTokensHtml(result.tokens));
1237
+ }
1238
+ return lines.join(`
1239
+ `);
1240
+ }
1098
1241
  function part(root, name) {
1099
1242
  const element = root.querySelector(`[data-ce="${name}"]`);
1100
1243
  if (!element)
@@ -1118,6 +1261,8 @@ class EditorView {
1118
1261
  widgetLayer;
1119
1262
  measure;
1120
1263
  status;
1264
+ hover;
1265
+ signature;
1121
1266
  keymap;
1122
1267
  highlighter = null;
1123
1268
  metrics = { lineHeight: 20, charWidth: 8, padTop: 0, padBottom: 0, padLeft: 0, padRight: 0 };
@@ -1134,6 +1279,12 @@ class EditorView {
1134
1279
  diagnostics = [];
1135
1280
  ghost = null;
1136
1281
  announced = "";
1282
+ hoverOffset = -1;
1283
+ signatureOffset = -1;
1284
+ pointerX = 0;
1285
+ pointerY = 0;
1286
+ pointerTimer;
1287
+ pointerInHint = false;
1137
1288
  composing = false;
1138
1289
  focused = false;
1139
1290
  tabLeavesNext = false;
@@ -1159,6 +1310,8 @@ class EditorView {
1159
1310
  this.widgetLayer = part(root, "widgets");
1160
1311
  this.measure = part(root, "measure");
1161
1312
  this.status = root.querySelector('[data-ce="status"]');
1313
+ this.hover = root.querySelector('[data-ce="hover"]');
1314
+ this.signature = root.querySelector('[data-ce="signature"]');
1162
1315
  if (this.textarea.value !== this.core.value)
1163
1316
  this.textarea.value = this.core.value;
1164
1317
  this.listen(this.textarea, "beforeinput", (event) => this.onBeforeInput(event));
@@ -1198,7 +1351,31 @@ class EditorView {
1198
1351
  if (root.ownerDocument.activeElement === this.textarea)
1199
1352
  this.syncSelection();
1200
1353
  });
1201
- this.listen(this.scroller, "scroll", () => this.scheduleRender(), { passive: true });
1354
+ this.listen(this.scroller, "scroll", () => {
1355
+ this.scheduleRender();
1356
+ this.options.onScroll?.();
1357
+ }, { passive: true });
1358
+ this.listen(this.textarea, "mousemove", (event) => {
1359
+ const move = event;
1360
+ if (move.buttons !== 0)
1361
+ return;
1362
+ if (move.clientX === this.pointerX && move.clientY === this.pointerY)
1363
+ return;
1364
+ this.pointerX = move.clientX;
1365
+ this.pointerY = move.clientY;
1366
+ this.schedulePointerRest();
1367
+ }, { passive: true });
1368
+ this.listen(this.textarea, "mousedown", () => this.pointerGone());
1369
+ this.listen(this.root, "mouseleave", () => this.pointerGone());
1370
+ if (this.hover) {
1371
+ this.listen(this.hover, "mouseenter", () => {
1372
+ this.pointerInHint = true;
1373
+ this.cancelPointerRest();
1374
+ });
1375
+ this.listen(this.hover, "mouseleave", () => {
1376
+ this.pointerInHint = false;
1377
+ });
1378
+ }
1202
1379
  this.listen(this.gutter, "mousedown", (event) => this.onGutterMouseDown(event));
1203
1380
  if (typeof ResizeObserver !== "undefined") {
1204
1381
  this.resizeObserver = new ResizeObserver(() => this.layout());
@@ -1670,6 +1847,130 @@ class EditorView {
1670
1847
  this.scheduleIdle();
1671
1848
  }, 0);
1672
1849
  }
1850
+ schedulePointerRest() {
1851
+ this.cancelPointerRest();
1852
+ this.pointerTimer = setTimeout(() => {
1853
+ this.pointerTimer = undefined;
1854
+ if (!this.destroyed && !this.pointerInHint)
1855
+ this.options.onPointerRest?.(this.offsetAtPoint(this.pointerX, this.pointerY));
1856
+ }, this.options.hoverDelay ?? HOVER_DELAY);
1857
+ }
1858
+ cancelPointerRest() {
1859
+ if (this.pointerTimer !== undefined) {
1860
+ clearTimeout(this.pointerTimer);
1861
+ this.pointerTimer = undefined;
1862
+ }
1863
+ }
1864
+ pointerGone() {
1865
+ this.cancelPointerRest();
1866
+ this.pointerInHint = false;
1867
+ this.options.onPointerLeave?.();
1868
+ }
1869
+ offsetAtPoint(clientX, clientY) {
1870
+ const { lineHeight, padTop, padLeft, charWidth } = this.metrics;
1871
+ const bounds = this.region.getBoundingClientRect();
1872
+ const x = clientX - bounds.left - padLeft;
1873
+ const y = clientY - bounds.top - padTop;
1874
+ if (x < 0 || y < 0)
1875
+ return null;
1876
+ const doc = this.core.doc;
1877
+ const line = Math.floor(y / lineHeight);
1878
+ if (line >= doc.lineCount)
1879
+ return null;
1880
+ const text = doc.line(line);
1881
+ let column;
1882
+ if (PRINTABLE_ASCII.test(text)) {
1883
+ column = Math.floor(x / charWidth);
1884
+ } else {
1885
+ let low = 0;
1886
+ let high = text.length;
1887
+ while (low < high) {
1888
+ const middle = low + high + 1 >> 1;
1889
+ if (this.widthOf(text, middle) <= x)
1890
+ low = middle;
1891
+ else
1892
+ high = middle - 1;
1893
+ }
1894
+ column = low;
1895
+ }
1896
+ return column >= text.length ? null : doc.lineInfo(line).from + column;
1897
+ }
1898
+ placeHint(element, offset, side) {
1899
+ const anchor = this.caretAnchor(offset);
1900
+ const above = side ? side === "above" : anchor.spaceAbove >= HINT_ROOM || anchor.spaceAbove > anchor.spaceBelow;
1901
+ element.style.display = "";
1902
+ element.dataset.placement = above ? "above" : "below";
1903
+ const visibleRight = this.scroller.scrollLeft + this.scroller.clientWidth - this.gutterWidth;
1904
+ const left = Math.max(this.scroller.scrollLeft, Math.min(anchor.left, visibleRight - element.offsetWidth - this.metrics.charWidth));
1905
+ element.style.transform = `translate(${Math.max(0, left)}px, ${above ? anchor.top : anchor.bottom}px)${above ? " translateY(-100%)" : ""}`;
1906
+ }
1907
+ hintBlock(block) {
1908
+ const element = this.root.ownerDocument.createElement(block.kind === "code" ? "pre" : "p");
1909
+ element.className = block.kind === "code" ? "stx-ce-hint-code" : "stx-ce-hint-text";
1910
+ if (block.kind === "code" && block.html !== undefined)
1911
+ element.innerHTML = block.html;
1912
+ else
1913
+ element.textContent = block.text;
1914
+ return element;
1915
+ }
1916
+ showHover(blocks, offset) {
1917
+ if (!this.hover || this.destroyed)
1918
+ return;
1919
+ if (blocks.length === 0) {
1920
+ this.hideHover();
1921
+ return;
1922
+ }
1923
+ this.hoverOffset = Math.max(0, Math.min(this.core.doc.length, offset));
1924
+ this.hover.replaceChildren(...blocks.map((block) => this.hintBlock(block)));
1925
+ this.hover.scrollTop = 0;
1926
+ this.placeHint(this.hover, this.hoverOffset);
1927
+ }
1928
+ hideHover() {
1929
+ this.pointerInHint = false;
1930
+ if (!this.hover || this.hoverOffset < 0)
1931
+ return;
1932
+ this.hoverOffset = -1;
1933
+ this.hover.style.display = "none";
1934
+ this.hover.replaceChildren();
1935
+ }
1936
+ get hoverOpen() {
1937
+ return this.hoverOffset >= 0;
1938
+ }
1939
+ showSignature(display, offset, side) {
1940
+ if (!this.signature || this.destroyed)
1941
+ return;
1942
+ const document = this.root.ownerDocument;
1943
+ const label = document.createElement("div");
1944
+ label.className = "stx-ce-signature-label";
1945
+ label.append(display.before);
1946
+ if (display.active !== "") {
1947
+ const active = document.createElement("strong");
1948
+ active.className = "stx-ce-signature-active";
1949
+ active.textContent = display.active;
1950
+ label.append(active);
1951
+ }
1952
+ label.append(display.after);
1953
+ const children = [label];
1954
+ if (display.documentation) {
1955
+ const note = document.createElement("p");
1956
+ note.className = "stx-ce-hint-text";
1957
+ note.textContent = display.documentation;
1958
+ children.push(note);
1959
+ }
1960
+ this.signatureOffset = Math.max(0, Math.min(this.core.doc.length, offset));
1961
+ this.signature.replaceChildren(...children);
1962
+ this.placeHint(this.signature, this.signatureOffset, side);
1963
+ }
1964
+ hideSignature() {
1965
+ if (!this.signature || this.signatureOffset < 0)
1966
+ return;
1967
+ this.signatureOffset = -1;
1968
+ this.signature.style.display = "none";
1969
+ this.signature.replaceChildren();
1970
+ }
1971
+ get signatureOpen() {
1972
+ return this.signatureOffset >= 0;
1973
+ }
1673
1974
  caretAnchor(offset = this.core.main.head) {
1674
1975
  const { lineHeight, padTop, padLeft } = this.metrics;
1675
1976
  const position = this.core.doc.positionAt(offset);
@@ -1797,6 +2098,7 @@ class EditorView {
1797
2098
  this.resizeObserver?.disconnect();
1798
2099
  if (this.idle !== undefined)
1799
2100
  clearTimeout(this.idle);
2101
+ this.cancelPointerRest();
1800
2102
  const view = this.root.ownerDocument.defaultView;
1801
2103
  if (this.frame !== 0 && view?.cancelAnimationFrame)
1802
2104
  view.cancelAnimationFrame(this.frame);
@@ -1805,7 +2107,8 @@ class EditorView {
1805
2107
  }
1806
2108
 
1807
2109
  // src/ui/code-editor/controller.ts
1808
- var CLOSED_POPUP = { open: false, items: [], activeId: "", left: 0, top: 0, placement: "below" };
2110
+ var CLOSED_POPUP = { open: false, items: [], activeId: "", left: 0, top: 0, placement: "below", detail: "", documentation: "" };
2111
+ var CALL_WINDOW = 4000;
1809
2112
  var WORD2 = /^[\p{L}\p{N}_$]*$/u;
1810
2113
  var TRIGGER = /[\p{L}\p{N}_$.]/u;
1811
2114
  var POPUP_LIMIT = 80;
@@ -1825,6 +2128,13 @@ class CodeEditorController {
1825
2128
  languageRequest = 0;
1826
2129
  userKeymap;
1827
2130
  popupWasOpen = false;
2131
+ popupSide = "";
2132
+ hoverEnabled;
2133
+ signatureEnabled;
2134
+ hoverTarget = null;
2135
+ hoverSerial = 0;
2136
+ signatureTarget = null;
2137
+ hintTokenizers = new Map;
1828
2138
  destroyed = false;
1829
2139
  constructor(root, config = {}, hooks = {}) {
1830
2140
  this.hooks = hooks;
@@ -1833,6 +2143,8 @@ class CodeEditorController {
1833
2143
  this.autoComplete = config.autoComplete ?? true;
1834
2144
  this.highlight = config.highlight ?? true;
1835
2145
  this.userKeymap = config.keymap ?? {};
2146
+ this.hoverEnabled = config.hover ?? true;
2147
+ this.signatureEnabled = config.signatureHelp ?? true;
1836
2148
  this.core = new EditorCore({
1837
2149
  value: config.value ?? "",
1838
2150
  tabSize: config.tabSize,
@@ -1852,8 +2164,17 @@ class CodeEditorController {
1852
2164
  onFocus: () => this.hooks.onFocus?.(),
1853
2165
  onBlur: () => {
1854
2166
  this.closeCompletions();
2167
+ this.closeHover();
2168
+ this.closeSignature();
1855
2169
  this.hooks.onBlur?.();
1856
- }
2170
+ },
2171
+ hoverDelay: config.hoverDelay,
2172
+ onPointerRest: (offset) => this.onPointerRest(offset),
2173
+ onPointerLeave: () => {
2174
+ if (!this.hoverTarget?.explicit)
2175
+ this.closeHover();
2176
+ },
2177
+ onScroll: () => this.closeHover()
1857
2178
  });
1858
2179
  this.view.setReadOnly(config.readOnly ?? false);
1859
2180
  this.view.setLineNumbers(config.lineNumbers ?? true);
@@ -1956,6 +2277,19 @@ class CodeEditorController {
1956
2277
  this.requestCompletions(true, "");
1957
2278
  return true;
1958
2279
  }
2280
+ if (name === "showHover") {
2281
+ const head = this.core.main.head;
2282
+ const range = this.wordRangeAt(head, true);
2283
+ if (range)
2284
+ this.requestHover(head, range, true);
2285
+ else
2286
+ this.view.announce("Nothing to describe here.");
2287
+ return true;
2288
+ }
2289
+ if (name === "triggerSignature") {
2290
+ this.requestSignature(true, "");
2291
+ return true;
2292
+ }
1959
2293
  return false;
1960
2294
  }
1961
2295
  runSelectionDetail() {
@@ -1972,6 +2306,8 @@ class CodeEditorController {
1972
2306
  this.hooks.onChange?.(this.core.value, transaction);
1973
2307
  if (transaction.selectionChanged || transaction.docChanged)
1974
2308
  this.hooks.onSelection?.(this.selection);
2309
+ this.closeHover();
2310
+ this.followSignature(transaction);
1975
2311
  if (this.list.isOpen) {
1976
2312
  this.narrowCompletions();
1977
2313
  } else if (this.autoComplete && transaction.origin === "input" && transaction.kind === "type" && transaction.changes.length === 1) {
@@ -2049,8 +2385,21 @@ class CodeEditorController {
2049
2385
  this.publishPopup();
2050
2386
  }
2051
2387
  onKeydown(event) {
2052
- if (!this.list.isOpen || event.ctrlKey || event.metaKey || event.altKey)
2388
+ if (event.ctrlKey || event.metaKey || event.altKey)
2389
+ return false;
2390
+ if (!this.list.isOpen) {
2391
+ if (event.key !== "Escape" || event.shiftKey)
2392
+ return false;
2393
+ if (this.view.hoverOpen || this.hoverTarget) {
2394
+ this.closeHover();
2395
+ return true;
2396
+ }
2397
+ if (this.signatureTarget) {
2398
+ this.closeSignature();
2399
+ return true;
2400
+ }
2053
2401
  return false;
2402
+ }
2054
2403
  switch (event.key) {
2055
2404
  case "ArrowDown":
2056
2405
  this.list.move(1);
@@ -2084,6 +2433,7 @@ class CodeEditorController {
2084
2433
  if (!this.list.isOpen) {
2085
2434
  if (this.popupWasOpen) {
2086
2435
  this.popupWasOpen = false;
2436
+ this.setPopupSide("");
2087
2437
  this.hooks.onPopup(CLOSED_POPUP);
2088
2438
  }
2089
2439
  return;
@@ -2091,6 +2441,7 @@ class CodeEditorController {
2091
2441
  this.popupWasOpen = true;
2092
2442
  const anchor = this.view.caretAnchor(this.list.from);
2093
2443
  const above = anchor.spaceBelow < POPUP_ROOM && anchor.spaceAbove > anchor.spaceBelow;
2444
+ this.setPopupSide(above ? "above" : "below");
2094
2445
  const selected = this.list.selectedIndex;
2095
2446
  const items = this.list.visible.slice(0, POPUP_LIMIT).map((entry, index) => ({
2096
2447
  id: `${this.popupId}-${index}`,
@@ -2106,12 +2457,195 @@ class CodeEditorController {
2106
2457
  activeId: items[selected]?.id ?? "",
2107
2458
  left: anchor.left,
2108
2459
  top: above ? anchor.top : anchor.bottom,
2109
- placement: above ? "above" : "below"
2460
+ placement: above ? "above" : "below",
2461
+ detail: this.list.selected?.detail ?? "",
2462
+ documentation: this.list.selected?.documentation ?? ""
2110
2463
  });
2111
2464
  }
2465
+ setPopupSide(side) {
2466
+ if (side === this.popupSide)
2467
+ return;
2468
+ this.popupSide = side;
2469
+ if (this.signatureTarget?.help)
2470
+ this.drawSignature(this.currentCall()?.active ?? 0);
2471
+ }
2472
+ setHover(enabled) {
2473
+ this.hoverEnabled = enabled;
2474
+ if (!enabled)
2475
+ this.closeHover();
2476
+ }
2477
+ wordRangeAt(offset, inclusiveEnd) {
2478
+ const line = this.core.doc.lineAt(Math.max(0, Math.min(this.core.doc.length, offset)));
2479
+ const range = wordAt(line.text, offset - line.from, inclusiveEnd);
2480
+ return range ? { from: line.from + range.from, to: line.from + range.to, word: range.word } : null;
2481
+ }
2482
+ onPointerRest(offset) {
2483
+ if (!this.hoverEnabled)
2484
+ return;
2485
+ const range = offset === null ? null : this.wordRangeAt(offset, false);
2486
+ if (!range) {
2487
+ if (!this.hoverTarget?.explicit)
2488
+ this.closeHover();
2489
+ return;
2490
+ }
2491
+ const current = this.hoverTarget;
2492
+ if (current && current.from === range.from && current.to === range.to)
2493
+ return;
2494
+ this.requestHover(offset, range, false);
2495
+ }
2496
+ requestHover(offset, range, explicit) {
2497
+ this.view.hideHover();
2498
+ this.hoverTarget = { from: range.from, to: range.to, explicit, serial: ++this.hoverSerial };
2499
+ const position = this.core.doc.positionAt(offset);
2500
+ this.hooks.onHoverRequest?.({ line: position.line, column: position.column, from: range.from, to: range.to, word: range.word, explicit });
2501
+ }
2502
+ showHover(content, position) {
2503
+ if (this.destroyed)
2504
+ return;
2505
+ let target = this.hoverTarget;
2506
+ if (position) {
2507
+ const offset = this.core.doc.offsetAt(position);
2508
+ if (target && (offset < target.from || offset > target.to))
2509
+ return;
2510
+ if (!target) {
2511
+ const range = this.wordRangeAt(offset, true) ?? { from: offset, to: offset };
2512
+ target = { from: range.from, to: range.to, explicit: false, serial: ++this.hoverSerial };
2513
+ }
2514
+ } else if (!target) {
2515
+ const head = this.core.main.head;
2516
+ const range = this.wordRangeAt(head, true) ?? { from: head, to: head };
2517
+ target = { from: range.from, to: range.to, explicit: true, serial: ++this.hoverSerial };
2518
+ }
2519
+ const segments = typeof content === "string" ? parseHoverContent(content) : [];
2520
+ if (segments.length === 0) {
2521
+ this.closeHover();
2522
+ return;
2523
+ }
2524
+ this.hoverTarget = target;
2525
+ const blocks = segments.map((segment) => ({ kind: segment.kind, text: segment.text }));
2526
+ this.view.showHover(blocks, target.from);
2527
+ if (target.explicit)
2528
+ this.view.announce(hoverPlainText(content));
2529
+ const { serial } = target;
2530
+ const pending = [];
2531
+ segments.forEach((segment, index) => {
2532
+ if (segment.kind !== "code")
2533
+ return;
2534
+ pending.push(this.hintTokenizer(segment.language || this.language).then((tokenizer) => {
2535
+ if (tokenizer)
2536
+ blocks[index] = { kind: "code", text: segment.text, html: renderCodeHtml(tokenizer, segment.text) };
2537
+ }));
2538
+ });
2539
+ if (pending.length > 0) {
2540
+ Promise.all(pending).then(() => {
2541
+ if (!this.destroyed && this.hoverTarget?.serial === serial && this.view.hoverOpen && blocks.some((block) => block.html !== undefined))
2542
+ this.view.showHover(blocks, target.from);
2543
+ });
2544
+ }
2545
+ }
2546
+ closeHover() {
2547
+ if (!this.hoverTarget && !this.view.hoverOpen)
2548
+ return;
2549
+ this.hoverTarget = null;
2550
+ this.hoverSerial++;
2551
+ this.view.hideHover();
2552
+ }
2553
+ hintTokenizer(language) {
2554
+ let tokenizer = this.hintTokenizers.get(language);
2555
+ if (!tokenizer) {
2556
+ tokenizer = this.highlight ? createLanguageTokenizer(language).catch(() => null) : Promise.resolve(null);
2557
+ this.hintTokenizers.set(language, tokenizer);
2558
+ }
2559
+ return tokenizer;
2560
+ }
2561
+ setSignatureHelp(enabled) {
2562
+ this.signatureEnabled = enabled;
2563
+ if (!enabled)
2564
+ this.closeSignature();
2565
+ }
2566
+ currentCall() {
2567
+ if (!rangeEmpty(this.core.main))
2568
+ return null;
2569
+ const head = this.core.main.head;
2570
+ const start = Math.max(0, head - CALL_WINDOW);
2571
+ const context = callContext(this.core.doc.slice(start, head));
2572
+ return context ? { open: start + context.open, active: context.active } : null;
2573
+ }
2574
+ requestSignature(explicit, trigger) {
2575
+ if (this.core.readOnly)
2576
+ return;
2577
+ const call = this.currentCall();
2578
+ if (!call) {
2579
+ this.closeSignature();
2580
+ return;
2581
+ }
2582
+ if (this.signatureTarget?.open !== call.open) {
2583
+ this.view.hideSignature();
2584
+ this.signatureTarget = { open: call.open, help: null, base: call.active };
2585
+ }
2586
+ const head = this.core.main.head;
2587
+ const cursor = this.core.cursor;
2588
+ this.hooks.onSignatureRequest?.({ line: cursor.line, column: cursor.column, offset: head, start: call.open + 1, active: call.active, trigger, explicit });
2589
+ }
2590
+ showSignature(help) {
2591
+ if (this.destroyed)
2592
+ return;
2593
+ const call = this.currentCall();
2594
+ if (!help || !help.label || !call) {
2595
+ this.closeSignature();
2596
+ return;
2597
+ }
2598
+ this.signatureTarget = { open: call.open, help, base: call.active };
2599
+ this.drawSignature(call.active);
2600
+ }
2601
+ closeSignature() {
2602
+ if (!this.signatureTarget)
2603
+ return;
2604
+ this.signatureTarget = null;
2605
+ this.view.hideSignature();
2606
+ }
2607
+ drawSignature(active) {
2608
+ const target = this.signatureTarget;
2609
+ if (!target?.help)
2610
+ return;
2611
+ const { help } = target;
2612
+ const index = help.active === undefined ? active : help.active + (active - target.base);
2613
+ const parts = signatureParts({ ...help, active: index });
2614
+ const head = this.core.main.head;
2615
+ const sameLine = this.core.doc.lineIndexAt(target.open) === this.core.doc.lineIndexAt(head);
2616
+ this.view.showSignature({
2617
+ before: parts.before,
2618
+ active: parts.active,
2619
+ after: parts.after,
2620
+ documentation: parts.parameterDocumentation || help.documentation || ""
2621
+ }, sameLine ? target.open + 1 : head, this.popupSide === "above" ? "below" : this.popupSide === "below" ? "above" : undefined);
2622
+ }
2623
+ followSignature(transaction) {
2624
+ const target = this.signatureTarget;
2625
+ if (target && transaction.docChanged)
2626
+ target.open = mapOffset(target.open, transaction.changes, 1);
2627
+ const typed = transaction.docChanged && (transaction.kind === "type" || transaction.kind === "pair") ? this.core.doc.slice(Math.max(0, this.core.main.head - 1), this.core.main.head) : "";
2628
+ if (this.signatureEnabled && (typed === "(" || typed === ",")) {
2629
+ this.requestSignature(false, typed);
2630
+ if (this.signatureTarget?.help)
2631
+ this.drawSignature(this.currentCall()?.active ?? 0);
2632
+ return;
2633
+ }
2634
+ if (!target)
2635
+ return;
2636
+ const call = this.currentCall();
2637
+ if (!call)
2638
+ this.closeSignature();
2639
+ else if (call.open !== target.open)
2640
+ this.requestSignature(false, "");
2641
+ else
2642
+ this.drawSignature(call.active);
2643
+ }
2112
2644
  destroy() {
2113
2645
  this.destroyed = true;
2114
2646
  this.list.close();
2647
+ this.hoverTarget = null;
2648
+ this.signatureTarget = null;
2115
2649
  this.view.destroy();
2116
2650
  }
2117
2651
  }
@@ -2126,6 +2660,7 @@ export {
2126
2660
  LineHighlighter,
2127
2661
  MAX_TOKENIZED_LINE_LENGTH,
2128
2662
  TextDocument,
2663
+ callContext,
2129
2664
  clampSelection,
2130
2665
  createKeymap,
2131
2666
  createLanguageTokenizer,
@@ -2133,6 +2668,7 @@ export {
2133
2668
  defaultKeymap,
2134
2669
  deleteBackward,
2135
2670
  handleTextInput,
2671
+ hoverPlainText,
2136
2672
  indentLess,
2137
2673
  indentMore,
2138
2674
  indentString,
@@ -2149,6 +2685,7 @@ export {
2149
2685
  matchScore,
2150
2686
  normalizeBinding,
2151
2687
  normalizeNewlines,
2688
+ parseHoverContent,
2152
2689
  range,
2153
2690
  rangeEmpty,
2154
2691
  rangeFrom,
@@ -2156,11 +2693,15 @@ export {
2156
2693
  rankCompletions,
2157
2694
  redo,
2158
2695
  registerDefaultCommands,
2696
+ renderCodeHtml,
2159
2697
  renderLineHtml,
2698
+ renderTokensHtml,
2160
2699
  selectionsEqual,
2700
+ signatureParts,
2161
2701
  singleSelection,
2162
2702
  toggleLineComment,
2163
2703
  tokenizerStateKey,
2164
2704
  undo,
2705
+ wordAt,
2165
2706
  wordStart
2166
2707
  };