Skip to content

Commit

Permalink
feat: make AI optional, and configurable by .nvimrc.ini
Browse files Browse the repository at this point in the history
  • Loading branch information
onjin committed Jun 19, 2024
1 parent e923e22 commit 2297bce
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .nvimrc.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
;enable AI
ai_enabled = 1
; set leader key
mapleader = ','
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ In neovim help:
- Support for `.editorconfig`https://github.com/editorconfig/editorconfig-vim
- Fuzzy search engine https://github.com/nvim-telescope/telescope.nvim
- Intellisense engine https://github.com/neovim/nvim-lspconfig + https://github.com/williamboman/mason-lspconfig.nvim
- AI support https://github.com/jcdickinson/codeium.nvim - free code assistance https://codeium.com/pricing
- AI support https://github.com/jcdickinson/codeium.nvim - free code assistance https://codeium.com/pricing (set .nvimrc.ini or vim.g.ai_enabled directly to enable)
- Displays available keybindings in popup — https://github.com/folke/which-key.nvim

For a full list of plugins refer to https://github.com/onjin/.vim/blob/main/lua/custom/plugins/

## Local .nvimrc.ini config for global variables

By creating `.nvimrc.ini` file in your project folder you can set global variables.

```dosini
;enable AI codeium
ai_enabled = 1

; set leader key
mapleader = ' '
```

## Screenshots

Which key keybindings popup, just press `,` and wait for help
Expand Down
51 changes: 50 additions & 1 deletion init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
--[[
-- Support of setting global variables per project by .nvimrc.ini file
--
-- Example .nvimrc.ini:
--
-- ai_enabled = 1
-- some_other = 'text'
--
-- Then you can use them by
-- if vim.g.ai_enabled then ... end
--
--]]
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end

local function load_ini_file(file)
local variables = {}
for line in io.lines(file) do
local key, value = line:match "^(.-)%s*=%s*(.-)%s*$"
if key and value then
key = trim(key)
value = trim(value)
if value:match "^'.*'$" or value:match '^".*"$' then
value = value:sub(2, -2) -- Remove quotes
elseif tonumber(value) then
value = tonumber(value)
end
variables[key] = value
end
end
return variables
end

local function set_global_variables(variables)
for key, value in pairs(variables) do
vim.g[key] = value
end
end

-- vim global settings here, so we can override it with .nvimrc.ini
vim.g.mapleader = ","

-- Load and set variables from .nvimrc.ini
local ini_file = vim.fn.getcwd() .. "/.nvimrc.ini"
if vim.fn.filereadable(ini_file) == 1 then
local variables = load_ini_file(ini_file)
set_global_variables(variables)
end

--[[
-- Setup initial configuration,
--
-- Primarily just download and execute lazy.nvim
--]]
vim.g.mapleader = ","

local lazypath = vim.fn.stdpath "data" .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
Expand Down
23 changes: 13 additions & 10 deletions lua/custom/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ vim.opt.shortmess:append "c"
local lspkind = require "lspkind"
lspkind.init {}

require("codeium").setup {}

local cmp = require "cmp"
local sources = {
{ name = "nvim_lsp" },
{ name = "cody" },
{ name = "path" },
{ name = "buffer" },
{ name = "luasnip" },
}

if vim.g.ai_enabled then
require("codeium").setup {}
table.insert(sources, { name = "codeium" })
end

cmp.setup {
sources = {
{ name = "nvim_lsp" },
{ name = "cody" },
{ name = "path" },
{ name = "buffer" },
{ name = "luasnip" },
{ name = "codeium" },
},
sources = sources,
mapping = {
["<C-n>"] = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Insert },
["<C-p>"] = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Insert },
Expand Down
2 changes: 1 addition & 1 deletion lua/custom/plugins/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ return {
"hrsh7th/cmp-buffer",
{ "L3MON4D3/LuaSnip", build = "make install_jsregexp" },
"saadparwaiz1/cmp_luasnip",
"jcdickinson/codeium.nvim",
"jcdickinson/codeium.nvim", -- set vim.g.ai_enabled to true to activate
"nvim-lua/plenary.nvim",
},
config = function()
Expand Down

0 comments on commit 2297bce

Please sign in to comment.