Logo

index : proclist

---

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/proclist.git/html/lua/proclist/init.lua blob: 77a26fd78dfa6e2770e25ebd47802d3726385d4e [raw] [clear marker]

        
0local su = require("proclist.string_util")
1
2local Proclist = {}
3local state = {}
4
5Proclist.prefix = "[Proclist]: "
6Proclist.check_file_extension = true
7Proclist.is_jai_file = false
8state.window_open = false
9
10
11function Proclist.show_matches()
12 local buf = vim.api.nvim_create_buf(false, true)
13
14 local display = {}
15
16 for _, match in ipairs(state.matches) do
17 table.insert(display, match.line_number .. ": " .. match.text)
18 end
19
20 vim.api.nvim_buf_set_lines(buf, 0, -1, false, display)
21
22 local width = math.floor(vim.o.columns * 0.8)
23 local height = math.min(#state.matches, math.floor(vim.o.lines * 0.8))
24
25 if width <= 0 or height <= 0 then
26 return
27 end
28
29 local col = math.floor((vim.o.columns - width) / 2)
30 local row = math.floor((vim.o.lines - height) / 2)
31
32 local win = vim.api.nvim_open_win(buf, true, {
33 relative = "editor",
34 width = width,
35 height = height,
36 row = row,
37 col = col,
38 style = "minimal",
39 border = "rounded",
40 })
41
42 vim.api.nvim_buf_set_option(buf, "modifiable", false)
43 vim.api.nvim_win_set_option(win, "cursorline", true)
44
45 local opts = { noremap = true, silent = true }
46
47 local function handle_close()
48 local line = vim.api.nvim_win_get_cursor(win)[1]
49 vim.api.nvim_win_close(win, true)
50 local target = state.matches[line]
51 if target then
52 vim.api.nvim_win_set_cursor(0, {target.line_number, 0})
53 end
54 Proclist.window_state_set(false)
55 end
56
57 vim.api.nvim_buf_set_keymap(buf, "n", "<Esc>", "", vim.tbl_extend("force", opts, {
58 callback = handle_close,
59 nowait = true,
60 }))
61
62 vim.api.nvim_buf_set_keymap(buf, "n", "q", "", vim.tbl_extend("force", opts, {
63 callback = handle_close,
64 nowait = true,
65 }))
66
67 vim.api.nvim_buf_set_keymap(buf, "n", "<CR>", "", vim.tbl_extend("force", opts, {
68 callback = handle_close,
69 nowait = true,
70 }))
71end
72
73function Proclist.window_state_set(b)
74 state.window_open = b
75end
76
77function Proclist.pattern(keyword)
78 Proclist.proc_pattern = "\\w\\s*::\\s*" .. keyword
79end
80
81function Proclist.proc()
82 Proclist.pattern("(")
83 Proclist.search()
84end
85
86function Proclist.struct()
87 Proclist.pattern("struct")
88 Proclist.search()
89end
90
91function Proclist.enum()
92 Proclist.pattern("enum")
93 Proclist.search()
94end
95
96function Proclist.search()
97 if Proclist.check_file_extension == true and Proclist.is_jai_file == false then
98 return
99 end
100
101 if state.window_open == true then
102 return
103 end
104
105 state.window_open = true
106
107 state.matches = {}
108 local buf = vim.api.nvim_get_current_buf()
109 local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
110
111 local i = 1
112 while i <= #lines do
113 local line = lines[i]
114 if vim.fn.match(line, Proclist.proc_pattern) >= 0 then
115 local start_line = i
116 local func_line = su.trim(line)
117 while not func_line:find("{") and i < #lines do
118 i = i + 1
119 func_line = func_line .. " " .. su.trim(lines[i])
120 end
121 local cleaned = su.trim(su.remove_braces(func_line))
122 table.insert(state.matches, { line_number = start_line, text = cleaned })
123 end
124 i = i + 1
125 end
126
127 if #state.matches == 0 then
128 print(Proclist.prefix .. "No matches found")
129 return
130 end
131
132 Proclist.show_matches()
133end
134
135function Proclist.setup(opts)
136 opts = opts or {}
137 local proc_key = opts.proc_key or "<leader>q"
138 local struct_key = opts.struct_key or "<leader>w"
139 local enum_key = opts.enum_key or "<leader>e"
140 Proclist.check_file_extension = opts.check_file_extension or true
141 vim.keymap.set("n", proc_key, Proclist.proc, { noremap = true, silent = true })
142 vim.keymap.set("n", struct_key, Proclist.struct, { noremap = true, silent = true })
143 vim.keymap.set("n", enum_key, Proclist.enum, { noremap = true, silent = true })
144end
145
146vim.api.nvim_create_autocmd("BufReadPost", {
147 pattern = "*.jai",
148 callback = function()
149 Proclist.is_jai_file = true
150 end,
151})
152
153return Proclist
154
155
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit