after creating file do :set filetype=filetype_details[choice] and ...
Draft
-- config
{
-- ...
manual_selector_text = "MANUAL_INPUT",
-- If it really matter to make default choice, not that hard to generate some defaults for win_conf
--[[
window-relative float
{relative='win', row=3, col=3, width=12, height=3}
vertical split left of the current window
{split = 'left', win = 0}
]]
win_conf = {} ---@see: `nvim_open_win`,
select_options = {"lua", "lua-prof", "foo-bar"},
filetype_details = {
["lua-prof"] = {
filetype = "lua",
content = {"strings..."},
cursor = { location = {0, 5}, insert = true },
---@type fun(string):string
filename_generator = function(scratch_path) end, --gen new name with folders if needed
win_conf = {} ---@see: `nvim_open_win`
}
}
}
Then, the function that creates a scratch buffer can appear in this way.
function M.create_scratch(scratch_file_dir, ft, filetype_details)
local opts = filetype_details[ft]
local abs_path = (opts.filename_generator or function(base_dir)
return base_dir .. os.date("%y-%m-%d_%H-%M-%S") .. "." .. ft
end)(scratch_file_dir)
local buf = vim.api.nvim_create_buf(true, false)
vim.api.nvim_buf_set_name(buf, abs_path)
local filetype = opts.filetype or vim.filetype.match({ filename = abs_path })
vim.api.nvim_set_option_value("filetype", filetype, { buf = buf })
local suc, msg = pcall(vim.api.nvim_open_win, buf, true, opts.win_config)
if not opts.win_config then
vim.api.nvim_set_current_buf(buf)
elseif not suc then
vim.notify(msg, vim.log.levels.WARN)
vim.api.nvim_set_current_buf(buf)
end
if opts.content then
vim.api.nvim_buf_set_lines(buf, 0, -1, false, opts.content)
end
if opts.cursor then
vim.api.nvim_win_set_cursor(0, opts.cursor.location)
if opts.cursor.insert_mode then
vim.api.nvim_feedkeys("a", "n", true)
end
end
end
Benefit
Replace chore: requireDir, filename and win_cwd, and give more power to specify what file type you need and how it should look.
Examples
If you make notes, then you may want to create scratch note, you know where them live, so you can
filetypes = {..., "note"},
filetype_details = {
...
note = {
filetype = "your_note_filetype",
filename_generator = function(_)
return NOTE_DIR .. os.time() .. ".ynf"
end
}
}
after creating file do
:set filetype=filetype_details[choice]and ...Draft
Then, the function that creates a scratch buffer can appear in this way.
Benefit
Replace chore:
requireDir,filenameandwin_cwd, and give more power to specify what file type you need and how it should look.Examples
If you make notes, then you may want to create scratch note, you know where them live, so you can