diff --git a/init.lua b/init.lua index c91d02e..a5b95b9 100644 --- a/init.lua +++ b/init.lua @@ -202,6 +202,7 @@ require("lazy").setup({ { "hrsh7th/vim-vsnip", event = "InsertEnter" }, { "hrsh7th/vim-vsnip-integ", event = "InsertEnter" }, { "rafamadriz/friendly-snippets", event = "InsertEnter" }, + { "zbirenbaum/copilot-cmp", config = true, event = "InsertEnter" }, { "nvim-treesitter/nvim-treesitter", event = { "BufReadPost", "BufNewFile" } }, { "nvim-treesitter/nvim-treesitter-refactor", event = "BufRead" }, { "yioneko/nvim-yati", event = "BufRead" }, @@ -224,6 +225,8 @@ require("lazy").setup({ { "nvim-zh/colorful-winsep.nvim", config = true, event = "WinNew" }, { "kevinhwang91/nvim-bqf", ft = "qf" }, { "vim-jp/vimdoc-ja", ft = "help" }, + { "zbirenbaum/copilot.lua", cmd = "Copilot" }, + { "CopilotC-Nvim/CopilotChat.nvim", build = "make tiktoken" }, --non-lazy { "vim-denops/denops.vim", lazy = false }, @@ -443,10 +446,10 @@ local function map(mode, lhs, rhs, opts) vim.api.nvim_set_keymap(mode, lhs, rhs, options) end -map("n", "6", ":lua require'dap'.continue()", { silent = true }) -map("n", "7", ":lua require'dap'.step_over()", { silent = true }) -map("n", "8", ":lua require'dap'.step_into()", { silent = true }) -map("n", "9", ":lua require'dap'.step_out()", { silent = true }) +map("n", "1", ":lua require'dap'.continue()", { silent = true }) +map("n", "2", ":lua require'dap'.step_over()", { silent = true }) +map("n", "3", ":lua require'dap'.step_into()", { silent = true }) +map("n", "4", ":lua require'dap'.step_out()", { silent = true }) map("n", ";", ":lua require'dap'.toggle_breakpoint()", { silent = true }) map("n", "'", ":lua require'dap'.set_breakpoint(vim.fn.input('Breakpoint condition: '))", { silent = true }) map( @@ -601,12 +604,14 @@ cmp.setup({ mode = "symbol", maxwidth = 50, ellipsis_char = "...", + symbol_map = { Copilot = "" }, }), }, sources = cmp.config.sources({ { name = "nvim_lsp", max_item_count = 15, keyword_length = 2 }, { name = "vsnip", max_item_count = 15, keyword_length = 2 }, + { name = "copilot", max_item_count = 15, keyword_length = 2 }, { name = "nvim_lsp_signature_help" }, { name = "buffer", max_item_count = 15, keyword_length = 2 }, }), @@ -633,6 +638,25 @@ cmp.setup.cmdline(":", { local capabilities = require("cmp_nvim_lsp").default_capabilities() vim.cmd("let g:vsnip_filetypes = {}") +local has_words_before = function() + if vim.api.nvim_buf_get_option(0, "buftype") == "prompt" then + return false + end + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 and vim.api.nvim_buf_get_text(0, line - 1, 0, line - 1, col, {})[1]:match("^%s*$") == nil +end +cmp.setup({ + mapping = { + [""] = vim.schedule_wrap(function(fallback) + if cmp.visible() and has_words_before() then + cmp.select_next_item({ behavior = cmp.SelectBehavior.Select }) + else + fallback() + end + end), + }, +}) + --nvim-treesitter require("nvim-treesitter.configs").setup({ highlight = { @@ -917,5 +941,68 @@ vim.keymap.set("n", "ga", ":Gin add .", { silent = true }) vim.keymap.set("n", "gc", ":Gin commit -m ") vim.keymap.set("n", "gp", ":Gin push") +--copilot +local select = require("CopilotChat.select") + +require("copilot").setup({ + suggestion = { enabled = false }, + panel = { enabled = false }, +}) + +require("CopilotChat").setup({ + debug = true, + + window = { + layout = "float", + relative = "editor", + }, + prompts = { + Explain = { + prompt = "/COPILOT_EXPLAIN 選択されたコードの説明を段落をつけて書いてください。", + }, + Review = { + prompt = "/COPILOT_REVIEW 選択されたコードをレビューしてください。", + callback = function(response, source) end, + }, + Fix = { + prompt = "/COPILOT_FIX このコードには問題があります。バグを修正したコードに書き直してください。", + }, + Optimize = { + prompt = "/COPILOT_REFACTOR 選択されたコードを最適化してパフォーマンスと可読性を向上させてください。", + }, + Docs = { + prompt = "/COPILOT_DOCS 選択されたコードに対してドキュメンテーションコメントを追加してください。", + }, + Tests = { + prompt = "/COPILOT_TESTS 選択されたコードの詳細な単体テスト関数を書いてください。", + }, + FixDiagnostic = { + prompt = "ファイル内の次のような診断上の問題を解決してください:", + selection = select.diagnostics, + }, + }, +}) + +function CopilotChatBuffer() + local input = vim.fn.input("Quick Chat: ") + if input ~= "" then + require("CopilotChat").ask(input, { selection = require("CopilotChat.select").buffer }) + end +end + +vim.api.nvim_set_keymap("n", "9", "lua CopilotChatBuffer()", { noremap = true, silent = true }) + +function ShowCopilotChatActionPrompt() + local actions = require("CopilotChat.actions") + require("CopilotChat.integrations.telescope").pick(actions.prompt_actions()) +end + +vim.api.nvim_set_keymap( + "n", + "0", + "lua ShowCopilotChatActionPrompt()", + { noremap = true, silent = true } +) + --OTHER SETTINGS vim.cmd("colorscheme edge")