11let s: save_cpo = &cpoptions
22set cpoptions &vim
33
4- " Accepts a Funcref callback argument, to be called after the response is
5- " returned (synchronously or asynchronously) with a boolean 'found' result
4+ " Navigate to the definition of the symbol under the cursor.
5+ " Optional arguments:
6+ " Callback: When a callback is passed in, it is called after the response is
7+ " returned (synchronously or asynchronously) with the found
8+ " location and a flag for whether it is in a file in the project or
9+ " from the metadata. This is done instead of navigating to the found
10+ " location.
11+ " editcommand: The command to use to open buffers, e.g. 'split', 'vsplit',
12+ " 'tabedit' or 'edit' (default).
613function ! OmniSharp#actions#definition#Find (... ) abort
7- let opts = a: 0 && a: 1 isnot 0 ? { ' Callback' : a: 1 } : {}
8- if g: OmniSharp_server_stdio
14+ if a: 0 && type (a: 1 ) == type (function (' tr' ))
15+ let Callback = a: 1
16+ else
17+ let opts = { ' editcommand' : ' edit' }
18+ if a: 0 && type (a: 1 ) == type (' ' ) && a: 1 !=# ' '
19+ let opts.editcommand = a: 1
20+ endif
921 let Callback = function (' s:CBGotoDefinition' , [opts])
22+ endif
23+
24+ if g: OmniSharp_server_stdio
1025 call s: StdioFind (Callback)
1126 else
1227 let loc = OmniSharp#py#Eval (' gotoDefinition()' )
1328 if OmniSharp#py#CheckForError () | return 0 | endif
14- " Mock metadata info for old server based setups
15- return s: CBGotoDefinition (opts, loc , { ' MetadataSource ' : {}} )
29+ " We never come from metadata here
30+ return Callback ( loc , 0 )
1631 endif
1732endfunction
1833
19- function ! OmniSharp#actions#definition#Preview (... ) abort
20- let opts = a: 0 && a: 1 isnot 0 ? { ' Callback' : a: 1 } : {}
34+ function ! OmniSharp#actions#definition#Preview () abort
2135 if g: OmniSharp_server_stdio
22- let Callback = function (' s:CBPreviewDefinition' , [opts] )
36+ let Callback = function (' s:CBPreviewDefinition' )
2337 call s: StdioFind (Callback)
2438 else
2539 let loc = OmniSharp#py#Eval (' gotoDefinition()' )
2640 if OmniSharp#py#CheckForError () | return 0 | endif
27- call s: CBPreviewDefinition ({}, loc , {})
41+ " We never come from metadata here
42+ call s: CBPreviewDefinition (loc , 0 )
2843 endif
2944endfunction
3045
@@ -42,45 +57,74 @@ function! s:StdioFindRH(Callback, response) abort
4257 if ! a: response .Success | return | endif
4358 let body = a: response .Body
4459 if type (body) == type ({}) && get (body, ' FileName' , v: null ) != v: null
45- call a: Callback (OmniSharp#locations#Parse ([body])[0 ], body )
60+ call a: Callback (OmniSharp#locations#Parse ([body])[0 ], 0 )
4661 else
47- call a: Callback (0 , body)
62+ if g: OmniSharp_lookup_metadata
63+ \ && type (body) == type ({})
64+ \ && type (body.MetadataSource) == type ({})
65+ let Callback = function (' s:CBMetadataFind' , [a: Callback ])
66+ call s: StdioMetadataFind (Callback, body)
67+ else
68+ call a: Callback (0 , 1 )
69+ endif
4870 endif
4971endfunction
5072
51- function ! s: CBGotoDefinition (opts, location, metadata) abort
52- let went_to_metadata = 0
73+ function ! s: StdioMetadataFind (Callback, metadata) abort
74+ let opts = {
75+ \ ' ResponseHandler' : function (' s:StdioMetadataFindRH' , [a: Callback , a: metadata ]),
76+ \ ' Parameters' : a: metadata .MetadataSource
77+ \}
78+ call OmniSharp#stdio#Request (' /metadata' , opts)
79+ endfunction
80+
81+ function ! s: StdioMetadataFindRH (Callback, metadata, response) abort
82+ if ! a: response .Success || a: response .Body.Source == v: null | return 0 | endif
83+ call a: Callback (a: response .Body, a: metadata )
84+ endfunction
85+
86+ function ! s: CBMetadataFind (Callback, response, metadata) abort
87+ let host = OmniSharp#GetHost ()
88+ let metadata_filename = fnamemodify (
89+ \ OmniSharp#util#TranslatePathForClient (a: response .SourceName), ' :t' )
90+ let temp_file = OmniSharp#util#TempDir () . ' /' . metadata_filename
91+ let lines = split (a: response .Source , " \n " , 1 )
92+ let lines = map (lines , {i ,v - > substitute (v , ' \r' , ' ' , ' g' )})
93+ call writefile (lines , temp_file, ' b' )
94+ let bufnr = bufadd (temp_file)
95+ call setbufvar (bufnr , ' OmniSharp_host' , host)
96+ call setbufvar (bufnr , ' OmniSharp_metadata_filename' , a: response .SourceName)
97+ let location = {
98+ \ ' filename' : temp_file,
99+ \ ' lnum' : a: metadata .Line,
100+ \ ' col' : a: metadata .Column
101+ \}
102+ call a: Callback (location, 1 )
103+ endfunction
104+
105+ function ! s: CBGotoDefinition (opts, location, fromMetadata) abort
53106 if type (a: location ) != type ({}) " Check whether a dict was returned
54- if g: OmniSharp_lookup_metadata
55- \ && type (a: metadata ) == type ({})
56- \ && type (a: metadata .MetadataSource) == type ({})
57- let found = OmniSharp#actions#metadata#Find (0 , a: metadata , a: opts )
58- let went_to_metadata = 1
59- else
60- echo ' Not found'
61- let found = 0
62- endif
107+ echo ' Not found'
108+ let found = 0
63109 else
64- let found = OmniSharp#locations#Navigate (a: location , 0 )
65- endif
66- if has_key ( a: opts , ' Callback ' ) && ! went_to_metadata
67- call a: opts . Callback (found)
110+ let found = OmniSharp#locations#Navigate (a: location , get ( a: opts , ' editcommand ' , ' edit ' ) )
111+ if found && a: fromMetadata
112+ setlocal nomodifiable readonly
113+ endif
68114 endif
69115 return found
70116endfunction
71117
72- function ! s: CBPreviewDefinition (opts, location, metadata ) abort
118+ function ! s: CBPreviewDefinition (location, fromMetadata ) abort
73119 if type (a: location ) != type ({}) " Check whether a dict was returned
74- if g: OmniSharp_lookup_metadata
75- \ && type (a: metadata ) == type ({})
76- \ && type (a: metadata .MetadataSource) == type ({})
77- let found = OmniSharp#actions#metadata#Find (1 , a: metadata , a: opts )
78- else
79- echo ' Not found'
80- endif
120+ echo ' Not found'
81121 else
122+ let jumped_from_preview = &previewwindow
82123 call OmniSharp#locations#Preview (a: location )
83- echo fnamemodify (a: location .filename, ' :.' )
124+ echo OmniSharp#locations#Modify (a: location ).filename
125+ if a: fromMetadata && ! jumped_from_preview && &previewwindow
126+ silent wincmd p
127+ endif
84128 endif
85129endfunction
86130
0 commit comments