Create subdirs when creating new note#268
Conversation
previously it failed with error message
vim.ui.select is meant to be used as an asynchronous operation, in order to do this, it receives a callback as a third argument. This commit adjusts check_dir_and_ask to leverage the asynchronous usage of vim.ui.select, as a side effect to make this work, I also refactored the code that depends on the function in order to leverage the asynchronous operations. References: - stevearc/dressing.nvim#80 (comment) - https://github.com/stevearc/dressing.nvim - neovim/neovim#15771 Fixes: - nvim-telekasten#266 One reason this bug can sneak into the codebase is because the default unmodified behavior of vim.ui.select is to use the :Messages section of neovim to ask for input and this UI is a synchronous blocking operation. However, this is not the intended usage of vim.ui.select as presented above.
lua/telekasten.lua
Outdated
| pinfo.calendar_info, | ||
| function() | ||
| opts.erase = true | ||
| opts.erase_file = fname |
There was a problem hiding this comment.
Stylua complains here that fname is not defined properly. Why not keep pinfo.filepath?
There was a problem hiding this comment.
hm, looks like this was an oversight on my part
|
I have not reviewed the whole PR, but some preliminary tests on my side seems to indicate it works well. I activated the pipeline so any further commits will be checked directly for linting issues. It is a rather large PR, I will try to review it by the end of the week-end and merge it for Monday. Anyway, thank you already for this massive contribution! |
|
No problem, sincerely loving this plugin. I will add comments to the diff tomorrow, in order to make it easier to parse for you. |
| end | ||
|
|
||
| local function check_dir_and_ask(dir, purpose) | ||
| local function check_dir_and_ask(dir, purpose, callback) |
There was a problem hiding this comment.
this is the biggest source of change. Added callback param to check_dir_and_ask, now all callsites to check_dir_and_ask need to pass the "after selection" operations as a callback.
| vim.cmd('echomsg " "') | ||
| vim.cmd('echomsg "' .. dir .. ' created"') | ||
| ret = true | ||
| callback(ret) |
There was a problem hiding this comment.
"resumes" the code that was passed as callback
| -- unreachable: plenary.Path:mkdir() will error out | ||
| tkutils.print_error("Could not create directory " .. dir) | ||
| ret = false | ||
| callback(ret) |
There was a problem hiding this comment.
"resumes" the code that was passed as callback
| else | ||
| ret = true | ||
| if callback ~= nil then | ||
| callback(ret) |
There was a problem hiding this comment.
even though vim.ui.select was not called in this branch, code still needs to continue through callback
| end | ||
|
|
||
| local function global_dir_check() | ||
| local function global_dir_check(callback) |
There was a problem hiding this comment.
this is one of the main functions affected by the refactor of check_dir_and_ask, therefore all functions that call global_dir_check are affected and need to pass in a callback
| if not global_dir_check() then | ||
| return | ||
| end | ||
| global_dir_check(function(dir_check) |
There was a problem hiding this comment.
so most changes look like this, code is the same as before but now it includes a function callback.
| ) .. "\n" | ||
| ) | ||
| end | ||
| local file_dir = filepath:match("(.*/)") or "" |
There was a problem hiding this comment.
get directory from filepath
lua/telekasten.lua
Outdated
| check_dir_and_ask(file_dir, file_dir, function(dir_succeed) | ||
| if dir_succeed == false then | ||
| return | ||
| end | ||
|
|
||
| local ofile = io.open(filepath, "a") | ||
|
|
||
| for _, line in pairs(lines) do | ||
| ofile:write( | ||
| templates.subst_templated_values( | ||
| line, | ||
| title, | ||
| calendar_info, | ||
| uuid, | ||
| M.Cfg.calendar_opts.calendar_monday | ||
| ) .. "\n" | ||
| ) | ||
| end |
There was a problem hiding this comment.
this is the bit that asks to create non-existent directories from the note title
With the new callback parameter it becomes necessary to fill the nil arguments of a function if it is to receive the callback as the last argument to reproduce bug try creating a new weekly note.
|
Thank you very much for this PR! Feel free to tackle other issues or pickup the big refactoring I started on the |
Proposed change
Context inside commit messages and linked issues.
Details
Replicate
One way to look at the broken functionality before this commit is to install
dressing.nvimalong withTelekastenand try to triggercheck_dirfunctionType of change
It is difficult to split this into separate PRs
Additional information
Checklist
.stylua.tomlfile is provided).luacheckrcfile is provided)README.mdhas been updated according to this change.doc/telekasten.txthelpfile has been updated according to this change.