Skip to content

Commit

Permalink
[nvim] 🩹 Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
masa0x80 committed Jun 7, 2024
1 parent cc387b0 commit c8990a4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .config/nvim/lazy-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"lua-json5": { "branch": "master", "commit": "18d8308656f79fdaa5936b4e42bb07b4bbd2354e" },
"lualine.nvim": { "branch": "master", "commit": "0a5a66803c7407767b799067986b4dc3036e1983" },
"markdown-preview.nvim": { "branch": "feat/support-gantt", "commit": "4baae284e4116834fa34dfa7536c317461af9b09" },
"markdown.nvim": { "branch": "feat/supports-insert-mode", "commit": "8f73d7c4aa10ecc2dbce2861190ec1f6cf3f0a27" },
"markdown.nvim": { "branch": "master", "commit": "de349a70df54a32b3856e790ab856e6dfb94a825" },
"mason-lspconfig.nvim": { "branch": "main", "commit": "a4caa0d083aab56f6cd5acf2d42331b74614a585" },
"mason.nvim": { "branch": "main", "commit": "0950b15060067f752fde13a779a994f59516ce3d" },
"mkdir.nvim": { "branch": "main", "commit": "c55d1dee4f099528a1853b28bb28caa802eba217" },
Expand Down
99 changes: 69 additions & 30 deletions .config/nvim/lua/plugins/conf/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ local UNORDERED_LIST_PATTERN = "^%s*[-*+] $"
local ORDERED_LIST_PATTERN = "^%s*%d+[%.%)] $"
local TASK_PATTERN = "^%s*[-*+] %[[x ]%] $"

local function cursor_pos()
local cursor = vim.api.nvim_win_get_cursor(0) -- 1-based row, 0-based col
local row = cursor[1] - 1
local col = vim.fn.charcol("$") - 1
return row, col
local function is_list_item()
local md_ts = require("markdown.treesitter")
local curr_row = vim.fn.line(".") - 1
local curr_eol = vim.fn.col("$") - 1

return md_ts.find_node(function(node)
return node:type() == "list_item"
end, { pos = { curr_row, curr_eol } })
end

local function backspace()
local row, col = cursor_pos()
local col = vim.fn.col("$") - 1
local str = vim.api.nvim_buf_get_text(0, row, 0, row, col, {})[1]

local cw = vim.api.nvim_replace_termcodes("<C-w>", true, false, true)
Expand Down Expand Up @@ -70,54 +74,89 @@ require("markdown").setup({
on_attach = function(bufnr)
local map = vim.keymap.set
local opts = { noremap = true, buffer = bufnr }
map({ "n" }, "o", "<Cmd>MDListItemBelow<CR>", opts)
map({ "n" }, "O", "<Cmd>MDListItemAbove<CR>", opts)

-- New list item below on `o` if in a list
map("n", "o", function()
if is_list_item() then
vim.cmd("MDListItemBelow")
return
end

vim.api.nvim_feedkeys("o", "n", true)
end, { buffer = bufnr, desc = "Insert list item below" })

-- New list item above on `O` if in a list
map("n", "O", function()
if is_list_item() then
vim.cmd("MDListItemAbove")
return
end

vim.api.nvim_feedkeys("O", "n", true)
end, { buffer = bufnr, desc = "Insert list item above" })

-- New list item below on `<cr>` if in a list and at the end of the line
map("i", "<CR>", function()
local row = vim.fn.line(".") - 1
local col = vim.fn.col("$") - 1
local is_eol = vim.fn.col(".") == col + 1
local in_list = is_list_item()

local str = vim.api.nvim_buf_get_text(0, row, 0, row, col, {})[1]

if
string.match(str, UNORDERED_LIST_PATTERN)
or string.match(str, ORDERED_LIST_PATTERN)
or string.match(str, TASK_PATTERN)
then
vim.api.nvim_buf_set_text(0, row, 0, row, col, { "" })
elseif is_eol and in_list then
vim.cmd("MDListItemBelow")
return
end

local key = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
vim.api.nvim_feedkeys(key, "n", false)
end, { buffer = bufnr, desc = "Insert list item below" })

map("i", "<BS>", backspace, opts)
map("i", "<C-h>", backspace, opts)
map("i", "<Tab>", function()
local row, col = cursor_pos()
local row = vim.fn.line(".") - 1
local col = vim.fn.col("$") - 1
local str = vim.api.nvim_buf_get_text(0, row, 0, row, col, {})[1]

local tab = vim.api.nvim_replace_termcodes("<Tab>", true, false, true)
if
string.match(str, string.sub(UNORDERED_LIST_PATTERN, 0, -2))
or string.match(str, string.sub(ORDERED_LIST_PATTERN, 0, -2))
or string.match(str, string.sub(TASK_PATTERN, 0, -2))
then
utils.indent()
else
vim.api.nvim_feedkeys(tab, "n", true)
return
end

local tab = vim.api.nvim_replace_termcodes("<Tab>", true, false, true)
vim.api.nvim_feedkeys(tab, "n", true)
end, opts)
map("i", "<S-Tab>", function()
local row, col = cursor_pos()
local row = vim.fn.line(".") - 1
local col = vim.fn.col("$") - 1
local str = vim.api.nvim_buf_get_text(0, row, 0, row, col, {})[1]

local tab = vim.api.nvim_replace_termcodes("<S-Tab>", true, false, true)
if
string.match(str, string.sub(UNORDERED_LIST_PATTERN, 0, -2))
or string.match(str, string.sub(ORDERED_LIST_PATTERN, 0, -2))
or string.match(str, string.sub(TASK_PATTERN, 0, -2))
then
if string.sub(str, 0, 1) ~= " " then
return
end
utils.indent(false)
else
vim.api.nvim_feedkeys(tab, "n", true)
return
end
end, opts)
map("i", "<CR>", function()
local row, col = cursor_pos()
local str = vim.api.nvim_buf_get_text(0, row, 0, row, col, {})[1]

if
string.match(str, UNORDERED_LIST_PATTERN)
or string.match(str, ORDERED_LIST_PATTERN)
or string.match(str, TASK_PATTERN)
then
vim.api.nvim_buf_set_text(0, row, 0, row, col, { "" })
vim.fn.execute("startinsert!")
else
vim.fn.execute("MDListItemBelow")
end
local tab = vim.api.nvim_replace_termcodes("<S-Tab>", true, false, true)
vim.api.nvim_feedkeys(tab, "n", true)
end, opts)
end,
})
3 changes: 1 addition & 2 deletions .config/nvim/lua/plugins/markdown.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
return {
{
"masa0x80/markdown.nvim",
branch = "feat/supports-insert-mode",
"tadmccorkle/markdown.nvim",
ft = { "markdown" },
config = require("config.utils").load("conf/markdown"),
},
Expand Down

0 comments on commit c8990a4

Please sign in to comment.