cvigilv / esqueleto.nvim

Reduce your boilerplate code the lazy-bones way
MIT License
98 stars 7 forks source link

bug: Empty or no selection shouldn't throw an error #48

Closed jakebrinkmann closed 6 months ago

jakebrinkmann commented 6 months ago

Hello, I am not a strong lua/neovim developer, otherwise I would dig into this and find a fix.

Type number and <Enter> or click with the mouse (q or empty cancels): Error detected while processing BufNewFile Autocommands for "*
":
Error executing lua callback: ...brew/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:24: Error executing lua: ...brew/Cellar/ne
ovim/0.9.5/share/nvim/runtime/filetype.lua:25: BufNewFile Autocommands for "*"..FileType Autocommands for "markdown": Vim(append):Er
ror executing lua callback: ...l/share/nvim/lazy/esqueleto.nvim/lua/esqueleto/utils.lua:173: bad argument #1 to 'fs_realpath' (strin
g expected, got nil)
stack traceback:
        [C]: in function 'fs_realpath'
        ...l/share/nvim/lazy/esqueleto.nvim/lua/esqueleto/utils.lua:173: in function 'on_choice'
        ...ew/Cellar/neovim/0.9.5/share/nvim/runtime/lua/vim/ui.lua:52: in function 'select'
        ...l/share/nvim/lazy/esqueleto.nvim/lua/esqueleto/utils.lua:172: in function 'selecttemplate'
        ...l/share/nvim/lazy/esqueleto.nvim/lua/esqueleto/utils.lua:199: in function 'inserttemplate'
jakebrinkmann commented 6 months ago

:shrug: this seems to work, IDK if it's "correct" :shrug:

diff --git a/lua/esqueleto/utils.lua b/lua/esqueleto/utils.lua
index 41949d1..1524c3d 100644
--- a/lua/esqueleto/utils.lua
+++ b/lua/esqueleto/utils.lua
@@ -170,7 +170,9 @@ M.selecttemplate = function(templates, opts)

   -- Select template
   vim.ui.select(templatenames, { prompt = "Select skeleton to use:" }, function(choice)
-    M.writetemplate(vim.loop.fs_realpath(templates[choice]), opts)
+    if templates[choice] then
+        M.writetemplate(vim.loop.fs_realpath(templates[choice]), opts)
+    end
   end)
 end
cvigilv commented 6 months ago

Hi @jakebrinkmann! Thanks for the issue and pull request.

I left it like that to raise an error on empty directories on purpose, but maybe its time to make this part fail more graciously.

I will add some thigs to you PR and push it to the main branch. Thanks for your contribution!