Repository navigation
Expand file tree
/
Copy pathtodos.tl
More file actions
72 lines (66 loc) · 2.36 KB
/
Copy pathtodos.tl
File metadata and controls
72 lines (66 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
local argparse <const> = require("argparse")
local log <const> = require("cyan.log")
local fs <const> = require("cyan.fs")
local cs <const> = require("cyan.colorstring")
local util <const> = require("cyan.util")
local pad_left <const>, map <const> = util.str.pad_left, util.tab.map
local has_ltreesitter <const>, ts <const> = pcall(require, "ltreesitter")
if not has_ltreesitter then
log.warn("todos requires the ltreesitter module, which lua was unable to find\n", ts as string)
return
end
local has_teal_parser <const>, teal_parser <const> = pcall(ts.require, "teal")
if not has_teal_parser then
log.warn("todos requires tree-sitter-teal, which ltreesitter could not find:\n", teal_parser as string)
return
end
local parser <const> = argparse("todos", "detect todos in the source")
parser:flag("--raw", "Output in a raw format useful for piping into other commands")
local parsed <const> = parser:parse()
local raw <const> = parsed.raw as boolean
local query <const> = teal_parser:query[[
((comment) @c
(match? @c "TODO"))]]
local todo <const> = log.create_logger(
io.stderr,
"normal",
cs.highlight(cs.colors.teal, "Todo"),
cs.highlight(cs.colors.teal, "...")
)
local record Todo
line: integer
content: string
end
for path in fs.scan_dir(".", {"src/cyan/**/*"}) do
local real_path <const> = path:to_real_path()
local text <const> = fs.read(real_path)
local root <const> = teal_parser:parse_string(text):root()
local todos <const>: {Todo} = {}
for match in query:match(root) do
local node <const> = match.captures.c
table.insert(todos, {
line = node:start_point().row,
content = node:source():match("TODO%s*:?%s*(.*)"),
})
end
if #todos > 0 then
if raw then
for _, t in ipairs(todos) do
io.write(real_path, ":", tostring(t.line), ":", t.content, "\n")
end
else
todo(
cs.highlight(cs.colors.emphasis, #todos .. " todo comment" .. (#todos == 1 and "" or "s")), " in ",
cs.highlight(cs.colors.file, real_path),
"\n ",
table.concat(map(todos, function(t: Todo): string
return (
cs.highlight(cs.colors.number, pad_left(tostring(t.line), 4)) .. " | "
.. t.content
):tostring()
end), "\n "),
"\n"
)
end
end
end