-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
959 lines (892 loc) · 27.2 KB
/
Copy pathinit.lua
File metadata and controls
959 lines (892 loc) · 27.2 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
---@diagnostic disable: undefined-global
-- Streamlined Neovim Config for Go, Rust, C++ and Python Development
-- Container-aware for containerd development
-- Detect container environment
local function is_in_container()
return os.getenv("CONTAINER") ~= nil or vim.fn.filereadable("/.dockerenv") == 1
end
local in_container = is_in_container()
vim.notify("Container mode: " .. tostring(in_container), vim.log.levels.WARN)
-- Set <space> as the leader key
vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- Set to true if you have a Nerd Font installed
vim.g.have_nerd_font = false
-- Containerd development configuration
vim.g.containerd_dev = {
root = vim.fn.expand("~/Dev/containerd"),
bin_path = vim.fn.expand("~/Dev/containerd/bin"),
vhd_test_path = "/mnt/vhd-test",
}
-- Basic Settings
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.mouse = "a"
vim.opt.showmode = false
vim.opt.clipboard = "unnamedplus"
vim.opt.breakindent = true
vim.opt.undofile = true
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.signcolumn = "yes"
vim.opt.updatetime = 250
vim.opt.timeoutlen = 300
vim.opt.splitright = true
vim.opt.splitbelow = true
vim.opt.list = true
vim.opt.listchars = { tab = "» ", trail = "·", nbsp = "␣" }
vim.opt.inccommand = "split"
vim.opt.cursorline = true
vim.opt.scrolloff = 10
-- Folding settings
vim.opt.foldmethod = "expr"
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"
vim.opt.foldenable = false -- Start with folds open
vim.opt.foldlevelstart = 99 -- Start with all folds open
-- Basic Keymaps
vim.keymap.set("n", "<Esc>", "<cmd>nohlsearch<CR>")
vim.keymap.set("n", "<leader>q", vim.diagnostic.setloclist, { desc = "Open diagnostic [Q]uickfix list" })
vim.keymap.set("t", "<Esc><Esc>", "<C-\\><C-n>", { desc = "Exit terminal mode" })
-- Window navigation
vim.keymap.set("n", "<C-h>", "<C-w><C-h>", { desc = "Move focus to the left window" })
vim.keymap.set("n", "<C-l>", "<C-w><C-l>", { desc = "Move focus to the right window" })
vim.keymap.set("n", "<C-j>", "<C-w><C-j>", { desc = "Move focus to the lower window" })
vim.keymap.set("n", "<C-k>", "<C-w><C-k>", { desc = "Move focus to the upper window" })
-- Highlight when yanking (copying) text
vim.api.nvim_create_autocmd("TextYankPost", {
desc = "Highlight when yanking text",
group = vim.api.nvim_create_augroup("highlight-yank", { clear = true }),
callback = function()
vim.highlight.on_yank()
end,
})
-- Install lazy.nvim plugin manager if not already installed
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
end
vim.opt.rtp:prepend(lazypath)
if vim.lsp and vim.lsp.config then
vim.lsp.config("rust_analyzer", {
root_dir = function(bufnr, callback)
if type(bufnr) ~= "number" or not vim.api.nvim_buf_is_valid(bufnr) then
return
end
local fname = vim.api.nvim_buf_get_name(bufnr)
if fname == "" then
return
end
local root = vim.fs.root(bufnr, { "Cargo.toml", "rust-project.json" })
if root then
callback(root)
end
end,
})
end
-- Plugin Configuration
require("lazy").setup({
-- Theme
{
"folke/tokyonight.nvim",
priority = 1000,
init = function()
vim.cmd.colorscheme("tokyonight-night")
vim.cmd.hi("Comment gui=none")
end,
},
-- File Explorer
{
"nvim-neo-tree/neo-tree.nvim",
branch = "v3.x",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-tree/nvim-web-devicons",
"MunifTanjim/nui.nvim",
},
config = function()
require("neo-tree").setup({
window = {
position = "left",
width = 30,
},
filesystem = {
follow_current_file = {
enabled = true,
},
filtered_items = {
visible = false,
hide_dotfiles = false,
hide_gitignored = false,
},
},
})
vim.keymap.set("n", "<leader>e", "<cmd>Neotree toggle<CR>", { desc = "Toggle file explorer" })
end,
},
-- Status Line
{
"nvim-lualine/lualine.nvim",
dependencies = { "nvim-tree/nvim-web-devicons" },
opts = {
options = {
icons_enabled = vim.g.have_nerd_font,
theme = "tokyonight",
component_separators = "|",
section_separators = "",
},
},
},
-- Fuzzy Finder (files, lsp, etc)
{
"nvim-telescope/telescope.nvim",
event = "VimEnter",
branch = "0.1.x",
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-telescope/telescope-fzf-native.nvim",
"nvim-tree/nvim-web-devicons",
},
config = function()
local telescope = require("telescope")
telescope.setup({
defaults = {
mappings = {
i = {
["<C-u>"] = false,
["<C-d>"] = false,
},
},
},
})
pcall(telescope.load_extension, "fzf")
local builtin = require("telescope.builtin")
vim.keymap.set("n", "<leader>ff", builtin.find_files, { desc = "Find files" })
vim.keymap.set("n", "<leader>fg", builtin.live_grep, { desc = "Find text" })
vim.keymap.set("n", "<leader>fb", builtin.buffers, { desc = "Find buffers" })
vim.keymap.set("n", "<leader>fh", builtin.help_tags, { desc = "Find help" })
end,
},
{
"nvim-telescope/telescope-fzf-native.nvim",
build = "make",
cond = function()
return vim.fn.executable("make") == 1
end,
},
-- LSP Configuration & Plugins
{
"neovim/nvim-lspconfig",
dependencies = {
"williamboman/mason.nvim",
"williamboman/mason-lspconfig.nvim",
"WhoIsSethDaniel/mason-tool-installer.nvim",
"hrsh7th/cmp-nvim-lsp",
},
config = function()
-- Mason setup for installing language servers
-- Disable auto-install in containers, assume tools are pre-installed
require("mason").setup({
install_root_dir = vim.fn.stdpath("data") .. "/mason",
-- In containers, we'll use system tools instead
PATH = in_container and "skip" or "prepend",
})
-- LSP servers to install
local servers = {
gopls = {
settings = {
gopls = {
analyses = {
unusedparams = true,
},
staticcheck = true,
gofumpt = true,
usePlaceholders = true,
semanticTokens = true,
-- Container-specific: handle module cache
env = in_container and {
GOMODCACHE = "/go/pkg/mod",
GOCACHE = "/go/cache",
} or nil,
},
},
},
yamlls = {
settings = {
yaml = {
schemaStore = {
enable = true,
url = "https://www.schemastore.org/api/json/catalog.json",
},
},
},
},
lua_ls = {
settings = {
Lua = {
diagnostics = {
globals = { "vim" },
},
workspace = {
library = vim.api.nvim_get_runtime_file("", true),
checkThirdParty = false,
},
telemetry = { enable = false },
},
},
},
rust_analyzer = {
settings = {
["rust-analyzer"] = {
check = {
command = "clippy",
},
diagnostics = {
enable = true,
},
},
},
},
clangd = {
cmd = {
"clangd",
"--background-index",
"--clang-tidy",
"--header-insertion=iwyu",
"--completion-style=detailed",
"--offset-encoding=utf-16",
},
},
}
-- Formatters / linters / debuggers to install (skip in containers).
-- Language servers themselves are installed by mason-lspconfig (ensure_installed below).
local tools = not in_container
and {
"gofumpt", -- Go formatter
"goimports", -- Go imports manager
"golangci-lint", -- Go linter
"delve", -- Go debugger
"yamlfmt", -- YAML formatter
"clang-format", -- C/C++ formatter
"codelldb", -- C/C++/Rust debugger
"stylua", -- Lua formatter
}
or {}
if not in_container then
require("mason-tool-installer").setup({ ensure_installed = tools })
end
-- Setup LSP keymaps when a language server attaches to a buffer
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("lsp-attach", { clear = true }),
callback = function(event)
local map = function(keys, func, desc)
vim.keymap.set("n", keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
end
-- LSP actions
map("gd", vim.lsp.buf.definition, "Goto Definition")
map("gr", vim.lsp.buf.references, "Goto References")
map("gD", vim.lsp.buf.declaration, "Goto Declaration")
map("gI", vim.lsp.buf.implementation, "Goto Implementation")
map("K", vim.lsp.buf.hover, "Hover Documentation")
map("<leader>rn", vim.lsp.buf.rename, "Rename")
map("<leader>ca", vim.lsp.buf.code_action, "Code Action")
map("<leader>ds", require("telescope.builtin").lsp_document_symbols, "Document Symbols")
-- Toggle inlay hints if supported
local client = vim.lsp.get_client_by_id(event.data.client_id)
if client and client.server_capabilities.inlayHintProvider then
map("<leader>th", function()
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = event.buf }))
end, "Toggle Inlay Hints")
end
end,
})
-- Default capabilities for every server (advertise nvim-cmp's completion support).
local capabilities = vim.lsp.protocol.make_client_capabilities()
local has_cmp, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
if has_cmp then
capabilities = vim.tbl_deep_extend("force", capabilities, cmp_nvim_lsp.default_capabilities())
end
vim.lsp.config("*", { capabilities = capabilities })
-- Register each server's config; this merges on top of nvim-lspconfig's built-in defaults.
for server_name, config in pairs(servers) do
vim.lsp.config(server_name, config)
end
if in_container then
-- Containers ship the language servers on PATH; enable the ones that are present.
for server_name in pairs(servers) do
if vim.fn.executable(server_name) == 1 or vim.fn.executable(server_name:gsub("_", "-")) == 1 then
vim.lsp.enable(server_name)
end
end
else
-- mason-lspconfig v2: installs the listed servers and auto-enables them
-- (via vim.lsp.enable) using the configs registered above.
require("mason-lspconfig").setup({
ensure_installed = vim.tbl_keys(servers),
automatic_enable = true,
})
end
end,
},
-- Autocompletion
{
"hrsh7th/nvim-cmp",
dependencies = {
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-path",
"hrsh7th/cmp-buffer",
},
config = function()
local cmp = require("cmp")
local luasnip = require("luasnip")
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
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 luasnip.expand_or_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
elseif luasnip.jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "luasnip" },
{ name = "path" },
}, {
{ name = "buffer" },
}),
})
end,
},
-- Treesitter for syntax highlighting
{
"nvim-treesitter/nvim-treesitter",
-- Pin to the legacy `master` branch: the `main` branch is a full rewrite that
-- removed the `nvim-treesitter.configs` module and the highlight/indent options
-- used below (and the `nvim_treesitter#foldexpr()` folding wired up at the top).
branch = "master",
build = ":TSUpdate",
config = function()
require("nvim-treesitter.configs").setup({
ensure_installed = {
"go",
"gomod",
"gosum",
"gowork",
"yaml",
"lua",
"vim",
"vimdoc",
"query",
"rust",
"c",
"cpp",
"cmake",
"python",
},
sync_install = false,
auto_install = true,
ignore_install = {},
modules = {},
highlight = {
enable = true,
},
indent = {
enable = true,
},
})
end,
},
-- Formatting
{
"stevearc/conform.nvim",
event = { "BufWritePre" },
cmd = { "ConformInfo" },
keys = {
{
"<leader>f",
function()
require("conform").format({ async = true, lsp_format = "fallback", timeout_ms = 5000 })
end,
mode = { "n", "v" },
desc = "Format buffer",
},
},
opts = {
notify_on_error = false,
formatters_by_ft = {
go = { "goimports", "gofumpt" },
yaml = { "yamlfmt" },
lua = { "stylua" },
c = { "clang-format" },
cpp = { "clang-format" },
},
format_on_save = function(bufnr)
-- Skip formatting when the buffer has syntax/parse errors
local diagnostics = vim.diagnostic.get(bufnr, { severity = vim.diagnostic.severity.ERROR })
for _, d in ipairs(diagnostics) do
if d.source and d.source:match("treesitter") then
return nil
end
end
local ft = vim.bo[bufnr].filetype
-- Go gets a longer timeout
if ft == "go" then
return { timeout_ms = 5000, lsp_format = "fallback" }
end
return { timeout_ms = 1000, lsp_format = "fallback" }
end,
},
},
-- Go specific plugins
{
"ray-x/go.nvim",
dependencies = {
"ray-x/guihua.lua",
"neovim/nvim-lspconfig",
"nvim-treesitter/nvim-treesitter",
},
config = function()
require("go").setup({
lsp_cfg = {
settings = {
gopls = {
analyses = {
unusedparams = true,
},
staticcheck = true,
-- Container-specific Go settings
env = in_container and {
GOMODCACHE = "/go/pkg/mod",
GOCACHE = "/go/cache",
} or nil,
},
},
},
lsp_inlay_hints = { enable = true },
treesitter = false,
dap_debug = not in_container, -- Disable DAP in containers
gofmt = "gofumpt",
lsp_document_formatting = false, -- use conform.nvim instead
lsp_keymaps = false, -- we defined our own above
})
-- Go specific folding autocmd
vim.api.nvim_create_autocmd("FileType", {
pattern = "go",
callback = function()
vim.opt_local.foldmethod = "expr"
vim.opt_local.foldexpr = "nvim_treesitter#foldexpr()"
end,
})
-- Go-specific keymaps
-- vim.keymap.set('n', '<leader>gt', '<cmd>GoTest<CR>', { desc = 'Go Test' })
vim.keymap.set("n", "<leader>gt", "<cmd>UnifiedTest<CR>", { desc = "Unified Test" })
vim.keymap.set("n", "<leader>gi", "<cmd>GoImport<CR>", { desc = "Go Import" })
vim.keymap.set("n", "<leader>ga", "<cmd>GoAlt<CR>", { desc = "Go to alternate file" })
end,
ft = { "go", "gomod" },
},
-- Git integration
{
"lewis6991/gitsigns.nvim",
opts = {
signs = {
add = { text = "+" },
change = { text = "~" },
delete = { text = "_" },
topdelete = { text = "‾" },
changedelete = { text = "~" },
},
on_attach = function(bufnr)
local gs = package.loaded.gitsigns
local function map(mode, key, action, desc)
vim.keymap.set(mode, key, action, { buffer = bufnr, desc = desc })
end
map("n", "<leader>gh", gs.preview_hunk, "Preview Git hunk")
map("n", "<leader>gb", function()
gs.blame_line({ full = true })
end, "Git blame line")
map("n", "<leader>gd", gs.diffthis, "Git diff")
end,
},
},
-- Unified test/run/build interface
{
"axkirillov/unified.nvim",
config = function()
-- Container-aware command wrapper (for future use)
local function wrap_command(cmd, args)
if in_container then
return cmd, args
end
return cmd, args
end
require("unified").setup({
-- Go configuration
go = {
test = {
command = "go",
args = { "test", "-v" },
file_pattern = "_test.go",
},
run = {
command = "go",
args = { "run", "." },
},
build = {
command = "go",
args = { "build", "." },
},
},
-- Rust configuration
rust = {
test = {
command = "cargo",
args = { "test", "--" },
},
run = {
command = "cargo",
args = { "run" },
},
build = {
command = "cargo",
args = { "build" },
},
},
-- C++ configuration (adjust target/executable names as needed)
cpp = {
-- Uses CTest via a CMake build directory named 'build'
test = {
command = "ctest",
args = { "--test-dir", "build", "--output-on-failure" },
file_pattern = { "test_*.cpp", "*_test.cpp" },
},
build = {
command = "cmake",
args = { "--build", "build", "--parallel" },
},
run = {
-- Replace 'app' with your executable target name
command = "./build/app",
args = {},
},
},
-- Python configuration
python = {
test = {
command = "python",
args = { "-m", "pytest", "-v" },
file_pattern = { "test_*.py", "*_test.py" },
},
run = {
command = "python",
args = { "main.py" },
},
build = {
command = "python",
args = { "-m", "pip", "install", "-e", "." },
},
},
})
-- Add keymaps for unified commands
vim.keymap.set("n", "<leader>ut", "<cmd>UnifiedTest<CR>", { desc = "Unified Test" })
vim.keymap.set("n", "<leader>ur", "<cmd>UnifiedRun<CR>", { desc = "Unified Run" })
vim.keymap.set("n", "<leader>ub", "<cmd>UnifiedBuild<CR>", { desc = "Unified Build" })
vim.keymap.set("n", "<leader>uc", "<cmd>UnifiedClean<CR>", { desc = "Unified Clean" })
end,
},
}, {
ui = {
icons = vim.g.have_nerd_font and {} or {
cmd = "⌘",
config = "🛠",
event = "📅",
ft = "📂",
init = "⚙",
keys = "🗝",
plugin = "🔌",
runtime = "💻",
require = "🌙",
source = "📄",
start = "🚀",
task = "📌",
lazy = "💤",
},
},
})
-- Additional keymaps
vim.keymap.set("n", "<leader>tt", "<cmd>Neotree toggle<CR>", { desc = "Toggle file tree" })
-- Container status indicator
if in_container then
vim.notify("Running in container mode", vim.log.levels.INFO)
-- Adjust terminal behavior in containers
vim.api.nvim_create_autocmd("TermOpen", {
callback = function()
vim.opt_local.scrollback = 10000
end,
})
end
-- ============================================================================
-- Containerd Development Tools
-- ============================================================================
-- Helper function to run commands in a split terminal
local function run_in_terminal(cmd, title)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(buf, title or "Command Output")
local win_height = math.floor(vim.o.lines * 0.3)
vim.cmd("botright " .. win_height .. "split")
local win = vim.api.nvim_get_current_win()
vim.api.nvim_win_set_buf(win, buf)
vim.fn.termopen(cmd, {
on_exit = function(_, code)
if code == 0 then
vim.notify("✓ " .. title .. " succeeded", vim.log.levels.INFO)
else
vim.notify("✗ " .. title .. " failed (exit code: " .. code .. ")", vim.log.levels.ERROR)
end
end,
})
-- Auto-enter insert mode in terminal
vim.cmd("startinsert")
end
-- Build cotainerd
vim.api.nvim_create_user_command("ContainerdBuild", function()
run_in_terminal("cd " .. vim.g.containerd_dev.root .. " && make bin/containerd bin/ctr", "Building containerd")
end, {})
-- Build and install containerd (requires sudo)
vim.api.nvim_create_user_command("ContainerdInstall", function()
run_in_terminal(
"cd " .. vim.g.containerd_dev.root .. " && make bin/containerd bin/ctr && sudo make install",
"Building and installing containerd"
)
end, {})
-- Stop system containerd
vim.api.nvim_create_user_command("ContainerdStop", function()
run_in_terminal('sudo pkill -9 containerd; sleep 1; echo "Containerd stopped"', "Stopping containerd")
end, {})
-- Start your custom containerd
vim.api.nvim_create_user_command("ContainerdStart", function()
run_in_terminal("sudo " .. vim.g.containerd_dev.bin_path .. "/containerd", "Starting custom containerd")
end, {})
-- Restart containerd (stop system, start custom)
vim.api.nvim_create_user_command("ContainerdRestart", function()
vim.notify("Restarting containerd...", vim.log.levels.INFO)
-- First stop
vim.fn.jobstart("sudo pkill -9 containerd", {
on_exit = function()
vim.defer_fn(function()
-- Then start custom
run_in_terminal("sudo " .. vim.g.containerd_dev.bin_path .. "/containerd", "Custom containerd daemon")
end, 1000)
end,
})
end, {})
-- List VHD registrations
vim.api.nvim_create_user_command("ContainerdVHDList", function()
run_in_terminal("sudo " .. vim.g.containerd_dev.bin_path .. "/ctr vhd list", "VHD Registrations")
end, {})
-- Register a VHD
vim.api.nvim_create_user_command("ContainerdVHDRegister", function(opts)
local args = vim.split(opts.args, " ", { trimempty = true })
if #args < 2 then
vim.notify("Usage: ContainerdVHDRegister <digest> <path>", vim.log.levels.ERROR)
return
end
run_in_terminal(
string.format("sudo %s/ctr vhd register %s %s", vim.g.containerd_dev.bin_path, args[1], args[2]),
"Registering VHD"
)
end, { nargs = "+" })
-- Inspect images and layers
vim.api.nvim_create_user_command("ContainerdInspect", function(opts)
local image_filter = opts.args ~= "" and "| grep -i '" .. opts.args .. "'" or ""
run_in_terminal(
string.format(
[[
echo "=== Images ===" &&
sudo ctr image list %s &&
echo -e "\n=== Recent Content ===" &&
sudo ctr content ls | head -20
]],
image_filter
),
"Containerd Inspection"
)
end, { nargs = "?" })
-- Pull and inspect Alpine (quick test)
vim.api.nvim_create_user_command("ContainerdQuickTest", function()
run_in_terminal(
[[
echo "Pulling alpine:latest..." &&
sudo ctr image pull docker.io/library/alpine:latest &&
echo -e "\n=== Image Info ===" &&
sudo ctr image list -q | grep alpine &&
echo -e "\n=== Layer Digests ===" &&
sudo ctr content ls | grep -v CREATED | head -10
]],
"Quick Test: Alpine"
)
end, {})
-- Calculate digest for layer directory (matches containerd's layer digest calculation)
vim.api.nvim_create_user_command("ContainerdCalculateLayerDigest", function(opts)
if opts.args == "" then
vim.notify("Usage: ContainerdCalculateLayerDigest <directory>", vim.log.levels.ERROR)
return
end
run_in_terminal(
string.format("tar -C %s -cf - . | sha256sum | awk '{print \"sha256:\" $1}'", opts.args),
"Calculating layer digest"
)
end, { nargs = 1 })
-- Setup VHD test environment
vim.api.nvim_create_user_command("ContainerdVHDTestSetup", function()
run_in_terminal(
[[
echo "Setting up VHD test environment..." &&
sudo mkdir -p /mnt/vhd-test/layer1 /mnt/vhd-test/layer2 &&
echo "test-data" | sudo tee /mnt/vhd-test/layer1/test.txt > /dev/null &&
sudo ls -la /mnt/vhd-test &&
echo "✓ VHD test directories created at /mnt/vhd-test"
]],
"VHD Test Setup"
)
end, {})
-- Inspect image layers and digests
vim.api.nvim_create_user_command("ContainerdInspectImage", function(opts)
local image = opts.args ~= "" and opts.args or "docker.io/library/alpine:latest"
run_in_terminal(
string.format(
[[
echo "=== Inspecting image: %s ===" &&
sudo ctr image pull %s &&
echo -e "\n=== Image layers ===" &&
sudo ctr content ls | grep -v CREATED | head -20 &&
echo -e "\n=== Image info ===" &&
sudo ctr image list | grep %s
]],
image,
image,
vim.fn.split(image, "/")[#vim.fn.split(image, "/")]
),
"Image Layer Inspection"
)
end, { nargs = "?" })
-- Run containerd tests
vim.api.nvim_create_user_command("ContainerdTest", function(opts)
local test_path = opts.args ~= "" and opts.args or "./..."
run_in_terminal(
string.format("cd %s && sudo -E go test -v %s", vim.g.containerd_dev.root, test_path),
"Running containerd tests"
)
end, { nargs = "?" })
-- Test VHD registration workflow
vim.api.nvim_create_user_command("ContainerdVHDTestWorkflow", function()
run_in_terminal(
string.format(
[[
echo "=== VHD Test Workflow ===" &&
cd %s &&
echo "1. Calculating digest for test layer..." &&
DIGEST=$(tar -C /mnt/vhd-test/layer1 -cf - . | sha256sum | awk '{print "sha256:" $1}') &&
echo " Digest: $DIGEST" &&
echo -e "\n2. Registering VHD layer..." &&
sudo ./bin/ctr vhd register $DIGEST /mnt/vhd-test/layer1 &&
echo -e "\n3. Listing registered VHDs..." &&
sudo ./bin/ctr vhd list &&
echo -e "\n✓ VHD workflow test complete"
]],
vim.g.containerd_dev.root
),
"VHD Workflow Test"
)
end, {})
-- Extract layer content for VHD simulation
vim.api.nvim_create_user_command("ContainerdExtractLayer", function(opts)
local args = vim.split(opts.args, " ", { trimempty = true })
if #args < 2 then
vim.notify("Usage: ContainerdExtractLayer <digest> <target-dir>", vim.log.levels.ERROR)
return
end
run_in_terminal(
string.format(
'sudo mkdir -p %s && sudo ctr content get %s | sudo tar -xz -C %s && echo "Extracted to %s"',
args[2],
args[1],
args[2],
args[2]
),
"Extracting Layer"
)
end, { nargs = "+" })
-- Keybindings for containerd development
vim.keymap.set("n", "<leader>xb", ":ContainerdBuild<CR>", { desc = "[X] Build containerd" })
vim.keymap.set("n", "<leader>xi", ":ContainerdInstall<CR>", { desc = "[X] Install containerd" })
vim.keymap.set("n", "<leader>xr", ":ContainerdRestart<CR>", { desc = "[X] Restart containerd" })
vim.keymap.set("n", "<leader>xs", ":ContainerdStop<CR>", { desc = "[X] Stop containerd" })
vim.keymap.set("n", "<leader>xS", ":ContainerdStart<CR>", { desc = "[X] Start containerd" })
vim.keymap.set("n", "<leader>xv", ":ContainerdVHDList<CR>", { desc = "[X] List VHDs" })
vim.keymap.set("n", "<leader>xV", ":ContainerdVHDTestSetup<CR>", { desc = "[X] Setup VHD test env" })
vim.keymap.set("n", "<leader>xw", ":ContainerdVHDTestWorkflow<CR>", { desc = "[X] Run VHD workflow test" })
vim.keymap.set("n", "<leader>xI", ":ContainerdInspect<CR>", { desc = "[X] Inspect images" })
vim.keymap.set("n", "<leader>xl", ":ContainerdInspectImage<CR>", { desc = "[X] Inspect image layers" })
vim.keymap.set("n", "<leader>xq", ":ContainerdQuickTest<CR>", { desc = "[X] Quick test (alpine)" })
vim.keymap.set("n", "<leader>xt", ":ContainerdTest<CR>", { desc = "[X] Run tests" })
-- Show containerd commands help
vim.keymap.set("n", "<leader>x?", function()
local commands = {
"Containerd VHD Development Commands:",
"",
"Building & Running:",
" <leader>xb - Build containerd",
" <leader>xi - Build and install containerd",
" <leader>xr - Restart containerd daemon",
" <leader>xs - Stop containerd",
" <leader>xS - Start custom containerd",
"",
"VHD Testing:",
" <leader>xV - Setup VHD test environment",
" <leader>xw - Run VHD workflow test",
" <leader>xv - List VHD registrations",
"",
"Inspection:",
" <leader>xI - Inspect images and layers",
" <leader>xl - Inspect specific image layers",
" <leader>xq - Quick test (pull alpine)",
"",
"Testing:",
" <leader>xt - Run Go tests",
"",
"Commands:",
" :ContainerdVHDRegister <digest> <path>",
" :ContainerdCalculateLayerDigest <dir>",
" :ContainerdInspectImage [image]",
" :ContainerdExtractLayer <digest> <dir>",
" :ContainerdTest [path]",
}
vim.notify(table.concat(commands, "\n"), vim.log.levels.INFO)
end, { desc = "[X] Show containerd help" })
-- vim: ts=2 sts=2 sw=2 et