Skip to content

Commit ad51913

Browse files
committed
doc: add WIP nvim example config
1 parent e88c920 commit ad51913

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

examples/nvim.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
root_dir = vim.loop.cwd(),
23+
init_options = {
24+
-- cmp-ai configuration goes here.
25+
memory = {
26+
file_store = {}
27+
},
28+
models = {
29+
model1 = {
30+
type = "llama_cpp",
31+
repository = "mmnga/codegemma-1.1-2b-gguf",
32+
name = "codegemma-1.1-2b-Q8_0.gguf",
33+
n_ctx = 2048,
34+
n_gpu_layers = 999
35+
}
36+
},
37+
completion = {
38+
model = "model1",
39+
parameters = {
40+
fim = {
41+
start = "<|fim_prefix|>",
42+
middle = "<|fim_suffix|>",
43+
["end"] = "<|fim_middle|>"
44+
},
45+
max_context = 2000,
46+
max_new_tokens = 32
47+
}
48+
}
49+
}
50+
}
51+
52+
local function attach_buffer()
53+
vim.lsp.start(lsp_ai_config)
54+
end
55+
56+
vim.api.nvim_create_autocmd({"BufEnter", "BufWinEnter"}, {
57+
callback = attach_buffer
58+
})
59+
```
60+
61+
## Example ghost-text setup
62+
63+
For a copilot-like ghost-text experience, here is an example configuration using the
64+
[nvim-cmp](https://github.com/hrsh7th/nvim-cmp) plugin, assuming you use the
65+
[cmp-nvim-lsp](https://github.com/hrsh7th/cmp-nvim-lsp) source. This is **not a full configuration**, please refer to
66+
the nvim-cmp documentation for a full starter config without ghost text if you need one.
67+
68+
This configuration enables ghost-text in nvim-cmp, and registers a custom comparator that puts `cmp-ai` suggestions
69+
at the top so that they're the ones being drawn with ghost text.
70+
71+
```lua
72+
local function ai_top_comparator(entry1, entry2)
73+
local comp_item = entry1:get_completion_item()
74+
if comp_item ~= nil then
75+
if string.sub(comp_item.label, 1, 4) == "ai -" then
76+
return true
77+
end
78+
end
79+
comp_item = entry2:get_completion_item()
80+
if comp_item ~= nil then
81+
if string.sub(comp_item.label, 1, 4) == "ai -" then
82+
return false
83+
end
84+
end
85+
return nil
86+
end
87+
88+
local default_sorting = require('cmp.config.default')().sorting
89+
local my_sorting = vim.tbl_extend("force", {}, default_sorting)
90+
table.insert(my_sorting.comparators, 1, ai_top_comparator)
91+
92+
cmp.setup({
93+
-- <your nvim-cmp config here>
94+
-- ...
95+
experimental = {
96+
ghost_text = true
97+
},
98+
sorting = my_sorting
99+
})
100+
```
101+
102+
Notes and known issues:
103+
104+
* You'll need a very recent version of `nvim-cmp` for multiline ghost text to work. Note that ghost-text is an
105+
experimental feature of `nvim-cmp`.
106+
* The completions window is currently drawn below the cursor, which hides ghost-text on the following lines. This is
107+
a known limitation of nvim-cmp, currently being addressed in PR 1955, so you may want to use the PR 1955 branch for
108+
now.
109+
* The first character of the suggestion is not being properly drawn.

0 commit comments

Comments
 (0)