Skip to content

Commit 4b392a7

Browse files
committed
fix: whitespacing fixes
1 parent 6014c41 commit 4b392a7

File tree

3 files changed

+136
-70
lines changed

3 files changed

+136
-70
lines changed

lua/markdown-shortcuts/commands.lua

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,34 @@ function M.insert_bold(opts)
145145
if opts.range == 2 then
146146
local start_pos = vim.fn.getpos("'<")
147147
local end_pos = vim.fn.getpos("'>")
148-
local text = get_visual_selection()
148+
149+
-- Check for invalid positions (e.g., first use before selection)
150+
if start_pos[2] == 0 or end_pos[2] == 0 then
151+
vim.notify("MarkdownTools: No visual selection marks found. Select text first.", vim.log.levels.WARN)
152+
return -- Exit if marks are invalid
153+
end
154+
155+
-- Ensure start_pos is always before end_pos
156+
if start_pos[2] > end_pos[2] or (start_pos[2] == end_pos[2] and start_pos[3] > end_pos[3]) then
157+
start_pos, end_pos = end_pos, start_pos -- Swap them
158+
end
159+
160+
-- No need to set/restore marks, get_visual_selection uses current ones
161+
local text = get_visual_selection() -- Get the actual text content
149162
local new_text = { "**" .. text .. "**" }
150163

151-
-- Calculate 0-based indices for nvim_buf_set_text
164+
-- Calculate 0-based indices for nvim_buf_set_text using the ordered positions
152165
local start_lnum = start_pos[2] - 1
153166
local start_col = start_pos[3] - 1
154167
local end_lnum = end_pos[2] - 1
155168
local end_col = end_pos[3] -- end_col is exclusive
156169

157170
-- Replace the selected text directly
158171
vim.api.nvim_buf_set_text(0, start_lnum, start_col, end_lnum, end_col, new_text)
172+
173+
-- Re-select the modified text
174+
vim.cmd('normal! gv')
159175
else
160-
-- No range, prompt for input
161176
vim.ui.input({ prompt = "Bold text: " }, function(text)
162177
if text and text ~= "" then
163178
vim.api.nvim_put({ "**" .. text .. "**" }, "c", true, true)
@@ -176,19 +191,34 @@ function M.insert_italic(opts)
176191
if opts.range == 2 then
177192
local start_pos = vim.fn.getpos("'<")
178193
local end_pos = vim.fn.getpos("'>")
179-
local text = get_visual_selection()
194+
195+
-- Check for invalid positions (e.g., first use before selection)
196+
if start_pos[2] == 0 or end_pos[2] == 0 then
197+
vim.notify("MarkdownTools: No visual selection marks found. Select text first.", vim.log.levels.WARN)
198+
return -- Exit if marks are invalid
199+
end
200+
201+
-- Ensure start_pos is always before end_pos
202+
if start_pos[2] > end_pos[2] or (start_pos[2] == end_pos[2] and start_pos[3] > end_pos[3]) then
203+
start_pos, end_pos = end_pos, start_pos -- Swap them
204+
end
205+
206+
-- No need to set/restore marks, get_visual_selection uses current ones
207+
local text = get_visual_selection() -- Get the actual text content
180208
local new_text = { "*" .. text .. "*" }
181209

182-
-- Calculate 0-based indices for nvim_buf_set_text
210+
-- Calculate 0-based indices for nvim_buf_set_text using the ordered positions
183211
local start_lnum = start_pos[2] - 1
184212
local start_col = start_pos[3] - 1
185213
local end_lnum = end_pos[2] - 1
186214
local end_col = end_pos[3] -- end_col is exclusive
187215

188216
-- Replace the selected text directly
189217
vim.api.nvim_buf_set_text(0, start_lnum, start_col, end_lnum, end_col, new_text)
218+
219+
-- Re-select the modified text
220+
vim.cmd('normal! gv')
190221
else
191-
-- No range, prompt for input
192222
vim.ui.input({ prompt = "Italic text: " }, function(text)
193223
if text and text ~= "" then
194224
vim.api.nvim_put({ "*" .. text .. "*" }, "l", true, true)
@@ -205,14 +235,34 @@ function M.insert_link(opts)
205235

206236
-- Check if the command was called with a range
207237
if opts.range == 2 then
208-
local text = get_visual_selection()
209-
vim.api.nvim_command("normal! gvd")
238+
local start_pos = vim.fn.getpos("'<")
239+
local end_pos = vim.fn.getpos("'>")
240+
241+
-- Check for invalid positions
242+
if start_pos[2] == 0 or end_pos[2] == 0 then
243+
vim.notify("MarkdownTools: No visual selection marks found.", vim.log.levels.WARN)
244+
return
245+
end
246+
247+
-- Ensure start_pos is always before end_pos
248+
if start_pos[2] > end_pos[2] or (start_pos[2] == end_pos[2] and start_pos[3] > end_pos[3]) then
249+
start_pos, end_pos = end_pos, start_pos -- Swap them
250+
end
251+
252+
local text = get_visual_selection() -- Get the selected text
253+
210254
vim.ui.input({ prompt = "URL: " }, function(url)
211-
if url and url ~= "" then
212-
vim.api.nvim_put({ "[" .. text .. "](" .. url .. ")" }, "c", false, true)
213-
else
214-
vim.api.nvim_put({ "[" .. text .. "](url)" }, "c", false, true)
215-
end
255+
local final_url = (url and url ~= "") and url or "url"
256+
local new_text = { "[" .. text .. "](" .. final_url .. ")" }
257+
258+
-- Calculate 0-based indices for nvim_buf_set_text
259+
local start_lnum = start_pos[2] - 1
260+
local start_col = start_pos[3] - 1
261+
local end_lnum = end_pos[2] - 1
262+
local end_col = end_pos[3] -- end_col is exclusive
263+
264+
-- Replace the selected text directly
265+
vim.api.nvim_buf_set_text(0, start_lnum, start_col, end_lnum, end_col, new_text)
216266
end)
217267
else
218268
-- No range, prompt for input

lua/markdown-shortcuts/keymaps.lua

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -57,66 +57,83 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
5757
end,
5858
desc = "Header",
5959
},
60-
-- Removed insert_list_item keymap
6160
{
6261
command_key = "insert_code_block",
63-
mode = { "n", "v" },
62+
mode = "n", -- Normal mode only
6463
key = keymaps.insert_code_block,
6564
cmd = function()
66-
local mode = vim.fn.mode(1)
67-
local opts = {}
68-
if mode:match("^[vV\022]") then
69-
opts.range = 2
70-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
71-
end
72-
require("markdown-shortcuts.commands").insert_code_block(opts)
65+
require("markdown-shortcuts.commands").insert_code_block({ range = 0 })
66+
end,
67+
desc = "Code block (Normal)",
68+
},
69+
{
70+
command_key = "insert_code_block",
71+
mode = "v", -- Visual mode only
72+
key = keymaps.insert_code_block,
73+
cmd = function()
74+
-- Still need Lua for prompt, exit visual first
75+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
76+
require("markdown-shortcuts.commands").insert_code_block({ range = 2 })
7377
end,
74-
desc = "Code block",
78+
desc = "Code block (Visual)",
7579
},
7680
{
7781
command_key = "insert_bold",
78-
mode = { "n", "v" },
82+
mode = "n", -- Normal mode only
7983
key = keymaps.insert_bold,
8084
cmd = function()
81-
local mode = vim.fn.mode(1)
82-
local opts = {}
83-
if mode:match("^[vV\022]") then
84-
opts.range = 2
85-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
86-
end
87-
require("markdown-shortcuts.commands").insert_bold(opts)
85+
-- Call command function (prompts for input in normal mode)
86+
require("markdown-shortcuts.commands").insert_bold({ range = 0 }) -- Explicitly pass range 0
8887
end,
89-
desc = "Bold text",
88+
desc = "Bold text (Normal)",
89+
},
90+
{
91+
command_key = "insert_bold",
92+
mode = "v", -- Visual mode only
93+
key = keymaps.insert_bold,
94+
-- Use Vim command sequence for visual wrapping
95+
cmd = 's**<C-r>"**<Esc>',
96+
desc = "Bold text (Visual)",
97+
opts = { remap = true } -- Allow remapping <C-r>
9098
},
9199
{
92100
command_key = "insert_italic",
93-
mode = { "n", "v" },
101+
mode = "n", -- Normal mode only
94102
key = keymaps.insert_italic,
95103
cmd = function()
96-
local mode = vim.fn.mode(1)
97-
local opts = {}
98-
if mode:match("^[vV\022]") then
99-
opts.range = 2
100-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
101-
end
102-
require("markdown-shortcuts.commands").insert_italic(opts)
104+
-- Call command function (prompts for input in normal mode)
105+
require("markdown-shortcuts.commands").insert_italic({ range = 0 }) -- Explicitly pass range 0
103106
end,
104-
desc = "Italic text",
107+
desc = "Italic text (Normal)",
108+
},
109+
{
110+
command_key = "insert_italic",
111+
mode = "v", -- Visual mode only
112+
key = keymaps.insert_italic,
113+
-- Use Vim command sequence for visual wrapping
114+
cmd = 's*<C-r>"*<Esc>',
115+
desc = "Italic text (Visual)",
116+
opts = { remap = true } -- Allow remapping <C-r>
105117
},
106118
{
107119
command_key = "insert_link",
108-
mode = { "n", "v" },
120+
mode = "n", -- Normal mode only
109121
key = keymaps.insert_link,
110122
cmd = function()
111-
local mode = vim.fn.mode(1)
112-
local opts = {}
113-
if mode:match("^[vV\022]") then
114-
opts.range = 2
115-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
116-
end
117-
require("markdown-shortcuts.commands").insert_link(opts)
123+
require("markdown-shortcuts.commands").insert_link({ range = 0 })
124+
end,
125+
desc = "Link (Normal)",
126+
},
127+
{
128+
command_key = "insert_link",
129+
mode = "v", -- Visual mode only
130+
key = keymaps.insert_link,
131+
cmd = function()
132+
-- Still need Lua for prompt, exit visual first
133+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
134+
require("markdown-shortcuts.commands").insert_link({ range = 2 })
118135
end,
119-
desc = "Link",
136+
desc = "Link (Visual)",
120137
},
121138
{
122139
command_key = "insert_table",
@@ -126,19 +143,22 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
126143
desc = "Insert table",
127144
},
128145
{
129-
command_key = "insert_checkbox", -- Renamed key
130-
mode = { "n", "v" },
131-
key = keymaps.insert_checkbox, -- Renamed key
146+
command_key = "insert_checkbox",
147+
mode = "n", -- Normal mode only
148+
key = keymaps.insert_checkbox,
132149
cmd = function()
133-
local mode = vim.fn.mode(1)
134-
local opts = {}
135-
if mode:match("^[vV\022]") then
136-
opts.range = 2
137-
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'n', false)
138-
end
139-
require("markdown-shortcuts.commands").insert_checkbox(opts)
150+
require("markdown-shortcuts.commands").insert_checkbox({ range = 0 })
140151
end,
141-
desc = "Checkbox", -- Updated description
152+
desc = "Checkbox (Normal)",
153+
},
154+
{
155+
command_key = "insert_checkbox",
156+
mode = "v", -- Visual mode only
157+
key = keymaps.insert_checkbox,
158+
-- Use Vim command sequence for visual wrapping
159+
cmd = 's- [ ] <C-r>"<Esc>',
160+
desc = "Checkbox (Visual)",
161+
opts = { remap = true } -- Allow remapping <C-r>
142162
},
143163
{
144164
command_key = "toggle_checkbox", -- Renamed key
@@ -160,16 +180,12 @@ function M.setup_keymaps(keymaps, commands_enabled, file_types)
160180
for _, config in ipairs(keymap_configs) do
161181
-- Only set keymap if the command is enabled
162182
if commands_enabled[config.command_key] then
163-
local opts = config.expr and { expr = config.expr } or {}
183+
-- Combine base opts with config-specific opts
184+
local base_opts = { desc = config.desc, buffer = true }
185+
local final_opts = vim.tbl_extend("force", base_opts, config.opts or {})
164186

165-
local modes = config.mode
166-
if type(modes) == "string" then
167-
setup_keymap(modes, config.key, config.cmd, config.desc, opts)
168-
else
169-
for _, mode in ipairs(modes) do
170-
setup_keymap(mode, config.key, config.cmd, config.desc, opts)
171-
end
172-
end
187+
-- Use setup_keymap helper
188+
setup_keymap(config.mode, config.key, config.cmd, config.desc, final_opts)
173189
end
174190
end
175191

File renamed without changes.

0 commit comments

Comments
 (0)