Skip to content

Commit 94c1ac7

Browse files
committed
feat(recipes): start working on an AI recipes section
1 parent ff21038 commit 94c1ac7

File tree

1 file changed

+115
-0
lines changed
  • src/content/docs/recipes

1 file changed

+115
-0
lines changed

src/content/docs/recipes/ai.mdx

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
id: ai
3+
title: AI Completion
4+
---
5+
6+
There are many AI plugins out there which provide completion through various mechanisms. This page goes over how to build onto an AstroNvim configuration in order to easily hook up and configure different AI completion plugins.
7+
8+
### Completion Engine Integration
9+
10+
In order to make AI plugins play nicely with AstroNvim it's important to set up an integration point with completion engines in Neovim in order to accept AI recommended code. Depending on which completion plugin you are using, you may need to modify the configuration of the mappings to account for interacting with normal completion as well as AI recommended code. By default AstroNvim utilizes a "super-tab" like configuration where the `<Tab>` key is used for triggering completion, navigating through snippets, and navigating through completion items. These code snippets change this configuration so that the `<Tab>` key is only used for snippet navigation and AI completion. To navigate through the completion lists, it would then be recommended to use `<C-n>` and `<C-p>`.
11+
12+
These configurations set up the completion engines to look for a function stored in `vim.g.ai_accept` which can be later configured by an AI completion plugin to choose what action should be taken when attempting to accept an AI suggestion if available.
13+
14+
#### [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) Integration
15+
16+
By default AstroNvim comes with [`nvim-cmp`](https://github.com/hrsh7th/nvim-cmp) for completion. This modifies the default configuration to set up the `<Tab>` key as described above:
17+
18+
```lua title="lua/plugins/cmp_ai.lua"
19+
return {
20+
"hrsh7th/nvim-cmp",
21+
optional = true,
22+
opts = function(_, opts)
23+
local cmp = require("cmp")
24+
if not opts.mapping then
25+
opts.mapping = {}
26+
end
27+
opts.mapping["<Tab>"] = cmp.mapping(function(fallback)
28+
-- Snippet jump next (with support for several popular snippet plugins)
29+
local mini_snippets_avail, mini_snippets = pcall(require, "mini.snippets")
30+
local luasnip_avail, luasnip = pcall(require, "luasnip")
31+
if mini_snippets_avail then
32+
if mini_snippets.session.get(false) then
33+
mini_snippets.session.jump("next")
34+
return
35+
end
36+
elseif luasnip_avail then
37+
if luasnip.locally_jumpable(1) then
38+
luasnip.jump(1)
39+
return
40+
end
41+
elseif vim.snippet and vim.snippet.active({ direction = 1 }) then
42+
vim.schedule(function()
43+
vim.snippet.jump(1)
44+
end)
45+
return
46+
end
47+
-- AI accept
48+
if vim.g.ai_accept and vim.g.ai_accept() then
49+
return
50+
end
51+
-- Fallback
52+
fallback()
53+
end, { "i", "s" })
54+
opts.mapping["<S-Tab>"] = cmp.mapping(function(fallback)
55+
-- Snippet jump previous
56+
local mini_snippets_avail, mini_snippets = pcall(require, "mini.snippets")
57+
local luasnip_avail, luasnip = pcall(require, "luasnip")
58+
if mini_snippets_avail then
59+
if mini_snippets.session.get(false) then
60+
mini_snippets.session.jump("prev")
61+
return
62+
end
63+
elseif luasnip_avail then
64+
if luasnip.locally_jumpable(-1) then
65+
luasnip.jump(-1)
66+
return
67+
end
68+
elseif vim.snippet and vim.snippet.active({ direction = -1 }) then
69+
vim.schedule(function()
70+
vim.snippet.jump(-1)
71+
end)
72+
return
73+
end
74+
-- Fallback
75+
fallback()
76+
end, { "i", "s" })
77+
end,
78+
}
79+
```
80+
81+
#### [blink.cmp](https://github.com/Saghen/blink.cmp) Integration
82+
83+
[blink.cmp](https://github.com/Saghen/blink.cmp) is another popular completion plugin. This configures the `<Tab>` key as described above:
84+
85+
```lua title="lua/plugins/cmp_ai.lua"
86+
return {
87+
"Saghen/blink.cmp",
88+
optional = true,
89+
opts = function(_, opts)
90+
if not opts.keymap then
91+
opts.keymap = {}
92+
end
93+
opts.keymap["<Tab>"] = {
94+
"snippet_forward",
95+
function()
96+
if vim.g.ai_accept then
97+
return vim.g.ai_accept()
98+
end
99+
end,
100+
"fallback",
101+
}
102+
opts.keymap["<S-Tab>"] = { "snippet_backward", "fallback" }
103+
end,
104+
}
105+
```
106+
107+
### AI Plugin Integration
108+
109+
Once our completion engine is configured, we can start choosing and adding in an AI plugin from the Neovim plugin ecosystem, and setting up the `vim.g.ai_accept` function appropriately to hook into the completion engine mappings that we previously set up.
110+
111+
#### [copilot.lua](https://github.com/zbirenbaum/copilot.lua)
112+
113+
#### [codeium.nvim](https://github.com/Exafunction/codeium.nvim)
114+
115+
#### [supermaven-nvim](https://github.com/supermaven-inc/supermaven-nvim)

0 commit comments

Comments
 (0)