Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

editor.ts

53 lines1.4 KB

1type OpenSourceData = {
2 type: 'open-source'
3 data: {
4 /** The source file to open */
5 source?: string
6 /** The react router route ID, usually discovered via the hook useMatches */
7 routeID?: string
8 /** The line number in the source file */
9 line?: number
10 /** The column number in the source file */
11 column?: number
12 }
14​
15export type EditorConfig = {
16 /** The name of the editor, used for debugging purposes */
17 name: string
18 /** Callback to open a file in the editor */
19 open: (
20 path: string,
21 lineNumber: string | undefined,
22 columnNumber?: string,
23 ) => Promise<void>
25​
26export const DEFAULT_EDITOR_CONFIG: EditorConfig = {
27 name: 'VSCode',
28 open: async (path, lineNumber, columnNumber) => {
29 const launch = (await import('launch-editor')).default
30 launch(
31 `${path}${lineNumber ? `:${lineNumber}` : ''}${columnNumber ? `:${columnNumber}` : ''}`,
32 undefined,
33 (filename, err) => {
34 console.warn(`Failed to open ${filename} in editor: ${err}`)
35 },
36 )
37 },
39​
40export const handleOpenSource = async ({
41 data,
42 openInEditor,
43}: {
44 data: OpenSourceData
45 openInEditor: EditorConfig['open']
46}) => {
47 const { source, line, column } = data.data
48 const lineNum = line ? `${line}` : undefined
49 const columnNum = column ? `${column}` : undefined
50 if (source) {
51 return openInEditor(source, lineNum, columnNum)
52 }