mirror of
https://github.com/neovim/neovim.git
synced 2025-10-22 17:11:49 +00:00
fix(lsp): create missing directory before creating file (#19835)
Co-authored-by: Mathias Fussenegger <f.mathias@zignar.net>
This commit is contained in:
@@ -772,8 +772,11 @@ local function create_file(change)
|
|||||||
-- from spec: Overwrite wins over `ignoreIfExists`
|
-- from spec: Overwrite wins over `ignoreIfExists`
|
||||||
local fname = vim.uri_to_fname(change.uri)
|
local fname = vim.uri_to_fname(change.uri)
|
||||||
if not opts.ignoreIfExists or opts.overwrite then
|
if not opts.ignoreIfExists or opts.overwrite then
|
||||||
|
vim.fn.mkdir(vim.fs.dirname(fname), 'p')
|
||||||
local file = io.open(fname, 'w')
|
local file = io.open(fname, 'w')
|
||||||
file:close()
|
if file then
|
||||||
|
file:close()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
vim.fn.bufadd(fname)
|
vim.fn.bufadd(fname)
|
||||||
end
|
end
|
||||||
|
@@ -1977,6 +1977,22 @@ describe('LSP', function()
|
|||||||
exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16')
|
exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16')
|
||||||
eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile))
|
eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile))
|
||||||
end)
|
end)
|
||||||
|
it('Supports file creation in folder that needs to be created with CreateFile payload', function()
|
||||||
|
local tmpfile = helpers.tmpname()
|
||||||
|
os.remove(tmpfile) -- Should not exist, only interested in a tmpname
|
||||||
|
tmpfile = tmpfile .. '/dummy/x/'
|
||||||
|
local uri = exec_lua('return vim.uri_from_fname(...)', tmpfile)
|
||||||
|
local edit = {
|
||||||
|
documentChanges = {
|
||||||
|
{
|
||||||
|
kind = 'create',
|
||||||
|
uri = uri,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exec_lua('vim.lsp.util.apply_workspace_edit(...)', edit, 'utf-16')
|
||||||
|
eq(true, exec_lua('return vim.loop.fs_stat(...) ~= nil', tmpfile))
|
||||||
|
end)
|
||||||
it('createFile does not touch file if it exists and ignoreIfExists is set', function()
|
it('createFile does not touch file if it exists and ignoreIfExists is set', function()
|
||||||
local tmpfile = helpers.tmpname()
|
local tmpfile = helpers.tmpname()
|
||||||
write_file(tmpfile, 'Dummy content')
|
write_file(tmpfile, 'Dummy content')
|
||||||
|
Reference in New Issue
Block a user