From: bernie Date: Mon, 1 Aug 2022 04:36:18 +0000 (-0400) Subject: Edit page nwiz.lua X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=edefe79e7586b7d8c01a3a3366223b1ad95a388b;p=wiki.git Edit page nwiz.lua --- diff --git a/nwiz.lua b/nwiz.lua new file mode 100644 index 0000000..31a1af1 --- /dev/null +++ b/nwiz.lua @@ -0,0 +1,346 @@ +-- +-- “This is the editor of a Real Coder. +-- Not as clumsy as a full blown IDE. +-- An elegant editor for a more civilized age.” +-- ― Bernie Innocenti, https://codewiz.org +-- + +-- Set this to replace GitHub default URLs +-- local github_url = 'https://github.com/' +local github_url = 'git@github.com:' + +-- Bootstrap Packer if not already installed +local fn = vim.fn +local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim' +if fn.empty(fn.glob(install_path)) > 0 then + local packer_url = github_url .. "wbthomason/packer.nvim" + print("Bootstrapping packer from ", packer_url) + packer_bootstrap = fn.system({'git', 'clone', '--depth', '1', packer_url, install_path}) +end + +require('packer').startup({function(use) + use 'wbthomason/packer.nvim' + use { 'nvim-treesitter/nvim-treesitter', + run = function() require('nvim-treesitter.install').update({ with_sync = true }) end, } + use { 'nvim-telescope/telescope.nvim', requires = { {'nvim-lua/plenary.nvim'} } } + use { 'nvim-lualine/lualine.nvim', requires = { 'kyazdani42/nvim-web-devicons', opt = true } } + use { 'kyazdani42/nvim-tree.lua', requires = { 'kyazdani42/nvim-web-devicons', }, tag = 'nightly' } + use 'dstein64/nvim-scrollview' + + use 'neovim/nvim-lspconfig' + use 'hrsh7th/cmp-nvim-lsp' + use 'hrsh7th/cmp-buffer' + use 'hrsh7th/cmp-path' + use 'hrsh7th/cmp-cmdline' + use 'hrsh7th/nvim-cmp' + use 'hrsh7th/cmp-vsnip' + use 'hrsh7th/vim-vsnip' + -- use 'hrsh7th/cmp-nvim-lsp-signature-help' + use "ray-x/lsp_signature.nvim" + + if packer_bootstrap then + -- FIXME: this runs asynchronously, causing the rest of the + -- lua script to fail on first run + require('packer').sync() + end +end, +config = { + git = { default_url_format = github_url .. '%s.git' } +}}) + +-- nvim-treesitter -- + +if github_url ~= nil then + -- Replace GitHub url for all treesitter plugins + for _, config in pairs(require("nvim-treesitter.parsers").get_parser_configs()) do + config.install_info.url = config.install_info.url:gsub("https://github.com/", github_url) + end +end + +require("nvim-treesitter.install").prefer_git = true +require("nvim-treesitter.configs").setup { + highlight = { + enable = true, + }, +} + +local opts = { noremap=true, silent=true } +local map = vim.api.nvim_set_keymap + +require("telescope").setup() +map('n', '', 'Telescope git_files', opts) +map('n', 'ff', 'Telescope find_files', opts) +map('n', 'fg', 'Telescope live_grep', opts) +map('n', 'fb', 'Telescope buffers', opts) +map('n', 'fh', 'Telescope help_tags', opts) + +require("nvim-tree").setup() +map('n', 'o', ':NvimTreeFocus', opts) +map('n', '', ':NvimTreeClose', opts) +map('n', '', ':NvimTreeFindFile', opts) + +require('lualine').setup { + options = { + theme = 'codedark', + section_separators = { left = '', right = '' }, + component_separators = { left = '', right = '' }, + -- section_separators = '', component_separators = '', + show_filename_only = false, + globalstatus = false, + }, + sections = { + lualine_a = { + { 'mode', fmt = function(str) return str:sub(1,1) end } + }, + lualine_b = { + { 'branch', fmt = function(branch) return branch == 'master' and '' or branch end }, + }, + lualine_c = { + { 'filename', path = 1, }, + }, + lualine_x = { + { 'filetype', icon_only = true, }, + }, + lualine_y = {}, + }, +} + +-- nvim-cmp -- + +local kind_icons = { + Text = "", + Method = "", + Function = "", + Constructor = "", + Field = "", + Variable = "", + Class = "ﴯ", + Interface = "", + Module = "", + Property = "ﰠ", + Unit = "", + Value = "", + Enum = "", + Keyword = "", + Snippet = "", + Color = "", + File = "", + Reference = "", + Folder = "", + EnumMember = "", + Constant = "", + Struct = "", + Event = "", + Operator = "", + TypeParameter = "" +} + +local has_words_before = function() + local line, col = 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 feedkey = function(key, mode) + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), mode, true) +end + +local cmp = require'cmp' +cmp.setup({ + -- view = { entries = "native" }, + formatting = { + fields = { + "kind", + "abbr", + "menu", + }, + format = function(entry, vim_item) + vim_item.kind = kind_icons[vim_item.kind] + local max_width = 25 + if vim_item.menu ~= nil and string.len(vim_item.menu) > max_width then + vim_item.menu = string.sub(vim_item.menu, 1, max_width - 1) .. "…" + end + return vim_item + end, + }, + snippet = { + -- REQUIRED - you must specify a snippet engine + expand = function(args) + vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. + end, + }, + mapping = cmp.mapping.preset.insert({ + [''] = 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 = true }), + + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif vim.fn["vsnip#available"](1) == 1 then + feedkey("(vsnip-expand-or-jump)", "") + elseif has_words_before() then + cmp.complete() + else + fallback() -- Sends a already mapped key. In this case, it's probably ``. + end + end, { "i", "s" }), + + [""] = cmp.mapping(function() + if cmp.visible() then + cmp.select_prev_item() + elseif vim.fn["vsnip#jumpable"](-1) == 1 then + feedkey("(vsnip-jump-prev)", "") + end + end, { "i", "s" }), + }), + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + -- { name = 'nvim_lsp_signature_help', }, + }, { + { name = 'vsnip' }, + -- { name = 'buffer' }, + }) +}) + +-- cmp.setup.cmdline('/', { +-- mapping = cmp.mapping.preset.cmdline(), +-- completion = { +-- keyword_length = 2, +-- }, +-- sources = { +-- { name = 'buffer' } +-- } +--}) + +cmp.setup.cmdline(':', { + mapping = cmp.mapping.preset.cmdline(), + completion = { + keyword_length = 2, + }, + sources = cmp.config.sources({ + { name = 'path' } + }, { + { name = 'cmdline' } + }) +}) + + +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities) + +-- Disable snippets support for rust-analyzer because it spams the completion +capabilities.textDocument.completion.completionItem.snippetSupport = false +-- print(vim.inspect(capabilities)) + +-- lsp-config -- + +-- vim.lsp.set_log_level("debug") +vim.diagnostic.config({ + underline = true, + virtual_text = false, + update_in_insert = false, + float = { + focusable = false, + style = 'minimal', + border = 'rounded', + source = 'always', + header = '', + prefix = '', + } +}) + +local on_attach = function(client, bufnr) + local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end + local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end + + buf_set_option("formatexpr", "v:lua.vim.lsp.formatexpr()") + buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc") + buf_set_option("tagfunc", "v:lua.vim.lsp.tagfunc") + + vim.opt.completeopt = {"menu", "menuone", "noinsert"} + + require "lsp_signature".on_attach({ + hi_parameter = "IncSearch" + }) + + local opts = { noremap=true, silent=true } + -- See `:help vim.lsp.*` for documentation on any of the below functions + buf_set_keymap('n', 'gD', 'lua vim.lsp.buf.declaration()', opts) + buf_set_keymap('n', '', 'lua vim.lsp.buf.declaration()', opts) + buf_set_keymap('n', 'gd', 'lua vim.lsp.buf.definition()', opts) + buf_set_keymap('n', 'K', 'lua vim.lsp.buf.hover()', opts) + buf_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()', opts) + buf_set_keymap('n', '', 'lua vim.lsp.buf.signature_help()', opts) + buf_set_keymap('n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()', opts) + buf_set_keymap('n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()', opts) + buf_set_keymap('n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opts) + buf_set_keymap('n', 'D', 'lua vim.lsp.buf.type_definition()', opts) + buf_set_keymap('n', 'rn', 'lua vim.lsp.buf.rename()', opts) + buf_set_keymap('n', 'ca', 'lua vim.lsp.buf.code_action()', opts) + buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', opts) + + buf_set_keymap('n', 'e', 'lua vim.diagnostic.open_float()', opts) + buf_set_keymap('n', 'q', 'lua vim.diagnostic.setloclist()', opts) + buf_set_keymap('n', '[d', 'lua vim.diagnostic.goto_prev()', opts) + buf_set_keymap('n', ']d', 'lua vim.diagnostic.goto_next()', opts) + + buf_set_keymap('n', "=", "lua vim.lsp.buf.formatting()", opts) + buf_set_keymap('v', "=", "lua vim.lsp.buf.formatting()", opts) + buf_set_keymap('n', "gq", "lua vim.lsp.buf.formatting()", opts) + buf_set_keymap('v', "gq", "lua vim.lsp.buf.formatting()", opts) +end + +local lspconfig = require'lspconfig'; +lspconfig.rust_analyzer.setup{ + on_attach = on_attach, + capabilities = capabilities, +} +lspconfig.tsserver.setup{ + on_attach = on_attach, + capabilities = capabilities, +} +lspconfig.clangd.setup{ + on_attach = on_attach, + capabilities = capabilities, + -- cmd = { vim.loop.os_homedir() .. "clangd", "--background-index" }, + cmd = { "clangd", "--background-index" }, + root_dir = lspconfig.util.root_pattern("compile_commands.json", "compile_flags.txt"), +} +lspconfig.pylsp.setup{ + on_attach = on_attach, + capabilities = capabilities, + settings = { + pylsp = { + configurationSources = {"pylint"}, + plugins = { + autopep8 = { enabled = true }, + mypy = { enabled = true }, + pylint = { enabled = true }, + flake8 = { enabled = false }, + pycodestyle = { enabled = false }, + pyflakes = { enabled = false }, + } + } + } +} + +-- Alt-key window navication and more +map('t', '', 'h', opts) +map('t', '', 'j', opts) +map('t', '', 'k', opts) +map('t', '', 'l', opts) +map('i', '', 'h', opts) +map('i', '', 'j', opts) +map('i', '', 'k', opts) +map('i', '', 'l', opts) +map('n', '', 'h', opts) +map('n', '', 'j', opts) +map('n', '', 'k', opts) +map('n', '', 'l', opts) +