Skip to content

Commit 818a305

Browse files
committed
feat: improve lua code
refer: #282
1 parent 1c3f089 commit 818a305

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

.stylua.toml

-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ indent_type = "Spaces"
33
indent_width = 2
44
quote_style = "AutoPreferDouble"
55
call_parentheses = "None"
6-
collapse_simple_statement = "Always"

lua/wiki/init.lua

+48-36
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,69 @@
11
local M = {}
22

3-
-- Returns shorten path
4-
local function filter(path)
5-
local root = vim.g.wiki_root .. "/"
6-
return path:gsub(root, "")
7-
end
8-
9-
-- Returns full path
10-
local function unfilter(path)
11-
local root = vim.g.wiki_root .. "/"
12-
return root .. path
13-
end
14-
153
function M.get_pages()
16-
local res = {}
17-
local pages = vim.fn["wiki#page#get_all"]()
18-
for _, p in pairs(pages) do
19-
local page = filter(p[1])
20-
table.insert(res, page)
21-
end
22-
vim.ui.select(res, { prompt = "WikiPages> " }, function(f)
23-
f = unfilter(f)
24-
vim.cmd("edit " .. f)
4+
-- wiki#page#get_all returns a list of path pairs, where the first element is
5+
-- the absolute path and the second element is the path relative to wiki
6+
-- root.
7+
vim.ui.select(vim.fn["wiki#page#get_all"](), {
8+
prompt = "WikiPages> ",
9+
format_item = function(item)
10+
return item[2]
11+
end,
12+
}, function(item)
13+
if item then
14+
vim.cmd.edit(item[1])
15+
end
2516
end)
2617
end
2718

2819
function M.get_tags()
29-
local res = {}
30-
local tags = vim.fn["wiki#tags#get_all"]()
31-
for key, val in pairs(tags) do
32-
for _, file in pairs(val) do
33-
local str = string.format("%s:%s:%s", key, file[2], filter(file[1]))
34-
table.insert(res, str)
20+
local tags_with_locations = vim.fn["wiki#tags#get_all"]()
21+
22+
local length = 0
23+
for tag, _ in pairs(tags_with_locations) do
24+
if #tag > length then
25+
length = #tag
3526
end
3627
end
37-
vim.ui.select(res, { prompt = "WikiTags> " }, function(t)
38-
t = unfilter(vim.split(t, ":")[3])
39-
vim.cmd("edit " .. t)
28+
local frmt = "%-" .. length .. "s %s:%s"
29+
30+
local root = vim.fn["wiki#get_root"]()
31+
local items = {}
32+
for tag, locations in pairs(tags_with_locations) do
33+
for _, loc in pairs(locations) do
34+
local path_rel = vim.fn["wiki#paths#relative"](loc[1], root)
35+
local str = string.format(frmt, tag, path_rel, loc[2])
36+
table.insert(items, { str, loc[1] })
37+
end
38+
end
39+
40+
vim.ui.select(items, {
41+
prompt = "WikiTags> ",
42+
format_item = function(item)
43+
return item[1]
44+
end,
45+
}, function(item)
46+
if item then
47+
vim.cmd.edit(item[2])
48+
end
4049
end)
41-
return res
4250
end
4351

4452
function M.toc()
45-
local res = {}
4653
local toc = vim.fn["wiki#toc#gather_entries"]()
54+
55+
local items = {}
4756
for _, hd in pairs(toc) do
4857
local indent = vim.fn["repeat"](".", hd.level - 1)
4958
local line = hd.lnum .. "|" .. indent .. hd.header
50-
table.insert(res, line)
59+
table.insert(items, line)
5160
end
52-
vim.ui.select(res, { prompt = "WikiToc> " }, function(t)
53-
t = vim.split(t, "|")[1]
54-
vim.cmd("execute " .. t)
61+
62+
vim.ui.select(items, { prompt = "WikiToc> " }, function(item)
63+
if item then
64+
item = vim.split(item, "|")[1]
65+
vim.cmd.execute(item)
66+
end
5567
end)
5668
end
5769

0 commit comments

Comments
 (0)