From 47b946d65ab2bd7fe1cc3976e766a6dbcbcae1dc Mon Sep 17 00:00:00 2001 From: Daniel Carrillo Date: Sun, 1 Oct 2023 17:38:01 +0200 Subject: [PATCH] [neovim] Use vim.loop to read custom files --- .config/nvim/lua/core/plugins.lua | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/.config/nvim/lua/core/plugins.lua b/.config/nvim/lua/core/plugins.lua index b059bbd..783a986 100644 --- a/.config/nvim/lua/core/plugins.lua +++ b/.config/nvim/lua/core/plugins.lua @@ -19,14 +19,32 @@ local lazy_opts = { }, } -local projects = function () - local file = io.open(os.getenv("HOME") .. "/.config/nvim/neovim-projects.json", "rb") - if not file then return {} end +local uv = vim.loop +local function readFile(path) + local fd = uv.fs_open(path, "r", 438) + if fd == nil then + return nil + end + local stat = uv.fs_fstat(fd) + if fd == nil then + return nil + end + local data = uv.fs_read(fd, stat.size, 0) + if data == nil then + return nil + end + assert(uv.fs_close(fd)) - local jsonString = file:read "*a" - file:close() + return data +end - return vim.json.decode(jsonString) +local projects = function() + local data = readFile(os.getenv("HOME") .. "/.config/nvim/neovim-projects.json") + if data then + return vim.json.decode(data) + else + return {} + end end require("lazy").setup({