mirror of
https://github.com/Ascyii/telekasten.nvim.git
synced 2026-01-01 14:14:24 -05:00
* fix(tags): fix rg command if PCRE not available (closes #145) Some linux distro do not compile ripgrep using the --pcre2 flag. This commit checks if the --pcre2 flag is available and then tweaks the ripgrep command accordingly. At the moment, it will therefore not be possible to filter-out hexademical color codes from #tags for people that do not have the --pcre2 flag available. This will be documented in a later commit. **Important**: the detection of the PCRE availability is done by trying a simple rg search using _os.execute_. This function interface was changed in Lua 5.2, which means it will break when Neovim will update to Lua >5.1. The fix is already there in commented form. Fixes #145 * refact: cleanup * doc(tag): add note about rg --pcre2 * doc(tag): add note about rg --pcre2 in vim help
173 lines
4.3 KiB
Lua
173 lines
4.3 KiB
Lua
local Job = require("plenary.job")
|
|
|
|
local M = {}
|
|
local hashtag_re = "(^|\\s|'|\")#[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*"
|
|
-- PCRE hashtag allows to remove the hex color codes from hastags
|
|
local hashtag_re_pcre =
|
|
"(^|\\s|'|\")((?!(#[a-fA-F0-9]{3})(\\W|$)|(#[a-fA-F0-9]{6})(\\W|$))#[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*)"
|
|
local colon_re = "(^|\\s):[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*:"
|
|
local yaml_re =
|
|
"(^|\\s)tags:\\s*\\[\\s*([a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\\-_]*(,\\s*)*)*\\s*]"
|
|
|
|
local function command_find_all_tags(opts)
|
|
opts = opts or {}
|
|
opts.cwd = opts.cwd or "."
|
|
opts.templateDir = opts.templateDir or ""
|
|
opts.rg_pcre = opts.rg_pcre or false
|
|
|
|
-- do not list tags in the template directory
|
|
local globArg = ""
|
|
if opts.templateDir ~= "" then
|
|
globArg = "--glob=!" .. "**/" .. opts.templateDir .. "/*.md"
|
|
end
|
|
|
|
local re = hashtag_re
|
|
|
|
if opts.tag_notation == ":tag:" then
|
|
re = colon_re
|
|
end
|
|
|
|
if opts.tag_notation == "yaml-bare" then
|
|
re = yaml_re
|
|
end
|
|
|
|
local rg_args = {
|
|
"--vimgrep",
|
|
globArg,
|
|
"-o",
|
|
re,
|
|
"--",
|
|
opts.cwd,
|
|
}
|
|
|
|
-- PCRE engine allows to remove hex color codes from #hastags
|
|
if opts.rg_pcre then
|
|
re = hashtag_re_pcre
|
|
|
|
rg_args = {
|
|
"--vimgrep",
|
|
"--pcre2",
|
|
globArg,
|
|
"-o",
|
|
re,
|
|
"--",
|
|
opts.cwd,
|
|
}
|
|
end
|
|
|
|
return "rg", rg_args
|
|
end
|
|
|
|
-- strips away leading ' or " , then trims whitespace
|
|
local function trim(s)
|
|
if s:sub(1, 1) == '"' or s:sub(1, 1) == "'" then
|
|
s = s:sub(2)
|
|
end
|
|
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
|
|
end
|
|
|
|
local function insert_tag(tbl, tag, entry)
|
|
entry.t = tag
|
|
if tbl[tag] == nil then
|
|
tbl[tag] = { entry }
|
|
else
|
|
tbl[tag][#tbl[tag] + 1] = entry
|
|
end
|
|
end
|
|
|
|
local function split(line, sep, n)
|
|
local startpos = 0
|
|
local endpos
|
|
local ret = {}
|
|
for _ = 1, n - 1 do
|
|
endpos = line:find(sep, startpos + 1)
|
|
ret[#ret + 1] = line:sub(startpos + 1, endpos - 1)
|
|
startpos = endpos
|
|
end
|
|
-- now the remainder
|
|
ret[n] = line:sub(startpos + 1)
|
|
return ret
|
|
end
|
|
|
|
local function yaml_to_tags(line, entry, ret)
|
|
local _, startpos = line:find("tags%s*:%s*%[")
|
|
local global_end = line:find("%]")
|
|
|
|
line = line:sub(startpos + 1, global_end)
|
|
|
|
local i = 1
|
|
local j
|
|
local prev_i = 1
|
|
local tag
|
|
while true do
|
|
i, j = line:find("%s*(%S*)%s*,", i)
|
|
if i == nil then
|
|
tag = line:sub(prev_i)
|
|
tag = tag:gsub("%s*(%S*)%s*", "%1")
|
|
else
|
|
tag = line:sub(i, j)
|
|
tag = tag:gsub("%s*(%S*)%s*,", "%1")
|
|
end
|
|
|
|
local new_entry = {}
|
|
|
|
-- strip trailing ]
|
|
tag = tag:gsub("]", "")
|
|
new_entry.t = tag
|
|
new_entry.l = entry.l
|
|
new_entry.fn = entry.fn
|
|
new_entry.c = startpos + (i or prev_i)
|
|
insert_tag(ret, tag, new_entry)
|
|
if i == nil then
|
|
break
|
|
end
|
|
i = j + 1
|
|
prev_i = i
|
|
end
|
|
end
|
|
|
|
local function parse_entry(opts, line, ret)
|
|
local s = split(line, ":", 4)
|
|
local fn, l, c, t = s[1], s[2], s[3], s[4]
|
|
|
|
t = trim(t)
|
|
local entry = { fn = fn, l = l, c = c }
|
|
|
|
if opts.tag_notation == "yaml-bare" then
|
|
yaml_to_tags(t, entry, ret)
|
|
elseif opts.tag_notation == ":tag:" then
|
|
insert_tag(ret, t, entry)
|
|
else
|
|
insert_tag(ret, t, entry)
|
|
end
|
|
end
|
|
|
|
M.do_find_all_tags = function(opts)
|
|
local cmd, args = command_find_all_tags(opts)
|
|
--print(cmd .. " " .. vim.inspect(args))
|
|
local ret = {}
|
|
local _ = Job
|
|
:new({
|
|
command = cmd,
|
|
args = args,
|
|
enable_recording = true,
|
|
on_exit = function(j, return_val)
|
|
if return_val == 0 then
|
|
for _, line in pairs(j:result()) do
|
|
parse_entry(opts, line, ret)
|
|
end
|
|
else
|
|
print("rg return value: " .. tostring(return_val))
|
|
print("stderr: ", vim.inspect(j:stderr_result()))
|
|
end
|
|
end,
|
|
on_stderr = function(err, data, _)
|
|
print("error: " .. tostring(err) .. "data: " .. data)
|
|
end,
|
|
})
|
|
:sync()
|
|
-- print("final results: " .. vim.inspect(ret))
|
|
return ret
|
|
end
|
|
return M
|