list.vim 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. if !exists("g:go_list_type")
  2. let g:go_list_type = ""
  3. endif
  4. if !exists("g:go_list_type_commands")
  5. let g:go_list_type_commands = {}
  6. endif
  7. " Window opens the list with the given height up to 10 lines maximum.
  8. " Otherwise g:go_loclist_height is used.
  9. "
  10. " If no or zero height is given it closes the window by default.
  11. " To prevent this, set g:go_list_autoclose = 0
  12. function! go#list#Window(listtype, ...) abort
  13. " we don't use lwindow to close the location list as we need also the
  14. " ability to resize the window. So, we are going to use lopen and lclose
  15. " for a better user experience. If the number of errors in a current
  16. " location list increases/decreases, cwindow will not resize when a new
  17. " updated height is passed. lopen in the other hand resizes the screen.
  18. if !a:0 || a:1 == 0
  19. call go#list#Close(a:listtype)
  20. return
  21. endif
  22. let height = get(g:, "go_list_height", 0)
  23. if height == 0
  24. " prevent creating a large location height for a large set of numbers
  25. if a:1 > 10
  26. let height = 10
  27. else
  28. let height = a:1
  29. endif
  30. endif
  31. if a:listtype == "locationlist"
  32. exe 'lopen ' . height
  33. else
  34. exe 'copen ' . height
  35. endif
  36. endfunction
  37. " Get returns the current items from the list
  38. function! go#list#Get(listtype) abort
  39. if a:listtype == "locationlist"
  40. return getloclist(0)
  41. else
  42. return getqflist()
  43. endif
  44. endfunction
  45. " Populate populate the list with the given items
  46. function! go#list#Populate(listtype, items, title) abort
  47. if a:listtype == "locationlist"
  48. call setloclist(0, a:items, 'r')
  49. " The last argument ({what}) is introduced with 7.4.2200:
  50. " https://github.com/vim/vim/commit/d823fa910cca43fec3c31c030ee908a14c272640
  51. if has("patch-7.4.2200") | call setloclist(0, [], 'a', {'title': a:title}) | endif
  52. else
  53. call setqflist(a:items, 'r')
  54. if has("patch-7.4.2200") | call setqflist([], 'a', {'title': a:title}) | endif
  55. endif
  56. endfunction
  57. " Parse parses the given items based on the specified errorformat and
  58. " populates the list.
  59. function! go#list#ParseFormat(listtype, errformat, items, title) abort
  60. " backup users errorformat, will be restored once we are finished
  61. let old_errorformat = &errorformat
  62. " parse and populate the location list
  63. let &errorformat = a:errformat
  64. try
  65. call go#list#Parse(a:listtype, a:items, a:title)
  66. finally
  67. "restore back
  68. let &errorformat = old_errorformat
  69. endtry
  70. endfunction
  71. " Parse parses the given items based on the global errorformat and
  72. " populates the list.
  73. function! go#list#Parse(listtype, items, title) abort
  74. if a:listtype == "locationlist"
  75. lgetexpr a:items
  76. if has("patch-7.4.2200") | call setloclist(0, [], 'a', {'title': a:title}) | endif
  77. else
  78. cgetexpr a:items
  79. if has("patch-7.4.2200") | call setqflist([], 'a', {'title': a:title}) | endif
  80. endif
  81. endfunction
  82. " JumpToFirst jumps to the first item in the location list
  83. function! go#list#JumpToFirst(listtype) abort
  84. if a:listtype == "locationlist"
  85. ll 1
  86. else
  87. cc 1
  88. endif
  89. endfunction
  90. " Clean cleans and closes the location list
  91. function! go#list#Clean(listtype) abort
  92. if a:listtype == "locationlist"
  93. lex []
  94. else
  95. cex []
  96. endif
  97. call go#list#Close(a:listtype)
  98. endfunction
  99. " Close closes the location list
  100. function! go#list#Close(listtype) abort
  101. let autoclose_window = get(g:, 'go_list_autoclose', 1)
  102. if !autoclose_window
  103. return
  104. endif
  105. if a:listtype == "locationlist"
  106. lclose
  107. else
  108. cclose
  109. endif
  110. endfunction
  111. function! s:listtype(listtype) abort
  112. if g:go_list_type == "locationlist"
  113. return "locationlist"
  114. elseif g:go_list_type == "quickfix"
  115. return "quickfix"
  116. endif
  117. return a:listtype
  118. endfunction
  119. " s:default_list_type_commands is the defaults that will be used for each of
  120. " the supported commands (see documentation for g:go_list_type_commands). When
  121. " defining a default, quickfix should be used if the command operates on
  122. " multiple files, while locationlist should be used if the command operates on a
  123. " single file or buffer. Keys that begin with an underscore are not supported
  124. " in g:go_list_type_commands.
  125. let s:default_list_type_commands = {
  126. \ "GoBuild": "quickfix",
  127. \ "GoErrCheck": "quickfix",
  128. \ "GoFmt": "locationlist",
  129. \ "GoGenerate": "quickfix",
  130. \ "GoInstall": "quickfix",
  131. \ "GoLint": "quickfix",
  132. \ "GoMetaLinter": "quickfix",
  133. \ "GoMetaLinterAutoSave": "locationlist",
  134. \ "GoModifyTags": "locationlist",
  135. \ "GoRename": "quickfix",
  136. \ "GoRun": "quickfix",
  137. \ "GoTest": "quickfix",
  138. \ "GoVet": "quickfix",
  139. \ "_guru": "locationlist",
  140. \ "_term": "locationlist",
  141. \ "_job": "locationlist",
  142. \ }
  143. function! go#list#Type(for) abort
  144. let l:listtype = s:listtype(get(s:default_list_type_commands, a:for))
  145. if l:listtype == "0"
  146. call go#util#EchoError(printf(
  147. \ "unknown list type command value found ('%s'). Please open a bug report in the vim-go repo.",
  148. \ a:for))
  149. let l:listtype = "quickfix"
  150. endif
  151. return get(g:go_list_type_commands, a:for, l:listtype)
  152. endfunction
  153. " vim: sw=2 ts=2 et