From e227f2f5f5f1edc60a4ccb1e7e938a8812242c6d Mon Sep 17 00:00:00 2001 From: adrien Date: Thu, 12 Feb 2026 22:49:41 +0100 Subject: [PATCH] Added autocomplete + terminal and lazygit floating term --- init.lua | 97 +++++++++++++++++++++++++++++++++++++++----- lazy-lock.json | 5 +++ lua/autocomplete.lua | 71 ++++++++++++++++++++++++++++++++ lua/keymaps.lua | 22 ++++++++++ lua/lsp.lua | 17 -------- 5 files changed, 185 insertions(+), 27 deletions(-) create mode 100644 lua/autocomplete.lua delete mode 100644 lua/lsp.lua diff --git a/init.lua b/init.lua index 5a4ae7a..1a132be 100644 --- a/init.lua +++ b/init.lua @@ -15,6 +15,31 @@ require("lazy").setup({ { "neovim/nvim-lspconfig" }, { "folke/which-key.nvim", event = "VeryLazy" }, { "nvim-tree/nvim-tree.lua", dependencies = { "nvim-tree/nvim-web-devicons" } }, + + -- [NEW] Autocomplete & Snippets + { + "hrsh7th/nvim-cmp", + dependencies = { + "hrsh7th/cmp-nvim-lsp", -- Connects LSP to completion + "L3MON4D3/LuaSnip", -- Snippet engine (Required for arguments) + "saadparwaiz1/cmp_luasnip", + }, + }, + + { + 'akinsho/toggleterm.nvim', + version = "*", + config = function() + require("toggleterm").setup({ + size = 20, + open_mapping = [[t]], -- This sets the trigger + direction = 'float', -- This makes it floating + float_opts = { + border = 'curved', -- Options: 'single', 'double', 'shadow', 'curved' + }, + }) + end +} }) -- 3. Theme & Settings @@ -24,25 +49,77 @@ vim.opt.relativenumber = true -- 4. Tree Setup (Small & Minimal) require("nvim-tree").setup({ - view = { - width = 30, -- Small width - side = "left", + view = { width = 30, side = "left" }, + filters = { dotfiles = false, custom = { ".git", "zig-cache", "zig-out" } }, +}) + +-- 5. Autocomplete Setup +local cmp = require('cmp') +local luasnip = require('luasnip') + +-- This is the "secret sauce" that tells ZLS your editor can handle +-- the advanced "return ." and "self." completion data. +local capabilities = require('cmp_nvim_lsp').default_capabilities() + +cmp.setup({ + snippet = { + expand = function(args) luasnip.lsp_expand(args.body) end, }, - filters = { - dotfiles = false, - custom = { ".git", "zig-cache", "zig-out" }, -- Hide build clutter + completion = { + autocomplete = { cmp.TriggerEvent.TextChanged }, + keyword_length = 1, + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.confirm({ select = true }), + [""] = 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" }), + [""] = 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' }, + }), + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), }, }) --- 5. Modern LSP Setup (Neovim 0.11+) +-- 6. Modern LSP Setup (Neovim 0.11 native) +-- We define the config, then enable it. +vim.lsp.enable('zls') vim.lsp.config('zls', { cmd = { 'zls' }, filetypes = { 'zig', 'zir' }, - root_markers = { 'build.zig', 'zls.json', '.git' }, + root_markers = { 'build.zig', '.git' }, + -- Pass the capabilities and ZLS-specific settings here: + capabilities = capabilities, + settings = { + zls = { + enable_snippets = true, + enable_argument_placeholders = true, + operator_completions = true, + enable_autofix = true, + }, + }, }) -vim.lsp.enable('zls') --- 6. Auto-format +-- 7. Auto-format on Save vim.api.nvim_create_autocmd("BufWritePre", { pattern = "*.zig", callback = function() vim.lsp.buf.format() end, diff --git a/lazy-lock.json b/lazy-lock.json index f6e56a7..2208518 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -1,10 +1,15 @@ { + "LuaSnip": { "branch": "master", "commit": "dae4f5aaa3574bd0c2b9dd20fb9542a02c10471c" }, "catppuccin": { "branch": "main", "commit": "beaf41a30c26fd7d6c386d383155cbd65dd554cd" }, + "cmp-nvim-lsp": { "branch": "main", "commit": "cbc7b02bb99fae35cb42f514762b89b5126651ef" }, + "cmp_luasnip": { "branch": "master", "commit": "98d9cb5c2c38532bd9bdb481067b20fea8f32e90" }, "lazy.nvim": { "branch": "main", "commit": "306a05526ada86a7b30af95c5cc81ffba93fef97" }, + "nvim-cmp": { "branch": "main", "commit": "da88697d7f45d16852c6b2769dc52387d1ddc45f" }, "nvim-lspconfig": { "branch": "master", "commit": "f4e9d367d4e067d7a5fabc9fd3f1349b291eb718" }, "nvim-tree.lua": { "branch": "master", "commit": "c0b18e4879f7b29a17a240ad49f733af7a7fb348" }, "nvim-web-devicons": { "branch": "master", "commit": "746ffbb17975ebd6c40142362eee1b0249969c5c" }, "plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" }, "telescope.nvim": { "branch": "master", "commit": "d90956833d7c27e73c621a61f20b29fdb7122709" }, + "toggleterm.nvim": { "branch": "main", "commit": "50ea089fc548917cc3cc16b46a8211833b9e3c7c" }, "which-key.nvim": { "branch": "main", "commit": "3aab2147e74890957785941f0c1ad87d0a44c15a" } } diff --git a/lua/autocomplete.lua b/lua/autocomplete.lua new file mode 100644 index 0000000..7ae5942 --- /dev/null +++ b/lua/autocomplete.lua @@ -0,0 +1,71 @@ +-- REQUIRED PLUGINS (via your plugin manager, e.g., Packer/Lazy): +-- 1. 'hrsh7th/nvim-cmp' (The completion engine) +-- 2. 'hrsh7th/cmp-nvim-lsp' (Bridge between LSP and cmp) +-- 3. 'neovim/nvim-lspconfig' (Configs for ZLS) +-- 4. 'L3MON4D3/LuaSnip' (Snippet engine, required by cmp) + +local cmp = require('cmp') +local lspconfig = require('lspconfig') +local luasnip = require('luasnip') + +-- 1. SETUP THE COMPLETION ENGINE +cmp.setup({ + -- Snippet expansion is required for arguments to populate correctly + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + + -- Window styling for the suggestion box and documentation float + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, + + -- Key mappings + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), -- Force trigger completion + [''] = cmp.mapping.confirm({ select = true }), -- Accept suggestion + }), + + -- Sources: 'nvim_lsp' is what provides the intelligent 'self' data + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + { name = 'buffer' }, + }), + + -- formatting: This adds the "Method", "Field", "Text" icons + formatting = { + format = function(entry, vim_item) + vim_item.menu = ({ + nvim_lsp = "[LSP]", + luasnip = "[Snippet]", + buffer = "[Buffer]", + })[entry.source.name] + return vim_item + end + }, +}) + +-- 2. SETUP ZIG LANGUAGE SERVER (ZLS) +-- This captures the capabilities of nvim-cmp to send to ZLS +local capabilities = require('cmp_nvim_lsp').default_capabilities() + +lspconfig.zls.setup({ + capabilities = capabilities, + + -- ZLS Specific Settings to ensure you get arguments and details + settings = { + zls = { + enable_snippets = true, -- Auto-fill arguments + enable_argument_placeholders = true, -- Placeholders like (a: i32, b: bool) + highlight_global_var_declarations = true, + operator_completions = true, + include_at_in_builtins = true, -- Suggest @import, @cImport + }, + }, +}) diff --git a/lua/keymaps.lua b/lua/keymaps.lua index 1e32099..92c5b07 100644 --- a/lua/keymaps.lua +++ b/lua/keymaps.lua @@ -1,7 +1,29 @@ local wk = require("which-key") local builtin = require('telescope.builtin') +local Terminal = require('toggleterm.terminal').Terminal +local lazygit = Terminal:new({ + cmd = "lazygit", + dir = "git_dir", + direction = "float", + float_opts = { + border = "double", + }, + -- function to run on opening the terminal + on_open = function(term) + vim.cmd("startinsert!") + vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "close", {noremap = true, silent = true}) + end, +}) + +function _lazygit_toggle() + lazygit:toggle() +end + wk.add({ + { "t", "ToggleTerm", desc = "Toggle Floating Terminal"}, + {"g", "lua _lazygit_toggle()", desc = "Toggle LazyGit"}, + -- Essentials { "w", "w", desc = "Save File" }, { "q", "q", desc = "Quit" }, diff --git a/lua/lsp.lua b/lua/lsp.lua deleted file mode 100644 index be5bce9..0000000 --- a/lua/lsp.lua +++ /dev/null @@ -1,17 +0,0 @@ -local configs = require 'nvim_lsp/configs' -local util = require 'nvim_lsp/util' - -configs.zls = { - default_config = { - cmd = {"/usr/local/bin/zls"}; - filetypes = {"zig"}; - root_dir = util.root_pattern("build.zig", ".git"); - }; - docs = { - description = [[ ]]; - default_config = { - root_dir = [[root_pattern("build.zig", ".git")]]; - }; - }; -} --- vim:et ts=2 sw=2