-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.nix
383 lines (367 loc) · 11 KB
/
code.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Plugins that deal with actual code part
# of this whole deal. Mostly LSP stuff.
{ plugins, utils, pkgs, ... }:
with plugins;
with utils;
let
# Extensions to nix queries for treesitter
# to highlight embedded vimscript
# and lua in this very config
nixExt = pkgs.writeTextDir
"queries/nix/injections.scm"
''
;extends
(apply_expression
function: (_) @_func
argument: [
(string_expression (string_fragment) @lua)
(indented_string_expression (string_fragment) @lua)
]
(#match? @_func "(^|\\.)lua$"))
(apply_expression
function: (_) @_func
argument: [
(string_expression (string_fragment) @vim)
(indented_string_expression (string_fragment) @vim)
]
(#match? @_func "(^|\\.)vimscript$"))
'';
vim-tidal = pkgs.vimUtils.buildVimPlugin {
name = "vim-tidal";
src = pkgs.fetchFromGitHub {
owner = "tidalcycles";
repo = "vim-tidal";
rev = "f15f14b12176d8f1028d0596b031c3d6e1201c3b";
hash = "sha256-fLaBMm6jSLHytIH5IUQtUfi3kaPLdruO3cxv4UcjEO4=";
};
patchPhase = ''
rm Makefile
'';
};
in
[
# Treesitter. Fancier syntax highlighting,
# queries on source code used by other plugins.
# https://github.com/nvim-treesitter/nvim-treesitter
{
plugin = (nvim-treesitter.withPlugins (
plugins: with plugins; [
bash
c
comment
json
json5
lua
nix
python
regex
rust
toml
vim
yaml
]
));
config = vimscript ''
set runtimepath+=${nixExt}
set foldmethod=expr
set foldexpr=nvim_treesitter#foldexpr()
'' + lua ''
require'nvim-treesitter.configs'.setup({
highlight = {
enable = true,
}
})
'';
}
# Poor man's org-mode.
# https://github.com/nvim-neorg/neorg
{
plugin = neorg;
config = lua ''
require('neorg').setup({
load = {
["core.defaults"] = {}
}
})
'';
}
# Language servers
# https://github.com/neovim/nvim-lspconfig
{
plugin = nvim-lspconfig;
config = lua ''
-- A global for other plugins
LSPCommon = {
commands = {
code_action = {
keys = '<leader>ca',
cmd = '<cmd>lua vim.lsp.buf.code_action()<CR>'
},
declaration = { keys = 'gd', cmd = '<cmd>lua vim.lsp.buf.declaration()<CR>' },
definition = { keys = 'gD', cmd = '<cmd>lua vim.lsp.buf.definition()<CR>' },
references = { keys = 'gr', cmd = '<cmd>lua vim.lsp.buf.references()<CR>' },
implementation = {
keys = 'gi',
cmd = '<cmd>lua vim.lsp.buf.implementation()<CR>'
},
hover = { keys = 'K', cmd = '<cmd>lua vim.lsp.buf.hover()<CR>' },
ws_add_folder = {
keys = '<leader>wa',
cmd = '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>'
},
ws_remove_folder = {
keys = '<leader>wr',
cmd = '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>'
},
ws_list_folders = {
keys = '<leader>wl',
cmd = '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>'
},
type_defition = {
keys = '<leader>D',
cmd = '<cmd>lua vim.lsp.buf.type_definition()<CR>'
},
rename = {
keys = '<leader>cr',
cmd = '<cmd>lua vim.lsp.buf.rename()<CR>'
},
format = {
keys = '<leader>cf',
cmd = '<cmd>lua vim.lsp.buf.formatting()<CR>'
}
},
hooks = {},
on_attach = function(client, bufnr)
local buf_set_keymap = vim.api.nvim_buf_set_keymap
local opts = { noremap = true, silent = true }
for _, hook in pairs(LSPCommon.hooks) do hook(client, bufnr) end
for _, bind in pairs(LSPCommon.commands) do
buf_set_keymap(bufnr, 'n', bind.keys, bind.cmd, opts)
end
end
}
-- actual setup
local lspconfig = require('lspconfig')
lspconfig['lua_ls'].setup({
on_attach = LSPCommon.on_attach,
capabilities = LSPCommon.capabilities,
settings = { Lua = { diagnostics = { globals = { 'vim' } } } }
})
lspconfig['pylsp'].setup({
on_attach = LSPCommon.on_attach,
capabilities = LSPCommon.capabilities,
})
lspconfig['nil'].setup({
on_attach = LSPCommon.on_attach,
capabilities = LSPCommon.capabilities,
})
'';
}
# Show status of LSP server when loading
# https://github.com/j-hui/fidget.nvim
{
plugin = fidget-nvim;
config = genericConfig "fidget";
}
# Show a nice list of diagnostics
# https://github.com/folke/trouble.nvim
{
plugin = trouble-nvim;
config = lua ''
require('trouble').setup({})
LSPCommon.commands.workspace_errors = {
keys = '<leader>ce',
cmd = '<cmd>TroubleToggle workspace_diagnostics<cr>',
}
LSPCommon.commands.document_errors = {
keys = '<leader>cE',
cmd = '<cmd>TroubleToggle document_diagnostics<cr>',
}
'';
}
# In the same vein, show a list of
# symbols defined in the document
# https://github.com/simrat39/symbols-outline.nvim
{
plugin = symbols-outline-nvim;
config = lua ''
LSPCommon.commands.outline = {
keys = '<leader>cs',
cmd = '<cmd>SymbolsOutline<CR>'
}
'';
}
# Apply code actions easier
# https://github.com/weilbith/nvim-code-action-menu
{
plugin = nvim-code-action-menu;
config = lua ''
--vim.g.code_action_menu_show_details = false
LSPCommon.commands.code_action.cmd = '<cmd>CodeActionMenu<CR>'
'';
}
# Autoformat
# https://github.com/lukas-reineke/lsp-format.nvim
{
plugin = lsp-format-nvim;
config = lua ''
LSPCommon.hooks.format = require('lsp-format').on_attach
'';
}
# Improved experience for rust-analyzer
# https://github.com/simrat39/rust-tools.nvim
{
plugin = rust-tools-nvim;
# Workaround for something akin to this issue:
# https://github.com/wbthomason/packer.nvim/issues/698
# optional = true;
config = lua ''
local checkOptions = {
-- wastes some disk space in exchange for not
-- locking you from using cargo while check is running
extraArgs = { "--target-dir", "target/check" }
}
require('rust-tools').setup({
server = {
on_attach = LSPCommon.on_attach,
capabilities = LSPCommon.capabilities,
settings = {
["rust-analyzer"] = {
checkOnSave = checkOptions,
diagnostics = {
disabled = { "inactive-code" }
}
}
}
}
})
'';
}
{
plugin = crates-nvim;
config = lua ''
require('crates').setup({})
vim.api.nvim_create_autocmd("BufRead", {
group = vim.api.nvim_create_augroup("CmpSourceCargo", { clear = true }),
pattern = "Cargo.toml",
callback = function()
require('cmp').setup.buffer({ sources = { { name = "crates" } } })
end,
})
'';
}
{
plugin = lsp_lines-nvim;
config = lua ''
require("lsp_lines").setup()
-- Don't show virtual lines by default
vim.diagnostic.config({
virtual_lines = false,
virtual_text = true
})
-- Toggle
LSPCommon.toggle_diagnostics = function()
local lines = not vim.diagnostic.config().virtual_lines
local text = not vim.diagnostic.config().virtual_text
vim.diagnostic.config({ virtual_lines = lines, virtual_text = text })
end
LSPCommon.commands.diagnostics = {
keys = '<leader>e',
cmd = '<cmd>lua LSPCommon.toggle_diagnostics()<cr>'
}
'';
}
# Autocompletion
# https://github.com/hrsh7th/nvim-cmp
nvim-snippy
cmp-buffer
cmp-path
cmp-nvim-lsp
cmp-cmdline
cmp-snippy
lspkind-nvim
{
plugin = nvim-cmp;
config = lua ''
local has_words_before = function()
local line, col = table.unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0 and
vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(
col, col):match('%s') == nil
end
local cmp = require 'cmp'
local snippy = require 'snippy'
local lspkind = require 'lspkind'
cmp.setup({
snippet = {
expand = function(args)
require('snippy').expand_snippet(args.body) -- For `snippy` users.
end
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered()
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({ select = true }),
['<Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif snippy.can_expand_or_advance() then
snippy.expand_or_advance()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { 'i', 's' }),
['<S-Tab>'] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif snippy.can_jump(-1) then
snippy.previous()
else
fallback()
end
end, { 'i', 's' })
}),
sources = cmp.config.sources({
{ name = 'nvim_lsp' }, { name = 'snippy' }
}, { { name = 'buffer' } }),
formatting = {
format = lspkind.cmp_format({
mode = 'symbol_text',
menu = ({
buffer = '[Buffer]',
nvim_lsp = '[LSP]',
luasnip = '[LuaSnip]',
nvim_lua = '[Lua]',
latex_symbols = '[Latex]'
})
})
}
})
cmp.setup.cmdline('/', {
mapping = cmp.mapping.preset.cmdline(),
sources = { { name = 'buffer' } }
})
cmp.setup.cmdline(':', {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({ { name = 'path' } },
{ { name = 'cmdline' } })
})
local capabilities = require('cmp_nvim_lsp').default_capabilities()
LSPCommon.capabilities = capabilities
'';
}
{
plugin = vim-tidal;
config = vimscript ''
let g:tidal_target = "terminal"
'';
}
]