1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712 |
- local t = require('test.testutil')
- local n = require('test.functional.testnvim')()
- local command = n.command
- local clear = n.clear
- local exec_lua = n.exec_lua
- local eq = t.eq
- local neq = t.neq
- local matches = t.matches
- local api = n.api
- local pcall_err = t.pcall_err
- local fn = n.fn
- describe('vim.diagnostic', function()
- before_each(function()
- clear()
- exec_lua(function()
- require('vim.diagnostic')
- local function make_diagnostic(msg, lnum, col, end_lnum, end_col, severity, source, code)
- return {
- lnum = lnum,
- col = col,
- end_lnum = end_lnum,
- end_col = end_col,
- message = msg,
- severity = severity,
- source = source,
- code = code,
- }
- end
- function _G.make_error(msg, lnum, col, end_lnum, end_col, source, code)
- return make_diagnostic(
- msg,
- lnum,
- col,
- end_lnum,
- end_col,
- vim.diagnostic.severity.ERROR,
- source,
- code
- )
- end
- function _G.make_warning(msg, lnum, col, end_lnum, end_col, source, code)
- return make_diagnostic(
- msg,
- lnum,
- col,
- end_lnum,
- end_col,
- vim.diagnostic.severity.WARN,
- source,
- code
- )
- end
- function _G.make_info(msg, lnum, col, end_lnum, end_col, source, code)
- return make_diagnostic(
- msg,
- lnum,
- col,
- end_lnum,
- end_col,
- vim.diagnostic.severity.INFO,
- source,
- code
- )
- end
- function _G.make_hint(msg, lnum, col, end_lnum, end_col, source, code)
- return make_diagnostic(
- msg,
- lnum,
- col,
- end_lnum,
- end_col,
- vim.diagnostic.severity.HINT,
- source,
- code
- )
- end
- function _G.count_diagnostics(bufnr, severity, namespace)
- return #vim.diagnostic.get(bufnr, { severity = severity, namespace = namespace })
- end
- function _G.count_extmarks(bufnr, namespace)
- local ns = vim.diagnostic.get_namespace(namespace)
- local extmarks = 0
- if ns.user_data.virt_text_ns then
- extmarks = extmarks
- + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.virt_text_ns, 0, -1, {})
- end
- if ns.user_data.underline_ns then
- extmarks = extmarks
- + #vim.api.nvim_buf_get_extmarks(bufnr, ns.user_data.underline_ns, 0, -1, {})
- end
- return extmarks
- end
- function _G.get_virt_text_extmarks(ns)
- ns = vim.diagnostic.get_namespace(ns)
- local virt_text_ns = ns.user_data.virt_text_ns
- return vim.api.nvim_buf_get_extmarks(
- _G.diagnostic_bufnr,
- virt_text_ns,
- 0,
- -1,
- { details = true }
- )
- end
- function _G.get_virt_lines_extmarks(ns)
- ns = vim.diagnostic.get_namespace(ns)
- local virt_lines_ns = ns.user_data.virt_lines_ns
- return vim.api.nvim_buf_get_extmarks(
- _G.diagnostic_bufnr,
- virt_lines_ns,
- 0,
- -1,
- { details = true }
- )
- end
- ---@param ns integer
- function _G.get_underline_extmarks(ns)
- ---@type integer
- local underline_ns = vim.diagnostic.get_namespace(ns).user_data.underline_ns
- return vim.api.nvim_buf_get_extmarks(
- _G.diagnostic_bufnr,
- underline_ns,
- 0,
- -1,
- { details = true }
- )
- end
- end)
- exec_lua(function()
- _G.diagnostic_ns = vim.api.nvim_create_namespace('diagnostic_spec')
- _G.other_ns = vim.api.nvim_create_namespace('other_namespace')
- _G.diagnostic_bufnr = vim.api.nvim_create_buf(true, false)
- local lines = { '1st line of text', '2nd line of text', 'wow', 'cool', 'more', 'lines' }
- vim.fn.bufload(_G.diagnostic_bufnr)
- vim.api.nvim_buf_set_lines(_G.diagnostic_bufnr, 0, 1, false, lines)
- end)
- end)
- it('creates highlight groups', function()
- command('runtime plugin/diagnostic.vim')
- eq({
- 'DiagnosticDeprecated',
- 'DiagnosticError',
- 'DiagnosticFloatingError',
- 'DiagnosticFloatingHint',
- 'DiagnosticFloatingInfo',
- 'DiagnosticFloatingOk',
- 'DiagnosticFloatingWarn',
- 'DiagnosticHint',
- 'DiagnosticInfo',
- 'DiagnosticOk',
- 'DiagnosticSignError',
- 'DiagnosticSignHint',
- 'DiagnosticSignInfo',
- 'DiagnosticSignOk',
- 'DiagnosticSignWarn',
- 'DiagnosticUnderlineError',
- 'DiagnosticUnderlineHint',
- 'DiagnosticUnderlineInfo',
- 'DiagnosticUnderlineOk',
- 'DiagnosticUnderlineWarn',
- 'DiagnosticUnnecessary',
- 'DiagnosticVirtualLinesError',
- 'DiagnosticVirtualLinesHint',
- 'DiagnosticVirtualLinesInfo',
- 'DiagnosticVirtualLinesOk',
- 'DiagnosticVirtualLinesWarn',
- 'DiagnosticVirtualTextError',
- 'DiagnosticVirtualTextHint',
- 'DiagnosticVirtualTextInfo',
- 'DiagnosticVirtualTextOk',
- 'DiagnosticVirtualTextWarn',
- 'DiagnosticWarn',
- }, fn.getcompletion('Diagnostic', 'highlight'))
- end)
- it('retrieves diagnostics from all buffers and namespaces', function()
- local result = exec_lua(function()
- local other_bufnr = vim.api.nvim_create_buf(true, false)
- local lines = vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, -1, true)
- vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- _G.make_error('Diagnostic #2', 2, 1, 2, 1),
- })
- vim.diagnostic.set(_G.other_ns, other_bufnr, {
- _G.make_error('Diagnostic #3', 3, 1, 3, 1),
- })
- return vim.diagnostic.get()
- end)
- eq(3, #result)
- eq(
- 2,
- exec_lua(function()
- return #vim.tbl_filter(function(d)
- return d.bufnr == _G.diagnostic_bufnr
- end, result)
- end)
- )
- eq('Diagnostic #1', result[1].message)
- end)
- it('removes diagnostics from the cache when a buffer is removed', function()
- eq(
- 2,
- exec_lua(function()
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- local other_bufnr = vim.fn.bufadd('test | test')
- local lines = vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, -1, true)
- vim.api.nvim_buf_set_lines(other_bufnr, 0, 1, false, lines)
- vim.cmd('bunload! ' .. other_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- _G.make_error('Diagnostic #2', 2, 1, 2, 1),
- })
- vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, {
- _G.make_error('Diagnostic #3', 3, 1, 3, 1),
- })
- vim.api.nvim_set_current_buf(other_bufnr)
- vim.opt_local.buflisted = true
- vim.cmd('bwipeout!')
- return #vim.diagnostic.get()
- end)
- )
- eq(
- 2,
- exec_lua(function()
- vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
- vim.opt_local.buflisted = false
- return #vim.diagnostic.get()
- end)
- )
- eq(
- 0,
- exec_lua(function()
- vim.cmd('bwipeout!')
- return #vim.diagnostic.get()
- end)
- )
- end)
- it('removes diagnostic from stale cache on reset', function()
- local diagnostics = exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- _G.make_error('Diagnostic #2', 2, 1, 2, 1),
- })
- vim.fn.bufadd('test | test')
- vim.cmd('noautocmd bwipeout! ' .. _G.diagnostic_bufnr)
- return vim.diagnostic.get(_G.diagnostic_bufnr)
- end)
- eq(2, #diagnostics)
- diagnostics = exec_lua(function()
- vim.diagnostic.reset()
- return vim.diagnostic.get()
- end)
- eq(0, #diagnostics)
- end)
- it('always returns a copy of diagnostic tables', function()
- local result = exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- })
- local diag = vim.diagnostic.get()
- diag[1].col = 10000
- return vim.diagnostic.get()[1].col == 10000
- end)
- eq(false, result)
- end)
- it('resolves buffer number 0 to the current buffer', function()
- eq(
- 2,
- exec_lua(function()
- vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- _G.make_error('Diagnostic #2', 2, 1, 2, 1),
- })
- return #vim.diagnostic.get(0)
- end)
- )
- end)
- it('saves and count a single error', function()
- eq(
- 1,
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- })
- return _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- )
- end)
- )
- end)
- it('saves and count multiple errors', function()
- eq(
- 2,
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- _G.make_error('Diagnostic #2', 2, 1, 2, 1),
- })
- return _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- )
- end)
- )
- end)
- it('saves and count from multiple namespaces', function()
- eq(
- { 1, 1, 2 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic From Server 1', 1, 1, 1, 1),
- })
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic From Server 2', 1, 1, 1, 1),
- })
- return {
- -- First namespace
- _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- ),
- -- Second namespace
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.other_ns),
- -- All namespaces
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR),
- }
- end)
- )
- end)
- it('saves and count from multiple namespaces with respect to severity', function()
- eq(
- { 3, 0, 3 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
- _G.make_error('Diagnostic From Server 1:2', 2, 2, 2, 2),
- _G.make_error('Diagnostic From Server 1:3', 2, 3, 3, 2),
- })
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Warning From Server 2', 3, 3, 3, 3),
- })
- return {
- -- Namespace 1
- _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- ),
- -- Namespace 2
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.other_ns),
- -- All namespaces
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.ERROR),
- }
- end)
- )
- end)
- it('handles one namespace clearing highlights while the other still has highlights', function()
- exec_lua(function()
- vim.diagnostic.config({ virtual_text = true })
- end)
- -- 1 Error (1)
- -- 1 Warning (2)
- -- 1 Warning (2) + 1 Warning (1)
- -- 2 highlights and 2 underlines (since error)
- -- 1 highlight + 1 underline
- local all_highlights = { 1, 1, 2, 4, 2 }
- eq(
- all_highlights,
- exec_lua(function()
- local ns_1_diags = {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 2, 1, 2, 3),
- }
- local ns_2_diags = {
- _G.make_warning('Warning 1', 2, 1, 2, 3),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
- return {
- _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- ),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
- }
- end)
- )
- -- Clear diagnostics from namespace 1, and make sure we have the right amount of stuff for namespace 2
- eq(
- { 1, 1, 2, 0, 2 },
- exec_lua(function()
- vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
- return {
- _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- ),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
- }
- end)
- )
- -- Show diagnostics from namespace 1 again
- eq(
- all_highlights,
- exec_lua(function()
- vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
- return {
- _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- ),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
- }
- end)
- )
- end)
- it('does not display diagnostics when disabled', function()
- exec_lua(function()
- vim.diagnostic.config({ virtual_text = true })
- end)
- eq(
- { 0, 2 },
- exec_lua(function()
- local ns_1_diags = {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 2, 1, 2, 3),
- }
- local ns_2_diags = {
- _G.make_warning('Warning 1', 2, 1, 2, 3),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
- vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
- return {
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
- }
- end)
- )
- eq(
- { 4, 0 },
- exec_lua(function()
- vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
- vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.other_ns })
- return {
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
- }
- end)
- )
- end)
- describe('show() and hide()', function()
- it('works', function()
- local result = exec_lua(function()
- local other_bufnr = vim.api.nvim_create_buf(true, false)
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- local result = {}
- vim.diagnostic.config({ underline = false, virtual_text = true })
- local ns_1_diags = {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
- }
- local ns_2_diags = {
- _G.make_warning('Warning 1', 2, 1, 2, 5),
- }
- local other_buffer_diags = {
- _G.make_info('This is interesting', 0, 0, 0, 0),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
- vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
- -- All buffers and namespaces
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- -- Hide one namespace
- vim.diagnostic.hide(_G.diagnostic_ns)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- -- Show one namespace
- vim.diagnostic.show(_G.diagnostic_ns)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- -- Hide one buffer
- vim.diagnostic.hide(nil, other_bufnr)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- -- Hide everything
- vim.diagnostic.hide()
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- -- Show one buffer
- vim.diagnostic.show(nil, _G.diagnostic_bufnr)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- return result
- end)
- eq(4, result[1])
- eq(1, result[2])
- eq(4, result[3])
- eq(3, result[4])
- eq(0, result[5])
- eq(3, result[6])
- end)
- it("doesn't error after bwipeout on buffer", function()
- exec_lua(function()
- vim.diagnostic.set(
- _G.diagnostic_ns,
- _G.diagnostic_bufnr,
- { { message = '', lnum = 0, end_lnum = 0, col = 0, end_col = 0 } }
- )
- vim.cmd('bwipeout! ' .. _G.diagnostic_bufnr)
- vim.diagnostic.show(_G.diagnostic_ns)
- vim.diagnostic.hide(_G.diagnostic_ns)
- end)
- end)
- end)
- describe('enable() and disable()', function()
- it('validation', function()
- matches('expected boolean, got table', pcall_err(exec_lua, [[vim.diagnostic.enable({})]]))
- matches(
- 'filter: expected table, got string',
- pcall_err(exec_lua, [[vim.diagnostic.enable(false, '')]])
- )
- matches(
- 'Invalid buffer id: 42',
- pcall_err(exec_lua, [[vim.diagnostic.enable(true, { bufnr = 42 })]])
- )
- matches(
- 'expected boolean, got number',
- pcall_err(exec_lua, [[vim.diagnostic.enable(42, {})]])
- )
- matches('expected boolean, got table', pcall_err(exec_lua, [[vim.diagnostic.enable({}, 42)]]))
- -- Deprecated signature.
- matches('Invalid buffer id: 42', pcall_err(exec_lua, [[vim.diagnostic.enable(42)]]))
- -- Deprecated signature.
- matches(
- 'namespace does not exist or is anonymous',
- pcall_err(exec_lua, [[vim.diagnostic.enable(nil, 42)]])
- )
- end)
- it('without arguments', function()
- local result = exec_lua(function()
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- local result = {}
- vim.diagnostic.config({ underline = false, virtual_text = true })
- local ns_1_diags = {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
- }
- local ns_2_diags = {
- _G.make_warning('Warning 1', 2, 1, 2, 5),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- )
- vim.diagnostic.enable(false)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- )
- -- Create a new buffer
- local other_bufnr = vim.api.nvim_create_buf(true, false)
- local other_buffer_diags = {
- _G.make_info('This is interesting', 0, 0, 0, 0),
- }
- vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- vim.diagnostic.enable()
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- return result
- end)
- eq(3, result[1])
- eq(0, result[2])
- eq(0, result[3])
- eq(4, result[4])
- end)
- it('with buffer argument', function()
- local result = exec_lua(function()
- local other_bufnr = vim.api.nvim_create_buf(true, false)
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- local result = {}
- vim.diagnostic.config({ underline = false, virtual_text = true })
- local ns_1_diags = {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
- }
- local ns_2_diags = {
- _G.make_warning('Warning 1', 2, 1, 2, 5),
- }
- local other_buffer_diags = {
- _G.make_info('This is interesting', 0, 0, 0, 0),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
- vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr })
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr })
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- vim.diagnostic.enable(false, { bufnr = other_bufnr })
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- return result
- end)
- eq(4, result[1])
- eq(1, result[2])
- eq(4, result[3])
- eq(3, result[4])
- end)
- it('with a namespace argument', function()
- local result = exec_lua(function()
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- local result = {}
- vim.diagnostic.config({ underline = false, virtual_text = true })
- local ns_1_diags = {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
- }
- local ns_2_diags = {
- _G.make_warning('Warning 1', 2, 1, 2, 5),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- )
- vim.diagnostic.enable(false, { ns_id = _G.diagnostic_ns })
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- )
- vim.diagnostic.enable(true, { ns_id = _G.diagnostic_ns })
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- )
- vim.diagnostic.enable(false, { ns_id = _G.other_ns })
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- )
- return result
- end)
- eq(3, result[1])
- eq(1, result[2])
- eq(3, result[3])
- eq(2, result[4])
- end)
- --- @return table
- local function test_enable(legacy)
- return exec_lua(function()
- local other_bufnr = vim.api.nvim_create_buf(true, false)
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- local result = {}
- vim.diagnostic.config({ underline = false, virtual_text = true })
- local ns_1_diags = {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 2, 1, 2, 5),
- }
- local ns_2_diags = {
- _G.make_warning('Warning 1', 2, 1, 2, 5),
- }
- local other_buffer_diags = {
- _G.make_info('This is interesting', 0, 0, 0, 0),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
- vim.diagnostic.set(_G.diagnostic_ns, other_bufnr, other_buffer_diags)
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- if legacy then
- vim.diagnostic.disable(_G.diagnostic_bufnr, _G.diagnostic_ns)
- else
- vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
- end
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- if legacy then
- vim.diagnostic.disable(_G.diagnostic_bufnr, _G.other_ns)
- else
- vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.other_ns })
- end
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- if legacy then
- vim.diagnostic.enable(_G.diagnostic_bufnr, _G.diagnostic_ns)
- else
- vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
- end
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- if legacy then
- -- Should have no effect
- vim.diagnostic.disable(other_bufnr, _G.other_ns)
- else
- -- Should have no effect
- vim.diagnostic.enable(false, { bufnr = other_bufnr, ns_id = _G.other_ns })
- end
- table.insert(
- result,
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- + _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns)
- + _G.count_extmarks(other_bufnr, _G.diagnostic_ns)
- )
- return result
- end)
- end
- it('with both buffer and namespace arguments', function()
- local result = test_enable(false)
- eq(4, result[1])
- eq(2, result[2])
- eq(1, result[3])
- eq(3, result[4])
- eq(3, result[5])
- end)
- it('with both buffer and namespace arguments (deprecated signature)', function()
- -- Exercise the legacy/deprecated signature.
- local result = test_enable(true)
- eq(4, result[1])
- eq(2, result[2])
- eq(1, result[3])
- eq(3, result[4])
- eq(3, result[5])
- end)
- end)
- describe('reset()', function()
- it('diagnostic count is 0 and displayed diagnostics are 0 after call', function()
- exec_lua(function()
- vim.diagnostic.config({ virtual_text = true })
- end)
- -- 1 Error (1)
- -- 1 Warning (2)
- -- 1 Warning (2) + 1 Warning (1)
- -- 2 highlights and 2 underlines (since error)
- -- 1 highlight + 1 underline
- local all_highlights = { 1, 1, 2, 4, 2 }
- eq(
- all_highlights,
- exec_lua(function()
- local ns_1_diags = {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 2, 1, 2, 3),
- }
- local ns_2_diags = {
- _G.make_warning('Warning 1', 2, 1, 2, 3),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diags)
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diags)
- return {
- _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- ),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
- }
- end)
- )
- -- Reset diagnostics from namespace 1
- exec_lua([[ vim.diagnostic.reset( _G.diagnostic_ns) ]])
- -- Make sure we have the right diagnostic count
- eq(
- { 0, 1, 1, 0, 2 },
- exec_lua(function()
- local diagnostic_count = {}
- vim.wait(100, function()
- diagnostic_count = {
- _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- ),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
- }
- end)
- return diagnostic_count
- end)
- )
- -- Reset diagnostics from namespace 2
- exec_lua([[ vim.diagnostic.reset(_G.other_ns) ]])
- -- Make sure we have the right diagnostic count
- eq(
- { 0, 0, 0, 0, 0 },
- exec_lua(function()
- local diagnostic_count = {}
- vim.wait(100, function()
- diagnostic_count = {
- _G.count_diagnostics(
- _G.diagnostic_bufnr,
- vim.diagnostic.severity.ERROR,
- _G.diagnostic_ns
- ),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN, _G.other_ns),
- _G.count_diagnostics(_G.diagnostic_bufnr, vim.diagnostic.severity.WARN),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns),
- _G.count_extmarks(_G.diagnostic_bufnr, _G.other_ns),
- }
- end)
- return diagnostic_count
- end)
- )
- end)
- it("doesn't error after bwipeout called on buffer", function()
- exec_lua(function()
- vim.diagnostic.set(
- _G.diagnostic_ns,
- _G.diagnostic_bufnr,
- { { message = '', lnum = 0, end_lnum = 0, col = 0, end_col = 0 } }
- )
- vim.cmd('bwipeout! ' .. _G.diagnostic_bufnr)
- vim.diagnostic.reset(_G.diagnostic_ns)
- end)
- end)
- end)
- describe('get_next()', function()
- it('can find the next pos with only one namespace', function()
- eq(
- { 1, 1 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- local next = vim.diagnostic.get_next()
- return { next.lnum, next.col }
- end)
- )
- end)
- it('can find next pos with two errors', function()
- eq(
- { 4, 4 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- _G.make_error('Diagnostic #2', 4, 4, 4, 4),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 3, 1 })
- local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
- return { next.lnum, next.col }
- end)
- )
- end)
- it('can cycle when position is past error', function()
- eq(
- { 1, 1 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 3, 1 })
- local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
- return { next.lnum, next.col }
- end)
- )
- end)
- it('will not cycle when wrap is off', function()
- eq(
- nil,
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 3, 1 })
- local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns, wrap = false })
- return next
- end)
- )
- end)
- it('can cycle even from the last line', function()
- eq(
- { 4, 4 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #2', 4, 4, 4, 4),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { vim.api.nvim_buf_line_count(0), 1 })
- local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns })
- return { prev.lnum, prev.col }
- end)
- )
- end)
- it('works with diagnostics past the end of the line #16349', function()
- eq(
- { 4, 0 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 3, 9001, 3, 9001),
- _G.make_error('Diagnostic #2', 4, 0, 4, 0),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 1, 1 })
- vim.diagnostic.jump({ count = 1, float = false })
- local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
- return { next.lnum, next.col }
- end)
- )
- end)
- it('works with diagnostics before the start of the line', function()
- eq(
- { 4, 0 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 3, 9001, 3, 9001),
- _G.make_error('Diagnostic #2', 4, -1, 4, -1),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 1, 1 })
- vim.diagnostic.jump({ count = 1, float = false })
- local next = vim.diagnostic.get_next({ namespace = _G.diagnostic_ns })
- return { next.lnum, next.col }
- end)
- )
- end)
- it('jumps to diagnostic with highest severity', function()
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_info('Info', 1, 0, 1, 1),
- _G.make_error('Error', 2, 0, 2, 1),
- _G.make_warning('Warning', 3, 0, 3, 1),
- _G.make_error('Error', 4, 0, 4, 1),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- end)
- eq(
- { 3, 0 },
- exec_lua(function()
- vim.diagnostic.jump({ count = 1, _highest = true })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 5, 0 },
- exec_lua(function()
- vim.diagnostic.jump({ count = 1, _highest = true })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_info('Info', 1, 0, 1, 1),
- _G.make_hint('Hint', 2, 0, 2, 1),
- _G.make_warning('Warning', 3, 0, 3, 1),
- _G.make_hint('Hint', 4, 0, 4, 1),
- _G.make_warning('Warning', 5, 0, 5, 1),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- end)
- eq(
- { 4, 0 },
- exec_lua(function()
- vim.diagnostic.jump({ count = 1, _highest = true })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 6, 0 },
- exec_lua(function()
- vim.diagnostic.jump({ count = 1, _highest = true })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- end)
- it('jumps to next diagnostic if severity is non-nil', function()
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_info('Info', 1, 0, 1, 1),
- _G.make_error('Error', 2, 0, 2, 1),
- _G.make_warning('Warning', 3, 0, 3, 1),
- _G.make_error('Error', 4, 0, 4, 1),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- end)
- eq(
- { 2, 0 },
- exec_lua(function()
- vim.diagnostic.jump({ count = 1 })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 3, 0 },
- exec_lua(function()
- vim.diagnostic.jump({ count = 1 })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 4, 0 },
- exec_lua(function()
- vim.diagnostic.jump({ count = 1 })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- end)
- end)
- describe('get_prev()', function()
- it('can find the previous diagnostic with only one namespace', function()
- eq(
- { 1, 1 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 3, 1 })
- local prev = vim.diagnostic.get_prev()
- return { prev.lnum, prev.col }
- end)
- )
- end)
- it('can find the previous diagnostic with two errors', function()
- eq(
- { 1, 1 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- _G.make_error('Diagnostic #2', 4, 4, 4, 4),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 3, 1 })
- local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns })
- return { prev.lnum, prev.col }
- end)
- )
- end)
- it('can cycle when position is past error', function()
- eq(
- { 4, 4 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #2', 4, 4, 4, 4),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 3, 1 })
- local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns })
- return { prev.lnum, prev.col }
- end)
- )
- end)
- it('respects wrap parameter', function()
- eq(
- nil,
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #2', 4, 4, 4, 4),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.api.nvim_win_set_cursor(0, { 3, 1 })
- local prev = vim.diagnostic.get_prev({ namespace = _G.diagnostic_ns, wrap = false })
- return prev
- end)
- )
- end)
- it('works on blank line #28397', function()
- eq(
- { 0, 2 },
- exec_lua(function()
- local test_bufnr = vim.api.nvim_create_buf(true, false)
- vim.api.nvim_buf_set_lines(test_bufnr, 0, -1, false, {
- 'first line',
- '',
- '',
- 'end line',
- })
- vim.diagnostic.set(_G.diagnostic_ns, test_bufnr, {
- _G.make_info('Diagnostic #1', 0, 2, 0, 2),
- _G.make_info('Diagnostic #2', 2, 0, 2, 0),
- _G.make_info('Diagnostic #3', 2, 0, 2, 0),
- })
- vim.api.nvim_win_set_buf(0, test_bufnr)
- vim.api.nvim_win_set_cursor(0, { 3, 0 })
- return vim.diagnostic.get_prev_pos { namespace = _G.diagnostic_ns }
- end)
- )
- end)
- end)
- describe('jump()', function()
- before_each(function()
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 0, 0, 0, 2),
- _G.make_error('Diagnostic #2', 1, 1, 1, 4),
- _G.make_warning('Diagnostic #3', 2, -1, 2, -1),
- _G.make_info('Diagnostic #4', 3, 0, 3, 3),
- })
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- end)
- end)
- it('can move forward', function()
- eq(
- { 2, 1 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- vim.diagnostic.jump({ count = 1 })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 4, 0 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- vim.diagnostic.jump({ count = 3 })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 4, 0 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- vim.diagnostic.jump({ count = math.huge, wrap = false })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- end)
- it('can move backward', function()
- eq(
- { 3, 0 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 4, 0 })
- vim.diagnostic.jump({ count = -1 })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 1, 0 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 4, 0 })
- vim.diagnostic.jump({ count = -3 })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 1, 0 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 4, 0 })
- vim.diagnostic.jump({ count = -math.huge, wrap = false })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- end)
- it('can filter by severity', function()
- eq(
- { 3, 0 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- vim.diagnostic.jump({ count = 1, severity = vim.diagnostic.severity.WARN })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 3, 0 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- vim.diagnostic.jump({ count = 9999, severity = vim.diagnostic.severity.WARN })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- end)
- it('can wrap', function()
- eq(
- { 1, 0 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 4, 0 })
- vim.diagnostic.jump({ count = 1, wrap = true })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- eq(
- { 4, 0 },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- vim.diagnostic.jump({ count = -1, wrap = true })
- return vim.api.nvim_win_get_cursor(0)
- end)
- )
- end)
- end)
- describe('get()', function()
- it('returns an empty table when no diagnostics are present', function()
- eq(
- {},
- exec_lua [[return vim.diagnostic.get( _G.diagnostic_bufnr, {namespace=diagnostic_ns})]]
- )
- end)
- it('returns all diagnostics when no severity is supplied', function()
- eq(
- 2,
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
- })
- return #vim.diagnostic.get(_G.diagnostic_bufnr)
- end)
- )
- end)
- it('returns only requested diagnostics when severity range is supplied', function()
- eq(
- { 2, 3, 2 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
- _G.make_info('Ignored information', 1, 1, 2, 3),
- _G.make_hint("Here's a hint", 1, 1, 2, 3),
- })
- return {
- #vim.diagnostic.get(
- _G.diagnostic_bufnr,
- { severity = { min = vim.diagnostic.severity.WARN } }
- ),
- #vim.diagnostic.get(
- _G.diagnostic_bufnr,
- { severity = { max = vim.diagnostic.severity.WARN } }
- ),
- #vim.diagnostic.get(_G.diagnostic_bufnr, {
- severity = {
- min = vim.diagnostic.severity.INFO,
- max = vim.diagnostic.severity.WARN,
- },
- }),
- }
- end)
- )
- end)
- it('returns only requested diagnostics when severities are supplied', function()
- eq(
- { 1, 1, 2 },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
- _G.make_info('Ignored information', 1, 1, 2, 3),
- _G.make_hint("Here's a hint", 1, 1, 2, 3),
- })
- return {
- #vim.diagnostic.get(
- _G.diagnostic_bufnr,
- { severity = { vim.diagnostic.severity.WARN } }
- ),
- #vim.diagnostic.get(
- _G.diagnostic_bufnr,
- { severity = { vim.diagnostic.severity.ERROR } }
- ),
- #vim.diagnostic.get(_G.diagnostic_bufnr, {
- severity = {
- vim.diagnostic.severity.INFO,
- vim.diagnostic.severity.WARN,
- },
- }),
- }
- end)
- )
- end)
- it('allows filtering by line', function()
- eq(
- 2,
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
- _G.make_info('Ignored information', 1, 1, 2, 3),
- _G.make_error('Error On Other Line', 3, 1, 3, 5),
- })
- return #vim.diagnostic.get(_G.diagnostic_bufnr, { lnum = 2 })
- end)
- )
- end)
- end)
- describe('count', function()
- it('returns actually present severity counts', function()
- eq(
- exec_lua(function()
- return {
- [vim.diagnostic.severity.ERROR] = 4,
- [vim.diagnostic.severity.WARN] = 3,
- [vim.diagnostic.severity.INFO] = 2,
- [vim.diagnostic.severity.HINT] = 1,
- }
- end),
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 1, 1, 1, 2),
- _G.make_error('Error 2', 1, 3, 1, 4),
- _G.make_error('Error 3', 1, 5, 1, 6),
- _G.make_error('Error 4', 1, 7, 1, 8),
- _G.make_warning('Warning 1', 2, 1, 2, 2),
- _G.make_warning('Warning 2', 2, 3, 2, 4),
- _G.make_warning('Warning 3', 2, 5, 2, 6),
- _G.make_info('Info 1', 3, 1, 3, 2),
- _G.make_info('Info 2', 3, 3, 3, 4),
- _G.make_hint('Hint 1', 4, 1, 4, 2),
- })
- return vim.diagnostic.count(_G.diagnostic_bufnr)
- end)
- )
- eq(
- exec_lua(function()
- return {
- [vim.diagnostic.severity.ERROR] = 2,
- [vim.diagnostic.severity.INFO] = 1,
- }
- end),
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 1, 1, 1, 2),
- _G.make_error('Error 2', 1, 3, 1, 4),
- _G.make_info('Info 1', 3, 1, 3, 2),
- })
- return vim.diagnostic.count(_G.diagnostic_bufnr)
- end)
- )
- end)
- it('returns only requested diagnostics count when severity range is supplied', function()
- eq(
- exec_lua(function()
- return {
- { [vim.diagnostic.severity.ERROR] = 1, [vim.diagnostic.severity.WARN] = 1 },
- {
- [vim.diagnostic.severity.WARN] = 1,
- [vim.diagnostic.severity.INFO] = 1,
- [vim.diagnostic.severity.HINT] = 1,
- },
- { [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1 },
- }
- end),
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
- _G.make_info('Ignored information', 1, 1, 2, 3),
- _G.make_hint("Here's a hint", 1, 1, 2, 3),
- })
- return {
- vim.diagnostic.count(
- _G.diagnostic_bufnr,
- { severity = { min = vim.diagnostic.severity.WARN } }
- ),
- vim.diagnostic.count(
- _G.diagnostic_bufnr,
- { severity = { max = vim.diagnostic.severity.WARN } }
- ),
- vim.diagnostic.count(_G.diagnostic_bufnr, {
- severity = {
- min = vim.diagnostic.severity.INFO,
- max = vim.diagnostic.severity.WARN,
- },
- }),
- }
- end)
- )
- end)
- it('returns only requested diagnostics when severities are supplied', function()
- eq(
- exec_lua(function()
- return {
- { [vim.diagnostic.severity.WARN] = 1 },
- { [vim.diagnostic.severity.ERROR] = 1 },
- { [vim.diagnostic.severity.WARN] = 1, [vim.diagnostic.severity.INFO] = 1 },
- }
- end),
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
- _G.make_info('Ignored information', 1, 1, 2, 3),
- _G.make_hint("Here's a hint", 1, 1, 2, 3),
- })
- return {
- vim.diagnostic.count(
- _G.diagnostic_bufnr,
- { severity = { vim.diagnostic.severity.WARN } }
- ),
- vim.diagnostic.count(
- _G.diagnostic_bufnr,
- { severity = { vim.diagnostic.severity.ERROR } }
- ),
- vim.diagnostic.count(_G.diagnostic_bufnr, {
- severity = {
- vim.diagnostic.severity.INFO,
- vim.diagnostic.severity.WARN,
- },
- }),
- }
- end)
- )
- end)
- it('allows filtering by line', function()
- eq(
- exec_lua(function()
- return {
- [vim.diagnostic.severity.WARN] = 1,
- [vim.diagnostic.severity.INFO] = 1,
- }
- end),
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 1, 1, 1, 5),
- _G.make_warning('Warning on Server 1', 1, 1, 2, 3),
- _G.make_info('Ignored information', 1, 1, 2, 3),
- _G.make_error('Error On Other Line', 3, 1, 3, 5),
- })
- return vim.diagnostic.count(_G.diagnostic_bufnr, { lnum = 2 })
- end)
- )
- end)
- end)
- describe('config()', function()
- it('works with global, namespace, and ephemeral options', function()
- eq(
- 1,
- exec_lua(function()
- vim.diagnostic.config({
- virtual_text = false,
- })
- vim.diagnostic.config({
- virtual_text = true,
- underline = false,
- }, _G.diagnostic_ns)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Some Error', 4, 4, 4, 4),
- })
- return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- end)
- )
- eq(
- 1,
- exec_lua(function()
- vim.diagnostic.config({
- virtual_text = false,
- })
- vim.diagnostic.config({
- virtual_text = false,
- underline = false,
- }, _G.diagnostic_ns)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Some Error', 4, 4, 4, 4),
- }, { virtual_text = true })
- return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- end)
- )
- eq(
- 0,
- exec_lua(function()
- vim.diagnostic.config({
- virtual_text = false,
- })
- vim.diagnostic.config({
- virtual_text = { severity = vim.diagnostic.severity.ERROR },
- underline = false,
- }, _G.diagnostic_ns)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Some Warning', 4, 4, 4, 4),
- }, { virtual_text = true })
- return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- end)
- )
- eq(
- 1,
- exec_lua(function()
- vim.diagnostic.config({
- virtual_text = false,
- })
- vim.diagnostic.config({
- virtual_text = { severity = vim.diagnostic.severity.ERROR },
- underline = false,
- }, _G.diagnostic_ns)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Some Warning', 4, 4, 4, 4),
- }, {
- virtual_text = {}, -- An empty table uses default values
- })
- return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- end)
- )
- end)
- it('can use functions for config values', function()
- exec_lua(function()
- vim.diagnostic.config({
- virtual_text = function()
- return true
- end,
- }, _G.diagnostic_ns)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
- })
- end)
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- -- Now, don't enable virtual text.
- -- We should have one less extmark displayed.
- exec_lua(function()
- vim.diagnostic.config({
- virtual_text = function()
- return false
- end,
- }, _G.diagnostic_ns)
- end)
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(1, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- end)
- it('allows filtering by severity', function()
- local get_extmark_count_with_severity = function(min_severity)
- return exec_lua(function()
- vim.diagnostic.config({
- underline = false,
- virtual_text = {
- severity = { min = min_severity },
- },
- })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Delayed Diagnostic', 4, 4, 4, 4),
- })
- return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- end)
- end
- -- No messages with Error or higher
- eq(0, get_extmark_count_with_severity('ERROR'))
- -- But now we don't filter it
- eq(1, get_extmark_count_with_severity('WARN'))
- eq(1, get_extmark_count_with_severity('HINT'))
- end)
- it('allows sorting by severity', function()
- exec_lua(function()
- vim.diagnostic.config({
- underline = false,
- signs = true,
- virtual_text = true,
- })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Warning', 4, 4, 4, 4),
- _G.make_error('Error', 4, 4, 4, 4),
- _G.make_info('Info', 4, 4, 4, 4),
- })
- function _G.get_highest_underline_hl(severity_sort)
- vim.diagnostic.config({
- underline = true,
- severity_sort = severity_sort,
- })
- local extmarks = _G.get_underline_extmarks(_G.diagnostic_ns)
- table.sort(extmarks, function(a, b)
- return a[4].priority > b[4].priority
- end)
- return extmarks[1][4].hl_group
- end
- function _G.get_virt_text_and_signs(severity_sort)
- vim.diagnostic.config({
- severity_sort = severity_sort,
- })
- local virt_text = _G.get_virt_text_extmarks(_G.diagnostic_ns)[1][4].virt_text
- local virt_texts = {}
- for i = 2, #virt_text - 1 do
- table.insert(virt_texts, (string.gsub(virt_text[i][2], 'DiagnosticVirtualText', '')))
- end
- local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
- local sign_ns = ns.user_data.sign_ns
- local signs = {}
- local all_signs = vim.api.nvim_buf_get_extmarks(
- _G.diagnostic_bufnr,
- sign_ns,
- 0,
- -1,
- { type = 'sign', details = true }
- )
- table.sort(all_signs, function(a, b)
- return a[1] > b[1]
- end)
- for _, v in ipairs(all_signs) do
- local s = v[4].sign_hl_group:gsub('DiagnosticSign', '')
- if not vim.tbl_contains(signs, s) then
- signs[#signs + 1] = s
- end
- end
- return { virt_texts, signs }
- end
- end)
- local result = exec_lua [[return _G.get_virt_text_and_signs(false)]]
- -- Virt texts are defined lowest priority to highest, signs from
- -- highest to lowest
- eq({ 'Warn', 'Error', 'Info' }, result[1])
- eq({ 'Info', 'Error', 'Warn' }, result[2])
- result = exec_lua [[return _G.get_virt_text_and_signs(true)]]
- eq({ 'Info', 'Warn', 'Error' }, result[1])
- eq({ 'Error', 'Warn', 'Info' }, result[2])
- result = exec_lua [[return _G.get_virt_text_and_signs({ reverse = true })]]
- eq({ 'Error', 'Warn', 'Info' }, result[1])
- eq({ 'Info', 'Warn', 'Error' }, result[2])
- local underline_hl = exec_lua [[return _G.get_highest_underline_hl(true)]]
- eq('DiagnosticUnderlineError', underline_hl)
- underline_hl = exec_lua [[return _G.get_highest_underline_hl({ reverse = true })]]
- eq('DiagnosticUnderlineInfo', underline_hl)
- end)
- it('can show diagnostic sources in virtual text', function()
- local result = exec_lua(function()
- local diagnostics = {
- _G.make_error('Some error', 0, 0, 0, 0, 'source x'),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
- underline = false,
- virtual_text = {
- prefix = '',
- source = 'always',
- },
- })
- local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
- local virt_text = extmarks[1][4].virt_text[3][1]
- return virt_text
- end)
- eq(' source x: Some error', result)
- result = exec_lua(function()
- vim.diagnostic.config({
- underline = false,
- virtual_text = {
- prefix = '',
- source = 'if_many',
- },
- }, _G.diagnostic_ns)
- local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
- local virt_text = extmarks[1][4].virt_text[3][1]
- return virt_text
- end)
- eq(' Some error', result)
- result = exec_lua(function()
- local diagnostics = {
- _G.make_error('Some error', 0, 0, 0, 0, 'source x'),
- _G.make_error('Another error', 1, 1, 1, 1, 'source y'),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
- underline = false,
- virtual_text = {
- prefix = '',
- source = 'if_many',
- },
- })
- local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
- local virt_text = { extmarks[1][4].virt_text[3][1], extmarks[2][4].virt_text[3][1] }
- return virt_text
- end)
- eq(' source x: Some error', result[1])
- eq(' source y: Another error', result[2])
- end)
- it('supports a format function for diagnostic messages', function()
- local result = exec_lua(function()
- vim.diagnostic.config({
- underline = false,
- virtual_text = {
- prefix = '',
- format = function(diagnostic)
- if diagnostic.severity == vim.diagnostic.severity.ERROR then
- return string.format('🔥 %s', diagnostic.message)
- end
- return string.format('👀 %s', diagnostic.message)
- end,
- },
- })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Warning', 0, 0, 0, 0),
- _G.make_error('Error', 1, 0, 1, 0),
- })
- local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
- return { extmarks[1][4].virt_text, extmarks[2][4].virt_text }
- end)
- eq(' 👀 Warning', result[1][3][1])
- eq(' 🔥 Error', result[2][3][1])
- end)
- it('includes source for formatted diagnostics', function()
- local result = exec_lua(function()
- vim.diagnostic.config({
- underline = false,
- virtual_text = {
- prefix = '',
- source = 'always',
- format = function(diagnostic)
- if diagnostic.severity == vim.diagnostic.severity.ERROR then
- return string.format('🔥 %s', diagnostic.message)
- end
- return string.format('👀 %s', diagnostic.message)
- end,
- },
- })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Warning', 0, 0, 0, 0, 'some_linter'),
- _G.make_error('Error', 1, 0, 1, 0, 'another_linter'),
- })
- local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
- return { extmarks[1][4].virt_text, extmarks[2][4].virt_text }
- end)
- eq(' some_linter: 👀 Warning', result[1][3][1])
- eq(' another_linter: 🔥 Error', result[2][3][1])
- end)
- it('can add a prefix to virtual text', function()
- eq(
- 'E Some error',
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Some error', 0, 0, 0, 0),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
- underline = false,
- virtual_text = {
- prefix = 'E',
- suffix = '',
- },
- })
- local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
- local prefix = extmarks[1][4].virt_text[2][1]
- local message = extmarks[1][4].virt_text[3][1]
- return prefix .. message
- end)
- )
- eq(
- '[(1/1) err-code] Some error',
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Some error', 0, 0, 0, 0, nil, 'err-code'),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
- underline = false,
- virtual_text = {
- prefix = function(diag, i, total)
- return string.format('[(%d/%d) %s]', i, total, diag.code)
- end,
- suffix = '',
- },
- })
- local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
- local prefix = extmarks[1][4].virt_text[2][1]
- local message = extmarks[1][4].virt_text[3][1]
- return prefix .. message
- end)
- )
- end)
- it('can add a suffix to virtual text', function()
- eq(
- ' Some error ✘',
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Some error', 0, 0, 0, 0),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
- underline = false,
- virtual_text = {
- prefix = '',
- suffix = ' ✘',
- },
- })
- local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
- local virt_text = extmarks[1][4].virt_text[3][1]
- return virt_text
- end)
- )
- eq(
- ' Some error [err-code]',
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Some error', 0, 0, 0, 0, nil, 'err-code'),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics, {
- underline = false,
- virtual_text = {
- prefix = '',
- suffix = function(diag)
- return string.format(' [%s]', diag.code)
- end,
- },
- })
- local extmarks = _G.get_virt_text_extmarks(_G.diagnostic_ns)
- local virt_text = extmarks[1][4].virt_text[3][1]
- return virt_text
- end)
- )
- end)
- end)
- describe('handlers.virtual_lines', function()
- it('includes diagnostic code and message', function()
- local result = exec_lua(function()
- vim.diagnostic.config({ virtual_lines = true })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Missed symbol `,`', 0, 0, 0, 0, 'lua_ls', 'miss-symbol'),
- })
- local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
- return extmarks[1][4].virt_lines
- end)
- eq('miss-symbol: Missed symbol `,`', result[1][3][1])
- end)
- it('adds space to the left of the diagnostic', function()
- local error_offset = 5
- local result = exec_lua(function()
- vim.diagnostic.config({ virtual_lines = true })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error here!', 0, error_offset, 0, error_offset, 'foo_server'),
- })
- local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
- return extmarks[1][4].virt_lines
- end)
- eq(error_offset, result[1][1][1]:len())
- end)
- it('highlights diagnostics in multiple lines by default', function()
- local result = exec_lua(function()
- vim.diagnostic.config({ virtual_lines = true })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error here!', 0, 0, 0, 0, 'foo_server'),
- _G.make_error('Another error there!', 1, 0, 1, 0, 'foo_server'),
- })
- local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
- return extmarks
- end)
- eq(2, #result)
- eq('Error here!', result[1][4].virt_lines[1][3][1])
- eq('Another error there!', result[2][4].virt_lines[1][3][1])
- end)
- it('can highlight diagnostics only in the current line', function()
- local result = exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- vim.diagnostic.config({ virtual_lines = { current_line = true } })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error here!', 0, 0, 0, 0, 'foo_server'),
- _G.make_error('Another error there!', 1, 0, 1, 0, 'foo_server'),
- })
- local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
- return extmarks
- end)
- eq(1, #result)
- eq('Error here!', result[1][4].virt_lines[1][3][1])
- end)
- it('supports a format function for diagnostic messages', function()
- local result = exec_lua(function()
- vim.diagnostic.config({
- virtual_lines = {
- format = function()
- return 'Error here!'
- end,
- },
- })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Invalid syntax', 0, 0, 0, 0),
- })
- local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns)
- return extmarks[1][4].virt_lines
- end)
- eq('Error here!', result[1][3][1])
- end)
- end)
- describe('set()', function()
- it('validation', function()
- matches(
- 'expected a list of diagnostics',
- pcall_err(exec_lua, [[vim.diagnostic.set(1, 0, {lnum = 1, col = 2})]])
- )
- end)
- it('can perform updates after insert_leave', function()
- exec_lua(function()
- vim.diagnostic.config({ virtual_text = true })
- vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
- end)
- api.nvim_input('o')
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- -- Save the diagnostics
- exec_lua(function()
- vim.diagnostic.config({
- update_in_insert = false,
- })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
- })
- end)
- -- No diagnostics displayed yet.
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(0, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- api.nvim_input('<esc>')
- eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- end)
- it('does not perform updates when not needed', function()
- exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]]
- api.nvim_input('o')
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- -- Save the diagnostics
- exec_lua(function()
- vim.diagnostic.config({
- update_in_insert = false,
- virtual_text = true,
- })
- _G.DisplayCount = 0
- local set_virtual_text = vim.diagnostic.handlers.virtual_text.show
- vim.diagnostic.handlers.virtual_text.show = function(...)
- _G.DisplayCount = _G.DisplayCount + 1
- return set_virtual_text(...)
- end
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
- })
- end)
- -- No diagnostics displayed yet.
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(0, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- eq(0, exec_lua [[return _G.DisplayCount]])
- api.nvim_input('<esc>')
- eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- eq(1, exec_lua [[return _G.DisplayCount]])
- -- Go in and out of insert mode one more time.
- api.nvim_input('o')
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- api.nvim_input('<esc>')
- eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
- -- Should not have set the virtual text again.
- eq(1, exec_lua [[return _G.DisplayCount]])
- end)
- it('never sets virtual text, in combination with insert leave', function()
- exec_lua [[vim.api.nvim_set_current_buf( _G.diagnostic_bufnr)]]
- api.nvim_input('o')
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- -- Save the diagnostics
- exec_lua(function()
- vim.diagnostic.config({
- update_in_insert = false,
- virtual_text = false,
- })
- _G.DisplayCount = 0
- local set_virtual_text = vim.diagnostic.handlers.virtual_text.show
- vim.diagnostic.handlers.virtual_text.show = function(...)
- _G.DisplayCount = _G.DisplayCount + 1
- return set_virtual_text(...)
- end
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
- })
- end)
- -- No diagnostics displayed yet.
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(0, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- eq(0, exec_lua [[return _G.DisplayCount]])
- api.nvim_input('<esc>')
- eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(1, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- eq(0, exec_lua [[return _G.DisplayCount]])
- -- Go in and out of insert mode one more time.
- api.nvim_input('o')
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- api.nvim_input('<esc>')
- eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
- -- Should not have set the virtual text still.
- eq(0, exec_lua [[return _G.DisplayCount]])
- end)
- it('can perform updates while in insert mode, if desired', function()
- exec_lua(function()
- vim.diagnostic.config({ virtual_text = true })
- vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
- end)
- api.nvim_input('o')
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- -- Save the diagnostics
- exec_lua(function()
- vim.diagnostic.config({
- update_in_insert = true,
- })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Delayed Diagnostic', 4, 4, 4, 4),
- })
- end)
- -- Diagnostics are displayed, because the user wanted them that way!
- eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- api.nvim_input('<esc>')
- eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
- eq(
- 1,
- exec_lua [[return _G.count_diagnostics( _G.diagnostic_bufnr, vim.diagnostic.severity.ERROR, _G.diagnostic_ns)]]
- )
- eq(2, exec_lua [[return _G.count_extmarks( _G.diagnostic_bufnr, _G.diagnostic_ns)]])
- end)
- it('can set diagnostics without displaying them', function()
- exec_lua(function()
- vim.diagnostic.config({ virtual_text = true })
- end)
- eq(
- 0,
- exec_lua(function()
- vim.diagnostic.enable(false, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
- })
- return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- end)
- )
- eq(
- 2,
- exec_lua(function()
- vim.diagnostic.enable(true, { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns })
- return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- end)
- )
- end)
- it('can set display options', function()
- eq(
- 0,
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
- }, { virtual_text = false, underline = false })
- return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- end)
- )
- eq(
- 1,
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic From Server 1:1', 1, 1, 1, 1),
- }, { virtual_text = true, underline = false })
- return _G.count_extmarks(_G.diagnostic_bufnr, _G.diagnostic_ns)
- end)
- )
- end)
- it('sets and clears signs #26193 #26555', function()
- do
- local result = exec_lua(function()
- vim.diagnostic.config({
- signs = true,
- })
- local diagnostics = {
- _G.make_error('Error', 1, 1, 1, 2),
- _G.make_warning('Warning', 3, 3, 3, 3),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
- local sign_ns = ns.user_data.sign_ns
- local signs = vim.api.nvim_buf_get_extmarks(
- _G.diagnostic_bufnr,
- sign_ns,
- 0,
- -1,
- { type = 'sign', details = true }
- )
- local result = {}
- for _, s in ipairs(signs) do
- result[#result + 1] = { lnum = s[2] + 1, name = s[4].sign_hl_group }
- end
- return result
- end)
- eq({ 2, 'DiagnosticSignError' }, { result[1].lnum, result[1].name })
- eq({ 4, 'DiagnosticSignWarn' }, { result[2].lnum, result[2].name })
- end
- do
- local result = exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {})
- local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
- local sign_ns = ns.user_data.sign_ns
- return vim.api.nvim_buf_get_extmarks(
- _G.diagnostic_bufnr,
- sign_ns,
- 0,
- -1,
- { type = 'sign', details = true }
- )
- end)
- eq({}, result)
- end
- end)
- it('respects legacy signs placed with :sign define or sign_define #26618', function()
- -- Legacy signs for diagnostics were deprecated in 0.10 and will be removed in 0.12
- eq(0, n.fn.has('nvim-0.12'))
- n.command('sign define DiagnosticSignError text= texthl= linehl=ErrorMsg numhl=ErrorMsg')
- n.command('sign define DiagnosticSignWarn text= texthl= linehl=WarningMsg numhl=WarningMsg')
- n.command('sign define DiagnosticSignInfo text= texthl= linehl=Underlined numhl=Underlined')
- n.command('sign define DiagnosticSignHint text= texthl= linehl=Underlined numhl=Underlined')
- local result = exec_lua(function()
- vim.diagnostic.config({
- signs = true,
- })
- local diagnostics = {
- _G.make_error('Error', 1, 1, 1, 2),
- _G.make_warning('Warning', 3, 3, 3, 3),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local ns = vim.diagnostic.get_namespace(_G.diagnostic_ns)
- local sign_ns = ns.user_data.sign_ns
- local signs = vim.api.nvim_buf_get_extmarks(
- _G.diagnostic_bufnr,
- sign_ns,
- 0,
- -1,
- { type = 'sign', details = true }
- )
- local result = {}
- for _, s in ipairs(signs) do
- result[#result + 1] = {
- lnum = s[2] + 1,
- name = s[4].sign_hl_group,
- text = s[4].sign_text or '',
- numhl = s[4].number_hl_group,
- linehl = s[4].line_hl_group,
- }
- end
- return result
- end)
- eq({
- lnum = 2,
- name = 'DiagnosticSignError',
- text = '',
- numhl = 'ErrorMsg',
- linehl = 'ErrorMsg',
- }, result[1])
- eq({
- lnum = 4,
- name = 'DiagnosticSignWarn',
- text = '',
- numhl = 'WarningMsg',
- linehl = 'WarningMsg',
- }, result[2])
- end)
- end)
- describe('open_float()', function()
- it('can display a header', function()
- eq(
- { 'Diagnostics:', '1. Syntax error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float()
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { "We're no strangers to love...", '1. Syntax error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr =
- vim.diagnostic.open_float({ header = "We're no strangers to love..." })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { 'You know the rules', '1. Syntax error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr =
- vim.diagnostic.open_float({ header = { 'You know the rules', 'Search' } })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- end)
- it('can show diagnostics from the whole buffer', function()
- eq(
- { '1. Syntax error', '2. Some warning' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- _G.make_warning('Some warning', 1, 1, 1, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- end)
- it('can show diagnostics from a single line', function()
- -- Using cursor position
- eq(
- { '1. Some warning' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- _G.make_warning('Some warning', 1, 1, 1, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.api.nvim_win_set_cursor(0, { 2, 1 })
- local float_bufnr, winnr = vim.diagnostic.open_float({ header = false })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- -- With specified position
- eq(
- { '1. Some warning' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- _G.make_warning('Some warning', 1, 1, 1, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.api.nvim_win_set_cursor(0, { 1, 1 })
- local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, pos = 1 })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- -- End position is exclusive
- eq(
- nil,
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 1, 1, 2, 0),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.api.nvim_win_set_cursor(0, { 1, 1 })
- local _, winnr = vim.diagnostic.open_float(0, { header = false, pos = { 2, 0 } })
- return winnr
- end)
- )
- -- Works when width == 0
- eq(
- { '1. Syntax error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 2, 0, 2, 0),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.api.nvim_win_set_cursor(0, { 1, 1 })
- local float_bufnr, winnr =
- vim.diagnostic.open_float(0, { header = false, pos = { 2, 1 } })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- end)
- it('can show diagnostics from a specific position', function()
- -- Using cursor position
- eq(
- { 'Syntax error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 1, 1, 1, 3),
- _G.make_warning('Some warning', 1, 3, 1, 4),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.api.nvim_win_set_cursor(0, { 2, 2 })
- local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'cursor' })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- -- With specified position
- eq(
- { 'Some warning' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 1, 1, 1, 3),
- _G.make_warning('Some warning', 1, 3, 1, 4),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.api.nvim_win_set_cursor(0, { 1, 1 })
- local float_bufnr, winnr =
- vim.diagnostic.open_float({ header = false, scope = 'cursor', pos = { 1, 3 } })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- -- With column position past the end of the line. #16062
- eq(
- { 'Syntax error' },
- exec_lua(function()
- local first_line_len = #vim.api.nvim_buf_get_lines(_G.diagnostic_bufnr, 0, 1, true)[1]
- local diagnostics = {
- _G.make_error('Syntax error', 0, first_line_len + 1, 1, 0),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.api.nvim_win_set_cursor(0, { 1, 1 })
- local float_bufnr, winnr = vim.diagnostic.open_float({
- header = false,
- scope = 'cursor',
- pos = { 0, first_line_len },
- })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- -- End position is exclusive
- eq(
- nil,
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 1, 1, 1, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.api.nvim_win_set_cursor(0, { 1, 1 })
- local _, winnr =
- vim.diagnostic.open_float(0, { header = false, scope = 'cursor', pos = { 1, 3 } })
- return winnr
- end)
- )
- -- Works when width == 0
- eq(
- { 'Syntax error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 2, 0, 2, 0),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.api.nvim_win_set_cursor(0, { 1, 1 })
- local float_bufnr, winnr =
- vim.diagnostic.open_float({ header = false, scope = 'cursor', pos = { 2, 1 } })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- end)
- it(
- 'creates floating window and returns float bufnr and winnr if current line contains diagnostics',
- function()
- -- Two lines:
- -- Diagnostic:
- -- 1. <msg>
- eq(
- 2,
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr)
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return #lines
- end)
- )
- end
- )
- it('only reports diagnostics from the current buffer when bufnr is omitted #15710', function()
- eq(
- 2,
- exec_lua(function()
- local other_bufnr = vim.api.nvim_create_buf(true, false)
- local buf_1_diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- }
- local buf_2_diagnostics = {
- _G.make_warning('Some warning', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, buf_1_diagnostics)
- vim.diagnostic.set(_G.other_ns, other_bufnr, buf_2_diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float()
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return #lines
- end)
- )
- end)
- it('allows filtering by namespace', function()
- eq(
- 2,
- exec_lua(function()
- local ns_1_diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- }
- local ns_2_diagnostics = {
- _G.make_warning('Some warning', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, ns_1_diagnostics)
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, ns_2_diagnostics)
- local float_bufnr, winnr =
- vim.diagnostic.open_float(_G.diagnostic_bufnr, { namespace = _G.diagnostic_ns })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return #lines
- end)
- )
- end)
- it(
- 'creates floating window and returns float bufnr and winnr without header, if requested',
- function()
- -- One line (since no header):
- -- 1. <msg>
- eq(
- 1,
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr =
- vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return #lines
- end)
- )
- end
- )
- it('clamps diagnostic line numbers within the valid range', function()
- eq(
- 1,
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 6, 0, 6, 0),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr =
- vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false, pos = 5 })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return #lines
- end)
- )
- end)
- it('can show diagnostic source', function()
- exec_lua [[vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)]]
- eq(
- { '1. Syntax error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3, 'source x'),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, {
- header = false,
- source = 'if_many',
- })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { '1. source x: Syntax error' },
- exec_lua(function()
- local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, {
- header = false,
- source = 'always',
- })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { '1. source x: Syntax error', '2. source y: Another error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3, 'source x'),
- _G.make_error('Another error', 0, 1, 0, 3, 'source y'),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float(_G.diagnostic_bufnr, {
- header = false,
- source = 'if_many',
- })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- end)
- it('respects severity_sort', function()
- exec_lua [[vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)]]
- eq(
- { '1. Syntax error', '2. Info', '3. Error', '4. Warning' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- _G.make_info('Info', 0, 3, 0, 4),
- _G.make_error('Error', 0, 2, 0, 2),
- _G.make_warning('Warning', 0, 0, 0, 1),
- }
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- vim.diagnostic.config({ severity_sort = false })
- local float_bufnr, winnr =
- vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { '1. Syntax error', '2. Error', '3. Warning', '4. Info' },
- exec_lua(function()
- vim.diagnostic.config({ severity_sort = true })
- local float_bufnr, winnr =
- vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { '1. Info', '2. Warning', '3. Error', '4. Syntax error' },
- exec_lua(function()
- vim.diagnostic.config({ severity_sort = { reverse = true } })
- local float_bufnr, winnr =
- vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- end)
- it('can filter by severity', function()
- local count_diagnostics_with_severity = function(min_severity, max_severity)
- return exec_lua(function()
- vim.diagnostic.config({
- float = {
- severity = { min = min_severity, max = max_severity },
- },
- })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- _G.make_info('Info', 0, 3, 0, 4),
- _G.make_error('Error', 0, 2, 0, 2),
- _G.make_warning('Warning', 0, 0, 0, 1),
- })
- local float_bufnr, winnr =
- vim.diagnostic.open_float(_G.diagnostic_bufnr, { header = false })
- if not float_bufnr then
- return 0
- end
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return #lines
- end)
- end
- eq(2, count_diagnostics_with_severity('ERROR'))
- eq(3, count_diagnostics_with_severity('WARN'))
- eq(1, count_diagnostics_with_severity('WARN', 'WARN'))
- eq(4, count_diagnostics_with_severity('HINT'))
- eq(0, count_diagnostics_with_severity('HINT', 'HINT'))
- end)
- it('can add a prefix to diagnostics', function()
- -- Default is to add a number
- eq(
- { '1. Syntax error', '2. Some warning' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- _G.make_warning('Some warning', 1, 1, 1, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { 'Syntax error', 'Some warning' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- _G.make_warning('Some warning', 1, 1, 1, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr =
- vim.diagnostic.open_float({ header = false, scope = 'buffer', prefix = '' })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { '1. Syntax error', '2. Some warning' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- _G.make_warning('Some warning', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float({
- header = false,
- prefix = function(_, i, total)
- -- Only show a number if there is more than one diagnostic
- if total > 1 then
- return string.format('%d. ', i)
- end
- return ''
- end,
- })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { 'Syntax error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float({
- header = false,
- prefix = function(_, i, total)
- -- Only show a number if there is more than one diagnostic
- if total > 1 then
- return string.format('%d. ', i)
- end
- return ''
- end,
- })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- '.../diagnostic.lua:0: prefix: expected string|table|function, got number',
- pcall_err(exec_lua, [[ vim.diagnostic.open_float({ prefix = 42 }) ]])
- )
- end)
- it('can add a suffix to diagnostics', function()
- -- Default is to render the diagnostic error code
- eq(
- { '1. Syntax error [code-x]', '2. Some warning [code-y]' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3, nil, 'code-x'),
- _G.make_warning('Some warning', 1, 1, 1, 3, nil, 'code-y'),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- { '1. Syntax error', '2. Some warning' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3, nil, 'code-x'),
- _G.make_warning('Some warning', 1, 1, 1, 3, nil, 'code-y'),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr =
- vim.diagnostic.open_float({ header = false, scope = 'buffer', suffix = '' })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- -- Suffix is rendered on the last line of a multiline diagnostic
- eq(
- { '1. Syntax error', ' More context [code-x]' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error\nMore context', 0, 1, 0, 3, nil, 'code-x'),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float({ header = false, scope = 'buffer' })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- eq(
- '.../diagnostic.lua:0: suffix: expected string|table|function, got number',
- pcall_err(exec_lua, [[ vim.diagnostic.open_float({ suffix = 42 }) ]])
- )
- end)
- it('works with the old signature', function()
- eq(
- { '1. Syntax error' },
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Syntax error', 0, 1, 0, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- end)
- it('works for multi-line diagnostics #21949', function()
- -- create diagnostic
- exec_lua(function()
- local diagnostics = {
- _G.make_error('Error in two lines lnum is 1 and end_lnum is 2', 1, 1, 2, 3),
- }
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, diagnostics)
- end)
- -- open float failed non diagnostic lnum
- eq(
- nil,
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- local _, winnr = vim.diagnostic.open_float(0, { header = false })
- return winnr
- end)
- )
- eq(
- nil,
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 1, 0 })
- local _, winnr = vim.diagnostic.open_float(0, { header = false, scope = 'cursor' })
- return winnr
- end)
- )
- -- can open a float window on lnum 1
- eq(
- { '1. Error in two lines lnum is 1 and end_lnum is 2' },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 2, 0 })
- local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- -- can open a cursor-scoped float window on lnum 1
- eq(
- { 'Error in two lines lnum is 1 and end_lnum is 2' },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 2, 1 })
- local float_bufnr, winnr =
- vim.diagnostic.open_float(0, { header = false, scope = 'cursor' })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- -- can open a float window on end_lnum 2
- eq(
- { '1. Error in two lines lnum is 1 and end_lnum is 2' },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 3, 0 })
- local float_bufnr, winnr = vim.diagnostic.open_float(0, { header = false })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- -- can open a cursor-scoped float window on end_lnum 2
- eq(
- { 'Error in two lines lnum is 1 and end_lnum is 2' },
- exec_lua(function()
- vim.api.nvim_win_set_cursor(0, { 3, 2 })
- local float_bufnr, winnr =
- vim.diagnostic.open_float(0, { header = false, scope = 'cursor' })
- local lines = vim.api.nvim_buf_get_lines(float_bufnr, 0, -1, false)
- vim.api.nvim_win_close(winnr, true)
- return lines
- end)
- )
- end)
- end)
- describe('setloclist()', function()
- it('sets diagnostics in lnum order', function()
- local loc_list = exec_lua(function()
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Farther Diagnostic', 4, 4, 4, 4),
- _G.make_error('Lower Diagnostic', 1, 1, 1, 1),
- })
- vim.diagnostic.setloclist()
- return vim.fn.getloclist(0)
- end)
- assert(loc_list[1].lnum < loc_list[2].lnum)
- end)
- it('sets diagnostics in lnum order, regardless of namespace', function()
- local loc_list = exec_lua(function()
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Lower Diagnostic', 1, 1, 1, 1),
- })
- vim.diagnostic.set(_G.other_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Farther Diagnostic', 4, 4, 4, 4),
- })
- vim.diagnostic.setloclist()
- return vim.fn.getloclist(0)
- end)
- assert(loc_list[1].lnum < loc_list[2].lnum)
- end)
- end)
- describe('setqflist()', function()
- it('updates existing diagnostics quickfix if one already exists', function()
- local result = exec_lua(function()
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.fn.setqflist({}, ' ', { title = 'Diagnostics' })
- local diagnostics_qf_id = vim.fn.getqflist({ id = 0 }).id
- vim.diagnostic.setqflist({ title = 'Diagnostics' })
- local qf_id = vim.fn.getqflist({ id = 0, nr = '$' }).id
- return { diagnostics_qf_id, qf_id }
- end)
- eq(result[1], result[2])
- end)
- it('navigates to existing diagnostics quickfix if one already exists and open=true', function()
- local result = exec_lua(function()
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.fn.setqflist({}, ' ', { title = 'Diagnostics' })
- local diagnostics_qf_id = vim.fn.getqflist({ id = 0 }).id
- vim.fn.setqflist({}, ' ', { title = 'Other' })
- vim.diagnostic.setqflist({ title = 'Diagnostics', open = true })
- local qf_id = vim.fn.getqflist({ id = 0 }).id
- return { diagnostics_qf_id, qf_id }
- end)
- eq(result[1], result[2])
- end)
- it('sets new diagnostics quickfix as active when open=true', function()
- local result = exec_lua(function()
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.fn.setqflist({}, ' ', { title = 'Other' })
- local other_qf_id = vim.fn.getqflist({ id = 0 }).id
- vim.diagnostic.setqflist({ title = 'Diagnostics', open = true })
- local qf_id = vim.fn.getqflist({ id = 0 }).id
- return { other_qf_id, qf_id }
- end)
- neq(result[1], result[2])
- end)
- it('opens quickfix window when open=true', function()
- local qf_winid = exec_lua(function()
- vim.api.nvim_win_set_buf(0, _G.diagnostic_bufnr)
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error', 1, 1, 1, 1),
- })
- vim.diagnostic.setqflist({ open = true })
- return vim.fn.getqflist({ winid = 0 }).winid
- end)
- neq(0, qf_winid)
- end)
- end)
- describe('match()', function()
- it('matches a string', function()
- local msg = 'ERROR: george.txt:19:84:Two plus two equals five'
- local diagnostic = {
- severity = exec_lua [[return vim.diagnostic.severity.ERROR]],
- lnum = 18,
- col = 83,
- end_lnum = 18,
- end_col = 83,
- message = 'Two plus two equals five',
- }
- eq(
- diagnostic,
- exec_lua(function()
- return vim.diagnostic.match(
- msg,
- '^(%w+): [^:]+:(%d+):(%d+):(.+)$',
- { 'severity', 'lnum', 'col', 'message' }
- )
- end)
- )
- end)
- it('returns nil if the pattern fails to match', function()
- eq(
- nil,
- exec_lua(function()
- local msg = 'The answer to life, the universe, and everything is'
- return vim.diagnostic.match(msg, 'This definitely will not match', {})
- end)
- )
- end)
- it('respects default values', function()
- local msg = 'anna.txt:1:Happy families are all alike'
- local diagnostic = {
- severity = exec_lua [[return vim.diagnostic.severity.INFO]],
- lnum = 0,
- col = 0,
- end_lnum = 0,
- end_col = 0,
- message = 'Happy families are all alike',
- }
- eq(
- diagnostic,
- exec_lua(function()
- return vim.diagnostic.match(
- msg,
- '^[^:]+:(%d+):(.+)$',
- { 'lnum', 'message' },
- nil,
- { severity = vim.diagnostic.severity.INFO }
- )
- end)
- )
- end)
- it('accepts a severity map', function()
- local msg = '46:FATAL:Et tu, Brute?'
- local diagnostic = {
- severity = exec_lua [[return vim.diagnostic.severity.ERROR]],
- lnum = 45,
- col = 0,
- end_lnum = 45,
- end_col = 0,
- message = 'Et tu, Brute?',
- }
- eq(
- diagnostic,
- exec_lua(function()
- return vim.diagnostic.match(
- msg,
- '^(%d+):(%w+):(.+)$',
- { 'lnum', 'severity', 'message' },
- { FATAL = vim.diagnostic.severity.ERROR }
- )
- end)
- )
- end)
- end)
- describe('toqflist() and fromqflist()', function()
- it('works', function()
- local result = exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Error 1', 0, 1, 0, 1),
- _G.make_error('Error 2', 1, 1, 1, 1),
- _G.make_warning('Warning', 2, 2, 2, 2),
- })
- local diagnostics = vim.diagnostic.get(_G.diagnostic_bufnr)
- vim.fn.setqflist(vim.diagnostic.toqflist(diagnostics))
- local list = vim.fn.getqflist()
- local new_diagnostics = vim.diagnostic.fromqflist(list)
- -- Remove namespace since it isn't present in the return value of
- -- fromlist()
- for _, v in ipairs(diagnostics) do
- v.namespace = nil
- end
- return { diagnostics, new_diagnostics }
- end)
- eq(result[1], result[2])
- end)
- end)
- describe('handlers', function()
- it('checks that a new handler is a table', function()
- matches(
- [[.*handler: expected table, got string.*]],
- pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = "bar" ]])
- )
- matches(
- [[.*handler: expected table, got function.*]],
- pcall_err(exec_lua, [[ vim.diagnostic.handlers.foo = function() end ]])
- )
- end)
- it('can add new handlers', function()
- eq(
- true,
- exec_lua(function()
- local handler_called = false
- vim.diagnostic.handlers.test = {
- show = function(namespace, bufnr, diagnostics, opts)
- assert(namespace == _G.diagnostic_ns)
- assert(bufnr == _G.diagnostic_bufnr)
- assert(#diagnostics == 1)
- assert(opts.test.some_opt == 42)
- handler_called = true
- end,
- }
- vim.diagnostic.config({ test = { some_opt = 42 } })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Warning', 0, 0, 0, 0),
- })
- return handler_called
- end)
- )
- end)
- it('can disable handlers by setting the corresponding option to false', function()
- eq(
- false,
- exec_lua(function()
- local handler_called = false
- vim.diagnostic.handlers.test = {
- show = function(_, _, _, _)
- handler_called = true
- end,
- }
- vim.diagnostic.config({ test = false })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Warning', 0, 0, 0, 0),
- })
- return handler_called
- end)
- )
- end)
- it("always calls a handler's hide function if defined", function()
- eq(
- { false, true },
- exec_lua(function()
- local hide_called = false
- local show_called = false
- vim.diagnostic.handlers.test = {
- show = function(_, _, _, _)
- show_called = true
- end,
- hide = function(namespace, bufnr)
- assert(namespace == _G.diagnostic_ns)
- assert(bufnr == _G.diagnostic_bufnr)
- hide_called = true
- end,
- }
- vim.diagnostic.config({ test = false })
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_warning('Warning', 0, 0, 0, 0),
- })
- vim.diagnostic.hide(_G.diagnostic_ns, _G.diagnostic_bufnr)
- return { show_called, hide_called }
- end)
- )
- end)
- it('triggers the autocommand when diagnostics are set', function()
- eq(
- { true, true },
- exec_lua(function()
- -- Set a different buffer as current to test that <abuf> is being set properly in
- -- DiagnosticChanged callbacks
- local tmp = vim.api.nvim_create_buf(false, true)
- vim.api.nvim_set_current_buf(tmp)
- local triggered = {}
- vim.api.nvim_create_autocmd('DiagnosticChanged', {
- callback = function(args)
- triggered = { args.buf, #args.data.diagnostics }
- end,
- })
- vim.api.nvim_buf_set_name(_G.diagnostic_bufnr, 'test | test')
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic', 0, 0, 0, 0),
- })
- return {
- triggered[1] == _G.diagnostic_bufnr,
- triggered[2] == 1,
- }
- end)
- )
- end)
- it('triggers the autocommand when diagnostics are cleared', function()
- eq(
- true,
- exec_lua(function()
- local tmp = vim.api.nvim_create_buf(false, true)
- vim.api.nvim_set_current_buf(tmp)
- vim.g.diagnostic_autocmd_triggered = 0
- vim.cmd(
- 'autocmd DiagnosticChanged * let g:diagnostic_autocmd_triggered = +expand("<abuf>")'
- )
- vim.api.nvim_buf_set_name(_G.diagnostic_bufnr, 'test | test')
- vim.diagnostic.reset(_G.diagnostic_ns, _G.diagnostic_bufnr)
- return vim.g.diagnostic_autocmd_triggered == _G.diagnostic_bufnr
- end)
- )
- end)
- it('is_enabled', function()
- eq(
- { false, false, false, false, false },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- })
- vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
- vim.diagnostic.enable(false)
- return {
- vim.diagnostic.is_enabled(),
- vim.diagnostic.is_enabled { bufnr = 0 },
- vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr },
- vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns },
- vim.diagnostic.is_enabled { bufnr = 0, ns_id = _G.diagnostic_ns },
- }
- end)
- )
- eq(
- { true, true, true, true, true },
- exec_lua(function()
- vim.diagnostic.enable()
- return {
- vim.diagnostic.is_enabled(),
- vim.diagnostic.is_enabled { bufnr = 0 },
- vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr },
- vim.diagnostic.is_enabled { bufnr = _G.diagnostic_bufnr, ns_id = _G.diagnostic_ns },
- vim.diagnostic.is_enabled { bufnr = 0, ns_id = _G.diagnostic_ns },
- }
- end)
- )
- end)
- it('is_disabled (deprecated)', function()
- eq(
- { true, true, true, true },
- exec_lua(function()
- vim.diagnostic.set(_G.diagnostic_ns, _G.diagnostic_bufnr, {
- _G.make_error('Diagnostic #1', 1, 1, 1, 1),
- })
- vim.api.nvim_set_current_buf(_G.diagnostic_bufnr)
- vim.diagnostic.disable()
- return {
- vim.diagnostic.is_disabled(),
- vim.diagnostic.is_disabled(_G.diagnostic_bufnr),
- vim.diagnostic.is_disabled(_G.diagnostic_bufnr, _G.diagnostic_ns),
- vim.diagnostic.is_disabled(0, _G.diagnostic_ns),
- }
- end)
- )
- eq(
- { false, false, false, false },
- exec_lua(function()
- vim.diagnostic.enable()
- return {
- vim.diagnostic.is_disabled(),
- vim.diagnostic.is_disabled(_G.diagnostic_bufnr),
- vim.diagnostic.is_disabled(_G.diagnostic_bufnr, _G.diagnostic_ns),
- vim.diagnostic.is_disabled(0, _G.diagnostic_ns),
- }
- end)
- )
- end)
- end)
- end)
|