def.vim 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. let s:go_stack = []
  2. let s:go_stack_level = 0
  3. function! go#def#Jump(mode) abort
  4. let fname = fnamemodify(expand("%"), ':p:gs?\\?/?')
  5. " so guru right now is slow for some people. previously we were using
  6. " godef which also has it's own quirks. But this issue come up so many
  7. " times I've decided to support both. By default we still use guru as it
  8. " covers all edge cases, but now anyone can switch to godef if they wish
  9. let bin_name = get(g:, 'go_def_mode', 'guru')
  10. if bin_name == 'godef'
  11. if &modified
  12. " Write current unsaved buffer to a temp file and use the modified content
  13. let l:tmpname = tempname()
  14. call writefile(go#util#GetLines(), l:tmpname)
  15. let fname = l:tmpname
  16. endif
  17. let bin_path = go#path#CheckBinPath("godef")
  18. if empty(bin_path)
  19. return
  20. endif
  21. let command = printf("%s -f=%s -o=%s -t", go#util#Shellescape(bin_path),
  22. \ go#util#Shellescape(fname), go#util#OffsetCursor())
  23. let out = go#util#System(command)
  24. if exists("l:tmpname")
  25. call delete(l:tmpname)
  26. endif
  27. elseif bin_name == 'guru'
  28. let bin_path = go#path#CheckBinPath("guru")
  29. if empty(bin_path)
  30. return
  31. endif
  32. let cmd = [bin_path]
  33. let stdin_content = ""
  34. if &modified
  35. let content = join(go#util#GetLines(), "\n")
  36. let stdin_content = fname . "\n" . strlen(content) . "\n" . content
  37. call add(cmd, "-modified")
  38. endif
  39. if exists('g:go_build_tags')
  40. let tags = get(g:, 'go_build_tags')
  41. call extend(cmd, ["-tags", tags])
  42. endif
  43. let fname = fname.':#'.go#util#OffsetCursor()
  44. call extend(cmd, ["definition", fname])
  45. if go#util#has_job()
  46. let l:spawn_args = {
  47. \ 'cmd': cmd,
  48. \ 'complete': function('s:jump_to_declaration_cb', [a:mode, bin_name]),
  49. \ }
  50. if &modified
  51. let l:spawn_args.input = stdin_content
  52. endif
  53. call go#util#EchoProgress("searching declaration ...")
  54. call s:def_job(spawn_args)
  55. return
  56. endif
  57. let command = join(cmd, " ")
  58. if &modified
  59. let out = go#util#System(command, stdin_content)
  60. else
  61. let out = go#util#System(command)
  62. endif
  63. else
  64. call go#util#EchoError('go_def_mode value: '. bin_name .' is not valid. Valid values are: [godef, guru]')
  65. return
  66. endif
  67. if go#util#ShellError() != 0
  68. call go#util#EchoError(out)
  69. return
  70. endif
  71. call go#def#jump_to_declaration(out, a:mode, bin_name)
  72. endfunction
  73. function! s:jump_to_declaration_cb(mode, bin_name, job, exit_status, data) abort
  74. if a:exit_status != 0
  75. return
  76. endif
  77. call go#def#jump_to_declaration(a:data[0], a:mode, a:bin_name)
  78. call go#util#EchoSuccess(fnamemodify(a:data[0], ":t"))
  79. endfunction
  80. function! go#def#jump_to_declaration(out, mode, bin_name) abort
  81. let final_out = a:out
  82. if a:bin_name == "godef"
  83. " append the type information to the same line so our we can parse it.
  84. " This makes it compatible with guru output.
  85. let final_out = join(split(a:out, '\n'), ':')
  86. endif
  87. " strip line ending
  88. let out = split(final_out, go#util#LineEnding())[0]
  89. if go#util#IsWin()
  90. let parts = split(out, '\(^[a-zA-Z]\)\@<!:')
  91. else
  92. let parts = split(out, ':')
  93. endif
  94. let filename = parts[0]
  95. let line = parts[1]
  96. let col = parts[2]
  97. let ident = parts[3]
  98. " Remove anything newer than the current position, just like basic
  99. " vim tag support
  100. if s:go_stack_level == 0
  101. let s:go_stack = []
  102. else
  103. let s:go_stack = s:go_stack[0:s:go_stack_level-1]
  104. endif
  105. " increment the stack counter
  106. let s:go_stack_level += 1
  107. " push it on to the jumpstack
  108. let stack_entry = {'line': line("."), 'col': col("."), 'file': expand('%:p'), 'ident': ident}
  109. call add(s:go_stack, stack_entry)
  110. " needed for restoring back user setting this is because there are two
  111. " modes of switchbuf which we need based on the split mode
  112. let old_switchbuf = &switchbuf
  113. normal! m'
  114. if filename != fnamemodify(expand("%"), ':p:gs?\\?/?')
  115. " jump to existing buffer if, 1. we have enabled it, 2. the buffer is loaded
  116. " and 3. there is buffer window number we switch to
  117. if get(g:, 'go_def_reuse_buffer', 0) && bufloaded(filename) != 0 && bufwinnr(filename) != -1
  118. " jumpt to existing buffer if it exists
  119. execute bufwinnr(filename) . 'wincmd w'
  120. else
  121. if &modified
  122. let cmd = 'hide edit'
  123. else
  124. let cmd = 'edit'
  125. endif
  126. if a:mode == "tab"
  127. let &switchbuf = "useopen,usetab,newtab"
  128. if bufloaded(filename) == 0
  129. tab split
  130. else
  131. let cmd = 'sbuf'
  132. endif
  133. elseif a:mode == "split"
  134. split
  135. elseif a:mode == "vsplit"
  136. vsplit
  137. endif
  138. " open the file and jump to line and column
  139. exec cmd fnameescape(fnamemodify(filename, ':.'))
  140. endif
  141. endif
  142. call cursor(line, col)
  143. " also align the line to middle of the view
  144. normal! zz
  145. let &switchbuf = old_switchbuf
  146. endfunction
  147. function! go#def#SelectStackEntry() abort
  148. let target_window = go#ui#GetReturnWindow()
  149. if empty(target_window)
  150. let target_window = winnr()
  151. endif
  152. let highlighted_stack_entry = matchstr(getline("."), '^..\zs\(\d\+\)')
  153. if !empty(highlighted_stack_entry)
  154. execute target_window . "wincmd w"
  155. call go#def#Stack(str2nr(highlighted_stack_entry))
  156. endif
  157. call go#ui#CloseWindow()
  158. endfunction
  159. function! go#def#StackUI() abort
  160. if len(s:go_stack) == 0
  161. call go#util#EchoError("godef stack empty")
  162. return
  163. endif
  164. let stackOut = ['" <Up>,<Down>:navigate <Enter>:jump <Esc>,q:exit']
  165. let i = 0
  166. while i < len(s:go_stack)
  167. let entry = s:go_stack[i]
  168. let prefix = ""
  169. if i == s:go_stack_level
  170. let prefix = ">"
  171. else
  172. let prefix = " "
  173. endif
  174. call add(stackOut, printf("%s %d %s|%d col %d|%s",
  175. \ prefix, i+1, entry["file"], entry["line"], entry["col"], entry["ident"]))
  176. let i += 1
  177. endwhile
  178. if s:go_stack_level == i
  179. call add(stackOut, "> ")
  180. endif
  181. call go#ui#OpenWindow("GoDef Stack", stackOut, "godefstack")
  182. noremap <buffer> <silent> <CR> :<C-U>call go#def#SelectStackEntry()<CR>
  183. noremap <buffer> <silent> <Esc> :<C-U>call go#ui#CloseWindow()<CR>
  184. noremap <buffer> <silent> q :<C-U>call go#ui#CloseWindow()<CR>
  185. endfunction
  186. function! go#def#StackClear(...) abort
  187. let s:go_stack = []
  188. let s:go_stack_level = 0
  189. endfunction
  190. function! go#def#StackPop(...) abort
  191. if len(s:go_stack) == 0
  192. call go#util#EchoError("godef stack empty")
  193. return
  194. endif
  195. if s:go_stack_level == 0
  196. call go#util#EchoError("at bottom of the godef stack")
  197. return
  198. endif
  199. if !len(a:000)
  200. let numPop = 1
  201. else
  202. let numPop = a:1
  203. endif
  204. let newLevel = str2nr(s:go_stack_level) - str2nr(numPop)
  205. call go#def#Stack(newLevel + 1)
  206. endfunction
  207. function! go#def#Stack(...) abort
  208. if len(s:go_stack) == 0
  209. call go#util#EchoError("godef stack empty")
  210. return
  211. endif
  212. if !len(a:000)
  213. " Display interactive stack
  214. call go#def#StackUI()
  215. return
  216. else
  217. let jumpTarget = a:1
  218. endif
  219. if jumpTarget !~ '^\d\+$'
  220. if jumpTarget !~ '^\s*$'
  221. call go#util#EchoError("location must be a number")
  222. endif
  223. return
  224. endif
  225. let jumpTarget = str2nr(jumpTarget) - 1
  226. if jumpTarget >= 0 && jumpTarget < len(s:go_stack)
  227. let s:go_stack_level = jumpTarget
  228. let target = s:go_stack[s:go_stack_level]
  229. " jump
  230. if expand('%:p') != target["file"]
  231. if &modified
  232. exec 'hide edit' target["file"]
  233. else
  234. exec 'edit' target["file"]
  235. endif
  236. endif
  237. call cursor(target["line"], target["col"])
  238. normal! zz
  239. else
  240. call go#util#EchoError("invalid location. Try :GoDefStack to see the list of valid entries")
  241. endif
  242. endfunction
  243. function s:def_job(args) abort
  244. let callbacks = go#job#Spawn(a:args)
  245. let start_options = {
  246. \ 'callback': callbacks.callback,
  247. \ 'exit_cb': callbacks.exit_cb,
  248. \ 'close_cb': callbacks.close_cb,
  249. \ }
  250. if &modified
  251. let l:tmpname = tempname()
  252. call writefile(split(a:args.input, "\n"), l:tmpname, "b")
  253. let l:start_options.in_io = "file"
  254. let l:start_options.in_name = l:tmpname
  255. endif
  256. call job_start(a:args.cmd, start_options)
  257. endfunction
  258. " vim: sw=2 ts=2 et