<<
path:
root/public/proclist.git/html/lua/proclist/init.lua
blob: 77a26fd78dfa6e2770e25ebd47802d3726385d4e
[raw]
[clear marker]
0local su = require("proclist.string_util")
5Proclist.prefix = "[Proclist]: "
6Proclist.check_file_extension = true
7Proclist.is_jai_file = false
8state.window_open = false
11function Proclist.show_matches()
12 local buf = vim.api.nvim_create_buf(false, true)
16 for _, match in ipairs(state.matches) do
17 table.insert(display, match.line_number .. ": " .. match.text)
20 vim.api.nvim_buf_set_lines(buf, 0, -1, false, display)
22 local width = math.floor(vim.o.columns * 0.8)
23 local height = math.min(#state.matches, math.floor(vim.o.lines * 0.8))
25 if width <= 0 or height <= 0 then
29 local col = math.floor((vim.o.columns - width) / 2)
30 local row = math.floor((vim.o.lines - height) / 2)
32 local win = vim.api.nvim_open_win(buf, true, {
42 vim.api.nvim_buf_set_option(buf, "modifiable", false)
43 vim.api.nvim_win_set_option(win, "cursorline", true)
45 local opts = { noremap = true, silent = true }
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]
52 vim.api.nvim_win_set_cursor(0, {target.line_number, 0})
54 Proclist.window_state_set(false)
57 vim.api.nvim_buf_set_keymap(buf, "n", "<Esc>", "", vim.tbl_extend("force", opts, {
58 callback = handle_close,
62 vim.api.nvim_buf_set_keymap(buf, "n", "q", "", vim.tbl_extend("force", opts, {
63 callback = handle_close,
67 vim.api.nvim_buf_set_keymap(buf, "n", "<CR>", "", vim.tbl_extend("force", opts, {
68 callback = handle_close,
73function Proclist.window_state_set(b)
77function Proclist.pattern(keyword)
78 Proclist.proc_pattern = "\\w\\s*::\\s*" .. keyword
81function Proclist.proc()
86function Proclist.struct()
87 Proclist.pattern("struct")
91function Proclist.enum()
92 Proclist.pattern("enum")
96function Proclist.search()
97 if Proclist.check_file_extension == true and Proclist.is_jai_file == false then
101 if state.window_open == true then
105 state.window_open = true
108 local buf = vim.api.nvim_get_current_buf()
109 local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
113 local line = lines[i]
114 if vim.fn.match(line, Proclist.proc_pattern) >= 0 then
116 local func_line = su.trim(line)
117 while not func_line:find("{") and i < #lines do
119 func_line = func_line .. " " .. su.trim(lines[i])
121 local cleaned = su.trim(su.remove_braces(func_line))
122 table.insert(state.matches, { line_number = start_line, text = cleaned })
127 if #state.matches == 0 then
128 print(Proclist.prefix .. "No matches found")
132 Proclist.show_matches()
135function Proclist.setup(opts)
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 })
146vim.api.nvim_create_autocmd("BufReadPost", {
148 callback = function()
149 Proclist.is_jai_file = true