feat: use autocommands for filetype-specific setup

main
borja (aider) 2 months ago
parent 68d467089b
commit af2c61fa3f

@ -1,8 +1,15 @@
-- Create the module table
local M = {}
-- add new todo line when previous is already a todo
local function press_enter()
-- Default configuration
local defaults = {
filetypes = { "markdown", "text", "norg" }, -- Filetypes to activate the plugin for
}
-- Internal function to set up buffer-local keymaps
local function setup_buffer_keymaps()
-- add new todo line when previous is already a todo
local function press_enter()
local current_line = vim.api.nvim_get_current_line()
local index, spaces = string.find(current_line, "^%s*")
@ -24,10 +31,10 @@ local function press_enter()
else
vim.api.nvim_feedkeys("\n", "n", true)
end
end
end
-- indent line if tab is pressed when line is a todo
local function press_tab()
-- indent line if tab is pressed when line is a todo
local function press_tab()
local current_line = vim.api.nvim_get_current_line()
-- Check if current line matches the patterns
@ -38,10 +45,10 @@ local function press_tab()
print("tab pressed, allegedly")
vim.api.nvim_feedkeys("C-t", "i", true)
end
end
end
-- indent line if shift tab is pressed when line is a todo
local function press_shift_tab()
-- indent line if shift tab is pressed when line is a todo
local function press_shift_tab()
local current_line = vim.api.nvim_get_current_line()
-- Check if current line matches the patterns
@ -52,10 +59,10 @@ local function press_shift_tab()
print("shift tab pressed, allegedly")
vim.api.nvim_feedkeys("C-d", "i", true)
end
end
end
-- function that checks if the current line starts with the string "- [ ]" or "- [x]" and toggles the x
local function toggle_todo()
-- function that checks if the current line starts with the string "- [ ]" or "- [x]" and toggles the x
local function toggle_todo()
local openpattern = "%- %[[ ]%]"
local closedpattern = "%- %[[x]%]"
local titleopenpattern = "# %[[ ]%]"
@ -76,10 +83,10 @@ local function toggle_todo()
line = string.gsub(line, titleclosedpattern, "# [ ]")
vim.api.nvim_set_current_line(line)
end
end
end
-- function that checks if the current line doesn't start with the string "- [ ] " and, if it doesn't, adds the string at the beginning of the line
local function add_todo()
-- function that checks if the current line doesn't start with the string "- [ ] " and, if it doesn't, adds the string at the beginning of the line
local function add_todo()
local openpattern = "^%s*%- %[[ ]%]"
local closedpattern = "^%s*%- %[[x]%]"
@ -101,10 +108,10 @@ local function add_todo()
vim.api.nvim_set_current_line(line)
end
end
end
end
-- function that checks if the current line starts with the string "- [ ]" or "- [x]" and, if it does, removes that string from the line
local function remove_todo()
-- function that checks if the current line starts with the string "- [ ]" or "- [x]" and, if it does, removes that string from the line
local function remove_todo()
local openpattern = "%- %[[ ]%]"
local closedpattern = "%- %[[x]%]"
local line = vim.api.nvim_get_current_line()
@ -116,25 +123,43 @@ local function remove_todo()
line = string.gsub(line, closedpattern, "")
vim.api.nvim_set_current_line(line)
end
end
-- Set up buffer-local keymaps
-- Use buffer = 0 to target the current buffer
local map_opts = { noremap = true, silent = true, buffer = 0 }
local expr_map_opts = { noremap = true, expr = true, silent = true, buffer = 0 }
vim.keymap.set("n", "<leader>t", function() end, { desc = "+TODOs", buffer = 0 }) -- Placeholder, keep silent=false if needed
vim.keymap.set("i", "<CR>", press_enter, { desc = "Todoer: Handle Enter", expr = true, buffer = 0 }) -- Keep expr=true, noremap=true is default
vim.keymap.set("i", "<TAB>", press_tab, { desc = "Todoer: Handle Tab", expr = true, buffer = 0 }) -- Keep expr=true
vim.keymap.set("i", "<S-Tab>", press_shift_tab, { desc = "Todoer: Handle Shift-Tab", expr = true, buffer = 0 }) -- Keep expr=true
vim.keymap.set("n", "<leader>tt", toggle_todo, { desc = "Todoer: Toggle TODO", buffer = 0 })
vim.keymap.set("n", "<leader>ta", add_todo, { desc = "Todoer: Add TODO", buffer = 0 })
vim.keymap.set("n", "<leader>td", remove_todo, { desc = "Todoer: Remove TODO", buffer = 0 })
-- Optional: Notify that keymaps are set for this buffer
-- vim.notify("Todoer keymaps activated for this buffer", vim.log.levels.INFO)
end
-- Setup function
-- This function will be called by the user to configure and activate the plugin
-- For now, it just sets up the default keymaps
-- Setup function: Called by the user in their config
function M.setup(opts)
-- opts is a placeholder for future configuration options
opts = opts or {}
-- Set up the keymaps
vim.keymap.set("n", "<leader>t", function() end, { desc = "+TODOs" })
vim.keymap.set("i", "<CR>", press_enter, { desc = "On enter", noremap = true, expr = true })
vim.keymap.set("i", "<TAB>", press_tab, { desc = "On tab", noremap = true, expr = true })
vim.keymap.set("i", "S-Tab", press_shift_tab, { desc = "On shift tab", noremap = true, expr = true })
vim.keymap.set("n", "<leader>tt", toggle_todo, { desc = "Toggle TODO" })
vim.keymap.set("n", "<leader>ta", add_todo, { desc = "Add TODO" })
vim.keymap.set("n", "<leader>td", remove_todo, { desc = "Remove TODO" })
print("Todoer setup complete!") -- Added a print statement for confirmation
-- Merge user options with defaults
local config = vim.tbl_deep_extend("force", {}, defaults, opts or {})
-- Create an autocommand group to ensure we can clear it later if needed
local group = vim.api.nvim_create_augroup("TodoerUserSetup", { clear = true })
-- Create the autocommand
vim.api.nvim_create_autocmd("FileType", {
group = group,
pattern = config.filetypes, -- Use filetypes from config
desc = "Setup Todoer keymaps for specific filetypes",
callback = function()
-- Call the function that sets up buffer-local keymaps
setup_buffer_keymaps()
end,
})
end
-- Return the module table

@ -1,3 +1,8 @@
-- Load the main module and call its setup function.
-- This will execute the setup logic inside lua/todoer/init.lua immediately.
require('todoer').setup()
-- This file is intentionally left blank or can contain comments.
-- The plugin is now activated by calling require('todoer').setup()
-- in the user's Neovim configuration (e.g., init.lua or via a plugin manager).
-- Example user configuration (e.g., in init.lua or using lazy.nvim):
-- require('todoer').setup({
-- filetypes = { 'markdown', 'txt' } -- Optional: override default filetypes
-- })

Loading…
Cancel
Save