From 96429a7b7d08c34b42bfbfe70e32e05f36a4edae Mon Sep 17 00:00:00 2001 From: Maciej Sypien Date: Sun, 2 Feb 2025 17:29:34 +0100 Subject: [PATCH] feat(nvim): add code completion with ollama --- configuration/.config/nvim/core/plugins.lua | 1 + .../.config/nvim/lua/plugins/cmp-ai.lua | 48 +++++++++++++++++++ .../.config/nvim/lua/plugins/cmp-config.lua | 31 ++++++++++++ .../lua/plugins/nvim-lspconfig-config.lua | 8 +++- 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 configuration/.config/nvim/lua/plugins/cmp-ai.lua diff --git a/configuration/.config/nvim/core/plugins.lua b/configuration/.config/nvim/core/plugins.lua index 00ffffb..0893eb7 100644 --- a/configuration/.config/nvim/core/plugins.lua +++ b/configuration/.config/nvim/core/plugins.lua @@ -89,6 +89,7 @@ local plugins = { -- nvim LSP completition { import = "plugins/nvim-lspconfig-config" }, + { import = "plugins/cmp-ai" }, -- need to be before cmp-config { import = "plugins/cmp-config" }, { import = "plugins/luasnip-config" }, diff --git a/configuration/.config/nvim/lua/plugins/cmp-ai.lua b/configuration/.config/nvim/lua/plugins/cmp-ai.lua new file mode 100644 index 0000000..7e5ed57 --- /dev/null +++ b/configuration/.config/nvim/lua/plugins/cmp-ai.lua @@ -0,0 +1,48 @@ +return { + "tzachar/cmp-ai", + dependencies = { + "nvim-lua/plenary.nvim", + }, + config = function() + local cmp_ai = require("cmp_ai.config") + + cmp_ai:setup({ + max_lines = 10, + provider = "Ollama", + provider_options = { + model = "llama3.2", + auto_unload = false, -- Set to true to automatically unload the model when exiting nvim. + stream = true, + -- prompt = function(lines_before, lines_after) + -- return lines_before + -- end, + -- suffix = function(lines_after) + -- vim.notify("lines_after", lines_after) + -- return lines_after + -- end, + -- prompt = "<|fim▁begin|>" .. lines_before .. "<|fim▁hole|>" .. lines_after .. "<|fim▁end|>", + -- raw_response_cb = function(response) + -- -- the `response` parameter contains the raw response (JSON-like) object. + + -- vim.notify(vim.inspect(response)) -- show the response as a lua table + + -- vim.g.ai_raw_response = response -- store the raw response in a global variable so that you can use it somewhere else (like statusline). + -- end, + options = { + temperature = 0.2, + }, + }, + notify = true, + notify_callback = function(msg) + vim.notify(msg) + end, + run_on_every_keystroke = true, + ignored_file_types = { + -- default is not to ignore + -- uncomment to ignore in lua: + -- lua = true + html = true, + }, + }) + end, +} diff --git a/configuration/.config/nvim/lua/plugins/cmp-config.lua b/configuration/.config/nvim/lua/plugins/cmp-config.lua index 5435a86..ae1fd5f 100644 --- a/configuration/.config/nvim/lua/plugins/cmp-config.lua +++ b/configuration/.config/nvim/lua/plugins/cmp-config.lua @@ -12,6 +12,9 @@ return { config = function() local lspkind = require("lspkind") + -- for sorting the results + local compare = require("cmp.config.compare") + -- Set up nvim-cmp. local cmp = require("cmp") @@ -43,6 +46,13 @@ return { behavior = cmp.ConfirmBehavior.Insert, select = true, -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. }), + [""] = cmp.mapping(cmp.mapping.complete({ + config = { + sources = cmp.config.sources({ + { name = "cmp_ai" }, + }), + }, + })), }), -- you can also configure: @@ -51,6 +61,7 @@ return { -- max_item_count - useful when having to many results from specific section sources = cmp.config.sources({ -- The order reflect in completion results + { name = "cmp_ai" }, -- include input from cmp-ai plugin { name = "cmp_git" }, { name = "nvim_lsp" }, { name = "path" }, @@ -107,6 +118,11 @@ return { Event = "", Operator = "󰆕", TypeParameter = "", + -- icons for cmp-ai completion + HF = "", + OpenAI = "", + Codestral = "", + Bard = "", }, -- The function below will be called before any actual modifications from lspkind @@ -124,6 +140,21 @@ return { }), }, + sorting = { + priority_weight = 2, + comparators = { + require("cmp_ai.compare"), + compare.offset, + compare.exact, + compare.score, + compare.recently_used, + compare.kind, + compare.sort_text, + compare.length, + compare.order, + }, + }, + -- source: https://github.com/hrsh7th/nvim-cmp/issues/598 performance = { trigger_debounce_time = 100, -- time after last key-input to next reload of available options diff --git a/configuration/.config/nvim/lua/plugins/nvim-lspconfig-config.lua b/configuration/.config/nvim/lua/plugins/nvim-lspconfig-config.lua index 2d2695a..ad5f68d 100644 --- a/configuration/.config/nvim/lua/plugins/nvim-lspconfig-config.lua +++ b/configuration/.config/nvim/lua/plugins/nvim-lspconfig-config.lua @@ -49,8 +49,12 @@ return { desc = "[g]o to [i]mplementation", buffer = ev.buf, }) - keymap.set("n", "gr", vim.lsp.buf.references, { - desc = "[g]o to [r]eferences", + -- keymap.set("n", "gr", vim.lsp.buf.references, { + -- desc = "[g]o to [r]eferences", + -- buffer = ev.buf, + -- }) + keymap.set("n", "gr", "Telescope lsp_references", { + desc = "[g]o to telescope [r]eferences", buffer = ev.buf, }) keymap.set("n", "dl", "Telescope diagnostics", {