1
1
let s: save_cpo = &cpoptions
2
2
set cpoptions &vim
3
3
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).
6
13
function ! 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
9
21
let Callback = function (' s:CBGotoDefinition' , [opts])
22
+ endif
23
+
24
+ if g: OmniSharp_server_stdio
10
25
call s: StdioFind (Callback)
11
26
else
12
27
let loc = OmniSharp#py#Eval (' gotoDefinition()' )
13
28
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 )
16
31
endif
17
32
endfunction
18
33
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
21
35
if g: OmniSharp_server_stdio
22
- let Callback = function (' s:CBPreviewDefinition' , [opts] )
36
+ let Callback = function (' s:CBPreviewDefinition' )
23
37
call s: StdioFind (Callback)
24
38
else
25
39
let loc = OmniSharp#py#Eval (' gotoDefinition()' )
26
40
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 )
28
43
endif
29
44
endfunction
30
45
@@ -42,45 +57,74 @@ function! s:StdioFindRH(Callback, response) abort
42
57
if ! a: response .Success | return | endif
43
58
let body = a: response .Body
44
59
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 )
46
61
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
48
70
endif
49
71
endfunction
50
72
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
53
106
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
63
109
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
68
114
endif
69
115
return found
70
116
endfunction
71
117
72
- function ! s: CBPreviewDefinition (opts, location, metadata ) abort
118
+ function ! s: CBPreviewDefinition (location, fromMetadata ) abort
73
119
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'
81
121
else
122
+ let jumped_from_preview = &previewwindow
82
123
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
84
128
endif
85
129
endfunction
86
130
0 commit comments