From beac24d6f3afa8da992e9fdda2ce1d3235f45113 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 8 Dec 2021 19:20:55 -0700 Subject: [PATCH] fix(diagnostic): escape special chars in file names (#16588) --- runtime/lua/vim/diagnostic.lua | 10 ++++++++-- test/functional/lua/diagnostic_spec.lua | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index d6de3b3c49..a8ee1e7698 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -655,7 +655,10 @@ function M.set(namespace, bufnr, diagnostics, opts) vim.api.nvim_buf_call(bufnr, function() vim.api.nvim_command( - string.format("doautocmd DiagnosticChanged %s", vim.api.nvim_buf_get_name(bufnr)) + string.format( + "doautocmd DiagnosticChanged %s", + vim.fn.fnameescape(vim.api.nvim_buf_get_name(bufnr)) + ) ) end) end @@ -1339,7 +1342,10 @@ function M.reset(namespace, bufnr) end vim.api.nvim_command( - string.format("doautocmd DiagnosticChanged %s", vim.api.nvim_buf_get_name(bufnr)) + string.format( + "doautocmd DiagnosticChanged %s", + vim.fn.fnameescape(vim.api.nvim_buf_get_name(bufnr)) + ) ) end diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index c79b392082..a88da63e90 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -1932,5 +1932,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)