Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions flakes/nvim/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions flakes/nvim/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# xclip (or wl-clipboard) is required to clip to the system clipboard.
inherit xclip;
# Copilot requires a version of Node.js > 16.x
inherit nodejs_20;
inherit nodejs_22;
inherit nil;
inherit rust-analyzer;
inherit cargo;
Expand All @@ -44,12 +44,12 @@
inherit gotools; # Includes goimports and other useful Go tools
typescript-language-server = nodePackages.typescript-language-server;
yaml-language-server = nodePackages.yaml-language-server;
docker-language-server = nodePackages.dockerfile-language-server-nodejs;
json-language-server = nodePackages.vscode-langservers-extracted;
bash-language-server = nodePackages.bash-language-server;
# There is also terraform-lsp which I could try
inherit terraform-ls;
inherit taplo-lsp;
inherit taplo;
inherit dockerfile-language-server;

inherit ripgrep;
inherit wordnet;
Expand Down Expand Up @@ -106,7 +106,6 @@

copilot-lua
copilot-cmp
CopilotChat-nvim

fzf-lua

Expand Down
201 changes: 56 additions & 145 deletions flakes/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ local LSP_ENABLED_VAR_NAME = 'LSP_ENABLED'
local COPILOT_ENABLED_VAR_NAME = 'COPILOT_ENABLED'
local ENABLED_FORMATTING_TOOL_VAR_NAME = 'ENABLED_FORMATTING_TOOL'
local ENABLED_LINTING_TOOL_VAR_NAME = 'ENABLED_LINTING_TOOL'
local COPILOT_AI_MODEL_VAR_NAME = 'NVIM_COPILOT_AI_MODEL'
local CURRENT_LANG_VAR_NAME = 'CURRENT_LANG'
local DEFAULT_LANG = 'en_us'
local DEFAULT_COPILOT_AI_MODEL = 'claude-3.5-sonnet'

-- Table to store the current state of auto-formatting
-- for a filetype
Expand All @@ -23,10 +21,6 @@ local AUTO_FORMATTING_ENABLED = {
go = true,
}

local function get_copilot_ai_model()
return os.getenv(COPILOT_AI_MODEL_VAR_NAME) or DEFAULT_COPILOT_AI_MODEL
end

local function executable_is_available(executable_name)
local handle = io.popen(string.format('which %s 2> /dev/null', executable_name))
if not handle then
Expand Down Expand Up @@ -749,13 +743,7 @@ local function configure_status_bar()

local copilot_enabled, copilot_response = pcall(vim.api.nvim_buf_get_var, 0, COPILOT_ENABLED_VAR_NAME)
if copilot_enabled and copilot_response then
if pcall(require, 'CopilotChat') then
local chat_config = require('CopilotChat').config
tools = tools .. string.format('(copilot using %s)', chat_config.model)
else
tools = tools .. '(copilot)'
return
end
tools = tools .. '(copilot)'
end

local lsp_enabled, lsp_tool_name = pcall(vim.api.nvim_buf_get_var, 0, LSP_ENABLED_VAR_NAME)
Expand Down Expand Up @@ -876,46 +864,49 @@ local function configure_lastplace()
end

local function configure_lsp()
if not pcall(require, 'lspconfig') then
print('lspconfig is not installed.')
return
end
-- Using the built-in vim.lsp.config API for Neovim 0.11+
-- See :help lsp.config

local custom_lsp_attach = function(client, buffer_number)
-- Define on_attach function
local on_attach = function(client, buffer_number)
-- Use LSP as the handler for omnifunc.
-- See `:help omnifunc` and `:help ins-completion` for more information.
vim.api.nvim_set_option_value('omnifunc', 'v:lua.vim.lsp.omnifunc', {
buf = buffer_number,
})

-- Use LSP as the handler for formatexpr.
-- See `:help formatexpr` for more information.
vim.api.nvim_set_option_value('formatexpr', 'v:lua.vim.lsp.formatexpr()', {
buf = buffer_number,
})

-- Mappings.
-- See `:h :map-arguments` for the options available when mapping
local mapping_options = { noremap = true, silent = true, buffer = buffer_number }

-- See `:help vim.lsp.*` for documentation on any of the below functions
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, mapping_options)
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, mapping_options)

vim.keymap.set('n', '<C-s>', vim.lsp.buf.signature_help, mapping_options)

vim.api.nvim_buf_set_var(buffer_number, LSP_ENABLED_VAR_NAME, true)
end

local lsp_flags = {
-- This is the default in Nvim 0.7+
-- Define capabilities and flags
local capabilities = vim.lsp.protocol.make_client_capabilities()
local flags = {
debounce_text_changes = 150,
}

require('lspconfig').rust_analyzer.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
-- Server-specific settings...
-- Configure all LSP servers with the new API

-- Set default configuration for all servers
vim.lsp.config('*', {
on_attach = on_attach,
flags = flags,
capabilities = capabilities,
})

-- Configure Rust Analyzer
vim.lsp.config('rust_analyzer', {
settings = {
['rust-analyzer'] = {
cargo = {
Expand All @@ -925,42 +916,31 @@ local function configure_lsp()
},
})

-- Documented at https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#lua_ls
require('lspconfig').lua_ls.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
-- Configure Lua language server
vim.lsp.config('lua_ls', {
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = 'LuaJIT',
},
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = { 'vim' },
},
workspace = {
-- Make the server aware of Neovim runtime files
library = vim.api.nvim_get_runtime_file('', true),
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = {
enable = false,
},
},
},
})

-- Documented at https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#tsserver
require('lspconfig').ts_ls.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
})
-- Configure TypeScript server
vim.lsp.config('ts_ls', {})

-- Documented at https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#nil_ls
require('lspconfig').nil_ls.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
-- Configure Nix language server
vim.lsp.config('nil_ls', {
settings = {
['nil'] = {
nix = {
Expand All @@ -972,32 +952,24 @@ local function configure_lsp()
},
})

require('lspconfig').clangd.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
-- Configure C/C++ language server
vim.lsp.config('clangd', {
settings = {
['clangd'] = {},
},
})

require('lspconfig').jsonls.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
})
-- Configure JSON language server
vim.lsp.config('jsonls', {})

require('lspconfig').taplo.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
})
-- Configure TOML language server
vim.lsp.config('taplo', {})

require('lspconfig').dockerls.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
})
-- Configure Docker language server
vim.lsp.config('dockerls', {})

require('lspconfig').gopls.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
-- Configure Go language server
vim.lsp.config('gopls', {
settings = {
gopls = {
analyses = {
Expand All @@ -1008,13 +980,10 @@ local function configure_lsp()
},
})

-- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md#yamlls
require('lspconfig').yamlls.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
-- Configure YAML language server
vim.lsp.config('yamlls', {
settings = {
['yaml'] = {
-- Name has to be in camelCase because this is what the yaml-language-server uses.
keyOrdering = false,
redhat = {
telemetry = {
Expand All @@ -1025,29 +994,25 @@ local function configure_lsp()
},
})

require('lspconfig').bashls.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
})

-- Other option for terraform is terraform-lsp:
-- ```
-- require('lspconfig').terraform_lsp.setup({})
-- ```
require('lspconfig').terraformls.setup({
on_attach = custom_lsp_attach,
flags = lsp_flags,
})

-- See https://github.com/neovim/nvim-lspconfig#suggested-configuration for
-- the suggested top-level configuration and https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
-- for a list of all the available language servers.

-- TODO add sumneko_lua support.
-- TODO add eslint support?
-- TODO add vanilla JS support?
-- TODO add dockerfile support?
-- TODO add python support
-- Configure Bash language server
vim.lsp.config('bashls', {})

-- Configure Terraform language server
vim.lsp.config('terraformls', {})

-- Enable all configured servers individually
vim.lsp.enable('rust_analyzer')
vim.lsp.enable('lua_ls')
vim.lsp.enable('ts_ls')
vim.lsp.enable('nil_ls')
vim.lsp.enable('clangd')
vim.lsp.enable('jsonls')
vim.lsp.enable('taplo')
vim.lsp.enable('dockerls')
vim.lsp.enable('gopls')
vim.lsp.enable('yamlls')
vim.lsp.enable('bashls')
vim.lsp.enable('terraformls')
end

local function configure_key_bindings()
Expand Down Expand Up @@ -1084,8 +1049,6 @@ local function configure_key_bindings()
-- to edit the line by pressing Esc instead.
vim.api.nvim_set_keymap('t', '<Space><Esc>', '<C-\\><C-n>', { silent = false, noremap = true })

vim.api.nvim_set_keymap('n', '<C-n>', ':CopilotChatOpen<Enter>', { silent = false, noremap = true })

vim.api.nvim_set_keymap('n', '<C-b>', ':GitBlameCopyFileURL<Enter>', { silent = false, noremap = true })

-- Copy current buffer path to clipboard
Expand Down Expand Up @@ -1439,10 +1402,6 @@ local function configure_copilot()
print('copilot_cmp is not installed.')
return
end
if not pcall(require, 'CopilotChat') then
print('CopilotChat is not installed')
return
end

if os.getenv('NVIM_ENABLE_COPILOT') ~= 'true' then
return
Expand Down Expand Up @@ -1491,54 +1450,6 @@ local function configure_copilot()
})

require('copilot_cmp').setup({})

-- See https://github.com/CopilotC-Nvim/CopilotChat.nvim?tab=readme-ov-file#default-configuration for the
-- default configuration.
require('CopilotChat').setup({
debug = true,
allow_insecure = false,

model = get_copilot_ai_model(),
context = 'buffers',

window = {
layout = 'float',
relative = 'editor', -- 'editor', 'win', 'cursor', 'mouse'
border = 'single', -- 'none', single', 'double', 'rounded', 'solid', 'shadow'
width = 0.8, -- fractional width of parent
height = 0.6, -- fractional height of parent
title = 'Copilot Chat', -- title of chat window
zindex = 1, -- determines if window is on top or below other floating windows
},

mappings = {
close = {
normal = '<C-c>',
},
reset = {
normal = '<Del>',
},
complete = {
insert = '<Tab>',
},
submit_prompt = {
insert = '<CR>',
normal = '<CR>',
},
accept_diff = {
normal = 'a',
},
show_diff = {
normal = 'gd',
},
show_info = {
normal = 'gp',
},
show_context = {
normal = 'gs',
},
},
})
end

local function configure_git_blame()
Expand Down