diagnostic_spec.lua 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local command = n.command
  4. local clear = n.clear
  5. local exec_lua = n.exec_lua
  6. local eq = t.eq
  7. local neq = t.neq
  8. local matches = t.matches
  9. local api = n.api
  10. local pcall_err = t.pcall_err
  11. local fn = n.fn
  12. describe('vim.diagnostic', function()
  13. before_each(function()
  14. clear()
  15. exec_lua(function()
  16. require('vim.diagnostic')
  17. local function make_diagnostic(msg, lnum, col, end_lnum, end_col, severity, source, code)
  18. return {
  19. lnum = lnum,
  20. col = col,
  21. end_lnum = end_lnum,
  22. end_col = end_col,
  23. message = msg,
  24. severity = severity,
  25. source = source,
  26. code = code,
  27. }
  28. end
  29. function _G.make_error(msg, lnum, col, end_lnum, end_col, source, code)
  30. return make_diagnostic(
  31. msg,
  32. lnum,
  33. col,
  34. end_lnum,
  35. end_col,
  36. vim.diagnostic.severity.ERROR,
  37. source,
  38. code
  39. )
  40. end
  41. function _G.make_warning(msg, lnum, col, end_lnum, end_col, source, code)
  42. return make_diagnostic(
  43. msg,
  44. lnum,
  45. col,
  46. end_lnum,
  47. end_col,
  48. vim.diagnostic.severity.WARN,
  49. source,
  50. code
  51. )
  52. end
  53. function _G.make_info(msg, lnum, col, end_lnum, end_col, source, code)
  54. return make_diagnostic(
  55. msg,
  56. lnum,
  57. col,
  58. end_lnum,
  59. end_col,
  60. vim.diagnostic.severity.INFO,
  61. source,
  62. code
  63. )
  64. end
  65. function _G.make_hint(msg, lnum, col, end_lnum, end_col, source, code)
  66. return make_diagnostic(
  67. msg,
  68. lnum,
  69. col,
  70. end_lnum,
  71. end_col,
  72. vim.diagnostic.severity.HINT,
  73. source,
  74. code
  75. )
  76. end
  77. function _G.count_diagnostics(bufnr, severity, namespace)
  78. return #vim.diagnostic.get(bufnr, { severity = severity, namespace = namespace })
  79. end
  80. function _G.count_extmarks(bufnr, namespace)
  81. local ns = vim.diagnostic.get_namespace(namespace)
  82. local extmarks = 0
  83. if ns.user_data.virt_text_ns then
  84. extmarks = extmarks
  85. + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.virt_text_ns, 0, -1, {})
  86. end
  87. if ns.user_data.underline_ns then
  88. extmarks = extmarks
  89. + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.underline_ns, 0, -1, {})
  90. end
  91. return extmarks
  92. end
  93. function _G.get_virt_text_extmarks(ns)
  94. ns = vim.diagnostic.get_namespace(ns)
  95. local virt_text_ns = ns.user_data.virt_text_ns
  96. return vim.api.nvim_buf_get_extmarks(
  97. _G.diagnostic_bufnr,
  98. virt_text_ns,
  99. 0,
  100. -1,
  101. { details = true }
  102. )
  103. end
  104. ---@param ns integer
  105. function _G.get_underline_extmarks(ns)
  106. ---@type integer
  107. local underline_ns = vim.diagnostic.get_namespace(ns).user_data.underline_ns
  108. return vim.api.nvim_buf_get_extmarks(
  109. _G.diagnostic_bufnr,
  110. underline_ns,
  111. 0,
  112. -1,
  113. { details = true }
  114. )
  115. end
  116. end)
  117. exec_lua(function()
  118. _G.diagnostic_ns = vim.api.nvim_create_namespace('diagnostic_spec')
  119. _G.other_ns = vim.api.nvim_create_namespace('other_namespace')
  120. _G.diagnostic_bufnr = vim.api.nvim_create_buf(true, false)
  121. local lines = { '1st line of text', '2nd line of text', 'wow', 'cool', 'more', 'lines' }
  122. vim.fn.bufload(_G.diagnostic_bufnr)
  123. vim.api.nvim_buf_set_lines(_G.diagnostic_bufnr, 0, 1, false, lines)
  124. end)
  125. end)
  126. it('creates highlight groups', function()
  127. command('runtime plugin/diagnostic.vim')
  128. eq({
  129. 'DiagnosticDeprecated',
  130. 'DiagnosticError',
  131. 'DiagnosticFloatingError',
  132. 'DiagnosticFloatingHint',
  133. 'DiagnosticFloatingInfo',
  134. 'DiagnosticFloatingOk',
  135. 'DiagnosticFloatingWarn',
  136. 'DiagnosticHint',
  137. 'DiagnosticInfo',
  138. 'DiagnosticOk',
  139. 'DiagnosticSignError',
  140. 'DiagnosticSignHint',
  141. 'DiagnosticSignInfo',
  142. 'DiagnosticSignOk',
  143. 'DiagnosticSignWarn',
  144. 'DiagnosticUnderlineError',
  145. 'DiagnosticUnderlineHint',
  146. 'DiagnosticUnderlineInfo',
  147. 'DiagnosticUnderlineOk',
  148. 'DiagnosticUnderlineWarn',
  149. 'DiagnosticUnnecessary',
  150. 'DiagnosticVirtualTextError',
  151. 'DiagnosticVirtualTextHint',
  152. 'DiagnosticVirtualTextInfo',
  153. 'DiagnosticVirtualTextOk',
  154. 'DiagnosticVirtualTextWarn',
  155. 'DiagnosticWarn',
  156. }, fn.getcompletion('Diagnostic', 'highlight'))
  157. end)
  158. it('retrieves diagnostics from all buffers and namespaces', function()
  159. local result = exec_lua(function()
  160. local other_bufnr = vim.api.nvim_create_buf(true, false)
  161. local lines = vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, -1, true)
  162. vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines)
  163. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  164. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  165. _G.make_error('Diagnostic #2', 2, 1, 2, 1),
  166. })
  167. vim.diagnostic.set(_G.other_ns, other_bufnr, {
  168. _G.make_error('Diagnostic #3', 3, 1, 3, 1),
  169. })
  170. return vim.diagnostic.get()
  171. end)
  172. eq(3, #result)
  173. eq(
  174. 2,
  175. exec_lua(function()
  176. return #vim.tbl_filter(function(d)
  177. return d.bufnr == _G.diagnostic_bufnr
  178. end, result)
  179. end)
  180. )
  181. eq('Diagnostic #1', result[1].message)
  182. end)
  183. it('removes diagnostics from the cache when a buffer is removed', function()
  184. eq(
  185. 2,
  186. exec_lua(function()
  187. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  188. local other_bufnr = vim.fn.bufadd('test | test')
  189. local lines = vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, -1, true)
  190. vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines)
  191. vim.cmd('bunload! ' .. other_bufnr)
  192. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  193. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  194. _G.make_error('Diagnostic #2', 2, 1, 2, 1),
  195. })
  196. vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, {
  197. _G.make_error('Diagnostic #3', 3, 1, 3, 1),
  198. })
  199. vim.api.nvim_set_current_buf(other_bufnr)
  200. vim.opt_local.buflisted = true
  201. vim.cmd('bwipeout!')
  202. return #vim.diagnostic.get()
  203. end)
  204. )
  205. eq(
  206. 2,
  207. exec_lua(function()
  208. vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
  209. vim.opt_local.buflisted = false
  210. return #vim.diagnostic.get()
  211. end)
  212. )
  213. eq(
  214. 0,
  215. exec_lua(function()
  216. vim.cmd('bwipeout!')
  217. return #vim.diagnostic.get()
  218. end)
  219. )
  220. end)
  221. it('removes diagnostic from stale cache on reset', function()
  222. local diagnostics = exec_lua(function()
  223. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  224. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  225. _G.make_error('Diagnostic #2', 2, 1, 2, 1),
  226. })
  227. vim.fn.bufadd('test | test')
  228. vim.cmd('noautocmd bwipeout! ' .. _G.diagnostic_bufnr)
  229. return vim.diagnostic.get(_G.diagnostic_bufnr)
  230. end)
  231. eq(2, #diagnostics)
  232. diagnostics = exec_lua(function()
  233. vim.diagnostic.reset()
  234. return vim.diagnostic.get()
  235. end)
  236. eq(0, #diagnostics)
  237. end)
  238. it('always returns a copy of diagnostic tables', function()
  239. local result = exec_lua(function()
  240. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  241. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  242. })
  243. local diag = vim.diagnostic.get()
  244. diag[1].col = 10000
  245. return vim.diagnostic.get()[1].col == 10000
  246. end)
  247. eq(false, result)
  248. end)
  249. it('resolves buffer number 0 to the current buffer', function()
  250. eq(
  251. 2,
  252. exec_lua(function()
  253. vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
  254. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  255. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  256. _G.make_error('Diagnostic #2', 2, 1, 2, 1),
  257. })
  258. return #vim.diagnostic.get(0)
  259. end)
  260. )
  261. end)
  262. it('saves and count a single error', function()
  263. eq(
  264. 1,
  265. exec_lua(function()
  266. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  267. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  268. })
  269. return _G.count_diagnostics(
  270. _G.diagnostic_bufnr,
  271. vim.diagnostic.severity.ERROR,
  272. _G.diagnostic_ns
  273. )
  274. end)
  275. )
  276. end)
  277. it('saves and count multiple errors', function()
  278. eq(
  279. 2,
  280. exec_lua(function()
  281. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  282. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  283. _G.make_error('Diagnostic #2', 2, 1, 2, 1),
  284. })
  285. return _G.count_diagnostics(
  286. _G.diagnostic_bufnr,
  287. vim.diagnostic.severity.ERROR,
  288. _G.diagnostic_ns
  289. )
  290. end)
  291. )
  292. end)
  293. it('saves and count from multiple namespaces', function()
  294. eq(
  295. { 1, 1, 2 },
  296. exec_lua(function()
  297. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  298. _G.make_error('Diagnostic From Server 1', 1, 1, 1, 1),
  299. })
  300. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
  301. _G.make_error('Diagnostic From Server 2', 1, 1, 1, 1),
  302. })
  303. return {
  304. -- First namespace
  305. _G.count_diagnostics(
  306. _G.diagnostic_bufnr,
  307. vim.diagnostic.severity.ERROR,
  308. _G.diagnostic_ns
  309. ),
  310. -- Second namespace
  311. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.other_ns),
  312. -- All namespaces
  313. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR),
  314. }
  315. end)
  316. )
  317. end)
  318. it('saves and count from multiple namespaces with respect to severity', function()
  319. eq(
  320. { 3, 0, 3 },
  321. exec_lua(function()
  322. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  323. _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
  324. _G.make_error('Diagnostic From Server 1:2', 2, 2, 2, 2),
  325. _G.make_error('Diagnostic From Server 1:3', 2, 3, 3, 2),
  326. })
  327. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
  328. _G.make_warning('Warning From Server 2', 3, 3, 3, 3),
  329. })
  330. return {
  331. -- Namespace 1
  332. _G.count_diagnostics(
  333. _G.diagnostic_bufnr,
  334. vim.diagnostic.severity.ERROR,
  335. _G.diagnostic_ns
  336. ),
  337. -- Namespace 2
  338. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.other_ns),
  339. -- All namespaces
  340. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR),
  341. }
  342. end)
  343. )
  344. end)
  345. it('handles one namespace clearing highlights while the other still has highlights', function()
  346. -- 1 Error (1)
  347. -- 1 Warning (2)
  348. -- 1 Warning (2) + 1 Warning (1)
  349. -- 2 highlights and 2 underlines (since error)
  350. -- 1 highlight + 1 underline
  351. local all_highlights = { 1, 1, 2, 4, 2 }
  352. eq(
  353. all_highlights,
  354. exec_lua(function()
  355. local ns_1_diags = {
  356. _G.make_error('Error 1', 1, 1, 1, 5),
  357. _G.make_warning('Warning on Server 1', 2, 1, 2, 3),
  358. }
  359. local ns_2_diags = {
  360. _G.make_warning('Warning 1', 2, 1, 2, 3),
  361. }
  362. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
  363. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
  364. return {
  365. _G.count_diagnostics(
  366. _G.diagnostic_bufnr,
  367. vim.diagnostic.severity.ERROR,
  368. _G.diagnostic_ns
  369. ),
  370. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
  371. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
  372. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
  373. _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
  374. }
  375. end)
  376. )
  377. -- Clear diagnostics from namespace 1, and make sure we have the right amount of stuff for namespace 2
  378. eq(
  379. { 1, 1, 2, 0, 2 },
  380. exec_lua(function()
  381. vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
  382. return {
  383. _G.count_diagnostics(
  384. _G.diagnostic_bufnr,
  385. vim.diagnostic.severity.ERROR,
  386. _G.diagnostic_ns
  387. ),
  388. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
  389. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
  390. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
  391. _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
  392. }
  393. end)
  394. )
  395. -- Show diagnostics from namespace 1 again
  396. eq(
  397. all_highlights,
  398. exec_lua(function()
  399. vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
  400. return {
  401. _G.count_diagnostics(
  402. _G.diagnostic_bufnr,
  403. vim.diagnostic.severity.ERROR,
  404. _G.diagnostic_ns
  405. ),
  406. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
  407. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
  408. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
  409. _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
  410. }
  411. end)
  412. )
  413. end)
  414. it('does not display diagnostics when disabled', function()
  415. eq(
  416. { 0, 2 },
  417. exec_lua(function()
  418. local ns_1_diags = {
  419. _G.make_error('Error 1', 1, 1, 1, 5),
  420. _G.make_warning('Warning on Server 1', 2, 1, 2, 3),
  421. }
  422. local ns_2_diags = {
  423. _G.make_warning('Warning 1', 2, 1, 2, 3),
  424. }
  425. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
  426. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
  427. vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
  428. return {
  429. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
  430. _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
  431. }
  432. end)
  433. )
  434. eq(
  435. { 4, 0 },
  436. exec_lua(function()
  437. vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
  438. vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.other_ns })
  439. return {
  440. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
  441. _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
  442. }
  443. end)
  444. )
  445. end)
  446. describe('show() and hide()', function()
  447. it('works', function()
  448. local result = exec_lua(function()
  449. local other_bufnr = vim.api.nvim_create_buf(true, false)
  450. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  451. local result = {}
  452. vim.diagnostic.config({ underline = false, virtual_text = true })
  453. local ns_1_diags = {
  454. _G.make_error('Error 1', 1, 1, 1, 5),
  455. _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
  456. }
  457. local ns_2_diags = {
  458. _G.make_warning('Warning 1', 2, 1, 2, 5),
  459. }
  460. local other_buffer_diags = {
  461. _G.make_info('This is interesting', 0, 0, 0, 0),
  462. }
  463. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
  464. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
  465. vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
  466. -- All buffers and namespaces
  467. table.insert(
  468. result,
  469. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  470. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  471. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  472. )
  473. -- Hide one namespace
  474. vim.diagnostic.hide(_G.diagnostic_ns)
  475. table.insert(
  476. result,
  477. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  478. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  479. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  480. )
  481. -- Show one namespace
  482. vim.diagnostic.show(_G.diagnostic_ns)
  483. table.insert(
  484. result,
  485. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  486. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  487. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  488. )
  489. -- Hide one buffer
  490. vim.diagnostic.hide(nil, other_bufnr)
  491. table.insert(
  492. result,
  493. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  494. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  495. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  496. )
  497. -- Hide everything
  498. vim.diagnostic.hide()
  499. table.insert(
  500. result,
  501. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  502. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  503. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  504. )
  505. -- Show one buffer
  506. vim.diagnostic.show(nil, _G.diagnostic_bufnr)
  507. table.insert(
  508. result,
  509. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  510. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  511. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  512. )
  513. return result
  514. end)
  515. eq(4, result[1])
  516. eq(1, result[2])
  517. eq(4, result[3])
  518. eq(3, result[4])
  519. eq(0, result[5])
  520. eq(3, result[6])
  521. end)
  522. it("doesn't error after bwipeout on buffer", function()
  523. exec_lua(function()
  524. vim.diagnostic.set(
  525. _G.diagnostic_ns,
  526. _G.diagnostic_bufnr,
  527. { { lnum = 0, end_lnum = 0, col = 0, end_col = 0 } }
  528. )
  529. vim.cmd('bwipeout! ' .. _G.diagnostic_bufnr)
  530. vim.diagnostic.show(_G.diagnostic_ns)
  531. vim.diagnostic.hide(_G.diagnostic_ns)
  532. end)
  533. end)
  534. end)
  535. describe('enable() and disable()', function()
  536. it('validation', function()
  537. matches('expected boolean, got table', pcall_err(exec_lua, [[vim.diagnostic.enable({})]]))
  538. matches(
  539. 'filter: expected table, got string',
  540. pcall_err(exec_lua, [[vim.diagnostic.enable(false, '')]])
  541. )
  542. matches(
  543. 'Invalid buffer id: 42',
  544. pcall_err(exec_lua, [[vim.diagnostic.enable(true, { bufnr = 42 })]])
  545. )
  546. matches(
  547. 'expected boolean, got number',
  548. pcall_err(exec_lua, [[vim.diagnostic.enable(42, {})]])
  549. )
  550. matches('expected boolean, got table', pcall_err(exec_lua, [[vim.diagnostic.enable({}, 42)]]))
  551. -- Deprecated signature.
  552. matches('Invalid buffer id: 42', pcall_err(exec_lua, [[vim.diagnostic.enable(42)]]))
  553. -- Deprecated signature.
  554. matches(
  555. 'namespace does not exist or is anonymous',
  556. pcall_err(exec_lua, [[vim.diagnostic.enable(nil, 42)]])
  557. )
  558. end)
  559. it('without arguments', function()
  560. local result = exec_lua(function()
  561. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  562. local result = {}
  563. vim.diagnostic.config({ underline = false, virtual_text = true })
  564. local ns_1_diags = {
  565. _G.make_error('Error 1', 1, 1, 1, 5),
  566. _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
  567. }
  568. local ns_2_diags = {
  569. _G.make_warning('Warning 1', 2, 1, 2, 5),
  570. }
  571. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
  572. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
  573. table.insert(
  574. result,
  575. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  576. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  577. )
  578. vim.diagnostic.enable(false)
  579. table.insert(
  580. result,
  581. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  582. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  583. )
  584. -- Create a new buffer
  585. local other_bufnr = vim.api.nvim_create_buf(true, false)
  586. local other_buffer_diags = {
  587. _G.make_info('This is interesting', 0, 0, 0, 0),
  588. }
  589. vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
  590. table.insert(
  591. result,
  592. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  593. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  594. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  595. )
  596. vim.diagnostic.enable()
  597. table.insert(
  598. result,
  599. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  600. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  601. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  602. )
  603. return result
  604. end)
  605. eq(3, result[1])
  606. eq(0, result[2])
  607. eq(0, result[3])
  608. eq(4, result[4])
  609. end)
  610. it('with buffer argument', function()
  611. local result = exec_lua(function()
  612. local other_bufnr = vim.api.nvim_create_buf(true, false)
  613. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  614. local result = {}
  615. vim.diagnostic.config({ underline = false, virtual_text = true })
  616. local ns_1_diags = {
  617. _G.make_error('Error 1', 1, 1, 1, 5),
  618. _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
  619. }
  620. local ns_2_diags = {
  621. _G.make_warning('Warning 1', 2, 1, 2, 5),
  622. }
  623. local other_buffer_diags = {
  624. _G.make_info('This is interesting', 0, 0, 0, 0),
  625. }
  626. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
  627. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
  628. vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
  629. table.insert(
  630. result,
  631. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  632. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  633. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  634. )
  635. vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr })
  636. table.insert(
  637. result,
  638. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  639. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  640. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  641. )
  642. vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr })
  643. table.insert(
  644. result,
  645. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  646. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  647. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  648. )
  649. vim.diagnostic.enable(false, { bufnr = other_bufnr })
  650. table.insert(
  651. result,
  652. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  653. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  654. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  655. )
  656. return result
  657. end)
  658. eq(4, result[1])
  659. eq(1, result[2])
  660. eq(4, result[3])
  661. eq(3, result[4])
  662. end)
  663. it('with a namespace argument', function()
  664. local result = exec_lua(function()
  665. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  666. local result = {}
  667. vim.diagnostic.config({ underline = false, virtual_text = true })
  668. local ns_1_diags = {
  669. _G.make_error('Error 1', 1, 1, 1, 5),
  670. _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
  671. }
  672. local ns_2_diags = {
  673. _G.make_warning('Warning 1', 2, 1, 2, 5),
  674. }
  675. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
  676. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
  677. table.insert(
  678. result,
  679. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  680. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  681. )
  682. vim.diagnostic.enable(false, { ns_id = _G.diagnostic_ns })
  683. table.insert(
  684. result,
  685. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  686. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  687. )
  688. vim.diagnostic.enable(true, { ns_id = _G.diagnostic_ns })
  689. table.insert(
  690. result,
  691. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  692. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  693. )
  694. vim.diagnostic.enable(false, { ns_id = _G.other_ns })
  695. table.insert(
  696. result,
  697. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  698. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  699. )
  700. return result
  701. end)
  702. eq(3, result[1])
  703. eq(1, result[2])
  704. eq(3, result[3])
  705. eq(2, result[4])
  706. end)
  707. --- @return table
  708. local function test_enable(legacy)
  709. return exec_lua(function()
  710. local other_bufnr = vim.api.nvim_create_buf(true, false)
  711. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  712. local result = {}
  713. vim.diagnostic.config({ underline = false, virtual_text = true })
  714. local ns_1_diags = {
  715. _G.make_error('Error 1', 1, 1, 1, 5),
  716. _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
  717. }
  718. local ns_2_diags = {
  719. _G.make_warning('Warning 1', 2, 1, 2, 5),
  720. }
  721. local other_buffer_diags = {
  722. _G.make_info('This is interesting', 0, 0, 0, 0),
  723. }
  724. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
  725. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
  726. vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
  727. table.insert(
  728. result,
  729. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  730. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  731. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  732. )
  733. if legacy then
  734. vim.diagnostic.disable(_G.diagnostic_bufnr, _G.diagnostic_ns)
  735. else
  736. vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
  737. end
  738. table.insert(
  739. result,
  740. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  741. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  742. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  743. )
  744. if legacy then
  745. vim.diagnostic.disable(_G.diagnostic_bufnr, _G.other_ns)
  746. else
  747. vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.other_ns })
  748. end
  749. table.insert(
  750. result,
  751. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  752. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  753. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  754. )
  755. if legacy then
  756. vim.diagnostic.enable(_G.diagnostic_bufnr, _G.diagnostic_ns)
  757. else
  758. vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
  759. end
  760. table.insert(
  761. result,
  762. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  763. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  764. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  765. )
  766. if legacy then
  767. -- Should have no effect
  768. vim.diagnostic.disable(other_bufnr, _G.other_ns)
  769. else
  770. -- Should have no effect
  771. vim.diagnostic.enable(false, { bufnr = other_bufnr, ns_id = _G.other_ns })
  772. end
  773. table.insert(
  774. result,
  775. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  776. + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
  777. + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
  778. )
  779. return result
  780. end)
  781. end
  782. it('with both buffer and namespace arguments', function()
  783. local result = test_enable(false)
  784. eq(4, result[1])
  785. eq(2, result[2])
  786. eq(1, result[3])
  787. eq(3, result[4])
  788. eq(3, result[5])
  789. end)
  790. it('with both buffer and namespace arguments (deprecated signature)', function()
  791. -- Exercise the legacy/deprecated signature.
  792. local result = test_enable(true)
  793. eq(4, result[1])
  794. eq(2, result[2])
  795. eq(1, result[3])
  796. eq(3, result[4])
  797. eq(3, result[5])
  798. end)
  799. end)
  800. describe('reset()', function()
  801. it('diagnostic count is 0 and displayed diagnostics are 0 after call', function()
  802. -- 1 Error (1)
  803. -- 1 Warning (2)
  804. -- 1 Warning (2) + 1 Warning (1)
  805. -- 2 highlights and 2 underlines (since error)
  806. -- 1 highlight + 1 underline
  807. local all_highlights = { 1, 1, 2, 4, 2 }
  808. eq(
  809. all_highlights,
  810. exec_lua(function()
  811. local ns_1_diags = {
  812. _G.make_error('Error 1', 1, 1, 1, 5),
  813. _G.make_warning('Warning on Server 1', 2, 1, 2, 3),
  814. }
  815. local ns_2_diags = {
  816. _G.make_warning('Warning 1', 2, 1, 2, 3),
  817. }
  818. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
  819. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
  820. return {
  821. _G.count_diagnostics(
  822. _G.diagnostic_bufnr,
  823. vim.diagnostic.severity.ERROR,
  824. _G.diagnostic_ns
  825. ),
  826. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
  827. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
  828. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
  829. _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
  830. }
  831. end)
  832. )
  833. -- Reset diagnostics from namespace 1
  834. exec_lua([[ vim.diagnostic.reset( _G.diagnostic_ns) ]])
  835. -- Make sure we have the right diagnostic count
  836. eq(
  837. { 0, 1, 1, 0, 2 },
  838. exec_lua(function()
  839. local diagnostic_count = {}
  840. vim.wait(100, function()
  841. diagnostic_count = {
  842. _G.count_diagnostics(
  843. _G.diagnostic_bufnr,
  844. vim.diagnostic.severity.ERROR,
  845. _G.diagnostic_ns
  846. ),
  847. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
  848. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
  849. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
  850. _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
  851. }
  852. end)
  853. return diagnostic_count
  854. end)
  855. )
  856. -- Reset diagnostics from namespace 2
  857. exec_lua([[ vim.diagnostic.reset(_G.other_ns) ]])
  858. -- Make sure we have the right diagnostic count
  859. eq(
  860. { 0, 0, 0, 0, 0 },
  861. exec_lua(function()
  862. local diagnostic_count = {}
  863. vim.wait(100, function()
  864. diagnostic_count = {
  865. _G.count_diagnostics(
  866. _G.diagnostic_bufnr,
  867. vim.diagnostic.severity.ERROR,
  868. _G.diagnostic_ns
  869. ),
  870. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
  871. _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
  872. _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
  873. _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
  874. }
  875. end)
  876. return diagnostic_count
  877. end)
  878. )
  879. end)
  880. it("doesn't error after bwipeout called on buffer", function()
  881. exec_lua(function()
  882. vim.diagnostic.set(
  883. _G.diagnostic_ns,
  884. _G.diagnostic_bufnr,
  885. { { lnum = 0, end_lnum = 0, col = 0, end_col = 0 } }
  886. )
  887. vim.cmd('bwipeout! ' .. _G.diagnostic_bufnr)
  888. vim.diagnostic.reset(_G.diagnostic_ns)
  889. end)
  890. end)
  891. end)
  892. describe('get_next()', function()
  893. it('can find the next pos with only one namespace', function()
  894. eq(
  895. { 1, 1 },
  896. exec_lua(function()
  897. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  898. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  899. })
  900. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  901. local next = vim.diagnostic.get_next()
  902. return { next.lnum, next.col }
  903. end)
  904. )
  905. end)
  906. it('can find next pos with two errors', function()
  907. eq(
  908. { 4, 4 },
  909. exec_lua(function()
  910. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  911. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  912. _G.make_error('Diagnostic #2', 4, 4, 4, 4),
  913. })
  914. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  915. vim.api.nvim_win_set_cursor(0, { 3, 1 })
  916. local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
  917. return { next.lnum, next.col }
  918. end)
  919. )
  920. end)
  921. it('can cycle when position is past error', function()
  922. eq(
  923. { 1, 1 },
  924. exec_lua(function()
  925. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  926. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  927. })
  928. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  929. vim.api.nvim_win_set_cursor(0, { 3, 1 })
  930. local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
  931. return { next.lnum, next.col }
  932. end)
  933. )
  934. end)
  935. it('will not cycle when wrap is off', function()
  936. eq(
  937. nil,
  938. exec_lua(function()
  939. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  940. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  941. })
  942. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  943. vim.api.nvim_win_set_cursor(0, { 3, 1 })
  944. local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns, wrap = false })
  945. return next
  946. end)
  947. )
  948. end)
  949. it('can cycle even from the last line', function()
  950. eq(
  951. { 4, 4 },
  952. exec_lua(function()
  953. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  954. _G.make_error('Diagnostic #2', 4, 4, 4, 4),
  955. })
  956. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  957. vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(0), 1 })
  958. local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns })
  959. return { prev.lnum, prev.col }
  960. end)
  961. )
  962. end)
  963. it('works with diagnostics past the end of the line #16349', function()
  964. eq(
  965. { 4, 0 },
  966. exec_lua(function()
  967. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  968. _G.make_error('Diagnostic #1', 3, 9001, 3, 9001),
  969. _G.make_error('Diagnostic #2', 4, 0, 4, 0),
  970. })
  971. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  972. vim.api.nvim_win_set_cursor(0, { 1, 1 })
  973. vim.diagnostic.jump({ count = 1, float = false })
  974. local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
  975. return { next.lnum, next.col }
  976. end)
  977. )
  978. end)
  979. it('works with diagnostics before the start of the line', function()
  980. eq(
  981. { 4, 0 },
  982. exec_lua(function()
  983. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  984. _G.make_error('Diagnostic #1', 3, 9001, 3, 9001),
  985. _G.make_error('Diagnostic #2', 4, -1, 4, -1),
  986. })
  987. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  988. vim.api.nvim_win_set_cursor(0, { 1, 1 })
  989. vim.diagnostic.jump({ count = 1, float = false })
  990. local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
  991. return { next.lnum, next.col }
  992. end)
  993. )
  994. end)
  995. it('jumps to diagnostic with highest severity', function()
  996. exec_lua(function()
  997. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  998. _G.make_info('Info', 1, 0, 1, 1),
  999. _G.make_error('Error', 2, 0, 2, 1),
  1000. _G.make_warning('Warning', 3, 0, 3, 1),
  1001. _G.make_error('Error', 4, 0, 4, 1),
  1002. })
  1003. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  1004. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  1005. end)
  1006. eq(
  1007. { 3, 0 },
  1008. exec_lua(function()
  1009. vim.diagnostic.jump({ count = 1, _highest = true })
  1010. return vim.api.nvim_win_get_cursor(0)
  1011. end)
  1012. )
  1013. eq(
  1014. { 5, 0 },
  1015. exec_lua(function()
  1016. vim.diagnostic.jump({ count = 1, _highest = true })
  1017. return vim.api.nvim_win_get_cursor(0)
  1018. end)
  1019. )
  1020. exec_lua(function()
  1021. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1022. _G.make_info('Info', 1, 0, 1, 1),
  1023. _G.make_hint('Hint', 2, 0, 2, 1),
  1024. _G.make_warning('Warning', 3, 0, 3, 1),
  1025. _G.make_hint('Hint', 4, 0, 4, 1),
  1026. _G.make_warning('Warning', 5, 0, 5, 1),
  1027. })
  1028. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  1029. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  1030. end)
  1031. eq(
  1032. { 4, 0 },
  1033. exec_lua(function()
  1034. vim.diagnostic.jump({ count = 1, _highest = true })
  1035. return vim.api.nvim_win_get_cursor(0)
  1036. end)
  1037. )
  1038. eq(
  1039. { 6, 0 },
  1040. exec_lua(function()
  1041. vim.diagnostic.jump({ count = 1, _highest = true })
  1042. return vim.api.nvim_win_get_cursor(0)
  1043. end)
  1044. )
  1045. end)
  1046. it('jumps to next diagnostic if severity is non-nil', function()
  1047. exec_lua(function()
  1048. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1049. _G.make_info('Info', 1, 0, 1, 1),
  1050. _G.make_error('Error', 2, 0, 2, 1),
  1051. _G.make_warning('Warning', 3, 0, 3, 1),
  1052. _G.make_error('Error', 4, 0, 4, 1),
  1053. })
  1054. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  1055. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  1056. end)
  1057. eq(
  1058. { 2, 0 },
  1059. exec_lua(function()
  1060. vim.diagnostic.jump({ count = 1 })
  1061. return vim.api.nvim_win_get_cursor(0)
  1062. end)
  1063. )
  1064. eq(
  1065. { 3, 0 },
  1066. exec_lua(function()
  1067. vim.diagnostic.jump({ count = 1 })
  1068. return vim.api.nvim_win_get_cursor(0)
  1069. end)
  1070. )
  1071. eq(
  1072. { 4, 0 },
  1073. exec_lua(function()
  1074. vim.diagnostic.jump({ count = 1 })
  1075. return vim.api.nvim_win_get_cursor(0)
  1076. end)
  1077. )
  1078. end)
  1079. end)
  1080. describe('get_prev()', function()
  1081. it('can find the previous diagnostic with only one namespace', function()
  1082. eq(
  1083. { 1, 1 },
  1084. exec_lua(function()
  1085. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1086. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  1087. })
  1088. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  1089. vim.api.nvim_win_set_cursor(0, { 3, 1 })
  1090. local prev = vim.diagnostic.get_prev()
  1091. return { prev.lnum, prev.col }
  1092. end)
  1093. )
  1094. end)
  1095. it('can find the previous diagnostic with two errors', function()
  1096. eq(
  1097. { 1, 1 },
  1098. exec_lua(function()
  1099. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1100. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  1101. _G.make_error('Diagnostic #2', 4, 4, 4, 4),
  1102. })
  1103. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  1104. vim.api.nvim_win_set_cursor(0, { 3, 1 })
  1105. local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns })
  1106. return { prev.lnum, prev.col }
  1107. end)
  1108. )
  1109. end)
  1110. it('can cycle when position is past error', function()
  1111. eq(
  1112. { 4, 4 },
  1113. exec_lua(function()
  1114. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1115. _G.make_error('Diagnostic #2', 4, 4, 4, 4),
  1116. })
  1117. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  1118. vim.api.nvim_win_set_cursor(0, { 3, 1 })
  1119. local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns })
  1120. return { prev.lnum, prev.col }
  1121. end)
  1122. )
  1123. end)
  1124. it('respects wrap parameter', function()
  1125. eq(
  1126. nil,
  1127. exec_lua(function()
  1128. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1129. _G.make_error('Diagnostic #2', 4, 4, 4, 4),
  1130. })
  1131. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  1132. vim.api.nvim_win_set_cursor(0, { 3, 1 })
  1133. local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns, wrap = false })
  1134. return prev
  1135. end)
  1136. )
  1137. end)
  1138. it('works on blank line #28397', function()
  1139. eq(
  1140. { 0, 2 },
  1141. exec_lua(function()
  1142. local test_bufnr = vim.api.nvim_create_buf(true, false)
  1143. vim.api.nvim_buf_set_lines(test_bufnr, 0, -1, false, {
  1144. 'first line',
  1145. '',
  1146. '',
  1147. 'end line',
  1148. })
  1149. vim.diagnostic.set(_G.diagnostic_ns, test_bufnr, {
  1150. _G.make_info('Diagnostic #1', 0, 2, 0, 2),
  1151. _G.make_info('Diagnostic #2', 2, 0, 2, 0),
  1152. _G.make_info('Diagnostic #3', 2, 0, 2, 0),
  1153. })
  1154. vim.api.nvim_win_set_buf(0, test_bufnr)
  1155. vim.api.nvim_win_set_cursor(0, { 3, 0 })
  1156. return vim.diagnostic.get_prev_pos { namespace = _G.diagnostic_ns }
  1157. end)
  1158. )
  1159. end)
  1160. end)
  1161. describe('jump()', function()
  1162. before_each(function()
  1163. exec_lua(function()
  1164. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1165. _G.make_error('Diagnostic #1', 0, 0, 0, 2),
  1166. _G.make_error('Diagnostic #2', 1, 1, 1, 4),
  1167. _G.make_warning('Diagnostic #3', 2, -1, 2, -1),
  1168. _G.make_info('Diagnostic #4', 3, 0, 3, 3),
  1169. })
  1170. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  1171. end)
  1172. end)
  1173. it('can move forward', function()
  1174. eq(
  1175. { 2, 1 },
  1176. exec_lua(function()
  1177. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  1178. vim.diagnostic.jump({ count = 1 })
  1179. return vim.api.nvim_win_get_cursor(0)
  1180. end)
  1181. )
  1182. eq(
  1183. { 4, 0 },
  1184. exec_lua(function()
  1185. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  1186. vim.diagnostic.jump({ count = 3 })
  1187. return vim.api.nvim_win_get_cursor(0)
  1188. end)
  1189. )
  1190. eq(
  1191. { 4, 0 },
  1192. exec_lua(function()
  1193. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  1194. vim.diagnostic.jump({ count = math.huge, wrap = false })
  1195. return vim.api.nvim_win_get_cursor(0)
  1196. end)
  1197. )
  1198. end)
  1199. it('can move backward', function()
  1200. eq(
  1201. { 3, 0 },
  1202. exec_lua(function()
  1203. vim.api.nvim_win_set_cursor(0, { 4, 0 })
  1204. vim.diagnostic.jump({ count = -1 })
  1205. return vim.api.nvim_win_get_cursor(0)
  1206. end)
  1207. )
  1208. eq(
  1209. { 1, 0 },
  1210. exec_lua(function()
  1211. vim.api.nvim_win_set_cursor(0, { 4, 0 })
  1212. vim.diagnostic.jump({ count = -3 })
  1213. return vim.api.nvim_win_get_cursor(0)
  1214. end)
  1215. )
  1216. eq(
  1217. { 1, 0 },
  1218. exec_lua(function()
  1219. vim.api.nvim_win_set_cursor(0, { 4, 0 })
  1220. vim.diagnostic.jump({ count = -math.huge, wrap = false })
  1221. return vim.api.nvim_win_get_cursor(0)
  1222. end)
  1223. )
  1224. end)
  1225. it('can filter by severity', function()
  1226. eq(
  1227. { 3, 0 },
  1228. exec_lua(function()
  1229. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  1230. vim.diagnostic.jump({ count = 1, severity = vim.diagnostic.severity.WARN })
  1231. return vim.api.nvim_win_get_cursor(0)
  1232. end)
  1233. )
  1234. eq(
  1235. { 3, 0 },
  1236. exec_lua(function()
  1237. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  1238. vim.diagnostic.jump({ count = 9999, severity = vim.diagnostic.severity.WARN })
  1239. return vim.api.nvim_win_get_cursor(0)
  1240. end)
  1241. )
  1242. end)
  1243. it('can wrap', function()
  1244. eq(
  1245. { 1, 0 },
  1246. exec_lua(function()
  1247. vim.api.nvim_win_set_cursor(0, { 4, 0 })
  1248. vim.diagnostic.jump({ count = 1, wrap = true })
  1249. return vim.api.nvim_win_get_cursor(0)
  1250. end)
  1251. )
  1252. eq(
  1253. { 4, 0 },
  1254. exec_lua(function()
  1255. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  1256. vim.diagnostic.jump({ count = -1, wrap = true })
  1257. return vim.api.nvim_win_get_cursor(0)
  1258. end)
  1259. )
  1260. end)
  1261. end)
  1262. describe('get()', function()
  1263. it('returns an empty table when no diagnostics are present', function()
  1264. eq(
  1265. {},
  1266. exec_lua [[return vim.diagnostic.get( _G.diagnostic_bufnr, {namespace=diagnostic_ns})]]
  1267. )
  1268. end)
  1269. it('returns all diagnostics when no severity is supplied', function()
  1270. eq(
  1271. 2,
  1272. exec_lua(function()
  1273. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1274. _G.make_error('Error 1', 1, 1, 1, 5),
  1275. _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
  1276. })
  1277. return #vim.diagnostic.get(_G.diagnostic_bufnr)
  1278. end)
  1279. )
  1280. end)
  1281. it('returns only requested diagnostics when severity range is supplied', function()
  1282. eq(
  1283. { 2, 3, 2 },
  1284. exec_lua(function()
  1285. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1286. _G.make_error('Error 1', 1, 1, 1, 5),
  1287. _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
  1288. _G.make_info('Ignored information', 1, 1, 2, 3),
  1289. _G.make_hint("Here's a hint", 1, 1, 2, 3),
  1290. })
  1291. return {
  1292. #vim.diagnostic.get(
  1293. _G.diagnostic_bufnr,
  1294. { severity = { min = vim.diagnostic.severity.WARN } }
  1295. ),
  1296. #vim.diagnostic.get(
  1297. _G.diagnostic_bufnr,
  1298. { severity = { max = vim.diagnostic.severity.WARN } }
  1299. ),
  1300. #vim.diagnostic.get(_G.diagnostic_bufnr, {
  1301. severity = {
  1302. min = vim.diagnostic.severity.INFO,
  1303. max = vim.diagnostic.severity.WARN,
  1304. },
  1305. }),
  1306. }
  1307. end)
  1308. )
  1309. end)
  1310. it('returns only requested diagnostics when severities are supplied', function()
  1311. eq(
  1312. { 1, 1, 2 },
  1313. exec_lua(function()
  1314. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1315. _G.make_error('Error 1', 1, 1, 1, 5),
  1316. _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
  1317. _G.make_info('Ignored information', 1, 1, 2, 3),
  1318. _G.make_hint("Here's a hint", 1, 1, 2, 3),
  1319. })
  1320. return {
  1321. #vim.diagnostic.get(
  1322. _G.diagnostic_bufnr,
  1323. { severity = { vim.diagnostic.severity.WARN } }
  1324. ),
  1325. #vim.diagnostic.get(
  1326. _G.diagnostic_bufnr,
  1327. { severity = { vim.diagnostic.severity.ERROR } }
  1328. ),
  1329. #vim.diagnostic.get(_G.diagnostic_bufnr, {
  1330. severity = {
  1331. vim.diagnostic.severity.INFO,
  1332. vim.diagnostic.severity.WARN,
  1333. },
  1334. }),
  1335. }
  1336. end)
  1337. )
  1338. end)
  1339. it('allows filtering by line', function()
  1340. eq(
  1341. 2,
  1342. exec_lua(function()
  1343. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1344. _G.make_error('Error 1', 1, 1, 1, 5),
  1345. _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
  1346. _G.make_info('Ignored information', 1, 1, 2, 3),
  1347. _G.make_error('Error On Other Line', 3, 1, 3, 5),
  1348. })
  1349. return #vim.diagnostic.get(_G.diagnostic_bufnr, { lnum = 2 })
  1350. end)
  1351. )
  1352. end)
  1353. end)
  1354. describe('count', function()
  1355. it('returns actually present severity counts', function()
  1356. eq(
  1357. exec_lua(function()
  1358. return {
  1359. [vim.diagnostic.severity.ERROR] = 4,
  1360. [vim.diagnostic.severity.WARN] = 3,
  1361. [vim.diagnostic.severity.INFO] = 2,
  1362. [vim.diagnostic.severity.HINT] = 1,
  1363. }
  1364. end),
  1365. exec_lua(function()
  1366. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1367. _G.make_error('Error 1', 1, 1, 1, 2),
  1368. _G.make_error('Error 2', 1, 3, 1, 4),
  1369. _G.make_error('Error 3', 1, 5, 1, 6),
  1370. _G.make_error('Error 4', 1, 7, 1, 8),
  1371. _G.make_warning('Warning 1', 2, 1, 2, 2),
  1372. _G.make_warning('Warning 2', 2, 3, 2, 4),
  1373. _G.make_warning('Warning 3', 2, 5, 2, 6),
  1374. _G.make_info('Info 1', 3, 1, 3, 2),
  1375. _G.make_info('Info 2', 3, 3, 3, 4),
  1376. _G.make_hint('Hint 1', 4, 1, 4, 2),
  1377. })
  1378. return vim.diagnostic.count(_G.diagnostic_bufnr)
  1379. end)
  1380. )
  1381. eq(
  1382. exec_lua(function()
  1383. return {
  1384. [vim.diagnostic.severity.ERROR] = 2,
  1385. [vim.diagnostic.severity.INFO] = 1,
  1386. }
  1387. end),
  1388. exec_lua(function()
  1389. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1390. _G.make_error('Error 1', 1, 1, 1, 2),
  1391. _G.make_error('Error 2', 1, 3, 1, 4),
  1392. _G.make_info('Info 1', 3, 1, 3, 2),
  1393. })
  1394. return vim.diagnostic.count(_G.diagnostic_bufnr)
  1395. end)
  1396. )
  1397. end)
  1398. it('returns only requested diagnostics count when severity range is supplied', function()
  1399. eq(
  1400. exec_lua(function()
  1401. return {
  1402. { [vim.diagnostic.severity.ERROR] = 1, [vim.diagnostic.severity.WARN] = 1 },
  1403. {
  1404. [vim.diagnostic.severity.WARN] = 1,
  1405. [vim.diagnostic.severity.INFO] = 1,
  1406. [vim.diagnostic.severity.HINT] = 1,
  1407. },
  1408. { [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1 },
  1409. }
  1410. end),
  1411. exec_lua(function()
  1412. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1413. _G.make_error('Error 1', 1, 1, 1, 5),
  1414. _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
  1415. _G.make_info('Ignored information', 1, 1, 2, 3),
  1416. _G.make_hint("Here's a hint", 1, 1, 2, 3),
  1417. })
  1418. return {
  1419. vim.diagnostic.count(
  1420. _G.diagnostic_bufnr,
  1421. { severity = { min = vim.diagnostic.severity.WARN } }
  1422. ),
  1423. vim.diagnostic.count(
  1424. _G.diagnostic_bufnr,
  1425. { severity = { max = vim.diagnostic.severity.WARN } }
  1426. ),
  1427. vim.diagnostic.count(_G.diagnostic_bufnr, {
  1428. severity = {
  1429. min = vim.diagnostic.severity.INFO,
  1430. max = vim.diagnostic.severity.WARN,
  1431. },
  1432. }),
  1433. }
  1434. end)
  1435. )
  1436. end)
  1437. it('returns only requested diagnostics when severities are supplied', function()
  1438. eq(
  1439. exec_lua(function()
  1440. return {
  1441. { [vim.diagnostic.severity.WARN] = 1 },
  1442. { [vim.diagnostic.severity.ERROR] = 1 },
  1443. { [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1 },
  1444. }
  1445. end),
  1446. exec_lua(function()
  1447. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1448. _G.make_error('Error 1', 1, 1, 1, 5),
  1449. _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
  1450. _G.make_info('Ignored information', 1, 1, 2, 3),
  1451. _G.make_hint("Here's a hint", 1, 1, 2, 3),
  1452. })
  1453. return {
  1454. vim.diagnostic.count(
  1455. _G.diagnostic_bufnr,
  1456. { severity = { vim.diagnostic.severity.WARN } }
  1457. ),
  1458. vim.diagnostic.count(
  1459. _G.diagnostic_bufnr,
  1460. { severity = { vim.diagnostic.severity.ERROR } }
  1461. ),
  1462. vim.diagnostic.count(_G.diagnostic_bufnr, {
  1463. severity = {
  1464. vim.diagnostic.severity.INFO,
  1465. vim.diagnostic.severity.WARN,
  1466. },
  1467. }),
  1468. }
  1469. end)
  1470. )
  1471. end)
  1472. it('allows filtering by line', function()
  1473. eq(
  1474. exec_lua(function()
  1475. return {
  1476. [vim.diagnostic.severity.WARN] = 1,
  1477. [vim.diagnostic.severity.INFO] = 1,
  1478. }
  1479. end),
  1480. exec_lua(function()
  1481. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1482. _G.make_error('Error 1', 1, 1, 1, 5),
  1483. _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
  1484. _G.make_info('Ignored information', 1, 1, 2, 3),
  1485. _G.make_error('Error On Other Line', 3, 1, 3, 5),
  1486. })
  1487. return vim.diagnostic.count(_G.diagnostic_bufnr, { lnum = 2 })
  1488. end)
  1489. )
  1490. end)
  1491. end)
  1492. describe('config()', function()
  1493. it('works with global, namespace, and ephemeral options', function()
  1494. eq(
  1495. 1,
  1496. exec_lua(function()
  1497. vim.diagnostic.config({
  1498. virtual_text = false,
  1499. })
  1500. vim.diagnostic.config({
  1501. virtual_text = true,
  1502. underline = false,
  1503. }, _G.diagnostic_ns)
  1504. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1505. _G.make_error('Some Error', 4, 4, 4, 4),
  1506. })
  1507. return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  1508. end)
  1509. )
  1510. eq(
  1511. 1,
  1512. exec_lua(function()
  1513. vim.diagnostic.config({
  1514. virtual_text = false,
  1515. })
  1516. vim.diagnostic.config({
  1517. virtual_text = false,
  1518. underline = false,
  1519. }, _G.diagnostic_ns)
  1520. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1521. _G.make_error('Some Error', 4, 4, 4, 4),
  1522. }, { virtual_text = true })
  1523. return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  1524. end)
  1525. )
  1526. eq(
  1527. 0,
  1528. exec_lua(function()
  1529. vim.diagnostic.config({
  1530. virtual_text = false,
  1531. })
  1532. vim.diagnostic.config({
  1533. virtual_text = { severity = vim.diagnostic.severity.ERROR },
  1534. underline = false,
  1535. }, _G.diagnostic_ns)
  1536. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1537. _G.make_warning('Some Warning', 4, 4, 4, 4),
  1538. }, { virtual_text = true })
  1539. return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  1540. end)
  1541. )
  1542. eq(
  1543. 1,
  1544. exec_lua(function()
  1545. vim.diagnostic.config({
  1546. virtual_text = false,
  1547. })
  1548. vim.diagnostic.config({
  1549. virtual_text = { severity = vim.diagnostic.severity.ERROR },
  1550. underline = false,
  1551. }, _G.diagnostic_ns)
  1552. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1553. _G.make_warning('Some Warning', 4, 4, 4, 4),
  1554. }, {
  1555. virtual_text = {}, -- An empty table uses default values
  1556. })
  1557. return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  1558. end)
  1559. )
  1560. end)
  1561. it('can use functions for config values', function()
  1562. exec_lua(function()
  1563. vim.diagnostic.config({
  1564. virtual_text = function()
  1565. return true
  1566. end,
  1567. }, _G.diagnostic_ns)
  1568. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1569. _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
  1570. })
  1571. end)
  1572. eq(
  1573. 1,
  1574. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  1575. )
  1576. eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  1577. -- Now, don't enable virtual text.
  1578. -- We should have one less extmark displayed.
  1579. exec_lua(function()
  1580. vim.diagnostic.config({
  1581. virtual_text = function()
  1582. return false
  1583. end,
  1584. }, _G.diagnostic_ns)
  1585. end)
  1586. eq(
  1587. 1,
  1588. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  1589. )
  1590. eq(1, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  1591. end)
  1592. it('allows filtering by severity', function()
  1593. local get_extmark_count_with_severity = function(min_severity)
  1594. return exec_lua(function()
  1595. vim.diagnostic.config({
  1596. underline = false,
  1597. virtual_text = {
  1598. severity = { min = min_severity },
  1599. },
  1600. })
  1601. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1602. _G.make_warning('Delayed Diagnostic', 4, 4, 4, 4),
  1603. })
  1604. return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  1605. end)
  1606. end
  1607. -- No messages with Error or higher
  1608. eq(0, get_extmark_count_with_severity('ERROR'))
  1609. -- But now we don't filter it
  1610. eq(1, get_extmark_count_with_severity('WARN'))
  1611. eq(1, get_extmark_count_with_severity('HINT'))
  1612. end)
  1613. it('allows sorting by severity', function()
  1614. exec_lua(function()
  1615. vim.diagnostic.config({
  1616. underline = false,
  1617. signs = true,
  1618. virtual_text = true,
  1619. })
  1620. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1621. _G.make_warning('Warning', 4, 4, 4, 4),
  1622. _G.make_error('Error', 4, 4, 4, 4),
  1623. _G.make_info('Info', 4, 4, 4, 4),
  1624. })
  1625. function _G.get_highest_underline_hl(severity_sort)
  1626. vim.diagnostic.config({
  1627. underline = true,
  1628. severity_sort = severity_sort,
  1629. })
  1630. local extmarks = _G.get_underline_extmarks(_G.diagnostic_ns)
  1631. table.sort(extmarks, function(a, b)
  1632. return a[4].priority > b[4].priority
  1633. end)
  1634. return extmarks[1][4].hl_group
  1635. end
  1636. function _G.get_virt_text_and_signs(severity_sort)
  1637. vim.diagnostic.config({
  1638. severity_sort = severity_sort,
  1639. })
  1640. local virt_text = _G.get_virt_text_extmarks(_G.diagnostic_ns)[1][4].virt_text
  1641. local virt_texts = {}
  1642. for i = 2, #virt_text - 1 do
  1643. table.insert(virt_texts, (string.gsub(virt_text[i][2], 'DiagnosticVirtualText', '')))
  1644. end
  1645. local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
  1646. local sign_ns = ns.user_data.sign_ns
  1647. local signs = {}
  1648. local all_signs = vim.api.nvim_buf_get_extmarks(
  1649. _G.diagnostic_bufnr,
  1650. sign_ns,
  1651. 0,
  1652. -1,
  1653. { type = 'sign', details = true }
  1654. )
  1655. table.sort(all_signs, function(a, b)
  1656. return a[1] > b[1]
  1657. end)
  1658. for _, v in ipairs(all_signs) do
  1659. local s = v[4].sign_hl_group:gsub('DiagnosticSign', '')
  1660. if not vim.tbl_contains(signs, s) then
  1661. signs[#signs + 1] = s
  1662. end
  1663. end
  1664. return { virt_texts, signs }
  1665. end
  1666. end)
  1667. local result = exec_lua [[return _G.get_virt_text_and_signs(false)]]
  1668. -- Virt texts are defined lowest priority to highest, signs from
  1669. -- highest to lowest
  1670. eq({ 'Warn', 'Error', 'Info' }, result[1])
  1671. eq({ 'Info', 'Error', 'Warn' }, result[2])
  1672. result = exec_lua [[return _G.get_virt_text_and_signs(true)]]
  1673. eq({ 'Info', 'Warn', 'Error' }, result[1])
  1674. eq({ 'Error', 'Warn', 'Info' }, result[2])
  1675. result = exec_lua [[return _G.get_virt_text_and_signs({ reverse = true })]]
  1676. eq({ 'Error', 'Warn', 'Info' }, result[1])
  1677. eq({ 'Info', 'Warn', 'Error' }, result[2])
  1678. local underline_hl = exec_lua [[return _G.get_highest_underline_hl(true)]]
  1679. eq('DiagnosticUnderlineError', underline_hl)
  1680. underline_hl = exec_lua [[return _G.get_highest_underline_hl({ reverse = true })]]
  1681. eq('DiagnosticUnderlineInfo', underline_hl)
  1682. end)
  1683. it('can show diagnostic sources in virtual text', function()
  1684. local result = exec_lua(function()
  1685. local diagnostics = {
  1686. _G.make_error('Some error', 0, 0, 0, 0, 'source x'),
  1687. }
  1688. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
  1689. underline = false,
  1690. virtual_text = {
  1691. prefix = '',
  1692. source = 'always',
  1693. },
  1694. })
  1695. local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
  1696. local virt_text = extmarks[1][4].virt_text[3][1]
  1697. return virt_text
  1698. end)
  1699. eq(' source x: Some error', result)
  1700. result = exec_lua(function()
  1701. vim.diagnostic.config({
  1702. underline = false,
  1703. virtual_text = {
  1704. prefix = '',
  1705. source = 'if_many',
  1706. },
  1707. }, _G.diagnostic_ns)
  1708. local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
  1709. local virt_text = extmarks[1][4].virt_text[3][1]
  1710. return virt_text
  1711. end)
  1712. eq(' Some error', result)
  1713. result = exec_lua(function()
  1714. local diagnostics = {
  1715. _G.make_error('Some error', 0, 0, 0, 0, 'source x'),
  1716. _G.make_error('Another error', 1, 1, 1, 1, 'source y'),
  1717. }
  1718. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
  1719. underline = false,
  1720. virtual_text = {
  1721. prefix = '',
  1722. source = 'if_many',
  1723. },
  1724. })
  1725. local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
  1726. local virt_text = { extmarks[1][4].virt_text[3][1], extmarks[2][4].virt_text[3][1] }
  1727. return virt_text
  1728. end)
  1729. eq(' source x: Some error', result[1])
  1730. eq(' source y: Another error', result[2])
  1731. end)
  1732. it('supports a format function for diagnostic messages', function()
  1733. local result = exec_lua(function()
  1734. vim.diagnostic.config({
  1735. underline = false,
  1736. virtual_text = {
  1737. prefix = '',
  1738. format = function(diagnostic)
  1739. if diagnostic.severity == vim.diagnostic.severity.ERROR then
  1740. return string.format('🔥 %s', diagnostic.message)
  1741. end
  1742. return string.format('👀 %s', diagnostic.message)
  1743. end,
  1744. },
  1745. })
  1746. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1747. _G.make_warning('Warning', 0, 0, 0, 0),
  1748. _G.make_error('Error', 1, 0, 1, 0),
  1749. })
  1750. local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
  1751. return { extmarks[1][4].virt_text, extmarks[2][4].virt_text }
  1752. end)
  1753. eq(' 👀 Warning', result[1][3][1])
  1754. eq(' 🔥 Error', result[2][3][1])
  1755. end)
  1756. it('includes source for formatted diagnostics', function()
  1757. local result = exec_lua(function()
  1758. vim.diagnostic.config({
  1759. underline = false,
  1760. virtual_text = {
  1761. prefix = '',
  1762. source = 'always',
  1763. format = function(diagnostic)
  1764. if diagnostic.severity == vim.diagnostic.severity.ERROR then
  1765. return string.format('🔥 %s', diagnostic.message)
  1766. end
  1767. return string.format('👀 %s', diagnostic.message)
  1768. end,
  1769. },
  1770. })
  1771. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1772. _G.make_warning('Warning', 0, 0, 0, 0, 'some_linter'),
  1773. _G.make_error('Error', 1, 0, 1, 0, 'another_linter'),
  1774. })
  1775. local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
  1776. return { extmarks[1][4].virt_text, extmarks[2][4].virt_text }
  1777. end)
  1778. eq(' some_linter: 👀 Warning', result[1][3][1])
  1779. eq(' another_linter: 🔥 Error', result[2][3][1])
  1780. end)
  1781. it('can add a prefix to virtual text', function()
  1782. eq(
  1783. 'E Some error',
  1784. exec_lua(function()
  1785. local diagnostics = {
  1786. _G.make_error('Some error', 0, 0, 0, 0),
  1787. }
  1788. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
  1789. underline = false,
  1790. virtual_text = {
  1791. prefix = 'E',
  1792. suffix = '',
  1793. },
  1794. })
  1795. local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
  1796. local prefix = extmarks[1][4].virt_text[2][1]
  1797. local message = extmarks[1][4].virt_text[3][1]
  1798. return prefix .. message
  1799. end)
  1800. )
  1801. eq(
  1802. '[(1/1) err-code] Some error',
  1803. exec_lua(function()
  1804. local diagnostics = {
  1805. _G.make_error('Some error', 0, 0, 0, 0, nil, 'err-code'),
  1806. }
  1807. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
  1808. underline = false,
  1809. virtual_text = {
  1810. prefix = function(diag, i, total)
  1811. return string.format('[(%d/%d) %s]', i, total, diag.code)
  1812. end,
  1813. suffix = '',
  1814. },
  1815. })
  1816. local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
  1817. local prefix = extmarks[1][4].virt_text[2][1]
  1818. local message = extmarks[1][4].virt_text[3][1]
  1819. return prefix .. message
  1820. end)
  1821. )
  1822. end)
  1823. it('can add a suffix to virtual text', function()
  1824. eq(
  1825. ' Some error ✘',
  1826. exec_lua(function()
  1827. local diagnostics = {
  1828. _G.make_error('Some error', 0, 0, 0, 0),
  1829. }
  1830. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
  1831. underline = false,
  1832. virtual_text = {
  1833. prefix = '',
  1834. suffix = ' ✘',
  1835. },
  1836. })
  1837. local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
  1838. local virt_text = extmarks[1][4].virt_text[3][1]
  1839. return virt_text
  1840. end)
  1841. )
  1842. eq(
  1843. ' Some error [err-code]',
  1844. exec_lua(function()
  1845. local diagnostics = {
  1846. _G.make_error('Some error', 0, 0, 0, 0, nil, 'err-code'),
  1847. }
  1848. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
  1849. underline = false,
  1850. virtual_text = {
  1851. prefix = '',
  1852. suffix = function(diag)
  1853. return string.format(' [%s]', diag.code)
  1854. end,
  1855. },
  1856. })
  1857. local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
  1858. local virt_text = extmarks[1][4].virt_text[3][1]
  1859. return virt_text
  1860. end)
  1861. )
  1862. end)
  1863. end)
  1864. describe('set()', function()
  1865. it('validation', function()
  1866. matches(
  1867. 'expected a list of diagnostics',
  1868. pcall_err(exec_lua, [[vim.diagnostic.set(1, 0, {lnum = 1, col = 2})]])
  1869. )
  1870. end)
  1871. it('can perform updates after insert_leave', function()
  1872. exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]]
  1873. api.nvim_input('o')
  1874. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  1875. -- Save the diagnostics
  1876. exec_lua(function()
  1877. vim.diagnostic.config({
  1878. update_in_insert = false,
  1879. })
  1880. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1881. _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
  1882. })
  1883. end)
  1884. -- No diagnostics displayed yet.
  1885. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  1886. eq(
  1887. 1,
  1888. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  1889. )
  1890. eq(0, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  1891. api.nvim_input('<esc>')
  1892. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  1893. eq(
  1894. 1,
  1895. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  1896. )
  1897. eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  1898. end)
  1899. it('does not perform updates when not needed', function()
  1900. exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]]
  1901. api.nvim_input('o')
  1902. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  1903. -- Save the diagnostics
  1904. exec_lua(function()
  1905. vim.diagnostic.config({
  1906. update_in_insert = false,
  1907. virtual_text = true,
  1908. })
  1909. _G.DisplayCount = 0
  1910. local set_virtual_text = vim.diagnostic.handlers.virtual_text.show
  1911. vim.diagnostic.handlers.virtual_text.show = function(...)
  1912. _G.DisplayCount = _G.DisplayCount + 1
  1913. return set_virtual_text(...)
  1914. end
  1915. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1916. _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
  1917. })
  1918. end)
  1919. -- No diagnostics displayed yet.
  1920. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  1921. eq(
  1922. 1,
  1923. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  1924. )
  1925. eq(0, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  1926. eq(0, exec_lua [[return _G.DisplayCount]])
  1927. api.nvim_input('<esc>')
  1928. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  1929. eq(
  1930. 1,
  1931. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  1932. )
  1933. eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  1934. eq(1, exec_lua [[return _G.DisplayCount]])
  1935. -- Go in and out of insert mode one more time.
  1936. api.nvim_input('o')
  1937. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  1938. api.nvim_input('<esc>')
  1939. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  1940. -- Should not have set the virtual text again.
  1941. eq(1, exec_lua [[return _G.DisplayCount]])
  1942. end)
  1943. it('never sets virtual text, in combination with insert leave', function()
  1944. exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]]
  1945. api.nvim_input('o')
  1946. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  1947. -- Save the diagnostics
  1948. exec_lua(function()
  1949. vim.diagnostic.config({
  1950. update_in_insert = false,
  1951. virtual_text = false,
  1952. })
  1953. _G.DisplayCount = 0
  1954. local set_virtual_text = vim.diagnostic.handlers.virtual_text.show
  1955. vim.diagnostic.handlers.virtual_text.show = function(...)
  1956. _G.DisplayCount = _G.DisplayCount + 1
  1957. return set_virtual_text(...)
  1958. end
  1959. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1960. _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
  1961. })
  1962. end)
  1963. -- No diagnostics displayed yet.
  1964. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  1965. eq(
  1966. 1,
  1967. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  1968. )
  1969. eq(0, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  1970. eq(0, exec_lua [[return _G.DisplayCount]])
  1971. api.nvim_input('<esc>')
  1972. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  1973. eq(
  1974. 1,
  1975. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  1976. )
  1977. eq(1, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  1978. eq(0, exec_lua [[return _G.DisplayCount]])
  1979. -- Go in and out of insert mode one more time.
  1980. api.nvim_input('o')
  1981. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  1982. api.nvim_input('<esc>')
  1983. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  1984. -- Should not have set the virtual text still.
  1985. eq(0, exec_lua [[return _G.DisplayCount]])
  1986. end)
  1987. it('can perform updates while in insert mode, if desired', function()
  1988. exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]]
  1989. api.nvim_input('o')
  1990. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  1991. -- Save the diagnostics
  1992. exec_lua(function()
  1993. vim.diagnostic.config({
  1994. update_in_insert = true,
  1995. })
  1996. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  1997. _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
  1998. })
  1999. end)
  2000. -- Diagnostics are displayed, because the user wanted them that way!
  2001. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  2002. eq(
  2003. 1,
  2004. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  2005. )
  2006. eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  2007. api.nvim_input('<esc>')
  2008. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  2009. eq(
  2010. 1,
  2011. exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
  2012. )
  2013. eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
  2014. end)
  2015. it('can set diagnostics without displaying them', function()
  2016. eq(
  2017. 0,
  2018. exec_lua(function()
  2019. vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
  2020. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  2021. _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
  2022. })
  2023. return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  2024. end)
  2025. )
  2026. eq(
  2027. 2,
  2028. exec_lua(function()
  2029. vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
  2030. return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  2031. end)
  2032. )
  2033. end)
  2034. it('can set display options', function()
  2035. eq(
  2036. 0,
  2037. exec_lua(function()
  2038. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  2039. _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
  2040. }, { virtual_text = false, underline = false })
  2041. return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  2042. end)
  2043. )
  2044. eq(
  2045. 1,
  2046. exec_lua(function()
  2047. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  2048. _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
  2049. }, { virtual_text = true, underline = false })
  2050. return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
  2051. end)
  2052. )
  2053. end)
  2054. it('sets and clears signs #26193 #26555', function()
  2055. do
  2056. local result = exec_lua(function()
  2057. vim.diagnostic.config({
  2058. signs = true,
  2059. })
  2060. local diagnostics = {
  2061. _G.make_error('Error', 1, 1, 1, 2),
  2062. _G.make_warning('Warning', 3, 3, 3, 3),
  2063. }
  2064. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2065. local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
  2066. local sign_ns = ns.user_data.sign_ns
  2067. local signs = vim.api.nvim_buf_get_extmarks(
  2068. _G.diagnostic_bufnr,
  2069. sign_ns,
  2070. 0,
  2071. -1,
  2072. { type = 'sign', details = true }
  2073. )
  2074. local result = {}
  2075. for _, s in ipairs(signs) do
  2076. result[#result + 1] = { lnum = s[2] + 1, name = s[4].sign_hl_group }
  2077. end
  2078. return result
  2079. end)
  2080. eq({ 2, 'DiagnosticSignError' }, { result[1].lnum, result[1].name })
  2081. eq({ 4, 'DiagnosticSignWarn' }, { result[2].lnum, result[2].name })
  2082. end
  2083. do
  2084. local result = exec_lua(function()
  2085. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {})
  2086. local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
  2087. local sign_ns = ns.user_data.sign_ns
  2088. return vim.api.nvim_buf_get_extmarks(
  2089. _G.diagnostic_bufnr,
  2090. sign_ns,
  2091. 0,
  2092. -1,
  2093. { type = 'sign', details = true }
  2094. )
  2095. end)
  2096. eq({}, result)
  2097. end
  2098. end)
  2099. it('respects legacy signs placed with :sign define or sign_define #26618', function()
  2100. -- Legacy signs for diagnostics were deprecated in 0.10 and will be removed in 0.12
  2101. eq(0, n.fn.has('nvim-0.12'))
  2102. n.command('sign define DiagnosticSignError text= texthl= linehl=ErrorMsg numhl=ErrorMsg')
  2103. n.command('sign define DiagnosticSignWarn text= texthl= linehl=WarningMsg numhl=WarningMsg')
  2104. n.command('sign define DiagnosticSignInfo text= texthl= linehl=Underlined numhl=Underlined')
  2105. n.command('sign define DiagnosticSignHint text= texthl= linehl=Underlined numhl=Underlined')
  2106. local result = exec_lua(function()
  2107. vim.diagnostic.config({
  2108. signs = true,
  2109. })
  2110. local diagnostics = {
  2111. _G.make_error('Error', 1, 1, 1, 2),
  2112. _G.make_warning('Warning', 3, 3, 3, 3),
  2113. }
  2114. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2115. local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
  2116. local sign_ns = ns.user_data.sign_ns
  2117. local signs = vim.api.nvim_buf_get_extmarks(
  2118. _G.diagnostic_bufnr,
  2119. sign_ns,
  2120. 0,
  2121. -1,
  2122. { type = 'sign', details = true }
  2123. )
  2124. local result = {}
  2125. for _, s in ipairs(signs) do
  2126. result[#result + 1] = {
  2127. lnum = s[2] + 1,
  2128. name = s[4].sign_hl_group,
  2129. text = s[4].sign_text or '',
  2130. numhl = s[4].number_hl_group,
  2131. linehl = s[4].line_hl_group,
  2132. }
  2133. end
  2134. return result
  2135. end)
  2136. eq({
  2137. lnum = 2,
  2138. name = 'DiagnosticSignError',
  2139. text = '',
  2140. numhl = 'ErrorMsg',
  2141. linehl = 'ErrorMsg',
  2142. }, result[1])
  2143. eq({
  2144. lnum = 4,
  2145. name = 'DiagnosticSignWarn',
  2146. text = '',
  2147. numhl = 'WarningMsg',
  2148. linehl = 'WarningMsg',
  2149. }, result[2])
  2150. end)
  2151. end)
  2152. describe('open_float()', function()
  2153. it('can display a header', function()
  2154. eq(
  2155. { 'Diagnostics:', '1. Syntax error' },
  2156. exec_lua(function()
  2157. local diagnostics = {
  2158. _G.make_error('Syntax error', 0, 1, 0, 3),
  2159. }
  2160. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2161. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2162. local float_bufnr, winnr = vim.diagnostic.open_float()
  2163. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2164. vim.api.nvim_win_close(winnr, true)
  2165. return lines
  2166. end)
  2167. )
  2168. eq(
  2169. { "We're no strangers to love...", '1. Syntax error' },
  2170. exec_lua(function()
  2171. local diagnostics = {
  2172. _G.make_error('Syntax error', 0, 1, 0, 3),
  2173. }
  2174. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2175. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2176. local float_bufnr, winnr =
  2177. vim.diagnostic.open_float({ header = "We're no strangers to love..." })
  2178. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2179. vim.api.nvim_win_close(winnr, true)
  2180. return lines
  2181. end)
  2182. )
  2183. eq(
  2184. { 'You know the rules', '1. Syntax error' },
  2185. exec_lua(function()
  2186. local diagnostics = {
  2187. _G.make_error('Syntax error', 0, 1, 0, 3),
  2188. }
  2189. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2190. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2191. local float_bufnr, winnr =
  2192. vim.diagnostic.open_float({ header = { 'You know the rules', 'Search' } })
  2193. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2194. vim.api.nvim_win_close(winnr, true)
  2195. return lines
  2196. end)
  2197. )
  2198. end)
  2199. it('can show diagnostics from the whole buffer', function()
  2200. eq(
  2201. { '1. Syntax error', '2. Some warning' },
  2202. exec_lua(function()
  2203. local diagnostics = {
  2204. _G.make_error('Syntax error', 0, 1, 0, 3),
  2205. _G.make_warning('Some warning', 1, 1, 1, 3),
  2206. }
  2207. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2208. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2209. local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
  2210. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2211. vim.api.nvim_win_close(winnr, true)
  2212. return lines
  2213. end)
  2214. )
  2215. end)
  2216. it('can show diagnostics from a single line', function()
  2217. -- Using cursor position
  2218. eq(
  2219. { '1. Some warning' },
  2220. exec_lua(function()
  2221. local diagnostics = {
  2222. _G.make_error('Syntax error', 0, 1, 0, 3),
  2223. _G.make_warning('Some warning', 1, 1, 1, 3),
  2224. }
  2225. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2226. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2227. vim.api.nvim_win_set_cursor(0, { 2, 1 })
  2228. local float_bufnr, winnr = vim.diagnostic.open_float({ header = false })
  2229. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2230. vim.api.nvim_win_close(winnr, true)
  2231. return lines
  2232. end)
  2233. )
  2234. -- With specified position
  2235. eq(
  2236. { '1. Some warning' },
  2237. exec_lua(function()
  2238. local diagnostics = {
  2239. _G.make_error('Syntax error', 0, 1, 0, 3),
  2240. _G.make_warning('Some warning', 1, 1, 1, 3),
  2241. }
  2242. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2243. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2244. vim.api.nvim_win_set_cursor(0, { 1, 1 })
  2245. local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, pos = 1 })
  2246. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2247. vim.api.nvim_win_close(winnr, true)
  2248. return lines
  2249. end)
  2250. )
  2251. -- End position is exclusive
  2252. eq(
  2253. nil,
  2254. exec_lua(function()
  2255. local diagnostics = {
  2256. _G.make_error('Syntax error', 1, 1, 2, 0),
  2257. }
  2258. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2259. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2260. vim.api.nvim_win_set_cursor(0, { 1, 1 })
  2261. local _, winnr = vim.diagnostic.open_float(0, { header = false, pos = { 2, 0 } })
  2262. return winnr
  2263. end)
  2264. )
  2265. -- Works when width == 0
  2266. eq(
  2267. { '1. Syntax error' },
  2268. exec_lua(function()
  2269. local diagnostics = {
  2270. _G.make_error('Syntax error', 2, 0, 2, 0),
  2271. }
  2272. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2273. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2274. vim.api.nvim_win_set_cursor(0, { 1, 1 })
  2275. local float_bufnr, winnr =
  2276. vim.diagnostic.open_float(0, { header = false, pos = { 2, 1 } })
  2277. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2278. vim.api.nvim_win_close(winnr, true)
  2279. return lines
  2280. end)
  2281. )
  2282. end)
  2283. it('can show diagnostics from a specific position', function()
  2284. -- Using cursor position
  2285. eq(
  2286. { 'Syntax error' },
  2287. exec_lua(function()
  2288. local diagnostics = {
  2289. _G.make_error('Syntax error', 1, 1, 1, 3),
  2290. _G.make_warning('Some warning', 1, 3, 1, 4),
  2291. }
  2292. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2293. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2294. vim.api.nvim_win_set_cursor(0, { 2, 2 })
  2295. local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'cursor' })
  2296. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2297. vim.api.nvim_win_close(winnr, true)
  2298. return lines
  2299. end)
  2300. )
  2301. -- With specified position
  2302. eq(
  2303. { 'Some warning' },
  2304. exec_lua(function()
  2305. local diagnostics = {
  2306. _G.make_error('Syntax error', 1, 1, 1, 3),
  2307. _G.make_warning('Some warning', 1, 3, 1, 4),
  2308. }
  2309. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2310. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2311. vim.api.nvim_win_set_cursor(0, { 1, 1 })
  2312. local float_bufnr, winnr =
  2313. vim.diagnostic.open_float({ header = false, scope = 'cursor', pos = { 1, 3 } })
  2314. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2315. vim.api.nvim_win_close(winnr, true)
  2316. return lines
  2317. end)
  2318. )
  2319. -- With column position past the end of the line. #16062
  2320. eq(
  2321. { 'Syntax error' },
  2322. exec_lua(function()
  2323. local first_line_len = #vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, 1, true)[1]
  2324. local diagnostics = {
  2325. _G.make_error('Syntax error', 0, first_line_len + 1, 1, 0),
  2326. }
  2327. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2328. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2329. vim.api.nvim_win_set_cursor(0, { 1, 1 })
  2330. local float_bufnr, winnr = vim.diagnostic.open_float({
  2331. header = false,
  2332. scope = 'cursor',
  2333. pos = { 0, first_line_len },
  2334. })
  2335. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2336. vim.api.nvim_win_close(winnr, true)
  2337. return lines
  2338. end)
  2339. )
  2340. -- End position is exclusive
  2341. eq(
  2342. nil,
  2343. exec_lua(function()
  2344. local diagnostics = {
  2345. _G.make_error('Syntax error', 1, 1, 1, 3),
  2346. }
  2347. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2348. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2349. vim.api.nvim_win_set_cursor(0, { 1, 1 })
  2350. local _, winnr =
  2351. vim.diagnostic.open_float(0, { header = false, scope = 'cursor', pos = { 1, 3 } })
  2352. return winnr
  2353. end)
  2354. )
  2355. -- Works when width == 0
  2356. eq(
  2357. { 'Syntax error' },
  2358. exec_lua(function()
  2359. local diagnostics = {
  2360. _G.make_error('Syntax error', 2, 0, 2, 0),
  2361. }
  2362. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2363. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2364. vim.api.nvim_win_set_cursor(0, { 1, 1 })
  2365. local float_bufnr, winnr =
  2366. vim.diagnostic.open_float({ header = false, scope = 'cursor', pos = { 2, 1 } })
  2367. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2368. vim.api.nvim_win_close(winnr, true)
  2369. return lines
  2370. end)
  2371. )
  2372. end)
  2373. it(
  2374. 'creates floating window and returns float bufnr and winnr if current line contains diagnostics',
  2375. function()
  2376. -- Two lines:
  2377. -- Diagnostic:
  2378. -- 1. <msg>
  2379. eq(
  2380. 2,
  2381. exec_lua(function()
  2382. local diagnostics = {
  2383. _G.make_error('Syntax error', 0, 1, 0, 3),
  2384. }
  2385. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2386. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2387. local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr)
  2388. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2389. vim.api.nvim_win_close(winnr, true)
  2390. return #lines
  2391. end)
  2392. )
  2393. end
  2394. )
  2395. it('only reports diagnostics from the current buffer when bufnr is omitted #15710', function()
  2396. eq(
  2397. 2,
  2398. exec_lua(function()
  2399. local other_bufnr = vim.api.nvim_create_buf(true, false)
  2400. local buf_1_diagnostics = {
  2401. _G.make_error('Syntax error', 0, 1, 0, 3),
  2402. }
  2403. local buf_2_diagnostics = {
  2404. _G.make_warning('Some warning', 0, 1, 0, 3),
  2405. }
  2406. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2407. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, buf_1_diagnostics)
  2408. vim.diagnostic.set(_G.other_ns, other_bufnr, buf_2_diagnostics)
  2409. local float_bufnr, winnr = vim.diagnostic.open_float()
  2410. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2411. vim.api.nvim_win_close(winnr, true)
  2412. return #lines
  2413. end)
  2414. )
  2415. end)
  2416. it('allows filtering by namespace', function()
  2417. eq(
  2418. 2,
  2419. exec_lua(function()
  2420. local ns_1_diagnostics = {
  2421. _G.make_error('Syntax error', 0, 1, 0, 3),
  2422. }
  2423. local ns_2_diagnostics = {
  2424. _G.make_warning('Some warning', 0, 1, 0, 3),
  2425. }
  2426. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2427. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diagnostics)
  2428. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diagnostics)
  2429. local float_bufnr, winnr =
  2430. vim.diagnostic.open_float(_G.diagnostic_bufnr, { namespace = _G.diagnostic_ns })
  2431. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2432. vim.api.nvim_win_close(winnr, true)
  2433. return #lines
  2434. end)
  2435. )
  2436. end)
  2437. it(
  2438. 'creates floating window and returns float bufnr and winnr without header, if requested',
  2439. function()
  2440. -- One line (since no header):
  2441. -- 1. <msg>
  2442. eq(
  2443. 1,
  2444. exec_lua(function()
  2445. local diagnostics = {
  2446. _G.make_error('Syntax error', 0, 1, 0, 3),
  2447. }
  2448. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2449. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2450. local float_bufnr, winnr =
  2451. vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
  2452. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2453. vim.api.nvim_win_close(winnr, true)
  2454. return #lines
  2455. end)
  2456. )
  2457. end
  2458. )
  2459. it('clamps diagnostic line numbers within the valid range', function()
  2460. eq(
  2461. 1,
  2462. exec_lua(function()
  2463. local diagnostics = {
  2464. _G.make_error('Syntax error', 6, 0, 6, 0),
  2465. }
  2466. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2467. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2468. local float_bufnr, winnr =
  2469. vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false, pos = 5 })
  2470. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2471. vim.api.nvim_win_close(winnr, true)
  2472. return #lines
  2473. end)
  2474. )
  2475. end)
  2476. it('can show diagnostic source', function()
  2477. exec_lua [[vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)]]
  2478. eq(
  2479. { '1. Syntax error' },
  2480. exec_lua(function()
  2481. local diagnostics = {
  2482. _G.make_error('Syntax error', 0, 1, 0, 3, 'source x'),
  2483. }
  2484. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2485. local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, {
  2486. header = false,
  2487. source = 'if_many',
  2488. })
  2489. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2490. vim.api.nvim_win_close(winnr, true)
  2491. return lines
  2492. end)
  2493. )
  2494. eq(
  2495. { '1. source x: Syntax error' },
  2496. exec_lua(function()
  2497. local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, {
  2498. header = false,
  2499. source = 'always',
  2500. })
  2501. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2502. vim.api.nvim_win_close(winnr, true)
  2503. return lines
  2504. end)
  2505. )
  2506. eq(
  2507. { '1. source x: Syntax error', '2. source y: Another error' },
  2508. exec_lua(function()
  2509. local diagnostics = {
  2510. _G.make_error('Syntax error', 0, 1, 0, 3, 'source x'),
  2511. _G.make_error('Another error', 0, 1, 0, 3, 'source y'),
  2512. }
  2513. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2514. local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, {
  2515. header = false,
  2516. source = 'if_many',
  2517. })
  2518. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2519. vim.api.nvim_win_close(winnr, true)
  2520. return lines
  2521. end)
  2522. )
  2523. end)
  2524. it('respects severity_sort', function()
  2525. exec_lua [[vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)]]
  2526. eq(
  2527. { '1. Syntax error', '2. Info', '3. Error', '4. Warning' },
  2528. exec_lua(function()
  2529. local diagnostics = {
  2530. _G.make_error('Syntax error', 0, 1, 0, 3),
  2531. _G.make_info('Info', 0, 3, 0, 4),
  2532. _G.make_error('Error', 0, 2, 0, 2),
  2533. _G.make_warning('Warning', 0, 0, 0, 1),
  2534. }
  2535. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2536. vim.diagnostic.config({ severity_sort = false })
  2537. local float_bufnr, winnr =
  2538. vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
  2539. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2540. vim.api.nvim_win_close(winnr, true)
  2541. return lines
  2542. end)
  2543. )
  2544. eq(
  2545. { '1. Syntax error', '2. Error', '3. Warning', '4. Info' },
  2546. exec_lua(function()
  2547. vim.diagnostic.config({ severity_sort = true })
  2548. local float_bufnr, winnr =
  2549. vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
  2550. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2551. vim.api.nvim_win_close(winnr, true)
  2552. return lines
  2553. end)
  2554. )
  2555. eq(
  2556. { '1. Info', '2. Warning', '3. Error', '4. Syntax error' },
  2557. exec_lua(function()
  2558. vim.diagnostic.config({ severity_sort = { reverse = true } })
  2559. local float_bufnr, winnr =
  2560. vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
  2561. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2562. vim.api.nvim_win_close(winnr, true)
  2563. return lines
  2564. end)
  2565. )
  2566. end)
  2567. it('can filter by severity', function()
  2568. local count_diagnostics_with_severity = function(min_severity, max_severity)
  2569. return exec_lua(function()
  2570. vim.diagnostic.config({
  2571. float = {
  2572. severity = { min = min_severity, max = max_severity },
  2573. },
  2574. })
  2575. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  2576. _G.make_error('Syntax error', 0, 1, 0, 3),
  2577. _G.make_info('Info', 0, 3, 0, 4),
  2578. _G.make_error('Error', 0, 2, 0, 2),
  2579. _G.make_warning('Warning', 0, 0, 0, 1),
  2580. })
  2581. local float_bufnr, winnr =
  2582. vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
  2583. if not float_bufnr then
  2584. return 0
  2585. end
  2586. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2587. vim.api.nvim_win_close(winnr, true)
  2588. return #lines
  2589. end)
  2590. end
  2591. eq(2, count_diagnostics_with_severity('ERROR'))
  2592. eq(3, count_diagnostics_with_severity('WARN'))
  2593. eq(1, count_diagnostics_with_severity('WARN', 'WARN'))
  2594. eq(4, count_diagnostics_with_severity('HINT'))
  2595. eq(0, count_diagnostics_with_severity('HINT', 'HINT'))
  2596. end)
  2597. it('can add a prefix to diagnostics', function()
  2598. -- Default is to add a number
  2599. eq(
  2600. { '1. Syntax error', '2. Some warning' },
  2601. exec_lua(function()
  2602. local diagnostics = {
  2603. _G.make_error('Syntax error', 0, 1, 0, 3),
  2604. _G.make_warning('Some warning', 1, 1, 1, 3),
  2605. }
  2606. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2607. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2608. local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
  2609. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2610. vim.api.nvim_win_close(winnr, true)
  2611. return lines
  2612. end)
  2613. )
  2614. eq(
  2615. { 'Syntax error', 'Some warning' },
  2616. exec_lua(function()
  2617. local diagnostics = {
  2618. _G.make_error('Syntax error', 0, 1, 0, 3),
  2619. _G.make_warning('Some warning', 1, 1, 1, 3),
  2620. }
  2621. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2622. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2623. local float_bufnr, winnr =
  2624. vim.diagnostic.open_float({ header = false, scope = 'buffer', prefix = '' })
  2625. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2626. vim.api.nvim_win_close(winnr, true)
  2627. return lines
  2628. end)
  2629. )
  2630. eq(
  2631. { '1. Syntax error', '2. Some warning' },
  2632. exec_lua(function()
  2633. local diagnostics = {
  2634. _G.make_error('Syntax error', 0, 1, 0, 3),
  2635. _G.make_warning('Some warning', 0, 1, 0, 3),
  2636. }
  2637. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2638. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2639. local float_bufnr, winnr = vim.diagnostic.open_float({
  2640. header = false,
  2641. prefix = function(_, i, total)
  2642. -- Only show a number if there is more than one diagnostic
  2643. if total > 1 then
  2644. return string.format('%d. ', i)
  2645. end
  2646. return ''
  2647. end,
  2648. })
  2649. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2650. vim.api.nvim_win_close(winnr, true)
  2651. return lines
  2652. end)
  2653. )
  2654. eq(
  2655. { 'Syntax error' },
  2656. exec_lua(function()
  2657. local diagnostics = {
  2658. _G.make_error('Syntax error', 0, 1, 0, 3),
  2659. }
  2660. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2661. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2662. local float_bufnr, winnr = vim.diagnostic.open_float({
  2663. header = false,
  2664. prefix = function(_, i, total)
  2665. -- Only show a number if there is more than one diagnostic
  2666. if total > 1 then
  2667. return string.format('%d. ', i)
  2668. end
  2669. return ''
  2670. end,
  2671. })
  2672. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2673. vim.api.nvim_win_close(winnr, true)
  2674. return lines
  2675. end)
  2676. )
  2677. eq(
  2678. '.../diagnostic.lua:0: prefix: expected string|table|function, got number',
  2679. pcall_err(exec_lua, [[ vim.diagnostic.open_float({ prefix = 42 }) ]])
  2680. )
  2681. end)
  2682. it('can add a suffix to diagnostics', function()
  2683. -- Default is to render the diagnostic error code
  2684. eq(
  2685. { '1. Syntax error [code-x]', '2. Some warning [code-y]' },
  2686. exec_lua(function()
  2687. local diagnostics = {
  2688. _G.make_error('Syntax error', 0, 1, 0, 3, nil, 'code-x'),
  2689. _G.make_warning('Some warning', 1, 1, 1, 3, nil, 'code-y'),
  2690. }
  2691. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2692. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2693. local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
  2694. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2695. vim.api.nvim_win_close(winnr, true)
  2696. return lines
  2697. end)
  2698. )
  2699. eq(
  2700. { '1. Syntax error', '2. Some warning' },
  2701. exec_lua(function()
  2702. local diagnostics = {
  2703. _G.make_error('Syntax error', 0, 1, 0, 3, nil, 'code-x'),
  2704. _G.make_warning('Some warning', 1, 1, 1, 3, nil, 'code-y'),
  2705. }
  2706. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2707. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2708. local float_bufnr, winnr =
  2709. vim.diagnostic.open_float({ header = false, scope = 'buffer', suffix = '' })
  2710. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2711. vim.api.nvim_win_close(winnr, true)
  2712. return lines
  2713. end)
  2714. )
  2715. -- Suffix is rendered on the last line of a multiline diagnostic
  2716. eq(
  2717. { '1. Syntax error', ' More context [code-x]' },
  2718. exec_lua(function()
  2719. local diagnostics = {
  2720. _G.make_error('Syntax error\nMore context', 0, 1, 0, 3, nil, 'code-x'),
  2721. }
  2722. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2723. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2724. local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
  2725. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2726. vim.api.nvim_win_close(winnr, true)
  2727. return lines
  2728. end)
  2729. )
  2730. eq(
  2731. '.../diagnostic.lua:0: suffix: expected string|table|function, got number',
  2732. pcall_err(exec_lua, [[ vim.diagnostic.open_float({ suffix = 42 }) ]])
  2733. )
  2734. end)
  2735. it('works with the old signature', function()
  2736. eq(
  2737. { '1. Syntax error' },
  2738. exec_lua(function()
  2739. local diagnostics = {
  2740. _G.make_error('Syntax error', 0, 1, 0, 3),
  2741. }
  2742. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2743. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2744. local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false })
  2745. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2746. vim.api.nvim_win_close(winnr, true)
  2747. return lines
  2748. end)
  2749. )
  2750. end)
  2751. it('works for multi-line diagnostics #21949', function()
  2752. -- create diagnostic
  2753. exec_lua(function()
  2754. local diagnostics = {
  2755. _G.make_error('Error in two lines lnum is 1 and end_lnum is 2', 1, 1, 2, 3),
  2756. }
  2757. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2758. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
  2759. end)
  2760. -- open float failed non diagnostic lnum
  2761. eq(
  2762. nil,
  2763. exec_lua(function()
  2764. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  2765. local _, winnr = vim.diagnostic.open_float(0, { header = false })
  2766. return winnr
  2767. end)
  2768. )
  2769. eq(
  2770. nil,
  2771. exec_lua(function()
  2772. vim.api.nvim_win_set_cursor(0, { 1, 0 })
  2773. local _, winnr = vim.diagnostic.open_float(0, { header = false, scope = 'cursor' })
  2774. return winnr
  2775. end)
  2776. )
  2777. -- can open a float window on lnum 1
  2778. eq(
  2779. { '1. Error in two lines lnum is 1 and end_lnum is 2' },
  2780. exec_lua(function()
  2781. vim.api.nvim_win_set_cursor(0, { 2, 0 })
  2782. local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false })
  2783. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2784. vim.api.nvim_win_close(winnr, true)
  2785. return lines
  2786. end)
  2787. )
  2788. -- can open a cursor-scoped float window on lnum 1
  2789. eq(
  2790. { 'Error in two lines lnum is 1 and end_lnum is 2' },
  2791. exec_lua(function()
  2792. vim.api.nvim_win_set_cursor(0, { 2, 1 })
  2793. local float_bufnr, winnr =
  2794. vim.diagnostic.open_float(0, { header = false, scope = 'cursor' })
  2795. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2796. vim.api.nvim_win_close(winnr, true)
  2797. return lines
  2798. end)
  2799. )
  2800. -- can open a float window on end_lnum 2
  2801. eq(
  2802. { '1. Error in two lines lnum is 1 and end_lnum is 2' },
  2803. exec_lua(function()
  2804. vim.api.nvim_win_set_cursor(0, { 3, 0 })
  2805. local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false })
  2806. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2807. vim.api.nvim_win_close(winnr, true)
  2808. return lines
  2809. end)
  2810. )
  2811. -- can open a cursor-scoped float window on end_lnum 2
  2812. eq(
  2813. { 'Error in two lines lnum is 1 and end_lnum is 2' },
  2814. exec_lua(function()
  2815. vim.api.nvim_win_set_cursor(0, { 3, 2 })
  2816. local float_bufnr, winnr =
  2817. vim.diagnostic.open_float(0, { header = false, scope = 'cursor' })
  2818. local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
  2819. vim.api.nvim_win_close(winnr, true)
  2820. return lines
  2821. end)
  2822. )
  2823. end)
  2824. end)
  2825. describe('setloclist()', function()
  2826. it('sets diagnostics in lnum order', function()
  2827. local loc_list = exec_lua(function()
  2828. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2829. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  2830. _G.make_error('Farther Diagnostic', 4, 4, 4, 4),
  2831. _G.make_error('Lower Diagnostic', 1, 1, 1, 1),
  2832. })
  2833. vim.diagnostic.setloclist()
  2834. return vim.fn.getloclist(0)
  2835. end)
  2836. assert(loc_list[1].lnum < loc_list[2].lnum)
  2837. end)
  2838. it('sets diagnostics in lnum order, regardless of namespace', function()
  2839. local loc_list = exec_lua(function()
  2840. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2841. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  2842. _G.make_error('Lower Diagnostic', 1, 1, 1, 1),
  2843. })
  2844. vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
  2845. _G.make_warning('Farther Diagnostic', 4, 4, 4, 4),
  2846. })
  2847. vim.diagnostic.setloclist()
  2848. return vim.fn.getloclist(0)
  2849. end)
  2850. assert(loc_list[1].lnum < loc_list[2].lnum)
  2851. end)
  2852. end)
  2853. describe('setqflist()', function()
  2854. it('updates existing diagnostics quickfix if one already exists', function()
  2855. local result = exec_lua(function()
  2856. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2857. vim.fn.setqflist({}, ' ', { title = 'Diagnostics' })
  2858. local diagnostics_qf_id = vim.fn.getqflist({ id = 0 }).id
  2859. vim.diagnostic.setqflist({ title = 'Diagnostics' })
  2860. local qf_id = vim.fn.getqflist({ id = 0, nr = '$' }).id
  2861. return { diagnostics_qf_id, qf_id }
  2862. end)
  2863. eq(result[1], result[2])
  2864. end)
  2865. it('navigates to existing diagnostics quickfix if one already exists and open=true', function()
  2866. local result = exec_lua(function()
  2867. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2868. vim.fn.setqflist({}, ' ', { title = 'Diagnostics' })
  2869. local diagnostics_qf_id = vim.fn.getqflist({ id = 0 }).id
  2870. vim.fn.setqflist({}, ' ', { title = 'Other' })
  2871. vim.diagnostic.setqflist({ title = 'Diagnostics', open = true })
  2872. local qf_id = vim.fn.getqflist({ id = 0 }).id
  2873. return { diagnostics_qf_id, qf_id }
  2874. end)
  2875. eq(result[1], result[2])
  2876. end)
  2877. it('sets new diagnostics quickfix as active when open=true', function()
  2878. local result = exec_lua(function()
  2879. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2880. vim.fn.setqflist({}, ' ', { title = 'Other' })
  2881. local other_qf_id = vim.fn.getqflist({ id = 0 }).id
  2882. vim.diagnostic.setqflist({ title = 'Diagnostics', open = true })
  2883. local qf_id = vim.fn.getqflist({ id = 0 }).id
  2884. return { other_qf_id, qf_id }
  2885. end)
  2886. neq(result[1], result[2])
  2887. end)
  2888. it('opens quickfix window when open=true', function()
  2889. local qf_winid = exec_lua(function()
  2890. vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
  2891. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  2892. _G.make_error('Error', 1, 1, 1, 1),
  2893. })
  2894. vim.diagnostic.setqflist({ open = true })
  2895. return vim.fn.getqflist({ winid = 0 }).winid
  2896. end)
  2897. neq(0, qf_winid)
  2898. end)
  2899. end)
  2900. describe('match()', function()
  2901. it('matches a string', function()
  2902. local msg = 'ERROR: george.txt:19:84:Two plus two equals five'
  2903. local diagnostic = {
  2904. severity = exec_lua [[return vim.diagnostic.severity.ERROR]],
  2905. lnum = 18,
  2906. col = 83,
  2907. end_lnum = 18,
  2908. end_col = 83,
  2909. message = 'Two plus two equals five',
  2910. }
  2911. eq(
  2912. diagnostic,
  2913. exec_lua(function()
  2914. return vim.diagnostic.match(
  2915. msg,
  2916. '^(%w+): [^:]+:(%d+):(%d+):(.+)$',
  2917. { 'severity', 'lnum', 'col', 'message' }
  2918. )
  2919. end)
  2920. )
  2921. end)
  2922. it('returns nil if the pattern fails to match', function()
  2923. eq(
  2924. nil,
  2925. exec_lua(function()
  2926. local msg = 'The answer to life, the universe, and everything is'
  2927. return vim.diagnostic.match(msg, 'This definitely will not match', {})
  2928. end)
  2929. )
  2930. end)
  2931. it('respects default values', function()
  2932. local msg = 'anna.txt:1:Happy families are all alike'
  2933. local diagnostic = {
  2934. severity = exec_lua [[return vim.diagnostic.severity.INFO]],
  2935. lnum = 0,
  2936. col = 0,
  2937. end_lnum = 0,
  2938. end_col = 0,
  2939. message = 'Happy families are all alike',
  2940. }
  2941. eq(
  2942. diagnostic,
  2943. exec_lua(function()
  2944. return vim.diagnostic.match(
  2945. msg,
  2946. '^[^:]+:(%d+):(.+)$',
  2947. { 'lnum', 'message' },
  2948. nil,
  2949. { severity = vim.diagnostic.severity.INFO }
  2950. )
  2951. end)
  2952. )
  2953. end)
  2954. it('accepts a severity map', function()
  2955. local msg = '46:FATAL:Et tu, Brute?'
  2956. local diagnostic = {
  2957. severity = exec_lua [[return vim.diagnostic.severity.ERROR]],
  2958. lnum = 45,
  2959. col = 0,
  2960. end_lnum = 45,
  2961. end_col = 0,
  2962. message = 'Et tu, Brute?',
  2963. }
  2964. eq(
  2965. diagnostic,
  2966. exec_lua(function()
  2967. return vim.diagnostic.match(
  2968. msg,
  2969. '^(%d+):(%w+):(.+)$',
  2970. { 'lnum', 'severity', 'message' },
  2971. { FATAL = vim.diagnostic.severity.ERROR }
  2972. )
  2973. end)
  2974. )
  2975. end)
  2976. end)
  2977. describe('toqflist() and fromqflist()', function()
  2978. it('works', function()
  2979. local result = exec_lua(function()
  2980. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  2981. _G.make_error('Error 1', 0, 1, 0, 1),
  2982. _G.make_error('Error 2', 1, 1, 1, 1),
  2983. _G.make_warning('Warning', 2, 2, 2, 2),
  2984. })
  2985. local diagnostics = vim.diagnostic.get(_G.diagnostic_bufnr)
  2986. vim.fn.setqflist(vim.diagnostic.toqflist(diagnostics))
  2987. local list = vim.fn.getqflist()
  2988. local new_diagnostics = vim.diagnostic.fromqflist(list)
  2989. -- Remove namespace since it isn't present in the return value of
  2990. -- fromlist()
  2991. for _, v in ipairs(diagnostics) do
  2992. v.namespace = nil
  2993. end
  2994. return { diagnostics, new_diagnostics }
  2995. end)
  2996. eq(result[1], result[2])
  2997. end)
  2998. end)
  2999. describe('handlers', function()
  3000. it('checks that a new handler is a table', function()
  3001. matches(
  3002. [[.*handler: expected table, got string.*]],
  3003. pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = "bar" ]])
  3004. )
  3005. matches(
  3006. [[.*handler: expected table, got function.*]],
  3007. pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = function() end ]])
  3008. )
  3009. end)
  3010. it('can add new handlers', function()
  3011. eq(
  3012. true,
  3013. exec_lua(function()
  3014. local handler_called = false
  3015. vim.diagnostic.handlers.test = {
  3016. show = function(namespace, bufnr, diagnostics, opts)
  3017. assert(namespace == _G.diagnostic_ns)
  3018. assert(bufnr == _G.diagnostic_bufnr)
  3019. assert(#diagnostics == 1)
  3020. assert(opts.test.some_opt == 42)
  3021. handler_called = true
  3022. end,
  3023. }
  3024. vim.diagnostic.config({ test = { some_opt = 42 } })
  3025. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  3026. _G.make_warning('Warning', 0, 0, 0, 0),
  3027. })
  3028. return handler_called
  3029. end)
  3030. )
  3031. end)
  3032. it('can disable handlers by setting the corresponding option to false', function()
  3033. eq(
  3034. false,
  3035. exec_lua(function()
  3036. local handler_called = false
  3037. vim.diagnostic.handlers.test = {
  3038. show = function(_, _, _, _)
  3039. handler_called = true
  3040. end,
  3041. }
  3042. vim.diagnostic.config({ test = false })
  3043. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  3044. _G.make_warning('Warning', 0, 0, 0, 0),
  3045. })
  3046. return handler_called
  3047. end)
  3048. )
  3049. end)
  3050. it("always calls a handler's hide function if defined", function()
  3051. eq(
  3052. { false, true },
  3053. exec_lua(function()
  3054. local hide_called = false
  3055. local show_called = false
  3056. vim.diagnostic.handlers.test = {
  3057. show = function(_, _, _, _)
  3058. show_called = true
  3059. end,
  3060. hide = function(namespace, bufnr)
  3061. assert(namespace == _G.diagnostic_ns)
  3062. assert(bufnr == _G.diagnostic_bufnr)
  3063. hide_called = true
  3064. end,
  3065. }
  3066. vim.diagnostic.config({ test = false })
  3067. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  3068. _G.make_warning('Warning', 0, 0, 0, 0),
  3069. })
  3070. vim.diagnostic.hide(_G.diagnostic_ns, _G.diagnostic_bufnr)
  3071. return { show_called, hide_called }
  3072. end)
  3073. )
  3074. end)
  3075. it('triggers the autocommand when diagnostics are set', function()
  3076. eq(
  3077. { true, true },
  3078. exec_lua(function()
  3079. -- Set a different buffer as current to test that <abuf> is being set properly in
  3080. -- DiagnosticChanged callbacks
  3081. local tmp = vim.api.nvim_create_buf(false, true)
  3082. vim.api.nvim_set_current_buf(tmp)
  3083. local triggered = {}
  3084. vim.api.nvim_create_autocmd('DiagnosticChanged', {
  3085. callback = function(args)
  3086. triggered = { args.buf, #args.data.diagnostics }
  3087. end,
  3088. })
  3089. vim.api.nvim_buf_set_name(_G.diagnostic_bufnr, 'test | test')
  3090. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  3091. _G.make_error('Diagnostic', 0, 0, 0, 0),
  3092. })
  3093. return {
  3094. triggered[1] == _G.diagnostic_bufnr,
  3095. triggered[2] == 1,
  3096. }
  3097. end)
  3098. )
  3099. end)
  3100. it('triggers the autocommand when diagnostics are cleared', function()
  3101. eq(
  3102. true,
  3103. exec_lua(function()
  3104. local tmp = vim.api.nvim_create_buf(false, true)
  3105. vim.api.nvim_set_current_buf(tmp)
  3106. vim.g.diagnostic_autocmd_triggered = 0
  3107. vim.cmd(
  3108. 'autocmd DiagnosticChanged * let g:diagnostic_autocmd_triggered = +expand("<abuf>")'
  3109. )
  3110. vim.api.nvim_buf_set_name(_G.diagnostic_bufnr, 'test | test')
  3111. vim.diagnostic.reset(_G.diagnostic_ns, _G.diagnostic_bufnr)
  3112. return vim.g.diagnostic_autocmd_triggered == _G.diagnostic_bufnr
  3113. end)
  3114. )
  3115. end)
  3116. it('is_enabled', function()
  3117. eq(
  3118. { false, false, false, false, false },
  3119. exec_lua(function()
  3120. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  3121. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  3122. })
  3123. vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
  3124. vim.diagnostic.enable(false)
  3125. return {
  3126. vim.diagnostic.is_enabled(),
  3127. vim.diagnostic.is_enabled { bufnr = 0 },
  3128. vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr },
  3129. vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns },
  3130. vim.diagnostic.is_enabled { bufnr = 0, ns_id = _G.diagnostic_ns },
  3131. }
  3132. end)
  3133. )
  3134. eq(
  3135. { true, true, true, true, true },
  3136. exec_lua(function()
  3137. vim.diagnostic.enable()
  3138. return {
  3139. vim.diagnostic.is_enabled(),
  3140. vim.diagnostic.is_enabled { bufnr = 0 },
  3141. vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr },
  3142. vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns },
  3143. vim.diagnostic.is_enabled { bufnr = 0, ns_id = _G.diagnostic_ns },
  3144. }
  3145. end)
  3146. )
  3147. end)
  3148. it('is_disabled (deprecated)', function()
  3149. eq(
  3150. { true, true, true, true },
  3151. exec_lua(function()
  3152. vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
  3153. _G.make_error('Diagnostic #1', 1, 1, 1, 1),
  3154. })
  3155. vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
  3156. vim.diagnostic.disable()
  3157. return {
  3158. vim.diagnostic.is_disabled(),
  3159. vim.diagnostic.is_disabled(_G.diagnostic_bufnr),
  3160. vim.diagnostic.is_disabled(_G.diagnostic_bufnr, _G.diagnostic_ns),
  3161. vim.diagnostic.is_disabled(0, _G.diagnostic_ns),
  3162. }
  3163. end)
  3164. )
  3165. eq(
  3166. { false, false, false, false },
  3167. exec_lua(function()
  3168. vim.diagnostic.enable()
  3169. return {
  3170. vim.diagnostic.is_disabled(),
  3171. vim.diagnostic.is_disabled(_G.diagnostic_bufnr),
  3172. vim.diagnostic.is_disabled(_G.diagnostic_bufnr, _G.diagnostic_ns),
  3173. vim.diagnostic.is_disabled(0, _G.diagnostic_ns),
  3174. }
  3175. end)
  3176. )
  3177. end)
  3178. end)
  3179. end)