-
Notifications
You must be signed in to change notification settings - Fork 21
/
useTooltipsExtension.tsx
213 lines (188 loc) · 6.1 KB
/
useTooltipsExtension.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { syntaxTree } from "@codemirror/language";
import { EditorView, hoverTooltip, repositionTooltips } from "@codemirror/view";
import { SyntaxNode } from "@lezer/common";
import { FC, PropsWithChildren, useEffect } from "react";
import { createRoot } from "react-dom/client";
import {
getFunctionDocumentation,
SqProject,
SqValue,
} from "@quri/squiggle-lang";
import { valueHasContext } from "../../lib/utility.js";
import { SquiggleValueChart } from "../SquiggleViewer/SquiggleValueChart.js";
import {
InnerViewerProvider,
useViewerContext,
} from "../SquiggleViewer/ViewerProvider.js";
import { FnDocumentation } from "../ui/FnDocumentation.js";
import { useReactiveExtension } from "./codemirrorHooks.js";
type Hover = NonNullable<ReturnType<typeof getFunctionDocumentation>>;
const TooltipBox: FC<PropsWithChildren<{ view: EditorView }>> = ({
view,
children,
}) => {
useEffect(() => {
// https://codemirror.net/docs/ref/#view.repositionTooltips needs to be called on each render.
repositionTooltips(view);
});
return (
<div className="border rounded-sm shadow-lg h-full overflow-y-auto">
{children}
</div>
);
};
const ValueTooltip: FC<{ value: SqValue; view: EditorView }> = ({
value,
view,
}) => {
const { globalSettings } = useViewerContext();
if (valueHasContext(value)) {
return (
<TooltipBox view={view}>
<div className="px-4 py-1">
{/* Force a standalone ephermeral ViewerProvider, so that we won't sync up collapsed state with the top-level viewer */}
<InnerViewerProvider
partialPlaygroundSettings={globalSettings}
viewerType="tooltip"
>
<SquiggleValueChart value={value} settings={globalSettings} />
</InnerViewerProvider>
</div>
</TooltipBox>
);
} else {
return null; // shouldn't happen
}
};
const HoverTooltip: FC<{ hover: Hover; view: EditorView }> = ({
hover,
view,
}) => (
<TooltipBox view={view}>
<div className="min-w-[200px] max-w-[600px] px-2">
<FnDocumentation documentation={hover} />
</div>
</TooltipBox>
);
// Based on https://codemirror.net/examples/tooltip/#hover-tooltips
// See also: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover
function buildWordHoverExtension({
project,
sourceId,
}: {
project: SqProject;
sourceId: string;
}) {
return hoverTooltip((view, pos, side) => {
const { doc } = view.state;
const tree = syntaxTree(view.state);
const cursor = tree.cursorAt(pos, side);
const getText = (node: SyntaxNode) => doc.sliceString(node.from, node.to);
const createBuiltinTooltip = (node: SyntaxNode) => {
const hover = getFunctionDocumentation(getText(node));
if (!hover) {
return null;
}
return {
pos: node.from,
end: node.to,
above: true,
create() {
const dom = document.createElement("div");
const root = createRoot(dom);
root.render(<HoverTooltip hover={hover} view={view} />);
return { dom };
},
};
};
const createTopLevelVariableNameTooltip = (
node: SyntaxNode,
value: SqValue
) => {
return {
pos: node.from,
end: node.to,
above: true,
create() {
const dom = document.createElement("div");
const root = createRoot(dom);
root.render(<ValueTooltip value={value} view={view} />);
return { dom };
},
};
};
switch (cursor.name) {
case "Identifier":
if (getText(cursor.node).match(/^[A-Z]/)) {
// TODO - expand the namespace to the identifier, or just show the namespace documentation
return null;
}
// TODO - check that the identifier is not overwritten by a local variable
return createBuiltinTooltip(cursor.node);
case "Field":
// `Namespace.function`; go up to fully identified name.
if (!cursor.parent()) {
return null;
}
return createBuiltinTooltip(cursor.node);
case "VariableName": {
const node = cursor.node;
// Let's find the statement that declares this variable.
if (!cursor.parent()) {
return null;
}
// Ascend through decorated statements.
while (cursor.type.is("Statement") && cursor.parent());
// Is this a top-level variable?
if (!cursor.type.is("Program")) {
return null;
}
const name = getText(node);
const bindings = project.getBindings(sourceId);
if (!bindings.ok) return null;
const value = bindings.value.get(name);
if (!value) return null;
// Should be LetStatement or DefunStatement
const valueAst = value.context?.valueAst;
if (
valueAst &&
(valueAst.type === "LetStatement" ||
valueAst.type === "DefunStatement") &&
// If these don't match then variable was probably shadowed by a later statement and we can't show its value.
// Or it could be caused by code rot, if we change the logic of how `valueAst` is computed, or add another statement type in AST.
// TODO - if we can prove that the variable was shadowed, show the tooltip pointing to the latest assignment.
valueAst.variable.location.start.offset === node.from &&
valueAst.variable.location.end.offset === node.to
) {
return createTopLevelVariableNameTooltip(node, value);
}
}
}
return null;
});
}
const tooltipTheme = EditorView.baseTheme({
".cm-tooltip-hover": {
backgroundColor: "white !important",
border: "0 !important",
},
".cm-tooltip-section": {
height: "100%", // necessary for scrolling, see also: "h-full" in `TooltipBox`
},
});
export function useTooltipsExtension(
view: EditorView | undefined,
{
project,
sourceId,
}: {
project: SqProject;
sourceId: string;
}
) {
return useReactiveExtension(
view,
() => [buildWordHoverExtension({ project, sourceId }), tooltipTheme],
[project, sourceId]
);
}