From 76f1f2fcc1e04cf72fb0aa6d7b40f5efba9a8581 Mon Sep 17 00:00:00 2001 From: Fergus Molloy Date: Fri, 20 Jun 2025 11:49:38 +0100 Subject: [PATCH] update fold method plus fix golangci lint causing error also add keybinds to codecompanion --- lua/options.lua | 4 + lua/plugins/ai/avante.lua | 49 ++ lua/plugins/ai/codecompanion.lua | 12 +- lua/plugins/ai/config.lua | 1122 ++++++++++++++++++++++++++ lua/plugins/ai/init.lua | 1 + lua/plugins/lsp/golangci_lint_ls.lua | 28 +- 6 files changed, 1199 insertions(+), 17 deletions(-) create mode 100644 lua/plugins/ai/avante.lua create mode 100644 lua/plugins/ai/config.lua diff --git a/lua/options.lua b/lua/options.lua index 1e6525b..c0999b3 100644 --- a/lua/options.lua +++ b/lua/options.lua @@ -65,3 +65,7 @@ vim.o.termguicolors = true -- enable spell checking vim.o.spell = true vim.o.spelllang = 'en_gb' + +vim.o.foldmethod = 'expr' +vim.o.foldexpr = 'nvim_treesitter#foldexpr()' +vim.o.foldenable = false diff --git a/lua/plugins/ai/avante.lua b/lua/plugins/ai/avante.lua new file mode 100644 index 0000000..557bae6 --- /dev/null +++ b/lua/plugins/ai/avante.lua @@ -0,0 +1,49 @@ +return { + { + 'yetone/avante.nvim', + build = 'make', + event = 'VeryLazy', + version = false, -- Never set this value to "*"! Never! + ---@module 'avante' + ---@type avante.Config + opts = { + -- add any opts here + -- for example + provider = 'ollama', + providers = { + ollama = { + model = 'deepseek-r1:8b', + timeout = 30000, -- Timeout in milliseconds + extra_request_body = { + temperature = 0.5, + max_tokens = 20480, + }, + }, + }, + selector = { + --- @alias avante.SelectorProvider "native" | "fzf_lua" | "mini_pick" | "snacks" | "telescope" | fun(selector: avante.ui.Selector): nil + --- @type avante.SelectorProvider + provider = 'fzf', + -- Options override for custom providers + provider_opts = {}, + }, + }, + dependencies = { + 'nvim-treesitter/nvim-treesitter', + 'nvim-lua/plenary.nvim', + 'MunifTanjim/nui.nvim', + --- The below dependencies are optional, + 'hrsh7th/nvim-cmp', -- autocompletion for avante commands and mentions + 'ibhagwan/fzf-lua', -- for file_selector provider fzf + 'nvim-tree/nvim-web-devicons', -- or echasnovski/mini.icons + { + -- Make sure to set this up properly if you have lazy=true + 'MeanderingProgrammer/render-markdown.nvim', + opts = { + file_types = { 'markdown', 'Avante' }, + }, + ft = { 'markdown', 'Avante' }, + }, + }, + }, +} diff --git a/lua/plugins/ai/codecompanion.lua b/lua/plugins/ai/codecompanion.lua index 7c4c30e..7b2c28f 100644 --- a/lua/plugins/ai/codecompanion.lua +++ b/lua/plugins/ai/codecompanion.lua @@ -5,17 +5,21 @@ return { { 'nvim-treesitter/nvim-treesitter', build = ':TSUpdate' }, { 'nvim-lua/plenary.nvim' }, }, + keys = { + { 'cc', 'CodeCompanionChat Toggle', desc = 'Open AI chat' }, + { 'cw', 'CodeCompanionAction', desc = 'Start new cody chat' }, + }, opts = { adapters = { ollama = function() return require('codecompanion.adapters').extend('ollama', { schema = { model = { - default = 'hf.co/Qwen/Qwen2.5-Coder-7B-Instruct-GGUF:latest', - }, - num_ctx = { - default = 4096, + default = 'deepseek-r1', }, + -- num_ctx = { + -- default = 4096, + -- }, }, }) end, diff --git a/lua/plugins/ai/config.lua b/lua/plugins/ai/config.lua new file mode 100644 index 0000000..4eaa046 --- /dev/null +++ b/lua/plugins/ai/config.lua @@ -0,0 +1,1122 @@ +local defaults = { + adapters = { + -- LLMs ------------------------------------------------------------------- + anthropic = 'anthropic', + azure_openai = 'azure_openai', + copilot = 'copilot', + deepseek = 'deepseek', + gemini = 'gemini', + githubmodels = 'githubmodels', + huggingface = 'huggingface', + novita = 'novita', + mistral = 'mistral', + ollama = 'ollama', + openai = 'openai', + xai = 'xai', + -- Non LLMs + jina = 'jina', + tavily = 'tavily', + -- OPTIONS ---------------------------------------------------------------- + opts = { + allow_insecure = false, -- Allow insecure connections? + cache_models_for = 1800, -- Cache adapter models for this long (seconds) + proxy = nil, -- [protocol://]host[:port] e.g. socks5://127.0.0.1:9999 + show_defaults = true, -- Show default adapters + show_model_choices = true, -- Show model choices when changing adapter + }, + }, + constants = constants, + strategies = { + -- CHAT STRATEGY ---------------------------------------------------------- + chat = { + adapter = 'copilot', + roles = { + ---The header name for the LLM's messages + ---@type string|fun(adapter: CodeCompanion.Adapter): string + llm = function(adapter) + return 'CodeCompanion (' .. adapter.formatted_name .. ')' + end, + + ---The header name for your messages + ---@type string + user = 'Me', + }, + tools = { + groups = { + ['full_stack_dev'] = { + description = 'Full Stack Developer - Can run code, edit code and modify files', + tools = { + 'cmd_runner', + 'create_file', + 'file_search', + 'grep_search', + 'insert_edit_into_file', + 'read_file', + 'web_search', + }, + opts = { + collapse_tools = true, + }, + }, + ['files'] = { + description = 'Tools related to creating, reading and editing files', + tools = { + 'create_file', + 'file_search', + 'insert_edit_into_file', + 'read_file', + }, + opts = { + collapse_tools = true, + }, + }, + }, + ['cmd_runner'] = { + callback = 'strategies.chat.agents.tools.cmd_runner', + description = 'Run shell commands initiated by the LLM', + opts = { + requires_approval = true, + }, + }, + ['create_file'] = { + callback = 'strategies.chat.agents.tools.create_file', + description = 'Create a file in the current working directory', + opts = { + requires_approval = true, + }, + }, + ['file_search'] = { + callback = 'strategies.chat.agents.tools.file_search', + description = 'Search for files in the current working directory by glob pattern', + opts = { + max_results = 500, + }, + }, + ['grep_search'] = { + callback = 'strategies.chat.agents.tools.grep_search', + enabled = function() + -- Currently this tool only supports ripgrep + return vim.fn.executable 'rg' == 1 + end, + description = 'Search for text in the current working directory', + opts = { + max_results = 100, + respect_gitignore = true, + }, + }, + ['insert_edit_into_file'] = { + callback = 'strategies.chat.agents.tools.insert_edit_into_file', + description = 'Insert code into an existing file', + opts = { + requires_approval = { -- Require approval before the tool is executed? + buffer = false, -- For editing buffers in Neovim + file = true, -- For editing files in the current working directory + }, + user_confirmation = true, -- Require confirmation from the user before moving on in the chat buffer? + }, + }, + ['read_file'] = { + callback = 'strategies.chat.agents.tools.read_file', + description = 'Read a file in the current working directory', + }, + ['web_search'] = { + callback = 'strategies.chat.agents.tools.web_search', + description = 'Search the web for information', + opts = { + adapter = 'tavily', -- tavily + opts = { + search_depth = 'advanced', + topic = 'general', + chunks_per_source = 3, + max_results = 5, + }, + }, + }, + ['next_edit_suggestion'] = { + callback = 'strategies.chat.agents.tools.next_edit_suggestion', + description = 'Suggest and jump to the next position to edit', + }, + opts = { + auto_submit_errors = false, -- Send any errors to the LLM automatically? + auto_submit_success = true, -- Send any successful output to the LLM automatically? + wait_timeout = 30000, -- How long to wait for user input before timing out (milliseconds) + ---Tools and/or groups that are always loaded in a chat buffer + ---@type string[] + default_tools = {}, + }, + }, + variables = { + ['buffer'] = { + callback = 'strategies.chat.variables.buffer', + description = 'Share the current buffer with the LLM', + opts = { + contains_code = true, + default_params = 'watch', -- watch|pin + has_params = true, + }, + }, + ['lsp'] = { + callback = 'strategies.chat.variables.lsp', + description = 'Share LSP information and code for the current buffer', + opts = { + contains_code = true, + }, + }, + ['viewport'] = { + callback = 'strategies.chat.variables.viewport', + description = 'Share the code that you see in Neovim with the LLM', + opts = { + contains_code = true, + }, + }, + }, + slash_commands = { + ['buffer'] = { + callback = 'strategies.chat.slash_commands.buffer', + description = 'Insert open buffers', + opts = { + contains_code = true, + default_params = 'watch', -- watch|pin + provider = providers.pickers, -- telescope|fzf_lua|mini_pick|snacks|default + }, + }, + ['fetch'] = { + callback = 'strategies.chat.slash_commands.fetch', + description = 'Insert URL contents', + opts = { + adapter = 'jina', -- jina + cache_path = vim.fn.stdpath 'data' .. '/codecompanion/urls', + provider = providers.pickers, -- telescope|fzf_lua|mini_pick|snacks|default + }, + }, + ['quickfix'] = { + callback = 'strategies.chat.slash_commands.quickfix', + description = 'Insert quickfix list entries', + opts = { + contains_code = true, + }, + }, + ['file'] = { + callback = 'strategies.chat.slash_commands.file', + description = 'Insert a file', + opts = { + contains_code = true, + max_lines = 1000, + provider = providers.pickers, -- telescope|fzf_lua|mini_pick|snacks|default + }, + }, + ['help'] = { + callback = 'strategies.chat.slash_commands.help', + description = 'Insert content from help tags', + opts = { + contains_code = false, + max_lines = 128, -- Maximum amount of lines to of the help file to send (NOTE: Each vimdoc line is typically 10 tokens) + provider = providers.help, -- telescope|fzf_lua|mini_pick|snacks + }, + }, + ['image'] = { + callback = 'strategies.chat.slash_commands.image', + description = 'Insert an image', + opts = { + dirs = {}, -- Directories to search for images + filetypes = { 'png', 'jpg', 'jpeg', 'gif', 'webp' }, -- Filetypes to search for + provider = providers.images, -- telescope|snacks|default + }, + }, + ['now'] = { + callback = 'strategies.chat.slash_commands.now', + description = 'Insert the current date and time', + opts = { + contains_code = false, + }, + }, + ['symbols'] = { + callback = 'strategies.chat.slash_commands.symbols', + description = 'Insert symbols for a selected file', + opts = { + contains_code = true, + provider = providers.pickers, -- telescope|fzf_lua|mini_pick|snacks|default + }, + }, + ['terminal'] = { + callback = 'strategies.chat.slash_commands.terminal', + description = 'Insert terminal output', + opts = { + contains_code = false, + }, + }, + ['workspace'] = { + callback = 'strategies.chat.slash_commands.workspace', + description = 'Load a workspace file', + opts = { + contains_code = true, + }, + }, + }, + keymaps = { + options = { + modes = { + n = '?', + }, + callback = 'keymaps.options', + description = 'Options', + hide = true, + }, + completion = { + modes = { + i = '', + }, + index = 1, + callback = 'keymaps.completion', + description = 'Completion Menu', + }, + send = { + modes = { + n = { '', '' }, + i = '', + }, + index = 2, + callback = 'keymaps.send', + description = 'Send', + }, + regenerate = { + modes = { + n = 'gr', + }, + index = 3, + callback = 'keymaps.regenerate', + description = 'Regenerate the last response', + }, + close = { + modes = { + n = '', + i = '', + }, + index = 4, + callback = 'keymaps.close', + description = 'Close Chat', + }, + stop = { + modes = { + n = 'q', + }, + index = 5, + callback = 'keymaps.stop', + description = 'Stop Request', + }, + clear = { + modes = { + n = 'gx', + }, + index = 6, + callback = 'keymaps.clear', + description = 'Clear Chat', + }, + codeblock = { + modes = { + n = 'gc', + }, + index = 7, + callback = 'keymaps.codeblock', + description = 'Insert Codeblock', + }, + yank_code = { + modes = { + n = 'gy', + }, + index = 8, + callback = 'keymaps.yank_code', + description = 'Yank Code', + }, + pin = { + modes = { + n = 'gp', + }, + index = 9, + callback = 'keymaps.pin_reference', + description = 'Pin Reference', + }, + watch = { + modes = { + n = 'gw', + }, + index = 10, + callback = 'keymaps.toggle_watch', + description = 'Watch Buffer', + }, + next_chat = { + modes = { + n = '}', + }, + index = 11, + callback = 'keymaps.next_chat', + description = 'Next Chat', + }, + previous_chat = { + modes = { + n = '{', + }, + index = 12, + callback = 'keymaps.previous_chat', + description = 'Previous Chat', + }, + next_header = { + modes = { + n = ']]', + }, + index = 13, + callback = 'keymaps.next_header', + description = 'Next Header', + }, + previous_header = { + modes = { + n = '[[', + }, + index = 14, + callback = 'keymaps.previous_header', + description = 'Previous Header', + }, + change_adapter = { + modes = { + n = 'ga', + }, + index = 15, + callback = 'keymaps.change_adapter', + description = 'Change adapter', + }, + fold_code = { + modes = { + n = 'gf', + }, + index = 15, + callback = 'keymaps.fold_code', + description = 'Fold code', + }, + debug = { + modes = { + n = 'gd', + }, + index = 16, + callback = 'keymaps.debug', + description = 'View debug info', + }, + system_prompt = { + modes = { + n = 'gs', + }, + index = 17, + callback = 'keymaps.toggle_system_prompt', + description = 'Toggle the system prompt', + }, + auto_tool_mode = { + modes = { + n = 'gta', + }, + index = 18, + callback = 'keymaps.auto_tool_mode', + description = 'Toggle automatic tool mode', + }, + goto_file_under_cursor = { + modes = { n = 'gR' }, + index = 19, + callback = 'keymaps.goto_file_under_cursor', + description = 'Open the file under cursor in a new tab.', + }, + }, + opts = { + blank_prompt = '', -- The prompt to use when the user doesn't provide a prompt + completion_provider = providers.completion, -- blink|cmp|coc|default + register = '+', -- The register to use for yanking code + yank_jump_delay_ms = 400, -- Delay in milliseconds before jumping back from the yanked code + ---@type string|fun(path: string) + goto_file_action = ui_utils.tabnew_reuse, + }, + }, + -- INLINE STRATEGY -------------------------------------------------------- + inline = { + adapter = 'copilot', + keymaps = { + accept_change = { + modes = { + n = 'ga', + }, + index = 1, + callback = 'keymaps.accept_change', + description = 'Accept change', + }, + reject_change = { + modes = { + n = 'gr', + }, + index = 2, + callback = 'keymaps.reject_change', + description = 'Reject change', + }, + }, + variables = { + ['buffer'] = { + callback = 'strategies.inline.variables.buffer', + description = 'Share the current buffer with the LLM', + opts = { + contains_code = true, + }, + }, + ['chat'] = { + callback = 'strategies.inline.variables.chat', + description = 'Share the currently open chat buffer with the LLM', + opts = { + contains_code = true, + }, + }, + ['clipboard'] = { + callback = 'strategies.inline.variables.clipboard', + description = 'Share the contents of the clipboard with the LLM', + opts = { + contains_code = true, + }, + }, + }, + }, + -- CMD STRATEGY ----------------------------------------------------------- + cmd = { + adapter = 'copilot', + opts = { + system_prompt = [[You are currently plugged in to the Neovim text editor on a user's machine. Your core task is to generate an command-line inputs that the user can run within Neovim. Below are some rules to adhere to: + +- Return plain text only +- Do not wrap your response in a markdown block or backticks +- Do not use any line breaks or newlines in you response +- Do not provide any explanations +- Generate an command that is valid and can be run in Neovim +- Ensure the command is relevant to the user's request]], + }, + }, + }, + -- PROMPT LIBRARIES --------------------------------------------------------- + prompt_library = { + ['Custom Prompt'] = { + strategy = 'inline', + description = 'Prompt the LLM from Neovim', + opts = { + index = 3, + is_default = true, + is_slash_cmd = false, + user_prompt = true, + }, + prompts = { + { + role = constants.SYSTEM_ROLE, + content = function(context) + return fmt( + [[I want you to act as a senior %s developer. I will ask you specific questions and I want you to return raw code only (no codeblocks and no explanations). If you can't respond with code, respond with nothing]], + context.filetype + ) + end, + opts = { + visible = false, + tag = 'system_tag', + }, + }, + }, + }, + ['Code workflow'] = { + strategy = 'workflow', + description = 'Use a workflow to guide an LLM in writing code', + opts = { + index = 4, + is_default = true, + short_name = 'cw', + }, + prompts = { + { + -- We can group prompts together to make a workflow + -- This is the first prompt in the workflow + { + role = constants.SYSTEM_ROLE, + content = function(context) + return fmt( + "You carefully provide accurate, factual, thoughtful, nuanced answers, and are brilliant at reasoning. If you think there might not be a correct answer, you say so. Always spend a few sentences explaining background context, assumptions, and step-by-step thinking BEFORE you try to answer a question. Don't be verbose in your answers, but do provide details and examples where it might help the explanation. You are an expert software engineer for the %s language", + context.filetype + ) + end, + opts = { + visible = false, + }, + }, + { + role = constants.USER_ROLE, + content = 'I want you to ', + opts = { + auto_submit = false, + }, + }, + }, + -- This is the second group of prompts + { + { + role = constants.USER_ROLE, + content = "Great. Now let's consider your code. I'd like you to check it carefully for correctness, style, and efficiency, and give constructive criticism for how to improve it.", + opts = { + auto_submit = true, + }, + }, + }, + -- This is the final group of prompts + { + { + role = constants.USER_ROLE, + content = "Thanks. Now let's revise the code based on the feedback, without additional explanations.", + opts = { + auto_submit = true, + }, + }, + }, + }, + }, + ['Edit<->Test workflow'] = { + strategy = 'workflow', + description = 'Use a workflow to repeatedly edit then test code', + opts = { + index = 5, + is_default = true, + short_name = 'et', + }, + prompts = { + { + { + name = 'Setup Test', + role = constants.USER_ROLE, + opts = { auto_submit = false }, + content = function() + -- Enable turbo mode!!! + vim.g.codecompanion_auto_tool_mode = true + + return [[### Instructions + +Your instructions here + +### Steps to Follow + +You are required to write code following the instructions provided above and test the correctness by running the designated test suite. Follow these steps exactly: + +1. Update the code in #buffer using the @insert_edit_into_file tool +2. Then use the @cmd_runner tool to run the test suite with `` (do this after you have updated the code) +3. Make sure you trigger both tools in the same response + +We'll repeat this cycle until the tests pass. Ensure no deviations from these steps.]] + end, + }, + }, + { + { + name = 'Repeat On Failure', + role = constants.USER_ROLE, + opts = { auto_submit = true }, + -- Scope this prompt to the cmd_runner tool + condition = function() + return _G.codecompanion_current_tool == 'cmd_runner' + end, + -- Repeat until the tests pass, as indicated by the testing flag + -- which the cmd_runner tool sets on the chat buffer + repeat_until = function(chat) + return chat.tools.flags.testing == true + end, + content = 'The tests have failed. Can you edit the buffer and run the test suite again?', + }, + }, + }, + }, + ['Explain'] = { + strategy = 'chat', + description = 'Explain how code in a buffer works', + opts = { + index = 6, + is_default = true, + is_slash_cmd = false, + modes = { 'v' }, + short_name = 'explain', + auto_submit = true, + user_prompt = false, + stop_context_insertion = true, + }, + prompts = { + { + role = constants.SYSTEM_ROLE, + content = [[When asked to explain code, follow these steps: + +1. Identify the programming language. +2. Describe the purpose of the code and reference core concepts from the programming language. +3. Explain each function or significant block of code, including parameters and return values. +4. Highlight any specific functions or methods used and their roles. +5. Provide context on how the code fits into a larger application if applicable.]], + opts = { + visible = false, + }, + }, + { + role = constants.USER_ROLE, + content = function(context) + local code = require('codecompanion.helpers.actions').get_code(context.start_line, context.end_line) + + return fmt( + [[Please explain this code from buffer %d: + +```%s +%s +``` +]], + context.bufnr, + context.filetype, + code + ) + end, + opts = { + contains_code = true, + }, + }, + }, + }, + ['Unit Tests'] = { + strategy = 'inline', + description = 'Generate unit tests for the selected code', + opts = { + index = 7, + is_default = true, + is_slash_cmd = false, + modes = { 'v' }, + short_name = 'tests', + auto_submit = true, + user_prompt = false, + placement = 'new', + stop_context_insertion = true, + }, + prompts = { + { + role = constants.SYSTEM_ROLE, + content = [[When generating unit tests, follow these steps: + +1. Identify the programming language. +2. Identify the purpose of the function or module to be tested. +3. List the edge cases and typical use cases that should be covered in the tests and share the plan with the user. +4. Generate unit tests using an appropriate testing framework for the identified programming language. +5. Ensure the tests cover: + - Normal cases + - Edge cases + - Error handling (if applicable) +6. Provide the generated unit tests in a clear and organized manner without additional explanations or chat.]], + opts = { + visible = false, + }, + }, + { + role = constants.USER_ROLE, + content = function(context) + local code = require('codecompanion.helpers.actions').get_code(context.start_line, context.end_line) + + return fmt( + [[ +Please generate unit tests for this code from buffer %d: + +```%s +%s +``` + +]], + context.bufnr, + context.filetype, + code + ) + end, + opts = { + contains_code = true, + }, + }, + }, + }, + ['Fix code'] = { + strategy = 'chat', + description = 'Fix the selected code', + opts = { + index = 8, + is_default = true, + is_slash_cmd = false, + modes = { 'v' }, + short_name = 'fix', + auto_submit = true, + user_prompt = false, + stop_context_insertion = true, + }, + prompts = { + { + role = constants.SYSTEM_ROLE, + content = [[When asked to fix code, follow these steps: + +1. **Identify the Issues**: Carefully read the provided code and identify any potential issues or improvements. +2. **Plan the Fix**: Describe the plan for fixing the code in pseudocode, detailing each step. +3. **Implement the Fix**: Write the corrected code in a single code block. +4. **Explain the Fix**: Briefly explain what changes were made and why. + +Ensure the fixed code: + +- Includes necessary imports. +- Handles potential errors. +- Follows best practices for readability and maintainability. +- Is formatted correctly. + +Use Markdown formatting and include the programming language name at the start of the code block.]], + opts = { + visible = false, + }, + }, + { + role = constants.USER_ROLE, + content = function(context) + local code = require('codecompanion.helpers.actions').get_code(context.start_line, context.end_line) + + return fmt( + [[Please fix this code from buffer %d: + +```%s +%s +``` +]], + context.bufnr, + context.filetype, + code + ) + end, + opts = { + contains_code = true, + }, + }, + }, + }, + ['Explain LSP Diagnostics'] = { + strategy = 'chat', + description = 'Explain the LSP diagnostics for the selected code', + opts = { + index = 9, + is_default = true, + is_slash_cmd = false, + modes = { 'v' }, + short_name = 'lsp', + auto_submit = true, + user_prompt = false, + stop_context_insertion = true, + }, + prompts = { + { + role = constants.SYSTEM_ROLE, + content = [[You are an expert coder and helpful assistant who can help debug code diagnostics, such as warning and error messages. When appropriate, give solutions with code snippets as fenced codeblocks with a language identifier to enable syntax highlighting.]], + opts = { + visible = false, + }, + }, + { + role = constants.USER_ROLE, + content = function(context) + local diagnostics = require('codecompanion.helpers.actions').get_diagnostics(context.start_line, context.end_line, context.bufnr) + + local concatenated_diagnostics = '' + for i, diagnostic in ipairs(diagnostics) do + concatenated_diagnostics = concatenated_diagnostics + .. i + .. '. Issue ' + .. i + .. '\n - Location: Line ' + .. diagnostic.line_number + .. '\n - Buffer: ' + .. context.bufnr + .. '\n - Severity: ' + .. diagnostic.severity + .. '\n - Message: ' + .. diagnostic.message + .. '\n' + end + + return fmt( + [[The programming language is %s. This is a list of the diagnostic messages: + +%s +]], + context.filetype, + concatenated_diagnostics + ) + end, + }, + { + role = constants.USER_ROLE, + content = function(context) + local code = require('codecompanion.helpers.actions').get_code(context.start_line, context.end_line, { show_line_numbers = true }) + return fmt( + [[ +This is the code, for context: + +```%s +%s +``` +]], + context.filetype, + code + ) + end, + opts = { + contains_code = true, + }, + }, + }, + }, + ['Generate a Commit Message'] = { + strategy = 'chat', + description = 'Generate a commit message', + opts = { + index = 10, + is_default = true, + is_slash_cmd = true, + short_name = 'commit', + auto_submit = true, + }, + prompts = { + { + role = constants.USER_ROLE, + content = function() + return fmt( + [[You are an expert at following the Conventional Commit specification. Given the git diff listed below, please generate a commit message for me: + +```diff +%s +``` +]], + vim.fn.system 'git diff --no-ext-diff --staged' + ) + end, + opts = { + contains_code = true, + }, + }, + }, + }, + ['Workspace File'] = { + strategy = 'chat', + description = 'Generate a Workspace file/group', + opts = { + index = 11, + ignore_system_prompt = true, + is_default = true, + short_name = 'workspace', + }, + references = { + { + type = 'file', + path = { + vim.fs.joinpath(vim.fn.getcwd(), 'codecompanion-workspace.json'), + }, + }, + }, + prompts = { + { + role = constants.SYSTEM_ROLE, + opts = { visible = false }, + content = function() + local schema = require('codecompanion').workspace_schema() + return fmt( + [[## CONTEXT + +A workspace is a JSON configuration file that organizes your codebase into related groups to help LLMs understand your project structure. Each group contains files, symbols, or URLs that provide context about specific functionality or features. + +The workspace file follows this structure: + +```json +%s +``` + +## OBJECTIVE + +Create or modify a workspace file that effectively organizes the user's codebase to provide optimal context for LLM interactions. + +## RESPONSE + +You must create or modify a workspace file through a series of prompts over multiple turns: + +1. First, ask the user about the project's overall purpose and structure if not already known +2. Then ask the user to identify key functional groups in your codebase +3. For each group, ask the user select relevant files, symbols, or URLs to include. Or, use your own knowledge to identify them +4. Generate the workspace JSON structure based on the input +5. Review and refine the workspace configuration together with the user]], + schema + ) + end, + }, + { + role = constants.USER_ROLE, + content = function() + local prompt = '' + if vim.fn.filereadable(vim.fs.joinpath(vim.fn.getcwd(), 'codecompanion-workspace.json')) == 1 then + prompt = [[Can you help me add a group to an existing workspace file?]] + else + prompt = [[Can you help me create a workspace file?]] + end + + local ok, _ = pcall(require, 'vectorcode') + if ok then + prompt = prompt .. ' Use the @vectorcode tool to help identify groupings of files' + end + return prompt + end, + }, + }, + }, + }, + -- DISPLAY OPTIONS ---------------------------------------------------------- + display = { + action_palette = { + width = 95, + height = 10, + prompt = 'Prompt ', -- Prompt used for interactive LLM calls + provider = providers.action_palette, -- telescope|mini_pick|snacks|default + opts = { + show_default_actions = true, -- Show the default actions in the action palette? + show_default_prompt_library = true, -- Show the default prompt library in the action palette? + }, + }, + chat = { + icons = { + pinned_buffer = ' ', + watched_buffer = '󰂥 ', + }, + debug_window = { + ---@return number|fun(): number + width = vim.o.columns - 5, + ---@return number|fun(): number + height = vim.o.lines - 2, + }, + window = { + layout = 'vertical', -- float|vertical|horizontal|buffer + position = nil, -- left|right|top|bottom (nil will default depending on vim.opt.splitright|vim.opt.splitbelow) + border = 'single', + height = 0.8, + ---@type number|"auto" using "auto" will allow full_height buffers to act like normal buffers + width = 0.45, + relative = 'editor', + full_height = true, + opts = { + breakindent = true, + cursorcolumn = false, + cursorline = false, + foldcolumn = '0', + linebreak = true, + list = false, + numberwidth = 1, + signcolumn = 'no', + spell = false, + wrap = true, + }, + }, + auto_scroll = true, -- Automatically scroll down and place the cursor at the end + intro_message = 'Welcome to CodeCompanion ✨! Press ? for options', + + show_header_separator = false, -- Show header separators in the chat buffer? Set this to false if you're using an external markdown formatting plugin + separator = '─', -- The separator between the different messages in the chat buffer + + show_references = true, -- Show references (from slash commands and variables) in the chat buffer? + show_settings = false, -- Show LLM settings at the top of the chat buffer? + show_tools_processing = true, -- Show the loading message when tools are being executed? + show_token_count = true, -- Show the token count for each response? + start_in_insert_mode = false, -- Open the chat buffer in insert mode? + + ---@param tokens number + ---@param adapter CodeCompanion.Adapter + token_count = function(tokens, adapter) -- The function to display the token count + return ' (' .. tokens .. ' tokens)' + end, + }, + diff = { + enabled = true, + close_chat_at = 240, -- Close an open chat buffer if the total columns of your display are less than... + layout = 'vertical', -- vertical|horizontal split for default provider + opts = { + 'internal', + 'filler', + 'closeoff', + 'algorithm:histogram', -- https://adamj.eu/tech/2024/01/18/git-improve-diff-histogram/ + 'indent-heuristic', -- https://blog.k-nut.eu/better-git-diffs + 'followwrap', + 'linematch:120', + }, + provider = providers.diff, -- mini_diff|default + }, + inline = { + -- If the inline prompt creates a new buffer, how should we display this? + layout = 'vertical', -- vertical|horizontal|buffer + }, + icons = { + loading = ' ', + warning = ' ', + }, + }, + -- EXTENSIONS ------------------------------------------------------ + extensions = {}, + -- GENERAL OPTIONS ---------------------------------------------------------- + opts = { + log_level = 'ERROR', -- TRACE|DEBUG|ERROR|INFO + language = 'English', -- The language used for LLM responses + + -- If this is false then any default prompt that is marked as containing code + -- will not be sent to the LLM. Please note that whilst I have made every + -- effort to ensure no code leakage, using this is at your own risk + ---@type boolean|function + ---@return boolean + send_code = true, + + job_start_delay = 1500, -- Delay in milliseconds between cmd tools + submit_delay = 2000, -- Delay in milliseconds before auto-submitting the chat buffer + + ---This is the default prompt which is sent with every request in the chat + ---strategy. It is primarily based on the GitHub Copilot Chat's prompt + ---but with some modifications. You can choose to remove this via + ---your own config but note that LLM results may not be as good + ---@param opts table + ---@return string + system_prompt = function(opts) + local language = opts.language or 'English' + return string.format( + [[You are an AI programming assistant named "CodeCompanion". You are currently plugged into the Neovim text editor on a user's machine. + +Your core tasks include: +- Answering general programming questions. +- Explaining how the code in a Neovim buffer works. +- Reviewing the selected code from a Neovim buffer. +- Generating unit tests for the selected code. +- Proposing fixes for problems in the selected code. +- Scaffolding code for a new workspace. +- Finding relevant code to the user's query. +- Proposing fixes for test failures. +- Answering questions about Neovim. +- Running tools. + +You must: +- Follow the user's requirements carefully and to the letter. +- Use the context and attachments the user provides. +- Keep your answers short and impersonal, especially if the user's context is outside your core tasks. +- Minimize additional prose unless clarification is needed. +- Use Markdown formatting in your answers. +- Include the programming language name at the start of each Markdown code block. +- Do not include line numbers in code blocks. +- Avoid wrapping the whole response in triple backticks. +- Only return code that's directly relevant to the task at hand. You may omit code that isn’t necessary for the solution. +- Avoid using H1, H2 or H3 headers in your responses as these are reserved for the user. +- Use actual line breaks in your responses; only use "\n" when you want a literal backslash followed by 'n'. +- All non-code text responses must be written in the %s language indicated. +- Multiple, different tools can be called as part of the same response. + +When given a task: +1. Think step-by-step and, unless the user requests otherwise or the task is very simple, describe your plan in detailed pseudocode. +2. Output the final code in a single code block, ensuring that only relevant code is included. +3. End your response with a short suggestion for the next user turn that directly supports continuing the conversation. +4. Provide exactly one complete reply per conversation turn. +5. If necessary, execute multiple tools in a single turn.]], + language + ) + end, + }, +} diff --git a/lua/plugins/ai/init.lua b/lua/plugins/ai/init.lua index f6b44e9..a57678a 100644 --- a/lua/plugins/ai/init.lua +++ b/lua/plugins/ai/init.lua @@ -3,4 +3,5 @@ if file ~= '' then return require 'plugins.ai.cody' else return require 'plugins.ai.codecompanion' + -- return require 'plugins.ai.avante' end diff --git a/lua/plugins/lsp/golangci_lint_ls.lua b/lua/plugins/lsp/golangci_lint_ls.lua index 3ab29e7..76f4376 100644 --- a/lua/plugins/lsp/golangci_lint_ls.lua +++ b/lua/plugins/lsp/golangci_lint_ls.lua @@ -1,19 +1,21 @@ return { enable = function() -- disable lsp if wrong version installed local enable = true - vim - .system({ 'golangci-lint', '--version' }, { text = true }, function(out) - if out.code ~= 0 then - enable = false - return - end - if out.stdout:match ' 1%.%d+%.%d+' then - enable = false - return - end - end) - :wait() + local ok, _ = pcall(function() + vim + .system({ 'golangci-lint', '--version' }, { text = true }, function(out) + if out.code ~= 0 then + enable = false + return + end + if out.stdout:match ' 1%.%d+%.%d+' then + enable = false + return + end + end) + :wait() + end) - return enable + return enable and ok end, }