Skip to content

Commit

Permalink
refactor: delete todos, add snips
Browse files Browse the repository at this point in the history
  • Loading branch information
phanen committed May 7, 2024
1 parent 8332058 commit 2e1c19e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 55 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ return {
{ '<leader>e', fl.find_notes, mode = { 'n', 'x' } },
{ '<leader>fo', fl.recentfiles, mode = { 'n', 'x' } },
{ '<leader>l', fl.find_dots, mode = { 'n', 'x' } },
{ '<leader>t', fl.todos, mode = { 'n', 'x' } },
{ '+l', fl.grep_dots, mode = { 'n', 'x' } },

-- all fzf-lua's builtin pickers work transparently with visual mode support
Expand Down
70 changes: 42 additions & 28 deletions lua/fzf-lua-overlay/actions.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local M = {}

local cfg = require('fzf-lua-overlay.config').opts
local u = require('fzf-lua-overlay.util')

M.toggle_daily = function(_, opts)
local o = opts.__call_opts
if opts.show_daily_only then
Expand All @@ -11,28 +14,54 @@ M.toggle_daily = function(_, opts)
opts.__call_fn(o)
end

M.create_notes = function(_, opts)
-- create notes, snips or todos
M.create_whatever = function(_, opts)
local query = require('fzf-lua').get_last_query()
-- no input then use date as name
if not query or query == '' then query = os.date('%m-%d') .. '.md' end
-- no ext -> append `.md`
local path = vim.fn.expand(('%s/%s'):format(opts.cwd, query))
local sec = vim.split(query, ' ', { trimempty = true })
local sec_nr = #sec
local parts = vim.split(query, ' ', { trimempty = true })
local part_nr = #parts
if part_nr == 0 then return end

-- multi fields, append todo
if part_nr > 1 then
return (function()
local tag = parts[1]
local content = table.concat(parts, ' ', 2)
local path = vim.fn.expand(vim.fs.joinpath(cfg.todo_dir, tag)) .. '.md'
content = ('* %s\n'):format(content)
local ok = u.write_file(path, content, 'a')
if not ok then return vim.notify('fail to write to storage', vim.log.levels.WARN) end
end)()
end

if sec_nr == 0 then return end
if sec_nr > 1 then return require('fzf-lua-overlay.actions').add_todos(query) end
-- query as path
local path_parts = vim.split(query, '.', { plain = true, trimempty = true })

-- non-suffix are md by default
if #(vim.split(sec[1], '.', { plain = true })) == 1 then path = path .. '.md' end
if #path_parts == 0 then
return -- dot only
end

-- complete name default to md
if #path_parts == 1 then
query = query .. 'md'
path_parts[2] = 'md'
end

-- router (query can be `a/b/c`)
local path
if path_parts[2] == 'md' then
path = vim.fn.expand(('%s/%s'):format(opts.cwd, query))
else
path = vim.fn.expand(('%s/%s'):format(cfg.snip_dir, query))
end

-- create, then open
if not vim.uv.fs_stat(path) then
local u = require('fzf-lua-overlay.util')
local ok = u.write_file(path, nil, 'w')
if not ok then return vim.notify(('fail to create file %s'):format(path)) end
if not ok then return vim.notify(('fail to create %s'):format(path)) end
end
vim.cmd.e(path)
vim.notify(('%s has been created'):format(path), vim.log.levels.INFO)
vim.notify(('%s created'):format(query), vim.log.levels.INFO)
end

M.delete_files = function(selected, opts)
Expand Down Expand Up @@ -70,19 +99,4 @@ M.rename_files = function(selected, opts)
vim.notify(('%s has been renamed to %s'):format(oldpath, newpath), vim.log.levels.INFO)
end

M.add_todos = function(query)
local line = query or require('fzf-lua').get_last_query()
local tag, content = unpack(vim.split(line, ': '))
if not tag or not content then
return vim.notify('format should be [tag: content]', vim.log.levels.WARN)
end
local u = require('fzf-lua-overlay.util')

local cfg = require('fzf-lua-overlay.config').opts
local filename = vim.fs.normalize(vim.fs.joinpath(cfg.todo_dir, tag)) .. '.md'
content = ('* %s\n'):format(content)
local ok = u.write_file(filename, content, 'a')
if not ok then return vim.notify('fail to write to storage', vim.log.levels.WARN) end
end

return M
2 changes: 1 addition & 1 deletion lua/fzf-lua-overlay/providers/find_notes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ M.opts = {
local last_query = require('fzf-lua').get_last_query()
return require('fzf-lua-overlay').grep_notes({ query = last_query })
end,
['ctrl-n'] = function(...) require('fzf-lua-overlay.actions').create_notes(...) end,
['ctrl-n'] = function(...) require('fzf-lua-overlay.actions').create_whatever(...) end,
['ctrl-x'] = function(...) require('fzf-lua-overlay.actions').delete_files(...) end,
},
fzf_opts = { ['--history'] = notes_history },
Expand Down
23 changes: 0 additions & 23 deletions lua/fzf-lua-overlay/providers/todos.lua

This file was deleted.

1 change: 1 addition & 0 deletions lua/fzf-lua-overlay/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ u.read_file = function(path)
end

u.write_file = function(path, str, flag)
vim.fn.mkdir(vim.fn.fnamemodify(path, ':p:h'), 'p')
local fd = io.open(path, flag or 'w')
if not fd then return false end
if str then fd:write(str) end
Expand Down
2 changes: 0 additions & 2 deletions plugin/fzf-lua-overlay.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ function! s:fzf_lua_overlay_complete(arg, line, pos) abort
\'rtp',
\"scriptnames",
\"todo_comment",
\"todos",
\"zoxide",
\]
let list = [l:builtin_list]
return join(list[0],"\n")
endfunction

command! -nargs=1 -complete=custom,s:fzf_lua_overlay_complete FL lua require('fzf-lua-overlay').<args>()

0 comments on commit 2e1c19e

Please sign in to comment.