diff --git a/Makefile b/Makefile index 99aab756e..37c9591c9 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,8 @@ install \ install-brew \ install-git \ + install-nvim \ + install-nvim-tree-sitter \ install-sh \ install-tmux \ install-vim \ @@ -104,10 +106,16 @@ install-npm: cp -pR -- npm/npmrc $(XDG_CONFIG_HOME)/npm/npmrc cp -p -- npm/profile.d/* $(HOME)/.profile.d/ -install-nvim: lint-lua +install-nvim: lint-lua install-nvim-tree-sitter mkdir -p -- $(XDG_CONFIG_HOME)/nvim cp -pR -- nvim/ $(XDG_CONFIG_HOME)/nvim/ +# NOTE: Requires luarocks to be installed. +install-nvim-tree-sitter: + luarocks --force-lock --lua-version=5.1 --tree=$(XDG_DATA_HOME)/nvim/rocks --dev install tree-sitter-lua + luarocks --force-lock --lua-version=5.1 --tree=$(XDG_DATA_HOME)/nvim/rocks --dev install tree-sitter-vimdoc + luarocks --force-lock --lua-version=5.1 --tree=$(XDG_DATA_HOME)/nvim/rocks --dev install tree-sitter-markdown + install-sh: lint-sh cp -p -- sh/profile $(HOME)/.profile mkdir -p -- $(HOME)/.profile.d/ diff --git a/README.md b/README.md index b4e03e0ef..f9b17f36f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ # .dotfiles My dotfiles, managed with `make`. + +## Neovim + +### Install Treesitter stuff + +```sh +luarocks --force-lock --lua-version=5.1 --tree=$HOME/.local/share/nvim/rocks --dev install tree-sitter-* +``` diff --git a/homebrew/personal.Brewfile b/homebrew/personal.Brewfile index abac422a2..a363a2992 100644 --- a/homebrew/personal.Brewfile +++ b/homebrew/personal.Brewfile @@ -24,6 +24,8 @@ brew "jq" brew "lua-language-server" # Tool for linting and static analysis of Lua code brew "luacheck" +# Package manager for the Lua programming language +brew "luarocks" # Macro processing language brew "m4", link: true # Mac App Store command-line interface diff --git a/nvim/after/ftplugin/lua.lua b/nvim/after/ftplugin/lua.lua index 79afbfdf0..2e47be527 100644 --- a/nvim/after/ftplugin/lua.lua +++ b/nvim/after/ftplugin/lua.lua @@ -1,5 +1,17 @@ -- Tabs should have 4 space width. vim.opt_local.tabstop = 4 --- Use luacheck as compiler for sh files. +-- Use luacheck as compiler for lua files. vim.cmd([[compiler luacheck]]) + +-- Show line numbers by default. +vim.o.number = true + +-- Enable treesitter for syntax highlighting. +pcall(vim.treesitter.start) + +-- Enable treesitter for folding. +vim.o.foldlevel = 99 +vim.o.foldlevelstart = 99 +vim.o.foldmethod = "expr" +vim.o.foldexpr = "v:lua.vim.treesitter.foldexpr()" diff --git a/nvim/after/plugin/conform.lua b/nvim/after/plugin/conform.lua index 6193fb232..fcd975342 100644 --- a/nvim/after/plugin/conform.lua +++ b/nvim/after/plugin/conform.lua @@ -1,12 +1,12 @@ -local options = { - formatters_by_ft = { - lua = { "stylua" }, - typescript = { { "prettierd", "prettier" }, "eslint_d" }, - }, -} - -require("conform").setup(options) - --- This should be okay to set globally, as will default back to the LSP --- formatexpr if present and then to nothing. -vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" +-- local options = { +-- formatters_by_ft = { +-- lua = { "stylua" }, +-- typescript = { { "prettierd", "prettier" }, "eslint_d" }, +-- }, +-- } +-- +-- require("conform").setup(options) +-- +-- -- This should be okay to set globally, as will default back to the LSP +-- -- formatexpr if present and then to nothing. +-- vim.o.formatexpr = "v:lua.require'conform'.formatexpr()" diff --git a/nvim/after/plugin/lint.lua b/nvim/after/plugin/lint.lua index fd71b4c04..54428fdd0 100644 --- a/nvim/after/plugin/lint.lua +++ b/nvim/after/plugin/lint.lua @@ -1,11 +1,11 @@ -require("lint").linters_by_ft = { - lua = { "luacheck" }, - typescript = { "eslint" }, -} - --- This will run the linters on save. -vim.api.nvim_create_autocmd({ "BufWritePost" }, { - callback = function() - require("lint").try_lint() - end, -}) +-- require("lint").linters_by_ft = { +-- lua = { "luacheck" }, +-- typescript = { "eslint" }, +-- } +-- +-- -- This will run the linters on save. +-- vim.api.nvim_create_autocmd({ "BufWritePost" }, { +-- callback = function() +-- require("lint").try_lint() +-- end, +-- }) diff --git a/nvim/after/plugin/lsp.lua b/nvim/after/plugin/lsp.lua index a7570f065..d509e714f 100644 --- a/nvim/after/plugin/lsp.lua +++ b/nvim/after/plugin/lsp.lua @@ -1,69 +1,69 @@ -- Setup language servers. -local lspconfig = require("lspconfig") -lspconfig.lua_ls.setup({ - on_init = function(client) - local path = client.workspace_folders[1].name - if vim.loop.fs_stat(path .. "/.luarc.json") or vim.loop.fs_stat(path .. "/.luarc.jsonc") then - return - end - - client.config.settings.Lua = vim.tbl_deep_extend("force", client.config.settings.Lua, { - runtime = { - -- Tell the language server which version of Lua you're using - -- (most likely LuaJIT in the case of Neovim) - version = "LuaJIT", - }, - -- Make the server aware of Neovim runtime files - workspace = { - checkThirdParty = false, - library = { - vim.env.VIMRUNTIME, - -- Depending on the usage, you might want to add additional paths here. - -- "${3rd}/luv/library" - -- "${3rd}/busted/library", - }, - -- or pull in all of 'runtimepath'. NOTE: this is a lot slower - -- library = vim.api.nvim_get_runtime_file("", true) - }, - }) - end, - settings = { - Lua = {}, - }, -}) -lspconfig.tsserver.setup({}) - --- Global mappings. --- See `:help vim.diagnostic.*` for documentation on any of the below functions -vim.keymap.set("n", "e", vim.diagnostic.open_float) -vim.keymap.set("n", "[d", vim.diagnostic.goto_prev) -vim.keymap.set("n", "]d", vim.diagnostic.goto_next) -vim.keymap.set("n", "q", vim.diagnostic.setloclist) - --- Use LspAttach autocommand to only map the following keys --- after the language server attaches to the current buffer -vim.api.nvim_create_autocmd("LspAttach", { - group = vim.api.nvim_create_augroup("UserLspConfig", {}), - callback = function(ev) - -- Enable completion triggered by - vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc" - - -- Buffer local mappings. - -- See `:help vim.lsp.*` for documentation on any of the below functions - local opts = { buffer = ev.buf } - vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts) - vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) - vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) - vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts) - vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) - vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, opts) - vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, opts) - vim.keymap.set("n", "wl", function() - print(vim.inspect(vim.lsp.buf.list_workspace_folders())) - end, opts) - vim.keymap.set("n", "D", vim.lsp.buf.type_definition, opts) - vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) - vim.keymap.set({ "n", "v" }, "ca", vim.lsp.buf.code_action, opts) - vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) - end, -}) +-- local lspconfig = require("lspconfig") +-- lspconfig.lua_ls.setup({ +-- on_init = function(client) +-- local path = client.workspace_folders[1].name +-- if vim.loop.fs_stat(path .. "/.luarc.json") or vim.loop.fs_stat(path .. "/.luarc.jsonc") then +-- return +-- end +-- +-- client.config.settings.Lua = vim.tbl_deep_extend("force", client.config.settings.Lua, { +-- runtime = { +-- -- Tell the language server which version of Lua you're using +-- -- (most likely LuaJIT in the case of Neovim) +-- version = "LuaJIT", +-- }, +-- -- Make the server aware of Neovim runtime files +-- workspace = { +-- checkThirdParty = false, +-- library = { +-- vim.env.VIMRUNTIME, +-- -- Depending on the usage, you might want to add additional paths here. +-- -- "${3rd}/luv/library" +-- -- "${3rd}/busted/library", +-- }, +-- -- or pull in all of 'runtimepath'. NOTE: this is a lot slower +-- -- library = vim.api.nvim_get_runtime_file("", true) +-- }, +-- }) +-- end, +-- settings = { +-- Lua = {}, +-- }, +-- }) +-- lspconfig.tsserver.setup({}) +-- +-- -- Global mappings. +-- -- See `:help vim.diagnostic.*` for documentation on any of the below functions +-- vim.keymap.set("n", "e", vim.diagnostic.open_float) +-- vim.keymap.set("n", "[d", vim.diagnostic.goto_prev) +-- vim.keymap.set("n", "]d", vim.diagnostic.goto_next) +-- vim.keymap.set("n", "q", vim.diagnostic.setloclist) +-- +-- -- Use LspAttach autocommand to only map the following keys +-- -- after the language server attaches to the current buffer +-- vim.api.nvim_create_autocmd("LspAttach", { +-- group = vim.api.nvim_create_augroup("UserLspConfig", {}), +-- callback = function(ev) +-- -- Enable completion triggered by +-- vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc" +-- +-- -- Buffer local mappings. +-- -- See `:help vim.lsp.*` for documentation on any of the below functions +-- local opts = { buffer = ev.buf } +-- vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts) +-- vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts) +-- vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) +-- vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts) +-- vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) +-- vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, opts) +-- vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, opts) +-- vim.keymap.set("n", "wl", function() +-- print(vim.inspect(vim.lsp.buf.list_workspace_folders())) +-- end, opts) +-- vim.keymap.set("n", "D", vim.lsp.buf.type_definition, opts) +-- vim.keymap.set("n", "rn", vim.lsp.buf.rename, opts) +-- vim.keymap.set({ "n", "v" }, "ca", vim.lsp.buf.code_action, opts) +-- vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) +-- end, +-- }) diff --git a/nvim/after/plugin/treesitter.lua b/nvim/after/plugin/treesitter.lua index 7c4ff3c61..41c1a4639 100644 --- a/nvim/after/plugin/treesitter.lua +++ b/nvim/after/plugin/treesitter.lua @@ -1,25 +1,25 @@ -local ok, treesitter_configs = pcall(require, "nvim-treesitter.configs") -if not ok then - return -end -treesitter_configs.setup({ - -- A list of parser names, or "all" - ensure_installed = { "lua" }, - - -- Install parsers synchronously (only applied to `ensure_installed`) - sync_install = false, - - -- Automatically install missing parsers when entering buffer - -- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally - auto_install = true, - - highlight = { - enable = true, - - -- Setting this to true will run `:h syntax` and tree-sitter at the same time. - -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation). - -- Using this option may slow down your editor, and you may see some duplicate highlights. - -- Instead of true it can also be a list of languages - additional_vim_regex_highlighting = false, - }, -}) +-- local ok, treesitter_configs = pcall(require, "nvim-treesitter.configs") +-- if not ok then +-- return +-- end +-- treesitter_configs.setup({ +-- -- A list of parser names, or "all" +-- ensure_installed = { "lua" }, +-- +-- -- Install parsers synchronously (only applied to `ensure_installed`) +-- sync_install = false, +-- +-- -- Automatically install missing parsers when entering buffer +-- -- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally +-- auto_install = true, +-- +-- highlight = { +-- enable = true, +-- +-- -- Setting this to true will run `:h syntax` and tree-sitter at the same time. +-- -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation). +-- -- Using this option may slow down your editor, and you may see some duplicate highlights. +-- -- Instead of true it can also be a list of languages +-- additional_vim_regex_highlighting = false, +-- }, +-- }) diff --git a/nvim/lua/dcp/init.lua b/nvim/lua/dcp/init.lua index c09a59625..05efc924c 100644 --- a/nvim/lua/dcp/init.lua +++ b/nvim/lua/dcp/init.lua @@ -4,3 +4,5 @@ require("dcp.set-background") require("dcp.keymaps") require("dcp.options") +require("dcp.lsp") +require("dcp.treesitter") diff --git a/nvim/lua/dcp/lsp.lua b/nvim/lua/dcp/lsp.lua new file mode 100644 index 000000000..aa28066ae --- /dev/null +++ b/nvim/lua/dcp/lsp.lua @@ -0,0 +1,95 @@ +-- Create an event handler for the FileType autocommand +vim.api.nvim_create_autocmd("FileType", { + -- This handler will fire when the buffer's 'filetype' is "lua" + pattern = "lua", + callback = function(_args) + vim.lsp.start({ + name = "lua-language-server", + cmd = { "lua-language-server" }, + + -- TODO: Update this comment. + -- Set the "root directory" to the parent directory of the file in the + -- current buffer (`args.buf`) that contains either a "setup.py" or a + -- "pyproject.toml" file. Files that share a root directory will reuse + -- the connection to the same LSP server. + -- root_dir = vim.fs.root(args.buf, { "setup.py", "pyproject.toml" }), + root_dir = vim.fs.root(0, { + ".luarc.json", + ".luarc.jsonc", + ".luacheckrc", + ".stylua.toml", + "stylua.toml", + "selene.toml", + "selene.yml", + ".git", + }), + + on_init = function(client) + local path = vim.tbl_get(client, "workspace_folders", 1, "name") + if not path then + vim.print("no workspace") + return + end + client.settings = vim.tbl_deep_extend("force", client.settings, { + Lua = { + runtime = { + version = "LuaJIT", + }, + -- Make the server aware of Neovim runtime files + workspace = { + checkThirdParty = false, + library = { + vim.env.VIMRUNTIME, + -- Depending on the usage, you might want to add additional paths here. + -- "${3rd}/luv/library" + -- "${3rd}/busted/library", + }, + -- or pull in all of 'runtimepath'. NOTE: this is a lot slower + -- library = vim.api.nvim_get_runtime_file("", true) + }, + }, + }) + end, + }) + end, +}) + +vim.api.nvim_create_autocmd("LspAttach", { + callback = function(args) + local client = vim.lsp.get_client_by_id(args.data.client_id) + if not client then + vim.print("no client") + return + end + if client.supports_method("textDocument/implementation") then + -- Create a keymap for vim.lsp.buf.implementation + vim.api.nvim_buf_set_keymap(args.buf, "n", "gi", "lua vim.lsp.buf.implementation()", { + noremap = true, + silent = true, + }) + end + + if client.supports_method("textDocument/completion") then + -- Enable auto-completion + vim.lsp.completion.enable(true, client.id, args.buf, { autotrigger = false }) + end + + if client.supports_method("textDocument/formatting") then + -- Format the current buffer on save + vim.api.nvim_create_autocmd("BufWritePre", { + buffer = args.buf, + callback = function() + vim.lsp.buf.format({ bufnr = args.buf, id = client.id }) + end, + }) + end + + if client.supports_method("textDocument/formatting") then + vim.lsp.handlers["textDocument/publishDiagnostics"] = + vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, { + -- Enable signs + signs = true, + }) + end + end, +}) diff --git a/nvim/lua/dcp/treesitter.lua b/nvim/lua/dcp/treesitter.lua new file mode 100644 index 000000000..d14a0bba4 --- /dev/null +++ b/nvim/lua/dcp/treesitter.lua @@ -0,0 +1,18 @@ +vim.opt.runtimepath:append( + vim.fs.joinpath( + vim.fn.stdpath("data") --[[@as string]], + "rocks", + "lib", + "luarocks", + "rocks-5.1", + "tree-sitter-*", + "*" + ) +) + +-- -- Move this to ftplugin file and maybe setup helper function to enable TS for syntax and folding. +-- vim.api.nvim_create_autocmd("FileType", { +-- callback = function() +-- pcall(vim.treesitter.start) +-- end, +-- }) diff --git a/nvim/pack/bundle/start/conform.nvim b/nvim/pack/bundle/start/conform.nvim deleted file mode 160000 index cd75be867..000000000 --- a/nvim/pack/bundle/start/conform.nvim +++ /dev/null @@ -1 +0,0 @@ -Subproject commit cd75be867f2331b22905f47d28c0c270a69466aa diff --git a/nvim/pack/bundle/start/copilot.vim b/nvim/pack/bundle/start/copilot.vim deleted file mode 160000 index 0668308e6..000000000 --- a/nvim/pack/bundle/start/copilot.vim +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 0668308e68b0ac28b332b204b469fbe04601536a diff --git a/nvim/pack/bundle/start/dispatch b/nvim/pack/bundle/start/dispatch deleted file mode 160000 index 4c695bc05..000000000 --- a/nvim/pack/bundle/start/dispatch +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4c695bc052cad2ae6b980aebbe48d046466e27ae diff --git a/nvim/pack/bundle/start/nvim-lint b/nvim/pack/bundle/start/nvim-lint deleted file mode 160000 index efc6fc83f..000000000 --- a/nvim/pack/bundle/start/nvim-lint +++ /dev/null @@ -1 +0,0 @@ -Subproject commit efc6fc83f0772283e064c53a8f9fb5645bde0bc0 diff --git a/nvim/pack/bundle/start/nvim-lspconfig b/nvim/pack/bundle/start/nvim-lspconfig deleted file mode 160000 index cf97d2485..000000000 --- a/nvim/pack/bundle/start/nvim-lspconfig +++ /dev/null @@ -1 +0,0 @@ -Subproject commit cf97d2485fc3f6d4df1b79a3ea183e24c272215e diff --git a/nvim/pack/bundle/start/nvim-treesitter b/nvim/pack/bundle/start/nvim-treesitter deleted file mode 160000 index fa611f612..000000000 --- a/nvim/pack/bundle/start/nvim-treesitter +++ /dev/null @@ -1 +0,0 @@ -Subproject commit fa611f612a7b04c239d07f61ba80e09cb95c5af4 diff --git a/nvim/pack/bundle/start/projectionist b/nvim/pack/bundle/start/projectionist deleted file mode 160000 index 3736bd4a5..000000000 --- a/nvim/pack/bundle/start/projectionist +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3736bd4a5f23b30821cbb892385bb1f1b0088cfc