presser.nvim/lua/presser/utils.lua
TheOnePath 69af7b05d1
Updated utils.lua
M@clean_buffer no longer escapes sequences (caused undesirable
behaviour).

Added M@get_selection which will return four values indicating the
starting row/column and ending row/column of a visual-mode selection. If
no selection has been performed in buffer instance, values may be -1.
Value is "cached" as defined Vim behaviour of '< and '> positions.

Added M@subslice is used to get a substring of a word between two
whitespace characters (i.e., spaces). The `line` is the string to check,
and `col` is a position (e.g., from the cursor) within the word which
will be extracted. Return value is a table of starting and ending
indices of the original string.
2023-01-23 18:21:44 +00:00

64 lines
1.3 KiB
Lua

local M = {}
M.iter_table_dump = function ( arr )
for k, v in pairs(arr) do
print("Table:", k, v)
end
end
local escape_chars = function ( text )
return string.gsub(text, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", {
["\\"] = "\\\\",
["-"] = "\\-",
["("] = "\\(",
[")"] = "\\)",
["["] = "\\[",
["]"] = "\\]",
["{"] = "\\{",
["}"] = "\\}",
["?"] = "\\?",
["+"] = "\\+",
["*"] = "\\*",
["^"] = "\\^",
["$"] = "\\$",
["."] = "\\.",
})
end
-- :@dev: clean the text once it has been fetched from buffer
M.clean_buf = function ( text )
if not type(text) == "string" or text == nil then
return ""
end
return text:match( "^%s*(.-)%s*$" )
end
M.get_selection = function()
local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<"))
local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>"))
if csrow < cerow or (csrow == cerow and cscol <= cecol) then
return csrow - 1, cscol - 1, cerow - 1, cecol
else
return cerow - 1, cecol - 1, csrow - 1, cscol
end
end
M.subslice = function(line, col)
local i = 0
local j = 0
while true do
j = string.find(line, " ", j+1) -- find 'next' newline
if j == nil then break end
if j > col then break end
i = j
end
return { i, j }
end
return M