|
| 1 | +# Neovim configuration |
| 2 | + |
| 3 | +## Register the language server with neovim |
| 4 | + |
| 5 | +TODO: contribute some defaults to the nvim-lspconfig repo and link to that instead. |
| 6 | + |
| 7 | +The following code will register the language server with Neovim, and naively attach it to every buffer. `lsp-ai` is |
| 8 | +configured with the `llama_cpp` backend to run the CodeGemma v1.1 model. |
| 9 | + |
| 10 | +Note: the model configuration is provided as an example/starting point only and I do not vouch for the quality of the |
| 11 | +generations. Adjust to taste. |
| 12 | + |
| 13 | +```lua |
| 14 | +local lsp_ai_config = { |
| 15 | + -- Uncomment the following line if you use nvim-cmp with the cmp_nvim_lsp source. |
| 16 | + -- capabilities = require('cmp_nvim_lsp').default_capabilities(), |
| 17 | + cmd = { 'lsp-ai' }, |
| 18 | + cmd_env = { |
| 19 | + -- Add required environment variables here, e.g. for CUDA device selection. |
| 20 | + -- CUDA_VISIBLE_DEVICES = "1" |
| 21 | + }, |
| 22 | + -- filetypes = { 'python' }, |
| 23 | + root_dir = vim.loop.cwd(), |
| 24 | + init_options = { |
| 25 | + -- cmp-ai configuration goes here. |
| 26 | + memory = { |
| 27 | + file_store = {} |
| 28 | + }, |
| 29 | + models = { |
| 30 | + model1 = { |
| 31 | + type = "llama_cpp", |
| 32 | + repository = "mmnga/codegemma-1.1-2b-gguf", |
| 33 | + name = "codegemma-1.1-2b-Q8_0.gguf", |
| 34 | + n_ctx = 2048, |
| 35 | + n_gpu_layers = 999 |
| 36 | + } |
| 37 | + }, |
| 38 | + completion = { |
| 39 | + model = "model1", |
| 40 | + parameters = { |
| 41 | + fim = { |
| 42 | + start = "<|fim_prefix|>", |
| 43 | + middle = "<|fim_suffix|>", |
| 44 | + ["end"] = "<|fim_middle|>" |
| 45 | + }, |
| 46 | + max_context = 2000, |
| 47 | + max_new_tokens = 32 |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +local function attach_buffer() |
| 54 | + vim.lsp.start(lsp_ai_config) |
| 55 | +end |
| 56 | + |
| 57 | +vim.api.nvim_create_autocmd({"BufEnter", "BufWinEnter"}, { |
| 58 | + callback = attach_buffer |
| 59 | +}) |
| 60 | +``` |
| 61 | + |
| 62 | +## Example ghost-text setup |
| 63 | + |
| 64 | +For a copilot-like ghost-text experience, here is an example configuration using the |
| 65 | +[nvim-cmp](https://github.com/hrsh7th/nvim-cmp) plugin, assuming you use the |
| 66 | +[cmp-nvim-lsp](https://github.com/hrsh7th/cmp-nvim-lsp) source. This is **not a full configuration**, please refer to |
| 67 | +the nvim-cmp documentation for a full starter config without ghost text if you need one. |
| 68 | + |
| 69 | +This configuration enables ghost-text in nvim-cmp, and registers a custom comparator that puts `cmp-ai` suggestions |
| 70 | +at the top so that they're the ones being drawn with ghost text. |
| 71 | + |
| 72 | +```lua |
| 73 | +local function ai_top_comparator(entry1, entry2) |
| 74 | + local comp_item = entry1:get_completion_item() |
| 75 | + if comp_item ~= nil then |
| 76 | + if string.sub(comp_item.label, 1, 4) == "ai -" then |
| 77 | + return true |
| 78 | + end |
| 79 | + end |
| 80 | + comp_item = entry2:get_completion_item() |
| 81 | + if comp_item ~= nil then |
| 82 | + if string.sub(comp_item.label, 1, 4) == "ai -" then |
| 83 | + return false |
| 84 | + end |
| 85 | + end |
| 86 | + return nil |
| 87 | +end |
| 88 | + |
| 89 | +local default_sorting = require('cmp.config.default')().sorting |
| 90 | +local my_sorting = vim.tbl_extend("force", {}, default_sorting) |
| 91 | +table.insert(my_sorting.comparators, 1, ai_top_comparator) |
| 92 | + |
| 93 | +cmp.setup({ |
| 94 | + -- <your nvim-cmp config here> |
| 95 | + -- ... |
| 96 | + experimental = { |
| 97 | + ghost_text = true |
| 98 | + }, |
| 99 | + sorting = my_sorting |
| 100 | +}) |
| 101 | +``` |
| 102 | + |
| 103 | +Notes and known issues: |
| 104 | + |
| 105 | +* You'll need a very recent version of `nvim-cmp` for multiline ghost text to work. Note that ghost-text is an |
| 106 | + experimental feature of `nvim-cmp`. |
| 107 | +* The completions window is currently drawn below the cursor, which hides ghost-text on the following lines. This is |
| 108 | + a known limitation of nvim-cmp, currently being addressed in PR 1955, so you may want to use the PR 1955 branch for |
| 109 | + now. |
| 110 | +* The first character of the suggestion is not being properly drawn. |
0 commit comments