Skip to content

Commit

Permalink
Added more keymaps for comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
atropos112 committed Feb 1, 2025
1 parent d6176ba commit d806ff8
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 114 deletions.
8 changes: 8 additions & 0 deletions lua/atro/globals/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
return require("atro.types.keymaps"):new({
comments = {
generate_annotation = { "<leader>co", "Generate annotation", "nv" },
-- Below keymaps are a bit "limited" in selection due to how "numToStr/Comment.nvim" works.
toggle_comment_line = { "gcc", "Toggle comment line", "n" }, -- Only works in normal mode, mode here is ignored
toggle_comment_block = { "gbc", "Toggle comment block", "n" }, -- Only works in normal mode, mode here is ignored
operator_pending_line = { "<leader>gc", "Toggle comment line", "v" }, -- Only works in visual mode, mode here is ignored. Keymap must start with <leader>.
operator_pending_block = { "<leader>gb", "Toggle comment block", "v" }, -- Only works in visual mode, mode here is ignored. Keymap must start with <leader>.
comment_above = { "gcO", "Comment line above", "n" },
comment_below = { "gco", "Comment line below", "n" },
comment_end_of_line = { "gC", "Comment to end of line", "n" },
},
lsp_on_attach = {
lsp_references = { "gR", "Show LSP references", "nv" },
Expand Down
87 changes: 87 additions & 0 deletions lua/atro/plugins/comments.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---@type LazyPlugin[]
return {
-- Section: Plugin to create docstrings and annotations for functions, classes, etc.
{
"danymat/neogen",
event = { "LspAttach" }, -- For some reason, the plugin doesn't work without this event
config = function()
local neogen = require("neogen")
local keymap = KEYMAPS.comments.generate_annotation

neogen.setup({
{
snippet_engine = "luasnip",
languages = {
python = {
template = {
annotation_convention = "google_docstrings",
},
},
},
},
})

KEYMAPS:set(keymap, neogen.generate)
end,
},

-- Section: Highlights comments when starting with correct keywards (like `TODO`, `FIXME`, etc.)
{
"folke/todo-comments.nvim",
event = { "BufRead" },
dependencies = { "nvim-lua/plenary.nvim" },
opts = {
keywords = {
FIX = {
icon = "", -- icon used for the sign, and in search results
color = "error", -- can be a hex color, or a named color (see below)
alt = { "FIXME", "BUG", "FIXIT", "ISSUE" }, -- a set of other keywords that all map to this FIX keywords
-- signs = false, -- configure signs for some keywords individually
},
TODO = { icon = "", color = "info", alt = { "FIXME" } },
HACK = { icon = "", color = "warning" },
WARN = { icon = "", color = "warning", alt = { "WARNING", "XXX" } },
SECTION = { icon = "", alt = { "SEC", "Section" } },
NOTE = { icon = "", color = "hint", alt = { "INFO" } },
TEST = { icon = "", color = "test", alt = { "TESTING", "PASSED", "FAILED" } },
},
},
},
-- Section: Comment plugin, support for treesitter, dot-repeats, left-right-up-down motions and hooks.
{
"numToStr/Comment.nvim",
event = { "BufRead" },
config = function()
local keys = KEYMAPS.comments

require("Comment").setup(
---@type CommentConfig
{
mappings = {
-- Operator-pending mapping; `gcc` `gbc` `gc[count]{motion}` `gb[count]{motion}`
basic = true,

-- Extra mapping; `gco`, `gcO`, `gcA`
extra = true,
},

toggler = {
line = keys.toggle_comment_line.key,
block = keys.toggle_comment_block.key,
},

opleader = {
line = KEYMAPS:key_without_leader(keys.operator_pending_line),
block = KEYMAPS:key_without_leader(keys.operator_pending_block),
},

extra = {
above = keys.comment_above.key,
below = keys.comment_below.key,
eol = keys.comment_end_of_line.key,
},
}
)
end,
},
}
119 changes: 12 additions & 107 deletions lua/atro/plugins/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,6 @@ return {
},
opts = {},
},
{
"saecki/live-rename.nvim",
event = "LspAttach",
config = function()
local live_rename = require("live-rename")

live_rename.setup({})
key("n", "gy", live_rename.map({ insert = true }), { desc = "LSP rename" })
end,
},
{
"chrisgrieser/nvim-puppeteer",
event = "BufRead",
},
{
-- TODO: Analyze all keys
"folke/ts-comments.nvim",
event = "VeryLazy",
opts = {},
},
{
"numToStr/Comment.nvim",
event = "BufRead",
opts = {
mappings = {
basic = true,
extra = true,
},
},
},
-- {
-- "smoka7/multicursors.nvim",
-- event = "VeryLazy",
Expand Down Expand Up @@ -161,26 +131,18 @@ return {
end,
},
{
-- INFO: Commenting plugin, allows commenting out lines and blocks of code
"folke/todo-comments.nvim",
event = "BufRead",
dependencies = { "nvim-lua/plenary.nvim" },
opts = {
keywords = {
FIX = {
icon = "", -- icon used for the sign, and in search results
color = "error", -- can be a hex color, or a named color (see below)
alt = { "FIXME", "BUG", "FIXIT", "ISSUE" }, -- a set of other keywords that all map to this FIX keywords
-- signs = false, -- configure signs for some keywords individually
},
TODO = { icon = "", color = "info" },
HACK = { icon = "", color = "warning" },
WARN = { icon = "", color = "warning", alt = { "WARNING", "XXX" } },
SECTION = { icon = "", alt = { "SEC", "Section" } },
NOTE = { icon = "", color = "hint", alt = { "INFO" } },
TEST = { icon = "", color = "test", alt = { "TESTING", "PASSED", "FAILED" } },
},
},
"saecki/live-rename.nvim",
event = { "LspAttach" },
config = function()
local live_rename = require("live-rename")

live_rename.setup({})
key("n", "gy", live_rename.map({ insert = true }), { desc = "LSP rename" })
end,
},
{
"chrisgrieser/nvim-puppeteer",
event = { "BufRead" },
},
-- Section: Plugin to pop-up a window with the definition of a function or variable.
-- The below implementation has window size set to 30% of the editor size and appaers in top right corner.
Expand Down Expand Up @@ -238,70 +200,13 @@ return {
})
end,
},
-- Section: Plugin overloads h and l to fold/unfold if the cursor is at the start of the line, otherwise it moves the cursor
-- as usual. Very natural, and small plugin.
{
"chrisgrieser/nvim-origami",
event = { "VeryLazy" },
opts = {}, -- needed even when using default config
},
-- Section: Adds some nicer folding capabilities, like folding based on treesitter or indent.
-- Also offers fold all and unfold all commands.
{
"kevinhwang91/nvim-ufo",
dependencies = {
"kevinhwang91/promise-async",
},
event = { "LspAttach" },
config = function()
local ufo = require("ufo")
local keys = KEYMAPS.fold

ufo.setup({
provider_selector = function(_, _, _)
return { "treesitter", "indent" }
end,
})
KEYMAPS:set_many({
{ keys.open_fold, "zo" }, -- "zo" is default we are "mapping" to
{ keys.close_fold, "zc" }, -- "zc" is default we are "mapping" to
{ keys.open_all_folds, ufo.openAllFolds },
{ keys.close_all_folds, ufo.closeAllFolds },
})
end,
},
-- Section: Plugin to peek at the line number we are jumping to when using :<n> inside of a file.
{
"nacro90/numb.nvim",
event = { "BufRead" },
opts = {},
},

-- Section: Plugin to create docstrings and annotations for functions, classes, etc.
{
"danymat/neogen",
event = { "LspAttach" }, -- For some reason, the plugin doesn't work without this event
config = function()
local neogen = require("neogen")
local keymap = KEYMAPS.comments.generate_annotation

neogen.setup({
{
snippet_engine = "luasnip",
languages = {
python = {
template = {
annotation_convention = "google_docstrings",
},
},
},
},
})

KEYMAPS:set(keymap, neogen.generate)
end,
},

-- Section: Plugin to show outline of the file in a window.
-- Showing a side window (on the right) with functions, classes, variables etc.
{
Expand Down
35 changes: 35 additions & 0 deletions lua/atro/plugins/folding.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---@type LazyPlugin[]
return {
-- Section: Plugin overloads h and l to fold/unfold if the cursor is at the start of the line, otherwise it moves the cursor
-- as usual. Very natural, and small plugin.
{
"chrisgrieser/nvim-origami",
event = { "VeryLazy" },
opts = {}, -- needed even when using default config
},
-- Section: Adds some nicer folding capabilities, like folding based on treesitter or indent.
-- Also offers fold all and unfold all commands.
{
"kevinhwang91/nvim-ufo",
dependencies = {
"kevinhwang91/promise-async",
},
event = { "LspAttach" },
config = function()
local ufo = require("ufo")
local keys = KEYMAPS.fold

ufo.setup({
provider_selector = function(_, _, _)
return { "treesitter", "indent" }
end,
})
KEYMAPS:set_many({
{ keys.open_fold, "zo" }, -- "zo" is default we are "mapping" to
{ keys.close_fold, "zc" }, -- "zc" is default we are "mapping" to
{ keys.open_all_folds, ufo.openAllFolds },
{ keys.close_all_folds, ufo.closeAllFolds },
})
end,
},
}
1 change: 1 addition & 0 deletions lua/atro/plugins/lang_specific/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ if CONFIG.languages["lua"] then
"dap",
"conform.nvim",
"nvim-dap",
"Comment.nvim",
},
enabled = function()
return true
Expand Down
16 changes: 9 additions & 7 deletions lua/atro/plugins/lang_specific/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ if CONFIG.languages["markdown"] then
},
config = function()
require("markview").setup({
modes = { "n", "no", "c" },
hybrid_modes = { "n" },
callbacks = {
on_enable = function(_, win)
vim.wo[win].conceallevel = 2
vim.wo[win].concealcursor = "c"
end,
preview = {
modes = { "n", "no", "c" },
hybrid_modes = { "n" },
callbacks = {
on_enable = function(_, win)
vim.wo[win].conceallevel = 2
vim.wo[win].concealcursor = "c"
end,
},
},
})
end,
Expand Down
30 changes: 30 additions & 0 deletions lua/atro/types/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@

---@class CommentKeymaps
---@field generate_annotation Keymap
---@field toggle_comment_line Keymap
---@field toggle_comment_block Keymap
---@field comment_above Keymap
---@field comment_below Keymap
---@field comment_end_of_line Keymap
---@field operator_pending_line Keymap
---@field operator_pending_block Keymap

---@class FoldKeymaps
---@field open_fold Keymap
Expand Down Expand Up @@ -170,6 +177,29 @@ local parse_keymap = function(keymap)
}
end

---@param key Keymap | table
---@param error_on_missing? boolean
---@return string
function Keymaps:key_without_leader(key, error_on_missing)
if error_on_missing == nil then
error_on_missing = true
end

key = parse_keymap(key)
-- Fetch first <leader> part of the key and see if equal to <leader>
local maybe_leader = key.key:sub(1, 8)

if maybe_leader == "<leader>" then
return key.key:sub(9)
end

if error_on_missing then
error("Keymap does not start with <leader>: " .. key.key)
end

return key.key
end

---@param key Keymap | table
---@param cmd string|function
---@param opts? vim.keymap.set.Opts
Expand Down

0 comments on commit d806ff8

Please sign in to comment.