fix(diagnostic): escape special chars in file names (#16527)

Co-authored-by: zeertzjq <zeertzjq@outlook.com>
Co-authored-by: Gregory Anders <greg@gpanders.com>
This commit is contained in:
Matthew Toohey 2021-12-05 21:39:00 -05:00 committed by GitHub
parent 24f9463dd0
commit 62f0157853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -658,7 +658,10 @@ function M.set(namespace, bufnr, diagnostics, opts)
vim.api.nvim_buf_call(bufnr, function()
vim.api.nvim_command(
string.format("doautocmd <nomodeline> DiagnosticChanged %s", vim.api.nvim_buf_get_name(bufnr))
string.format(
"doautocmd <nomodeline> DiagnosticChanged %s",
vim.fn.fnameescape(vim.api.nvim_buf_get_name(bufnr))
)
)
end)
end
@ -1333,7 +1336,7 @@ function M.reset(namespace, bufnr)
end
vim.api.nvim_command(
string.format("doautocmd <nomodeline> DiagnosticChanged %s", vim.api.nvim_buf_get_name(bufnr))
string.format("doautocmd <nomodeline> DiagnosticChanged %s", vim.fn.fnameescape(vim.api.nvim_buf_get_name(bufnr)))
)
end

View File

@ -1918,5 +1918,27 @@ describe('vim.diagnostic', function()
return {show_called, hide_called}
]])
end)
it('triggers the autocommand when diagnostics are set', function()
eq(1, exec_lua [[
vim.g.diagnostic_autocmd_triggered = 0
vim.cmd('autocmd DiagnosticChanged * let g:diagnostic_autocmd_triggered = 1')
vim.api.nvim_buf_set_name(diagnostic_bufnr, "test | test")
vim.diagnostic.set(diagnostic_ns, diagnostic_bufnr, {
make_error('Diagnostic', 0, 0, 0, 0)
})
return vim.g.diagnostic_autocmd_triggered
]])
end)
it('triggers the autocommand when diagnostics are cleared', function()
eq(1, exec_lua [[
vim.g.diagnostic_autocmd_triggered = 0
vim.cmd('autocmd DiagnosticChanged * let g:diagnostic_autocmd_triggered = 1')
vim.api.nvim_buf_set_name(diagnostic_bufnr, "test | test")
vim.diagnostic.reset(diagnostic_ns, diagnostic_bufnr)
return vim.g.diagnostic_autocmd_triggered
]])
end)
end)
end)