summaryrefslogtreecommitdiff
path: root/lua/aotrix/quickfix.lua
blob: f1f1008128705c9e016eb2ed0fed8834f091e25c (plain)
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
local quickfix_is_open = false;
vim.api.nvim_create_autocmd('BufWinEnter', {
    pattern = 'quickfix',
    callback = function(args) quickfix_is_open = true end
})
vim.api.nvim_create_autocmd('BufWinLeave', {
    pattern = 'quickfix',
    callback = function(args) quickfix_is_open = false end
})
local nb_elements_quickfix = 0;
vim.api.nvim_create_autocmd('DiagnosticChanged', {
    callback = function(args)
        nb_elements_quickfix = #vim.diagnostic.get()
        if quickfix_is_open then
            if nb_elements_quickfix == 0 then
                quickfix_is_open = false
                vim.defer_fn(function() vim.cmd.cclose() end, 0)
            else
                vim.diagnostic.setqflist()
            end
        end
    end
})
local current_quickfix_line = 1
vim.keymap.set("n", "<leader>q", function()
    if #vim.diagnostic.get() > 0 then
        if quickfix_is_open then
            vim.cmd.cclose()
            quickfix_is_open = false
        else
            vim.diagnostic.setqflist()
            vim.cmd.cfirst()
            current_quickfix_line = 1
        end
    end
end)
vim.keymap.set("n", "<M-j>", function()
    if #vim.diagnostic.get() > 0 and quickfix_is_open then
        if current_quickfix_line < nb_elements_quickfix then
            vim.cmd.cnext()
            current_quickfix_line = current_quickfix_line + 1
        else
            vim.cmd.cfirst()
            current_quickfix_line = 1
        end
    end
end)
vim.keymap.set("n", "<M-k>", function()
    if #vim.diagnostic.get() > 0 and quickfix_is_open then
        if current_quickfix_line > 1 then
            vim.cmd.cprev()
            current_quickfix_line = current_quickfix_line - 1
        else
            vim.cmd.clast()
            current_quickfix_line = nb_elements_quickfix
        end
    end
end)