111 lines
2.1 KiB
Lua
111 lines
2.1 KiB
Lua
local utils = {}
|
|
|
|
utils.if_nil = function(x, was_nil, was_not_nil)
|
|
if x == nil then
|
|
return was_nil
|
|
else
|
|
return was_not_nil
|
|
end
|
|
end
|
|
|
|
utils.get_default = function(x, default)
|
|
return utils.if_nil(x, default, x)
|
|
end
|
|
|
|
utils.get_lazy_default = function(x, defaulter, ...)
|
|
if x == nil then
|
|
return defaulter(...)
|
|
else
|
|
return x
|
|
end
|
|
end
|
|
|
|
local function reversedipairsiter(t, i)
|
|
i = i - 1
|
|
if i ~= 0 then
|
|
return i, t[i]
|
|
end
|
|
end
|
|
|
|
utils.reversed_ipairs = function(t)
|
|
return reversedipairsiter, t, #t + 1
|
|
end
|
|
|
|
utils.default_table_mt = {
|
|
__index = function(t, k)
|
|
local obj = {}
|
|
rawset(t, k, obj)
|
|
return obj
|
|
end
|
|
}
|
|
|
|
utils.repeated_table = function(n, val)
|
|
local empty_lines = {}
|
|
for _ = 1, n do
|
|
table.insert(empty_lines, val)
|
|
end
|
|
return empty_lines
|
|
end
|
|
|
|
utils.quickfix_items_to_entries = function(locations)
|
|
local results = {}
|
|
|
|
for _, entry in ipairs(locations) do
|
|
local vimgrep_str = string.format(
|
|
"%s:%s:%s: %s",
|
|
vim.fn.fnamemodify(entry.filename, ":."),
|
|
entry.lnum,
|
|
entry.col,
|
|
entry.text
|
|
)
|
|
|
|
table.insert(results, {
|
|
valid = true,
|
|
value = entry,
|
|
ordinal = vimgrep_str,
|
|
display = vimgrep_str,
|
|
})
|
|
end
|
|
|
|
return results
|
|
end
|
|
|
|
utils.new_ngram = function()
|
|
return require("telescope._private.NGram"):new()
|
|
end
|
|
|
|
-- TODO: Figure out how to do this... could include in plenary :)
|
|
-- NOTE: Don't use this yet. It will segfault sometimes.
|
|
--
|
|
-- opts.shorten_path and function(value)
|
|
-- local result = {
|
|
-- valid = true,
|
|
-- display = utils.path_shorten(value),
|
|
-- ordinal = value,
|
|
-- value = value
|
|
-- }
|
|
|
|
-- return result
|
|
-- end or nil)
|
|
utils.path_shorten = (function()
|
|
if jit then
|
|
local ffi = require('ffi')
|
|
ffi.cdef [[
|
|
typedef unsigned char char_u;
|
|
char_u *shorten_dir(char_u *str);
|
|
]]
|
|
|
|
return function(path)
|
|
local c_str = ffi.new("char[?]", #path)
|
|
ffi.copy(c_str, path)
|
|
return ffi.string(ffi.C.shorten_dir(c_str))
|
|
end
|
|
else
|
|
return function(path)
|
|
return path
|
|
end
|
|
end
|
|
end)()
|
|
|
|
return utils
|