summaryrefslogtreecommitdiff
path: root/gemfeed/examples/conf/dotfiles/nvim/init.lua
blob: c3b8701d6d5049f91b98f9ec654a341fcf57c2ae (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
59
60
61
62
63
64
65
66
67
68
69
70

require("CopilotChat").setup {
  -- See Configuration section for options
}

local timer = vim.loop.new_timer() -- Initialize the timer

vim.api.nvim_create_autocmd("BufEnter", {
    pattern = "*",
    callback = function()
        if vim.bo.filetype == "copilot-chat" then
            local copilot_chat_buf = vim.api.nvim_get_current_buf()
            vim.cmd("wincmd _") -- Maximize height
            vim.cmd("wincmd |") -- Maximize width
            local file_path = vim.fn.expand("~/.copilot_chat_output.txt")

            -- Start the timer with a 2-second interval
            timer:start(1000, 1000, vim.schedule_wrap(function()
                if copilot_chat_buf and vim.api.nvim_buf_is_valid(copilot_chat_buf) then
                    -- Get all lines in the buffer
                    local lines = vim.api.nvim_buf_get_lines(copilot_chat_buf, 0, -1, false)
                    
                    -- Check for the stopping condition
                    local user_line_count = 0
                    for _, line in ipairs(lines) do
                        if line:find("^## User") then
                            user_line_count = user_line_count + 1
                            if user_line_count >= 2 then
                                print("Stopping write process: Two '## User' lines detected.")
                                timer:stop()
                                -- Write the buffer content to the file
                                vim.api.nvim_buf_call(copilot_chat_buf, function()
                                    vim.cmd("write! " .. file_path)
                                end)
                                vim.cmd("qa!")
                                return
                            end
                        end
                    end
                    
                    -- Write the buffer content to the file
                    vim.api.nvim_buf_call(copilot_chat_buf, function()
                        vim.cmd("write! " .. file_path)
                    end)
                end
            end))
        end
    end,
})

vim.api.nvim_create_user_command('CopilotAsk', function(args)
    local chat = require("CopilotChat")
    local input
    if args.args and args.args ~= "" then
        input = args.args
    else
        local input_file = os.getenv("HOME") .. "/.copilot_chat_input.txt"
        local file = io.open(input_file, "r")
        if file then
            input = file:read("*all")
            file:close()
        else
            print("Error: Unable to open input file.")
            return
        end
    end
    chat.ask(input)
end, { force = true, range = true, nargs = "?" })