@lvce-editor/hover-worker 1.2.0 → 1.3.0

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.
@@ -54,6 +54,49 @@ class VError extends Error {
54
54
  }
55
55
  }
56
56
 
57
+ class AssertionError extends Error {
58
+ constructor(message) {
59
+ super(message);
60
+ this.name = 'AssertionError';
61
+ }
62
+ }
63
+ const Object$1 = 1;
64
+ const Number$1 = 2;
65
+ const Array$1 = 3;
66
+ const String = 4;
67
+ const Boolean = 5;
68
+ const Function = 6;
69
+ const Null = 7;
70
+ const Unknown = 8;
71
+ const getType = value => {
72
+ switch (typeof value) {
73
+ case 'number':
74
+ return Number$1;
75
+ case 'function':
76
+ return Function;
77
+ case 'string':
78
+ return String;
79
+ case 'object':
80
+ if (value === null) {
81
+ return Null;
82
+ }
83
+ if (Array.isArray(value)) {
84
+ return Array$1;
85
+ }
86
+ return Object$1;
87
+ case 'boolean':
88
+ return Boolean;
89
+ default:
90
+ return Unknown;
91
+ }
92
+ };
93
+ const number = value => {
94
+ const type = getType(value);
95
+ if (type !== Number$1) {
96
+ throw new AssertionError('expected value to be of type number');
97
+ }
98
+ };
99
+
57
100
  const isMessagePort = value => {
58
101
  return value && value instanceof MessagePort;
59
102
  };
@@ -836,7 +879,7 @@ const send = (transport, method, ...params) => {
836
879
  const message = create$4$1(method, params);
837
880
  transport.send(message);
838
881
  };
839
- const invoke$3 = (ipc, method, ...params) => {
882
+ const invoke$4 = (ipc, method, ...params) => {
840
883
  return invokeHelper(ipc, method, params, false);
841
884
  };
842
885
  const invokeAndTransfer$3 = (ipc, method, ...params) => {
@@ -856,7 +899,7 @@ const register = commandMap => {
856
899
  const getCommand$1 = key => {
857
900
  return commands[key];
858
901
  };
859
- const execute = (command, ...args) => {
902
+ const execute$1 = (command, ...args) => {
860
903
  const fn = getCommand$1(command);
861
904
  if (!fn) {
862
905
  throw new CommandNotFoundError(command);
@@ -875,7 +918,7 @@ const createRpc = ipc => {
875
918
  send(ipc, method, ...params);
876
919
  },
877
920
  invoke(method, ...params) {
878
- return invoke$3(ipc, method, ...params);
921
+ return invoke$4(ipc, method, ...params);
879
922
  },
880
923
  invokeAndTransfer(method, ...params) {
881
924
  return invokeAndTransfer$3(ipc, method, ...params);
@@ -897,7 +940,7 @@ const logError = () => {
897
940
  };
898
941
  const handleMessage = event => {
899
942
  const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
900
- const actualExecute = event?.target?.execute || execute;
943
+ const actualExecute = event?.target?.execute || execute$1;
901
944
  return handleJsonRpcMessage(event.target, event.data, actualExecute, resolve, preparePrettyError, logError, actualRequiresSocket);
902
945
  };
903
946
  const handleIpc = ipc => {
@@ -984,6 +1027,8 @@ const Text = 12;
984
1027
  const EditorWorker$1 = 99;
985
1028
  const ExtensionHostWorker = 44;
986
1029
 
1030
+ const SetFocusContext = 'Viewlet.setFocusContext';
1031
+
987
1032
  const rpcs = Object.create(null);
988
1033
  const set$5 = (id, rpc) => {
989
1034
  rpcs[id] = rpc;
@@ -1017,7 +1062,7 @@ const create$2 = rpcId => {
1017
1062
  };
1018
1063
 
1019
1064
  const {
1020
- invoke: invoke$2,
1065
+ invoke: invoke$3,
1021
1066
  invokeAndTransfer: invokeAndTransfer$2,
1022
1067
  set: set$4,
1023
1068
  dispose: dispose$3
@@ -1029,63 +1074,63 @@ const sendMessagePortToExtensionHostWorker$1 = async port => {
1029
1074
  'SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, command, 0);
1030
1075
  };
1031
1076
  // TODO add tests for this
1032
- const activateByEvent = async event => {
1077
+ const activateByEvent$1 = async event => {
1033
1078
  // @ts-ignore
1034
- await invoke$2('ActivateByEvent.activateByEvent', event);
1079
+ await invoke$3('ActivateByEvent.activateByEvent', event);
1035
1080
  };
1036
1081
  const applyEdit = async (editorUid, changes) => {
1037
1082
  // @ts-ignore
1038
- await invoke$2('Editor.applyEdit2', editorUid, changes);
1083
+ await invoke$3('Editor.applyEdit2', editorUid, changes);
1039
1084
  };
1040
1085
  const applyWorkspaceEdit = async (editorUid, changes) => {
1041
1086
  // @ts-ignore
1042
- await invoke$2('Editor.applyWorkspaceEdit', editorUid, changes);
1087
+ await invoke$3('Editor.applyWorkspaceEdit', editorUid, changes);
1043
1088
  };
1044
1089
  const closeWidget = async (editorUid, widgetId, widgetName, focusId) => {
1045
1090
  // @ts-ignore
1046
- await invoke$2('Editor.closeWidget2', editorUid, widgetId, widgetName, focusId);
1091
+ await invoke$3('Editor.closeWidget2', editorUid, widgetId, widgetName, focusId);
1047
1092
  };
1048
1093
  const getWordAt = async (uid, rowIndex, columnIndex) => {
1049
1094
  // @ts-ignore
1050
- const word = await invoke$2('Editor.getWordAt2', uid, rowIndex, columnIndex);
1095
+ const word = await invoke$3('Editor.getWordAt2', uid, rowIndex, columnIndex);
1051
1096
  return word;
1052
1097
  };
1053
1098
  const getLines = async editorUid => {
1054
- const lines = await invoke$2('Editor.getLines2', editorUid);
1099
+ const lines = await invoke$3('Editor.getLines2', editorUid);
1055
1100
  return lines;
1056
1101
  };
1057
1102
  const getPositionAtCursor$1 = async parentUid => {
1058
- const position = await invoke$2('Editor.getPositionAtCursor', parentUid);
1103
+ const position = await invoke$3('Editor.getPositionAtCursor', parentUid);
1059
1104
  return position;
1060
1105
  };
1061
- const getOffsetAtCursor = async editorId => {
1106
+ const getOffsetAtCursor$1 = async editorId => {
1062
1107
  // @ts-ignore
1063
- return await invoke$2('Editor.getOffsetAtCursor', editorId);
1108
+ return await invoke$3('Editor.getOffsetAtCursor', editorId);
1064
1109
  };
1065
1110
  const getSelections = async editorUid => {
1066
- const selections = await invoke$2('Editor.getSelections2', editorUid);
1111
+ const selections = await invoke$3('Editor.getSelections2', editorUid);
1067
1112
  return selections;
1068
1113
  };
1069
1114
  const getWordAtOffset2$1 = async editorUid => {
1070
- return invoke$2('Editor.getWordAtOffset2', editorUid);
1115
+ return invoke$3('Editor.getWordAtOffset2', editorUid);
1071
1116
  };
1072
1117
  const getWordBefore = async (editorUid, rowIndex, columnIndex) => {
1073
- return invoke$2('Editor.getWordBefore2', editorUid, rowIndex, columnIndex);
1118
+ return invoke$3('Editor.getWordBefore2', editorUid, rowIndex, columnIndex);
1074
1119
  };
1075
1120
  const updateDebugInfo = async info => {
1076
- await invoke$2('Editor.updateDebugInfo', info);
1121
+ await invoke$3('Editor.updateDebugInfo', info);
1077
1122
  };
1078
1123
  const getUri = async editorUid => {
1079
1124
  // @ts-ignore
1080
- return invoke$2('Editor.getUri', editorUid);
1125
+ return invoke$3('Editor.getUri', editorUid);
1081
1126
  };
1082
1127
  const getLanguageId = async editorUid => {
1083
1128
  // @ts-ignore
1084
- return invoke$2('Editor.getLanguageId', editorUid);
1129
+ return invoke$3('Editor.getLanguageId', editorUid);
1085
1130
  };
1086
1131
  const getProblems = async () => {
1087
1132
  // @ts-ignore
1088
- return invoke$2('Editor.getProblems');
1133
+ return invoke$3('Editor.getProblems');
1089
1134
  };
1090
1135
  const registerMockRpc$1 = commandMap => {
1091
1136
  const mockRpc = createMockRpc({
@@ -1097,14 +1142,14 @@ const registerMockRpc$1 = commandMap => {
1097
1142
 
1098
1143
  const EditorWorker = {
1099
1144
  __proto__: null,
1100
- activateByEvent,
1145
+ activateByEvent: activateByEvent$1,
1101
1146
  applyEdit,
1102
1147
  applyWorkspaceEdit,
1103
1148
  closeWidget,
1104
1149
  dispose: dispose$3,
1105
1150
  getLanguageId,
1106
1151
  getLines,
1107
- getOffsetAtCursor,
1152
+ getOffsetAtCursor: getOffsetAtCursor$1,
1108
1153
  getPositionAtCursor: getPositionAtCursor$1,
1109
1154
  getProblems,
1110
1155
  getSelections,
@@ -1112,7 +1157,7 @@ const EditorWorker = {
1112
1157
  getWordAt,
1113
1158
  getWordAtOffset2: getWordAtOffset2$1,
1114
1159
  getWordBefore,
1115
- invoke: invoke$2,
1160
+ invoke: invoke$3,
1116
1161
  invokeAndTransfer: invokeAndTransfer$2,
1117
1162
  registerMockRpc: registerMockRpc$1,
1118
1163
  sendMessagePortToExtensionHostWorker: sendMessagePortToExtensionHostWorker$1,
@@ -1121,22 +1166,22 @@ const EditorWorker = {
1121
1166
  };
1122
1167
 
1123
1168
  const {
1124
- invoke: invoke$1,
1169
+ invoke: invoke$2,
1125
1170
  invokeAndTransfer: invokeAndTransfer$1,
1126
1171
  set: set$3,
1127
1172
  dispose: dispose$2
1128
1173
  } = create$2(ExtensionHostWorker);
1129
1174
  const executeReferenceProvider = async (id, offset) => {
1130
1175
  // @ts-ignore
1131
- return invoke$1('ExtensionHostReference.executeReferenceProvider', id, offset);
1176
+ return invoke$2('ExtensionHostReference.executeReferenceProvider', id, offset);
1132
1177
  };
1133
1178
  const executeFileReferenceProvider = async id => {
1134
1179
  // @ts-ignore
1135
- return invoke$1('ExtensionHostReference.executeFileReferenceProvider', id);
1180
+ return invoke$2('ExtensionHostReference.executeFileReferenceProvider', id);
1136
1181
  };
1137
1182
  const getRuntimeStatus = async extensionId => {
1138
1183
  // @ts-ignore
1139
- return invoke$1('ExtensionHost.getRuntimeStatus', extensionId);
1184
+ return invoke$2('ExtensionHost.getRuntimeStatus', extensionId);
1140
1185
  };
1141
1186
  const registerMockRpc = commandMap => {
1142
1187
  const mockRpc = createMockRpc({
@@ -1152,14 +1197,14 @@ const ExtensionHost = {
1152
1197
  executeFileReferenceProvider,
1153
1198
  executeReferenceProvider,
1154
1199
  getRuntimeStatus,
1155
- invoke: invoke$1,
1200
+ invoke: invoke$2,
1156
1201
  invokeAndTransfer: invokeAndTransfer$1,
1157
1202
  registerMockRpc,
1158
1203
  set: set$3
1159
1204
  };
1160
1205
 
1161
1206
  const {
1162
- invoke,
1207
+ invoke: invoke$1,
1163
1208
  set: set$2,
1164
1209
  invokeAndTransfer,
1165
1210
  getWordAtOffset2
@@ -1175,7 +1220,7 @@ const close = async state => {
1175
1220
  editorUid
1176
1221
  } = state;
1177
1222
  // @ts-ignore
1178
- await invoke('Editor.closeWidget2', editorUid, Hover, 'Hovers', FocusEditorHovers);
1223
+ await invoke$1('Editor.closeWidget2', editorUid, Hover, 'Hovers', FocusEditorHovers);
1179
1224
  return state;
1180
1225
  };
1181
1226
 
@@ -1288,7 +1333,10 @@ const create = (uid, x, y, width, height, editorUid, editorLanguageId) => {
1288
1333
  hoverBorderRight: 1,
1289
1334
  hoverPaddingLeft: 8,
1290
1335
  hoverPaddingRight: 8,
1291
- hovverFullWidth: 400
1336
+ hoverFullWidth: 400,
1337
+ documentation: '',
1338
+ lineInfos: [],
1339
+ matchingDiagnostics: []
1292
1340
  };
1293
1341
  set$1(uid, state, state);
1294
1342
  };
@@ -1451,6 +1499,7 @@ const createExtensionHostRpc = async () => {
1451
1499
  };
1452
1500
 
1453
1501
  const {
1502
+ invoke,
1454
1503
  set
1455
1504
  } = ExtensionHost;
1456
1505
 
@@ -1459,27 +1508,180 @@ const initialize = async () => {
1459
1508
  set(rpc);
1460
1509
  };
1461
1510
 
1511
+ // @deprecated this doesn't work for variable width characters (unicode/emoji).
1512
+ // Use position computation in renderer process instead
1513
+
1514
+ /*
1515
+ * @param {object} editor
1516
+ * @param {number} rowIndex
1517
+ * @param {number} columnIndex
1518
+ * @returns
1519
+ */
1520
+ const x = (editor, rowIndex, columnIndex) => {
1521
+ const {
1522
+ columnWidth,
1523
+ x
1524
+ } = editor;
1525
+ const offsetX = columnIndex * columnWidth + x;
1526
+ return offsetX;
1527
+ };
1528
+ const y = (editor, rowIndex) => {
1529
+ const {
1530
+ rowHeight,
1531
+ y
1532
+ } = editor;
1533
+ const offsetY = (rowIndex + 1) * rowHeight + y;
1534
+ return offsetY;
1535
+ };
1536
+
1537
+ const getOffsetAtCursor = editorUid => {
1538
+ // TODO ask editor worker
1539
+ return 0;
1540
+ };
1541
+
1462
1542
  const getPositionAtCursor = async editorUid => {
1463
- const position = await invoke('Editor.getPositionAtCursor', editorUid);
1543
+ const position = await invoke$1('Editor.getPositionAtCursor', editorUid);
1464
1544
  return position;
1465
1545
  };
1466
1546
 
1547
+ const OnHover = 'onHover';
1548
+
1549
+ const HoverExecute = 'ExtensionHostHover.execute';
1550
+
1551
+ // TODO add tests for this
1552
+ const activateByEvent = async event => {
1553
+ // @ts-ignore
1554
+ await invoke$1('ActivateByEvent.activateByEvent', event);
1555
+ };
1556
+
1557
+ const execute = async ({
1558
+ editorLanguageId,
1559
+ editorUid,
1560
+ args,
1561
+ event,
1562
+ method,
1563
+ noProviderFoundMessage,
1564
+ noProviderFoundResult = undefined
1565
+ }) => {
1566
+ const fullEvent = `${event}:${editorLanguageId}`;
1567
+ await activateByEvent(fullEvent);
1568
+ const result = await invoke(method, editorUid, ...args);
1569
+ return result;
1570
+ };
1571
+
1572
+ const executeHoverProvider = async (editorUid, editorLanguageId, offset) => {
1573
+ number(editorUid);
1574
+ number(offset);
1575
+ return execute({
1576
+ event: OnHover,
1577
+ editorUid,
1578
+ method: HoverExecute,
1579
+ args: [offset],
1580
+ noProviderFoundMessage: 'No hover provider found'
1581
+ });
1582
+ };
1583
+
1584
+ const getHover = async (editorUid, editorLanguageId, offset) => {
1585
+ number(editorUid);
1586
+ number(offset);
1587
+ // TODO invoke extension host worker directly
1588
+ const hover = await executeHoverProvider(editorUid, editorLanguageId, offset);
1589
+ return hover;
1590
+ };
1591
+
1592
+ const measureTextBlockHeight = async (text, fontFamily, fontSize, lineHeight, width) => {
1593
+ // TODO ask editor worker or renderer process
1594
+ return 100;
1595
+ };
1596
+
1597
+ const tokenizeCodeBlock = async (codeBlock, languageId, tokenizerPath) => {
1598
+ // TODO maybe use syntax highlighting worker for this or another worker
1599
+ return [];
1600
+ };
1601
+
1602
+ const getMatchingDiagnostics = (diagnostics, rowIndex, columnIndex) => {
1603
+ const matching = [];
1604
+ for (const diagnostic of diagnostics) {
1605
+ if (diagnostic.rowIndex === rowIndex) {
1606
+ matching.push(diagnostic);
1607
+ }
1608
+ }
1609
+ return matching;
1610
+ };
1611
+ const getHoverPositionXy = (editor, rowIndex, wordStart, documentationHeight) => {
1612
+ const x$1 = x(editor, rowIndex, wordStart);
1613
+ const y$1 = editor.height - y(editor, rowIndex) + editor.y + 40;
1614
+ return {
1615
+ x: x$1,
1616
+ y: y$1
1617
+ };
1618
+ };
1619
+ const getEditorHoverInfo = async (editorUid, editorLanguageId, hoverFullWidth, hoverPaddingLeft, hoverPaddingRight, hoverBorderLeft, hoverBorderRight, hoverDocumentationFontFamily, hoverDocumentationFontSize, hoverDocumentationLineHeight, fallbackDisplayStringLanguageId) => {
1620
+ number(editorUid);
1621
+ const {
1622
+ rowIndex,
1623
+ columnIndex
1624
+ } = await getPositionAtCursor(editorUid);
1625
+ const offset = getOffsetAtCursor();
1626
+ const hover = await getHover(editorUid, editorLanguageId, offset);
1627
+ if (!hover) {
1628
+ return undefined;
1629
+ }
1630
+ const {
1631
+ documentation} = hover;
1632
+ const lineInfos = await tokenizeCodeBlock();
1633
+ const wordPart = await getWordBefore(editorUid, rowIndex, columnIndex);
1634
+ const wordStart = columnIndex - wordPart.length;
1635
+ await measureTextBlockHeight();
1636
+ // TODO
1637
+ const editor = {};
1638
+ const {
1639
+ x,
1640
+ y
1641
+ } = getHoverPositionXy(editor, rowIndex, wordStart);
1642
+ // @ts-ignore
1643
+ const diagnostics = editor.diagnostics || [];
1644
+ const matchingDiagnostics = getMatchingDiagnostics(diagnostics, rowIndex);
1645
+ return {
1646
+ lineInfos,
1647
+ documentation,
1648
+ x,
1649
+ y,
1650
+ matchingDiagnostics
1651
+ };
1652
+ };
1653
+
1467
1654
  const getWordAtOffset = getWordAtOffset2;
1468
1655
 
1469
1656
  const loadContent = async state => {
1470
1657
  const {
1471
- editorUid
1472
- } = state;
1658
+ editorLanguageId,
1659
+ editorUid} = state;
1473
1660
  const wordAtOffset = await getWordAtOffset(editorUid);
1474
1661
  const {
1475
1662
  x,
1476
1663
  y
1477
1664
  } = await getPositionAtCursor(editorUid);
1665
+ const info = await getEditorHoverInfo(editorUid, editorLanguageId);
1666
+ if (!info) {
1667
+ return {
1668
+ ...state
1669
+ };
1670
+ }
1671
+ const {
1672
+ documentation,
1673
+ lineInfos,
1674
+ matchingDiagnostics
1675
+ } = info;
1478
1676
  return {
1479
1677
  ...state,
1480
1678
  leadingWord: wordAtOffset,
1481
1679
  x,
1482
- y
1680
+ y,
1681
+ documentation,
1682
+ lineInfos,
1683
+ matchingDiagnostics,
1684
+ width: 300
1483
1685
  };
1484
1686
  };
1485
1687
 
@@ -1527,7 +1729,7 @@ const renderEventListeners = state => {
1527
1729
  };
1528
1730
 
1529
1731
  const renderFocusContext = (oldState, newState) => {
1530
- return [/* method */'Viewlet.setFocusContext', FocusEditorRename];
1732
+ return [SetFocusContext, FocusEditorRename];
1531
1733
  };
1532
1734
 
1533
1735
  const HoverDisplayString = 'HoverDisplayString';
@@ -1627,9 +1829,12 @@ const getHoverVirtualDom = (lineInfos, documentation, diagnostics) => {
1627
1829
 
1628
1830
  const renderItems = (oldState, newState) => {
1629
1831
  const {
1630
- uid
1832
+ uid,
1833
+ matchingDiagnostics,
1834
+ documentation,
1835
+ lineInfos
1631
1836
  } = newState;
1632
- const dom = getHoverVirtualDom([], {}, {});
1837
+ const dom = getHoverVirtualDom(lineInfos, documentation, matchingDiagnostics);
1633
1838
  return [/* method */'Viewlet.setDom2', uid, dom];
1634
1839
  };
1635
1840
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/hover-worker",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Web Worker for the find widget in Lvce Editor",
5
5
  "repository": {
6
6
  "type": "git",