From b760e0912739bf73b8b26c50a13a33f8a0ce477c Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Sun, 10 Apr 2022 14:57:14 +0200 Subject: My current configuration --- lua/user/cmp.lua | 122 ++++++++++++++++++++++++++++++++++ lua/user/config.lua | 26 ++++++++ lua/user/keymaps.lua | 50 ++++++++++++++ lua/user/lsp/handlers.lua | 89 +++++++++++++++++++++++++ lua/user/lsp/init.lua | 8 +++ lua/user/lsp/lsp-installer.lua | 20 ++++++ lua/user/lsp/settings/sumneko_lua.lua | 15 +++++ lua/user/lsp/test.py | 1 + lua/user/options.lua | 19 ++++++ lua/user/plugins.lua | 71 ++++++++++++++++++++ lua/user/telescope.lua | 83 +++++++++++++++++++++++ lua/user/treesitter.lua | 16 +++++ lua/user/vimplug.lua | 65 ++++++++++++++++++ 13 files changed, 585 insertions(+) create mode 100644 lua/user/cmp.lua create mode 100644 lua/user/config.lua create mode 100644 lua/user/keymaps.lua create mode 100644 lua/user/lsp/handlers.lua create mode 100644 lua/user/lsp/init.lua create mode 100644 lua/user/lsp/lsp-installer.lua create mode 100644 lua/user/lsp/settings/sumneko_lua.lua create mode 100644 lua/user/lsp/test.py create mode 100644 lua/user/options.lua create mode 100644 lua/user/plugins.lua create mode 100644 lua/user/telescope.lua create mode 100644 lua/user/treesitter.lua create mode 100644 lua/user/vimplug.lua diff --git a/lua/user/cmp.lua b/lua/user/cmp.lua new file mode 100644 index 0000000..960eb13 --- /dev/null +++ b/lua/user/cmp.lua @@ -0,0 +1,122 @@ +local cmp_status_ok, cmp = pcall(require, "cmp") +if not cmp_status_ok then + return +end + +local snip_status_ok, luasnip = pcall(require, "luasnip") +if not snip_status_ok then + return +end + +require("luasnip/loaders/from_vscode").lazy_load() + +local check_backspace = function() + local col = vim.fn.col "." - 1 + return col == 0 or vim.fn.getline("."):sub(col, col):match "%s" +end + +local kind_icons = { + Text = "", + Method = "m", + Function = "", + Constructor = "", + Field = "", + Variable = "", + Class = "", + Interface = "", + Module = "", + Property = "", + Unit = "", + Value = "", + Enum = "", + Keyword = "", + Snippet = "", + Color = "", + File = "", + Reference = "", + Folder = "", + EnumMember = "", + Constant = "", + Struct = "", + Event = "", + Operator = "", + TypeParameter = "", +} + +cmp.setup({ + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + mapping = { + [''] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }), + [''] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }), + [''] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }), + [''] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `` mapping. + [''] = cmp.mapping({ + i = cmp.mapping.abort(), + c = cmp.mapping.close(), + }), + [''] = cmp.mapping.confirm({ select = false }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expandable() then + luasnip.expand() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif check_backspace() then + fallback() + 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 = "nvim_lua" }, + { name = "luasnip" }, + { name = "buffer" }, + { name = "path" }, + }), + formatting = { + fields = { "kind", "abbr", "menu" }, + format = function(entry, vim_item) + vim_item.kind = string.format("%s", kind_icons[vim_item.kind]) + vim_item.menu = ({ + nvim_lsp = "[LSP]", + nvim_lua = "[NVIM_LUA]", + luasnip = "[Snippet]", + buffer = "[Buffer]", + path = "[Path]", + })[entry.source.name] + return vim_item + end, + }, + confirm_opts = { + behavior = cmp.ConfirmBehavior.Replace, + select = false, + }, + documentation = { + border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, + }, + experimental = { + -- ghost_text = true, + }, +}) diff --git a/lua/user/config.lua b/lua/user/config.lua new file mode 100644 index 0000000..4f6b233 --- /dev/null +++ b/lua/user/config.lua @@ -0,0 +1,26 @@ +vim.cmd [[colorscheme gruvbox]] + +-- local Plug = require 'vimplug' + +-- Plug.begin('~/.config/share/nvim/plugged') +-- +-- Plug 'https://github.com/vim-airline/vim-airline' +-- Plug 'https://github.com/rafi/awesome-vim-colorschemes' +-- Plug 'https://github.com/preservim/nerdtree' +-- Plug 'https://github.com/neovim/nvim-lspconfig' +-- Plug 'https://github.com/williamboman/nvim-lsp-installer' +-- Plug 'https://github.com/hrsh7th/nvim-cmp' +-- Plug 'https://github.com/hrsh7th/cmp-nvim-lsp' +-- Plug 'https://github.com/hrsh7th/cmp-buffer' +-- Plug 'https://github.com/hrsh7th/cmp-path' +-- Plug 'https://github.com/hrsh7th/cmp-cmdline' +-- Plug 'https://github.com/hrsh7th/nvim-cmp' +-- Plug 'https://github.com/L3MON4D3/LuaSnip' +-- Plug 'https://github.com/rafamadriz/friendly-snippets' +-- Plug 'https://github.com/saadparwaiz1/cmp_luasnip' +-- Plug 'https://github.com/nvim-lua/popup.nvim' +-- Plug 'https://github.com/nvim-lua/plenary.nvim' +-- +-- Plug.ends() +-- +-- vim.cmd [[colorscheme gruvbox]] diff --git a/lua/user/keymaps.lua b/lua/user/keymaps.lua new file mode 100644 index 0000000..3c5fc5b --- /dev/null +++ b/lua/user/keymaps.lua @@ -0,0 +1,50 @@ +local opts = { noremap = true, silent = true } + +local term_opts = { silent = true } + +local keymap = vim.api.nvim_set_keymap + +-- Better window navigation +keymap("n", "", "h", opts) +keymap("n", "", "j", opts) +keymap("n", "", "k", opts) +keymap("n", "", "l", opts) + + +-- Remap space as leader key +keymap("n", "", "", opts) +vim.g.mapleader = " " +vim.g.maplocalleader = " " + + +-- NERDTree keybindings +keymap("n", "", ":NERDTree", opts) +keymap("n", "", ":NERDTreeToggle", opts) + +-- Resizing windows (like i3) +keymap("n", "", ":resize -2", opts) +keymap("n", "", ":resize +2", opts) +keymap("n", "", ":vertical resize -2", opts) +keymap("n", "", ":vertical resize +2", opts) + +-- Moving between buffers +keymap("n", "", ":bnext", opts) +keymap("n", "", ":bprevious", opts) + +-- Move text up and down +keymap("n", "", ":m .+1==", opts) +keymap("n", "", ":m .-2==", opts) + +-- Visual mode -- +keymap("v", "<", "", ">gv", opts) + +-- Move text up and down +keymap("v", "", ":m .+1==", opts) +keymap("v", "", ":m .-2==", opts) +keymap("v", "p", '"_dP', opts) + +-- Telescope +-- keymap("n", "f", "lua require 'telescope.builtin'.find_files(require('telescope.themes').get_dropdown({previewer = false}))", opts) +keymap("n", "f", "Telescope find_files", opts) +-- keymap("n", "", "Telescope live_grep", opts) diff --git a/lua/user/lsp/handlers.lua b/lua/user/lsp/handlers.lua new file mode 100644 index 0000000..f5ab1ed --- /dev/null +++ b/lua/user/lsp/handlers.lua @@ -0,0 +1,89 @@ +local M = {} + +M.setup = function() + local signs = { + { name = "DiagnosticSignError", text = "" }, + { name = "DiagnosticSignWarn", text = "" }, + { name = "DiagnosticSignHint", text = "" }, + { name = "DiagnosticSignInfo", text = "" }, + } + + for _, sign in ipairs(signs) do + vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = "" }) + end + + local config = { + virtual_text = false, + signs = { + active = signs, + }, + update_in_insert = true, + underline = true, + severity_sort = true, + float = { + focusable = false, + style = "minimal", + border = "rounded", + source = "always", + header = "", + prefix = "", + }, + } + + vim.diagnostic.config(config) + + vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = "rounded" }) + vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = "rounded" }) +end + +local function lsp_keymaps(bufnr) + local function set_keymap(mode, shortcut, cmd, opts) + return vim.api.nvim_buf_set_keymap(bufnr, mode, shortcut, cmd, opts) + end + + local opts = { noremap = true, silent = true } + set_keymap("n", "gD", "lua vim.lsp.buf.declaration()", opts) + set_keymap("n", "gd", "lua vim.lsp.buf.definition()", opts) + set_keymap("n", "K", "lua vim.lsp.buf.hover()", opts) + set_keymap("n", "gi", "lua vim.lsp.buf.implementation()", opts) + set_keymap("n", "", "lua vim.lsp.buf.signature_help()", opts) + set_keymap("n", "gr", "lua vim.lsp.buf.references()", opts) + set_keymap("n", "[d", 'lua vim.diagnostic.goto_prev({border = "rounded"})', opts) + set_keymap("n", "gl", 'lua vim.diagnostic.open_float({border = "rounded"})', opts) + set_keymap("n", "]d", 'lua vim.diagnostic.goto_next({ border = "rounded" })', opts) + + vim.cmd [[ command! Format execute 'lua vim.lsp.buf.formatting()' ]] +end + +local function lsp_highlight_document(client) + -- Set autocommands conditional on server_capabilities + if client.resolved_capabilities.document_highlight then + vim.api.nvim_exec( + [[ + augroup lsp_document_highlight + autocmd! * + autocmd CursorHold lua vim.lsp.buf.document_highlight() + autocmd CursorMoved lua vim.lsp.buf.clear_references() + augroup END + ]], + false + ) + end +end + + +M.on_attach = function(client, bufnr) + lsp_keymaps(bufnr) + lsp_highlight_document(client) +end + +local capabilities = vim.lsp.protocol.make_client_capabilities() + +local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") +if not status_ok then + return +end + +M.capabilities = cmp_nvim_lsp.update_capabilities(capabilities) + +return M diff --git a/lua/user/lsp/init.lua b/lua/user/lsp/init.lua new file mode 100644 index 0000000..dd11327 --- /dev/null +++ b/lua/user/lsp/init.lua @@ -0,0 +1,8 @@ +local status_ok, _ = pcall(require, "lspconfig") +if not status_ok then + print "Lspconfig not opened correctly" + return +end + +require("user.lsp.lsp-installer") +require("user.lsp.handlers").setup() diff --git a/lua/user/lsp/lsp-installer.lua b/lua/user/lsp/lsp-installer.lua new file mode 100644 index 0000000..7ed988c --- /dev/null +++ b/lua/user/lsp/lsp-installer.lua @@ -0,0 +1,20 @@ +local status_ok, lsp_installer = pcall(require, "nvim-lsp-installer") +if not status_ok then + print "nvim-lsp-installer not opened correctly" + return +end + + +lsp_installer.on_server_ready(function(server) + local opts = { + on_attach = require("user.lsp.handlers").on_attach, + capabilities = require("user.lsp.handlers").capabilities, + } + + if server.name == "sumneko_lua" then + local sumneko_opts = require("user.lsp.settings.sumneko_lua") + opts = vim.tbl_deep_extend("force", sumneko_opts, opts) + end + + server:setup(opts) +end) diff --git a/lua/user/lsp/settings/sumneko_lua.lua b/lua/user/lsp/settings/sumneko_lua.lua new file mode 100644 index 0000000..ea650f0 --- /dev/null +++ b/lua/user/lsp/settings/sumneko_lua.lua @@ -0,0 +1,15 @@ +return { + settings = { + Lua = { + diagnostics = { + globals = { "vim" }, + }, + workspace = { + library = { + [vim.fn.expand("$VIMRUNTIME/lua")] = true, + [vim.fn.expand("config") .. "/lua"] = true, + }, + }, + }, + }, +} diff --git a/lua/user/lsp/test.py b/lua/user/lsp/test.py new file mode 100644 index 0000000..2399fb2 --- /dev/null +++ b/lua/user/lsp/test.py @@ -0,0 +1 @@ +import math.sqrt diff --git a/lua/user/options.lua b/lua/user/options.lua new file mode 100644 index 0000000..293aa31 --- /dev/null +++ b/lua/user/options.lua @@ -0,0 +1,19 @@ +local options = { + completeopt = { "menuone", "noselect" }, + tabstop = 4, + shiftwidth = 2, + scrolloff = 5, + number = true, + relativenumber = true, + wrap = false, + cursorline = true, + numberwidth = 4, + undofile = true, +} + + +for k, v in pairs(options) do + vim.opt[k] = v +end + +vim.cmd [[set matchpairs+=<:>]] diff --git a/lua/user/plugins.lua b/lua/user/plugins.lua new file mode 100644 index 0000000..f07da22 --- /dev/null +++ b/lua/user/plugins.lua @@ -0,0 +1,71 @@ +local fn = vim.fn + +-- Automatically install pakcer +local install_path = fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim" +if fn.empty(fn.glob(install_path)) > 0 then + PACKER_BOOTSTRAP = fn.system { + "git", + "clone", + "--depth", + "1", + "https://github.com/wbthomason/packer.nvim", + install_path, + } + print "Installing packer close and reopen Neovim..." + vim.cmd [[packadd packer.nvim]] +end + +vim.cmd [[ + augroup packer_user_config + autocmd! + autocmd BufWritePost plugins.lua source | PackerSync + augroup end +]] + +local status_ok, packer = pcall(require, "packer") +if not status_ok then + return +end + +packer.init { + display = { + open_fn = function() + return require("packer.util").float { border = "rounded" } + end, + }, +} + +return packer.startup(function(use) + use "wbthomason/packer.nvim" + use "nvim-lua/popup.nvim" + use "nvim-lua/plenary.nvim" + + use "vim-airline/vim-airline" + use "rafi/awesome-vim-colorschemes" + use "preservim/nerdtree" + + use 'hrsh7th/nvim-cmp' + use 'hrsh7th/cmp-nvim-lsp' + use 'hrsh7th/cmp-nvim-lua' + use 'hrsh7th/cmp-buffer' + use 'hrsh7th/cmp-path' + use 'hrsh7th/cmp-cmdline' + + use 'saadparwaiz1/cmp_luasnip' + use 'L3MON4D3/LuaSnip' + use 'rafamadriz/friendly-snippets' + + use 'neovim/nvim-lspconfig' + use 'williamboman/nvim-lsp-installer' + + use 'nvim-telescope/telescope.nvim' + use 'nvim-telescope/telescope-media-files.nvim' + + use { + 'nvim-treesitter/nvim-treesitter', + run = ":TSUpdate", + } + if PACKER_BOOTSTRAP then + require("packer").sync() + end +end) diff --git a/lua/user/telescope.lua b/lua/user/telescope.lua new file mode 100644 index 0000000..d27069b --- /dev/null +++ b/lua/user/telescope.lua @@ -0,0 +1,83 @@ +local status_ok, telescope = pcall(require, "telescope") +if not status_ok then + print "Error while loading telescope" + return +end + + +telescope.load_extension('media_files') + +local actions = require "telescope.actions" + +telescope.setup { + defaults = { + prompt_prefix = " ", + selection_caret = " ", + path_display = { "smart" }, + + mappings = { + i = { + [""] = actions.cycle_history_next, + [""] = actions.cycle_history_prev, + + [""] = actions.move_selection_next, + [""] = actions.move_selection_previous, + + [""] = actions.close, + + [""] = actions.move_selection_next, + [""] = actions.move_selection_previous, + + [""] = actions.select_default, + [""] = actions.select_horizontal, + [""] = actions.select_vertical, + [""] = actions.select_tab, + + [""] = actions.preview_scrolling_up, + [""] = actions.preview_scrolling_down, + + [""] = actions.results_scrolling_up, + [""] = actions.results_scrolling_down, + + [""] = actions.toggle_selection + actions.move_selection_worse, + [""] = actions.toggle_selection + actions.move_selection_better, + [""] = actions.send_to_qflist + actions.open_qflist, + [""] = actions.send_selected_to_qflist + actions.open_qflist, + [""] = actions.complete_tag, + [""] = actions.which_key, -- keys from pressing + }, + + n = { + [""] = actions.close, + [""] = actions.select_default, + [""] = actions.select_horizontal, + [""] = actions.select_vertical, + [""] = actions.select_tab, + + [""] = actions.toggle_selection + actions.move_selection_worse, + [""] = actions.toggle_selection + actions.move_selection_better, + [""] = actions.send_to_qflist + actions.open_qflist, + [""] = actions.send_selected_to_qflist + actions.open_qflist, + + ["j"] = actions.move_selection_next, + ["k"] = actions.move_selection_previous, + ["H"] = actions.move_to_top, + ["M"] = actions.move_to_middle, + ["L"] = actions.move_to_bottom, + + [""] = actions.move_selection_next, + [""] = actions.move_selection_previous, + ["gg"] = actions.move_to_top, + ["G"] = actions.move_to_bottom, + + [""] = actions.preview_scrolling_up, + [""] = actions.preview_scrolling_down, + + [""] = actions.results_scrolling_up, + [""] = actions.results_scrolling_down, + + ["?"] = actions.which_key, + }, + }, + }, +} diff --git a/lua/user/treesitter.lua b/lua/user/treesitter.lua new file mode 100644 index 0000000..ede8999 --- /dev/null +++ b/lua/user/treesitter.lua @@ -0,0 +1,16 @@ +local status_ok, configs = pcall(require, "nvim-treesitter.configs") +if not status_ok then + return +end + +configs.setup { + ensure_installed = "maintained", + sync_install = false, + ignore_install = { "" }, + highlight = { + enable = true, + disable = { "" }, + additional_vim_regex_highlighting = true, + }, + indent = { enable = true, disable = { "yaml" } }, +} diff --git a/lua/user/vimplug.lua b/lua/user/vimplug.lua new file mode 100644 index 0000000..f182a4b --- /dev/null +++ b/lua/user/vimplug.lua @@ -0,0 +1,65 @@ +local configs = { + lazy = {}, + start = {} +} + +local Plug = { + begin = vim.fn['plug#begin'], + + -- "end" is a keyword, need something else + ends = function() + vim.fn['plug#end']() + + for i, config in pairs(configs.start) do + config() + end + end +} + +-- Not a fan of global functions, but it'll work better +-- for the people that will copy/paste this +_G.VimPlugApplyConfig = function(plugin_name) + local fn = configs.lazy[plugin_name] + if type(fn) == 'function' then fn() end +end + +local plug_name = function(repo) + return repo:match("^[%w-]+/([%w-_.]+)$") +end + +-- "Meta-functions" +local meta = { + + -- Function call "operation" + __call = function(self, repo, opts) + opts = opts or vim.empty_dict() + + -- we declare some aliases for `do` and `for` + opts['do'] = opts.run + opts.run = nil + + opts['for'] = opts.ft + opts.ft = nil + + vim.call('plug#', repo, opts) + + -- Add basic support to colocate plugin config + if type(opts.config) == 'function' then + local plugin = opts.as or plug_name(repo) + + if opts['for'] == nil and opts.on == nil then + configs.start[plugin] = opts.config + else + configs.lazy[plugin] = opts.config + + local user_cmd = [[ autocmd! User %s ++once lua VimPlugApplyConfig('%s') ]] + vim.cmd(user_cmd:format(plugin, plugin)) + end + + end + end +} + +-- Meta-tables are awesome +return setmetatable(Plug, meta) + -- cgit v1.2.3