11import { EditorView } from "@codemirror/view" ;
2- import { tsFacetWorker } from "../index.js" ;
2+ import { type HoverInfo , tsFacetWorker } from "../index.js" ;
3+
4+ export function defaultGotoHandler (
5+ currentPath : string ,
6+ hoverData : HoverInfo ,
7+ view : EditorView ,
8+ ) {
9+ const definition = hoverData ?. typeDef ?. at ( 0 ) ;
10+
11+ if ( definition ) {
12+ if ( currentPath === definition . fileName ) {
13+ const tr = view . state . update ( {
14+ selection : {
15+ anchor : definition . textSpan . start ,
16+ head : definition . textSpan . start + definition . textSpan . length ,
17+ } ,
18+ } ) ;
19+ view . dispatch ( tr ) ;
20+ }
21+ }
22+ }
23+
24+ type ToGoOptions = {
25+ gotoHandler ?: typeof defaultGotoHandler ;
26+ } ;
327
428/**
529 * Supports 'going to' a variable definition by meta or
630 * ctrl-clicking on it.
731 */
8- export function tsGotoWorker ( ) {
32+ export function tsGotoWorker (
33+ opts : ToGoOptions = { gotoHandler : defaultGotoHandler } ,
34+ ) {
935 return EditorView . domEventHandlers ( {
1036 click : ( event , view ) => {
1137 const config = view . state . facet ( tsFacetWorker ) ;
12- if ( ! config ?. worker ) return false ;
38+ if ( ! config ?. worker || ! opts . gotoHandler ) return false ;
1339
1440 // TODO: maybe this should be _just_ meta?
1541 // I think ctrl should probably be preserved.
@@ -29,18 +55,12 @@ export function tsGotoWorker() {
2955 pos,
3056 } )
3157 . then ( ( hoverData ) => {
32- const definition = hoverData ?. typeDef ?. at ( 0 ) ;
33-
34- if ( definition ) {
35- if ( config . path === definition . fileName ) {
36- const tr = view . state . update ( {
37- selection : {
38- anchor : definition . textSpan . start ,
39- head : definition . textSpan . start + definition . textSpan . length ,
40- } ,
41- } ) ;
42- view . dispatch ( tr ) ;
43- }
58+ // In reality, we enforced that opts.gotoHandler
59+ // is non-nullable earlier, but TypeScript knows
60+ // that in this callback, that theoretically could
61+ // have changed.
62+ if ( hoverData && opts . gotoHandler ) {
63+ opts . gotoHandler ( config . path , hoverData , view ) ;
4464 }
4565 } ) ;
4666
0 commit comments