-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathindex.tsx
More file actions
318 lines (291 loc) · 11.2 KB
/
index.tsx
File metadata and controls
318 lines (291 loc) · 11.2 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { useBrowseNavigation } from "@/app/[domain]/browse/hooks/useBrowseNavigation";
import { KeyboardShortcutHint } from "@/app/components/keyboardShortcutHint";
import { useToast } from "@/components/hooks/use-toast";
import { Button } from "@/components/ui/button";
import { LoadingButton } from "@/components/ui/loading-button";
import { Separator } from "@/components/ui/separator";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { createAuditAction } from "@/ee/features/audit/actions";
import useCaptureEvent from "@/hooks/useCaptureEvent";
import { computePosition, flip, offset, shift, VirtualElement } from "@floating-ui/react";
import { ReactCodeMirrorRef } from "@uiw/react-codemirror";
import { Loader2 } from "lucide-react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { useHotkeys } from "react-hotkeys-hook";
import { SymbolDefinitionPreview } from "./symbolDefinitionPreview";
import { useHoveredOverSymbolInfo } from "./useHoveredOverSymbolInfo";
interface SymbolHoverPopupProps {
editorRef: ReactCodeMirrorRef;
language: string;
revisionName: string;
repoName: string;
fileName: string;
source: 'browse' | 'preview' | 'chat';
}
export const SymbolHoverPopup: React.FC<SymbolHoverPopupProps> = ({
editorRef,
revisionName,
language,
repoName,
fileName,
source,
}) => {
const ref = useRef<HTMLDivElement>(null);
const [isSticky, setIsSticky] = useState(false);
const { toast } = useToast();
const { navigateToPath } = useBrowseNavigation();
const captureEvent = useCaptureEvent();
const symbolInfo = useHoveredOverSymbolInfo({
editorRef,
isSticky,
revisionName,
language,
repoName,
});
// Positions the popup relative to the symbol
useEffect(() => {
if (!symbolInfo) {
return;
}
const virtualElement: VirtualElement = {
getBoundingClientRect: () => {
return symbolInfo.element.getBoundingClientRect();
}
}
if (ref.current) {
computePosition(virtualElement, ref.current, {
placement: 'top',
middleware: [
offset(2),
flip({
mainAxis: true,
crossAxis: false,
fallbackPlacements: ['bottom'],
boundary: editorRef.view?.dom,
padding: 20,
}),
shift({
padding: 5,
boundary: editorRef.view?.dom,
})
]
}).then(({ x, y }) => {
if (ref.current) {
ref.current.style.left = `${x}px`;
ref.current.style.top = `${y}px`;
}
})
}
}, [symbolInfo, editorRef]);
// Multiple symbol definitions can exist for the same symbol, but we can only navigate
// and display a preview of one. If the symbol definition exists in the current file,
// then we use that one, otherwise we fallback to the first definition in the list.
const previewedSymbolDefinition = useMemo(() => {
if (!symbolInfo?.symbolDefinitions || symbolInfo.symbolDefinitions.length === 0) {
return undefined;
}
const matchingDefinition = symbolInfo.symbolDefinitions.find(
(definition) => (
definition.fileName === fileName && definition.repoName === repoName
)
);
if (matchingDefinition) {
return matchingDefinition;
}
return symbolInfo.symbolDefinitions[0];
}, [fileName, repoName, symbolInfo?.symbolDefinitions]);
const onGotoDefinition = useCallback(() => {
if (
!symbolInfo ||
!symbolInfo.symbolDefinitions ||
!previewedSymbolDefinition
) {
return;
}
captureEvent('wa_goto_definition_pressed', {
source,
});
createAuditAction({
action: "user.performed_goto_definition",
metadata: {
message: symbolInfo.symbolName,
},
});
const {
fileName,
repoName,
revisionName,
language,
range: highlightRange,
} = previewedSymbolDefinition;
navigateToPath({
// Always navigate to the preview symbol definition.
repoName,
revisionName,
path: fileName,
pathType: 'blob',
highlightRange,
// If there are multiple definitions, we should open the Explore panel with the definitions.
...(symbolInfo.symbolDefinitions.length > 1 ? {
setBrowseState: {
selectedSymbolInfo: {
symbolName: symbolInfo.symbolName,
repoName,
revisionName,
language,
},
activeExploreMenuTab: "definitions",
isBottomPanelCollapsed: false,
}
} : {}),
});
}, [
captureEvent,
previewedSymbolDefinition,
navigateToPath,
source,
symbolInfo
]);
const onFindReferences = useCallback(() => {
if (!symbolInfo) {
return;
}
captureEvent('wa_find_references_pressed', {
source,
});
createAuditAction({
action: "user.performed_find_references",
metadata: {
message: symbolInfo.symbolName,
},
})
navigateToPath({
repoName,
revisionName,
path: fileName,
pathType: 'blob',
highlightRange: symbolInfo.range,
setBrowseState: {
selectedSymbolInfo: {
symbolName: symbolInfo.symbolName,
repoName,
revisionName,
language,
},
activeExploreMenuTab: "references",
isBottomPanelCollapsed: false,
}
})
}, [captureEvent, fileName, language, navigateToPath, repoName, revisionName, source, symbolInfo]);
// @todo: We should probably make the behaviour s.t., the ctrl / cmd key needs to be held
// down to navigate to the definition. We should also only show the underline when the key
// is held, hover is active, and we have found the symbol definition.
useEffect(() => {
if (!symbolInfo || !symbolInfo.symbolDefinitions) {
return;
}
symbolInfo.element.addEventListener("click", onGotoDefinition);
return () => {
symbolInfo.element.removeEventListener("click", onGotoDefinition);
}
}, [symbolInfo, onGotoDefinition]);
useHotkeys('alt+shift+f12', () => {
onFindReferences();
}, {
enableOnFormTags: true,
enableOnContentEditable: true,
description: "Open Explore Panel",
});
useHotkeys('alt+f12', () => {
if (!symbolInfo) {
return;
}
if (!symbolInfo.symbolDefinitions || symbolInfo.symbolDefinitions.length === 0) {
toast({
description: "No definition found for this symbol",
});
return;
}
onGotoDefinition();
}, {
enableOnFormTags: true,
enableOnContentEditable: true,
description: "Go to definition",
})
if (!symbolInfo) {
return null;
}
// We use a portal here to render the popup at the document body level.
// This avoids clipping issues that occur when the popup is rendered inside scrollable or overflow-hidden containers (like the editor or its parent).
// By rendering in a portal, the popup can be absolutely positioned anywhere in the viewport without being cut off by parent containers.
return createPortal(
<div
ref={ref}
className="absolute z-10 flex flex-col gap-2 bg-background border border-gray-300 dark:border-gray-700 rounded-md shadow-lg p-2 max-w-3xl"
onMouseOver={() => setIsSticky(true)}
onMouseOut={() => setIsSticky(false)}
>
{symbolInfo.isSymbolDefinitionsLoading ? (
<div className="flex flex-row items-center gap-2 text-sm">
<Loader2 className="w-4 h-4 animate-spin" />
Loading...
</div>
) : previewedSymbolDefinition ? (
<SymbolDefinitionPreview
symbolDefinition={previewedSymbolDefinition}
/>
) : (
<p className="text-sm font-medium text-muted-foreground">No hover info found</p>
)}
<Separator />
<div className="flex flex-row gap-2 mt-2">
<Tooltip delayDuration={500}>
<TooltipTrigger asChild>
<LoadingButton
loading={symbolInfo.isSymbolDefinitionsLoading}
disabled={!previewedSymbolDefinition}
variant="outline"
size="sm"
onClick={onGotoDefinition}
>
{
!symbolInfo.isSymbolDefinitionsLoading && !previewedSymbolDefinition ?
"No definition found" :
`Go to ${symbolInfo.symbolDefinitions && symbolInfo.symbolDefinitions.length > 1 ? "definitions" : "definition"}`
}
</LoadingButton>
</TooltipTrigger>
<TooltipContent
side="bottom"
className="flex flex-row items-center gap-2"
>
<KeyboardShortcutHint shortcut="alt+f12" />
<Separator orientation="vertical" className="h-4" />
<span>{`Go to ${symbolInfo.symbolDefinitions && symbolInfo.symbolDefinitions.length > 1 ? "definitions" : "definition"}`}</span>
</TooltipContent>
</Tooltip>
<Tooltip delayDuration={500}>
<TooltipTrigger asChild>
<Button
variant="outline"
size="sm"
onClick={onFindReferences}
>
Find references
</Button>
</TooltipTrigger>
<TooltipContent
side="bottom"
className="flex flex-row items-center gap-2"
>
<KeyboardShortcutHint shortcut="alt+shift+f12" />
<Separator orientation="vertical" className="h-4" />
<span>Find references</span>
</TooltipContent>
</Tooltip>
</div>
</div>,
document.body
);
};