[mini.ai]: Paragraph textobject? #1119
-
Based on the docs, it seems the best approach is to return a list of paragraph positions. Is there a [neo]vim function to get the selected region of a native text object? The implementation would be quite trivial in that case. I prefer to ask before implementing a buggy paragraph text object and then realize there's an easier way 😆 Anyway, i think adding at least the 'p' text object to mini.ai would be a great addition, i miss doing something like 'cinp', and the next best option is '}jcip', which is rather inconvenient. For 'w' and 'W', the alternatives 'wcw', 'WcW' are just as good or even better. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
I am not aware of a better way for paragraph textobject than writing a function which returns either a single target region or an array of regions. I think the latter is easier to implement. Here is a partial solution showcasing basic approach: local inner_paragraph = function()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local res, cur_paragraph = {}, { vis_mode = 'V' }
for i, l in ipairs(lines) do
if l:find('^%s*$') ~= nil then
if cur_paragraph.from ~= nil then
cur_paragraph.to = { line = i - 1, col = 1 }
table.insert(res, cur_paragraph)
cur_paragraph = { vis_mode = 'V' }
end
else
if cur_paragraph.from == nil then cur_paragraph.from = { line = i, col = 1 } end
end
end
if cur_paragraph.from ~= nil then
cur_paragraph.to = { line = #lines, col = 1 }
table.insert(res, cur_paragraph)
end
return res
end
local outer_paragraph = function()
-- Left as homework
end
local paragraph = function(ai_type, _, _)
return ai_type == 'i' and inner_paragraph() or outer_paragraph()
end
require('mini.ai').setup({
-- Is needed otherwise paragraphs more than `n_lines` lines will not be selected
n_lines = 9999,
custom_textobjects = {
p = paragraph,
},
}) Adding built-in paragraph textobject is an interesting idea and even probably doable in as Note: make sure to update to latest |
Beta Was this translation helpful? Give feedback.
-
Thanks @echasnovski , i did my homework and implemented outer_paragraph :), in case it helps anyone: local outer_paragraph = function()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
local res, cur_paragraph = {}, { vis_mode = "V" }
for i, l in ipairs(lines) do
if not l:find("^%s*$") and cur_paragraph.to then
table.insert(res, cur_paragraph)
cur_paragraph = { vis_mode = "V" }
end
if not l:find("^%s*$") and not cur_paragraph.from and not cur_paragraph.to then
cur_paragraph.from = { line = i, col = 1 }
elseif l:find("^%s*$") then
cur_paragraph.to = { line = i, col = 1 }
end
end
local last_from = cur_paragraph.from.line - 1 > 0 and cur_paragraph.from.line - 1 or 1
cur_paragraph.from = { line = last_from, col = 1 }
cur_paragraph.to = { line = #lines, col = 1 }
table.insert(res, cur_paragraph)
return res
end |
Beta Was this translation helpful? Give feedback.
I am not aware of a better way for paragraph textobject than writing a function which returns either a single target region or an array of regions. I think the latter is easier to implement.
Here is a partial solution showcasing basic approach: