|
1 | 1 | local M = {}
|
2 |
| -local last_test = nil |
3 |
| -local vial_path = "vial" |
4 |
| -local test_cmd = "cargo test -- %s --nocapture --color=always" |
5 |
| -local toggle_state = { |
6 |
| - enabled = false, |
7 |
| - extensions = {}, |
8 |
| -} |
| 2 | +local vial_path = nil |
| 3 | +local file_types = nil |
| 4 | +local enabled = false |
9 | 5 |
|
10 | 6 | function M.setup(opts)
|
11 | 7 | opts = opts or {}
|
12 | 8 | vial_path = opts.vial_path or vial_path
|
13 |
| - test_cmd = opts.command or test_cmd |
14 |
| - toggle_state.extensions = opts.extensions or {} |
| 9 | + file_types = opts.file_types |
| 10 | + enabled = true |
| 11 | + |
| 12 | + vim.cmd([[autocmd BufWritePost * lua require'vial'.on_buf_write_post()]]) |
| 13 | +end |
| 14 | + |
| 15 | +-- Function to get the current context and run command |
| 16 | +local function run_command(command) |
| 17 | + local handle = io.popen(string.format("%s send '%s' > /dev/null 2>&1", vial_path, command)) |
| 18 | + |
| 19 | + -- we don't handle responses yet |
| 20 | + -- local result = handle:read("*a") |
| 21 | + -- handle:close() |
15 | 22 | end
|
16 | 23 |
|
17 |
| -local function current_test_name() |
18 |
| - -- Get the current buffer and cursor position |
19 |
| - local bufnr = vim.api.nvim_get_current_buf() |
20 |
| - ---@diagnostic disable-next-line: deprecated |
21 |
| - local row, _ = unpack(vim.api.nvim_win_get_cursor(0)) |
22 |
| - |
23 |
| - local function_declaration_line = 0 |
24 |
| - local function_name = "" |
25 |
| - |
26 |
| - -- Check lines above the cursor for the #[test] attribute and function definition |
27 |
| - for i = row, 1, -1 do |
28 |
| - local line = vim.api.nvim_buf_get_lines(bufnr, i - 1, i, false)[1] |
29 |
| - |
30 |
| - -- Check for function start and capture the function name |
31 |
| - if line:match("^%s*fn%s") then |
32 |
| - function_declaration_line = i |
33 |
| - function_name = line:match("^%s*fn%s([%w_]+)%s*%(") |
34 |
| - break |
35 |
| - end |
| 24 | +local function current_settings() |
| 25 | + if file_types == nil then |
| 26 | + print("No file types defined") |
| 27 | + return |
36 | 28 | end
|
37 | 29 |
|
38 |
| - for i = function_declaration_line, 1, -1 do |
39 |
| - local line = vim.api.nvim_buf_get_lines(bufnr, i - 1, i, false)[1] |
40 |
| - if line:match("^%s*#%[test%]") then |
41 |
| - return function_name |
42 |
| - end |
| 30 | + local filetype = vim.bo.filetype |
| 31 | + return file_types[filetype] |
| 32 | +end |
| 33 | + |
| 34 | +local function get(setting) |
| 35 | + local settings = current_settings() |
| 36 | + if settings == nil then |
| 37 | + return nil |
43 | 38 | end
|
44 | 39 |
|
45 |
| - return nil |
| 40 | + return settings[setting] |
46 | 41 | end
|
47 | 42 |
|
48 |
| --- Function to get the current context and run command |
49 |
| -local function run_command(command) |
50 |
| - -- local file = vim.api.nvim_buf_get_name(0) |
51 |
| - -- local line = vim.fn.line(".") |
52 |
| - -- local context_command = string.format("%s -file=%s -line=%s", command, file, line) |
| 43 | +local function set(setting, value) |
| 44 | + local settings = current_settings() |
| 45 | + if settings == nil then |
| 46 | + return |
| 47 | + end |
53 | 48 |
|
54 |
| - -- Execute the Rust binary and pass the command |
55 |
| - local handle = io.popen(string.format("%s send '%s' > /dev/null 2>&1", vial_path, command)) |
56 |
| - -- local result = handle:read("*a") |
57 |
| - -- handle:close() |
| 49 | + settings[setting] = value |
58 | 50 | end
|
59 | 51 |
|
60 | 52 | local function run_test(test_name)
|
61 |
| - local cmd = string.format(test_cmd, test_name) |
| 53 | + local raw_cmd = get("command") |
| 54 | + if raw_cmd == nil then |
| 55 | + print("No command defined") |
| 56 | + return |
| 57 | + end |
| 58 | + |
| 59 | + local cmd = string.format(raw_cmd, test_name) |
62 | 60 | run_command(cmd)
|
63 | 61 | end
|
64 | 62 |
|
65 | 63 | function M.run_test()
|
66 |
| - local test_name = current_test_name() |
| 64 | + local test_name = get("extract")() |
67 | 65 |
|
68 | 66 | if test_name == nil then
|
69 | 67 | print("No test found")
|
70 | 68 | return
|
71 | 69 | end
|
72 | 70 |
|
73 |
| - last_test = test_name |
| 71 | + set("last_test", test_name) |
74 | 72 | run_test(test_name)
|
75 | 73 | end
|
76 | 74 |
|
77 | 75 | function M.run_last_test()
|
| 76 | + local last_test = get("last_test") |
| 77 | + |
78 | 78 | if last_test == nil then
|
79 |
| - print("No last test found") |
80 | 79 | return
|
81 | 80 | end
|
82 | 81 |
|
83 | 82 | run_test(last_test)
|
84 | 83 | end
|
85 | 84 |
|
86 |
| -local function is_valid_extension(extension) |
87 |
| - for _, ext in ipairs(toggle_state.extensions) do |
88 |
| - if ext == extension then |
89 |
| - return true |
90 |
| - end |
91 |
| - end |
92 |
| - return false |
93 |
| -end |
94 |
| - |
95 |
| -local function get_extension(filename) |
96 |
| - return filename:match("^.+(%..+)$") |
97 |
| -end |
| 85 | +local function is_inside_test() |
| 86 | + local extract_fn = get("extract") |
98 | 87 |
|
99 |
| -local function on_buf_write_post() |
100 |
| - if not toggle_state.enabled then |
101 |
| - return |
| 88 | + if extract_fn == nil then |
| 89 | + return false |
102 | 90 | end
|
103 | 91 |
|
104 |
| - local filename = vim.api.nvim_buf_get_name(0) |
105 |
| - local extension = get_extension(filename) |
| 92 | + return get("extract")() ~= nil |
| 93 | +end |
106 | 94 |
|
107 |
| - if is_valid_extension(extension) then |
| 95 | +local function run_last_or_current_test() |
| 96 | + if is_inside_test() then |
108 | 97 | M.run_test()
|
| 98 | + else |
| 99 | + M.run_last_test() |
109 | 100 | end
|
110 | 101 | end
|
111 | 102 |
|
112 |
| -function M.toggle_automated_tests() |
113 |
| - toggle_state.enabled = not toggle_state.enabled |
114 |
| - if toggle_state.enabled then |
| 103 | +local function on_buf_write_post() |
| 104 | + run_last_or_current_test() |
| 105 | +end |
| 106 | + |
| 107 | +function M.toggle_active() |
| 108 | + if enabled then |
115 | 109 | vim.cmd([[autocmd BufWritePost * lua require'vial'.on_buf_write_post()]])
|
116 |
| - print("Automated tests enabled") |
| 110 | + enabled = false |
117 | 111 | else
|
118 |
| - vim.cmd([[autocmd! BufWritePost * lua require'vial'.on_buf_write_post()]]) |
119 |
| - print("Automated tests disabled") |
| 112 | + vim.cmd([[autocmd BufWritePost * lua require'vial'.on_buf_write_post()]]) |
| 113 | + enabled = true |
120 | 114 | end
|
121 | 115 | end
|
122 | 116 |
|
| 117 | +function M.clear_last_test() |
| 118 | + set("last_test", nil) |
| 119 | +end |
| 120 | + |
| 121 | +function M.show_last_test() |
| 122 | + local last_test = get("last_test") |
| 123 | + |
| 124 | + if last_test == nil then |
| 125 | + print("No last test found") |
| 126 | + return |
| 127 | + end |
| 128 | + |
| 129 | + print(last_test) |
| 130 | +end |
| 131 | + |
123 | 132 | vim.api.nvim_set_keymap("n", "<leader>tt", "<cmd>lua require'vial'.run_test()<cr>", { noremap = true, silent = true })
|
124 | 133 | vim.api.nvim_set_keymap(
|
125 | 134 | "n",
|
126 | 135 | "<leader>tl",
|
127 | 136 | "<cmd>lua require'vial'.run_last_test()<cr>",
|
128 | 137 | { noremap = true, silent = true }
|
129 | 138 | )
|
| 139 | +vim.api.nvim_set_keymap( |
| 140 | + "n", |
| 141 | + "<leader>tc", |
| 142 | + "<cmd>lua require'vial'.clear_last_test()<cr>", |
| 143 | + { noremap = true, silent = true } |
| 144 | +) |
130 | 145 | vim.api.nvim_set_keymap(
|
131 | 146 | "n",
|
132 | 147 | "<leader>ta",
|
133 |
| - "<cmd>lua require'vial'.toggle_automated_tests()<cr>", |
| 148 | + "<cmd>lua require'vial'.toggle_active()<cr>", |
| 149 | + { noremap = true, silent = true } |
| 150 | +) |
| 151 | +vim.api.nvim_set_keymap( |
| 152 | + "n", |
| 153 | + "<leader>ts", |
| 154 | + "<cmd>lua require'vial'.show_last_test()<cr>", |
134 | 155 | { noremap = true, silent = true }
|
135 | 156 | )
|
136 | 157 |
|
|
0 commit comments