How do I select *Markdown* emphasis? #547
-
|
I'd like to be able to select the inside and outside of Markdown emphasis, e.g. for I'd like to be able to easily select: I'm not quite sure what to add, but I tried adding to ; extends
(emphasis) @text.emphasisThe former triggers an error. I'd like to set in -- Markdown
['ae'] = '@text.emphasis', |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
I use this in ; extends
((strong_emphasis) @emphasis.inner
(#offset! @emphasis.inner 0 2 0 -2))
((emphasis) @emphasis.inner
(#offset! @emphasis.inner 0 1 0 -1))
(strong_emphasis) @emphasis.outer
(emphasis) @emphasis.outer |
Beta Was this translation helpful? Give feedback.
-
|
I got the following to work. It includes italics, bold, italics + bold. In In -- Markdown emphasis
local tree_shared = require('nvim-treesitter-textobjects.shared')
-- From `select.lua` with comments removed.
---@param range Range
---@param selection_mode TSTextObjects.SelectionMode
local function update_selection(range, selection_mode)
---@type integer, integer, integer, integer
local start_row, start_col, end_row, end_col = vim.treesitter._range.unpack4(range)
selection_mode = selection_mode or 'v'
local mode = vim.api.nvim_get_mode()
if mode.mode ~= selection_mode then
selection_mode = vim.api.nvim_replace_termcodes(selection_mode, true, true, true)
vim.cmd.normal({ selection_mode, bang = true })
end
if end_col == 0 then
end_row = end_row - 1
end_col = #vim.api.nvim_buf_get_lines(0, end_row, end_row + 1, true)[1] + 1
end
local end_col_offset = 1
if selection_mode == 'v' and vim.o.selection == 'exclusive' then
end_col_offset = 0
end
end_col = end_col - end_col_offset
vim.api.nvim_win_set_cursor(0, { start_row + 1, start_col })
vim.cmd('normal! o')
vim.api.nvim_win_set_cursor(0, { end_row + 1, end_col })
end
local function select_emphasis(is_outer)
local opts = { lookahead = true }
local range1 = tree_shared.textobject_at_point('@emphasis', 'textobjects', nil, nil, opts)
local range2 = tree_shared.textobject_at_point('@emphasis_strong', 'textobjects', nil, nil, opts)
local range
if range1 == nil and range2 == nil then
return
elseif range1 ~= nil and range2 ~= nil then
local start_row_1, start_col_1, start_bytes_1, end_row_1, end_col_1 = unpack(range1)
local start_row_2, start_col_2, start_bytes_2, end_row_2, end_col_2 = unpack(range2)
if is_outer then
if start_bytes_1 < start_bytes_2 then
range = { start_row_1, start_col_1, end_row_1, end_col_1 }
else
range = { start_row_2, start_col_2, end_row_2, end_col_2 }
end
else
if start_row_1 == start_row_2 and end_row_1 == end_row_2 then
-- Bold and italic
if start_col_2 - start_col_1 == 1 and end_col_1 - end_col_2 == 1 then
range = { start_row_1, start_col_2 + 2, end_row_1, end_col_2 - 2 }
elseif start_col_1 - start_col_2 == 2 and end_col_2 - end_col_1 == 2 then
range = { start_row_2, start_col_1 + 1, end_row_2, end_col_1 - 1 }
end
end
if range == nil then
if start_bytes_1 < start_bytes_2 then
range = { start_row_1, start_col_1 + 1, end_row_1, end_col_1 - 1 }
else
range = { start_row_2, start_col_2 + 2, end_row_2, end_col_2 - 2 }
end
end
end
elseif range1 ~= nil then
local start_row_1, start_col_1, start_bytes_1, end_row_1, end_col_1 = unpack(range1)
if is_outer then
range = { start_row_1, start_col_1, end_row_1, end_col_1 }
else
range = { start_row_1, start_col_1 + 1, end_row_1, end_col_1 - 1 }
end
elseif range2 ~= nil then
local start_row_2, start_col_2, start_bytes_2, end_row_2, end_col_2 = unpack(range2)
if is_outer then
range = { start_row_2, start_col_2, end_row_2, end_col_2 }
else
range = { start_row_1, start_col_1 + 2, end_row_1, end_col_1 - 2 }
end
end
update_selection(range)
end
return {
-- ...
-- *Markdown*
{ 'ae', function() select_emphasis(true) end, mode = { 'x', 'o' }, desc = 'outer emphasis', ft = 'markdown' },
{ 'ie', function() select_emphasis(false) end, mode = { 'x', 'o' }, desc = 'inner emphasis', ft = 'markdown' },
-- ... |
Beta Was this translation helpful? Give feedback.
I got the following to work. It includes italics, bold, italics + bold. In
~/.config/nvim/after/queries/markdown_inline/textobjects.scm:In
~/.config/nvim/lua/plugins/treesitter-textobjects.lua: