summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-10-15 17:44:27 -0500
committerToby Vincent <tobyv13@gmail.com>2022-10-15 17:44:27 -0500
commitc527a733709d95ef363e4a9cbed7a2f1d14f5043 (patch)
tree0531088dbf2ce13c8962d0c1100244caa8c45a19
parentb002ac2e7cb32dbfda1bc6130d4c1cef30400f55 (diff)
feat(nvim): improve utils and clean up workspace errors
-rw-r--r--nvim/.config/nvim/lua/tobyvin/autocmds.lua30
-rw-r--r--nvim/.config/nvim/lua/tobyvin/plugins/dap.lua85
-rw-r--r--nvim/.config/nvim/lua/tobyvin/utils.lua10
-rw-r--r--nvim/.config/nvim/lua/tobyvin/utils/fs.lua62
-rw-r--r--nvim/.config/nvim/lua/tobyvin/utils/init.lua12
5 files changed, 124 insertions, 75 deletions
diff --git a/nvim/.config/nvim/lua/tobyvin/autocmds.lua b/nvim/.config/nvim/lua/tobyvin/autocmds.lua
index 3fbee55..5d6179f 100644
--- a/nvim/.config/nvim/lua/tobyvin/autocmds.lua
+++ b/nvim/.config/nvim/lua/tobyvin/autocmds.lua
@@ -46,8 +46,8 @@ M.setup = function()
vim.api.nvim_create_autocmd("FileType", {
group = augroup_fmt,
pattern = { "sh", "zsh", "xml", "html", "xhtml", "css", "scss", "javascript", "lua", "dart", "markdown" },
- callback = function()
- vim.opt_local.tabstop = 2
+ callback = function(args)
+ vim.bo[args.buf].tabstop = 2
end,
desc = "Set tabstop",
})
@@ -57,8 +57,8 @@ M.setup = function()
vim.api.nvim_create_autocmd("FileType", {
group = augroup_view,
pattern = { "qf", "help", "gitcommit", "gitrebase" },
- callback = function()
- vim.opt_local.buflisted = false
+ callback = function(args)
+ vim.bo[args.buf].buflisted = false
end,
desc = "Set buffer as unlisted",
})
@@ -66,15 +66,27 @@ M.setup = function()
vim.api.nvim_create_autocmd("FileType", {
group = augroup_view,
pattern = "help",
- callback = function()
- vim.opt_local.wrap = true
- vim.opt_local.textwidth = 120
- vim.opt_local.colorcolumn = nil
+ callback = function(args)
+ vim.wo.wrap = true
+ vim.bo[args.buf].textwidth = 120
+ vim.wo.colorcolumn = nil
vim.cmd("wincmd L")
- vim.cmd("vertical resize " .. vim.opt.textwidth:get())
+ vim.api.nvim_win_set_width(0, vim.o.textwidth)
end,
desc = "Setup and resize help window",
})
+
+ vim.api.nvim_create_autocmd("FileType", {
+ group = vim.api.nvim_create_augroup("tobyvin_reload", { clear = true }),
+ pattern = "lua",
+ callback = function(args)
+ local utils = require("tobyvin.utils")
+ if utils.fs.module_from_path(args.file) then
+ vim.keymap.set("n", "<leader>R", utils.fs.reload, { desc = "reload lua module" })
+ end
+ end,
+ desc = "Setup lua module reloader",
+ })
end
return M
diff --git a/nvim/.config/nvim/lua/tobyvin/plugins/dap.lua b/nvim/.config/nvim/lua/tobyvin/plugins/dap.lua
index 58e5df5..858855e 100644
--- a/nvim/.config/nvim/lua/tobyvin/plugins/dap.lua
+++ b/nvim/.config/nvim/lua/tobyvin/plugins/dap.lua
@@ -107,25 +107,21 @@ M.setup = function()
callback({ type = "server", host = config.host, port = config.port })
end
- dap.configurations.lua = {
- {
- type = "nlua",
- request = "attach",
- name = "Attach to running Neovim instance",
- host = function()
- local value = vim.fn.input("Host [127.0.0.1]: ")
- if value ~= "" then
- return value
- end
- return "127.0.0.1"
- end,
- port = function()
- local val = tonumber(vim.fn.input("Port: "))
- assert(val, "Please provide a port number")
- return val
- end,
- },
- }
+ dap.configurations.lua = function()
+ vim.ui.input({ prompt = "Host: ", default = "127.0.0.1" }, function(host)
+ vim.ui.input({ prompt = "Port: ", default = "7777" }, function(port)
+ dap.configurations.lua = {
+ {
+ type = "nlua",
+ request = "attach",
+ name = "Attach to running Neovim instance",
+ host = host,
+ port = tonumber(port),
+ },
+ }
+ end)
+ end)
+ end
-- lldb
dap.adapters.lldb = {
@@ -134,26 +130,39 @@ M.setup = function()
name = "lldb",
}
- dap.configurations.cpp = {
- {
- name = "Launch",
- type = "lldb",
- request = "launch",
- program = function()
- return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file")
- end,
- cwd = "${workspaceFolder}",
- stopOnEntry = false,
- args = {},
- runInTerminal = false,
- },
- }
-
- dap.configurations.c = dap.configurations.cpp
+ dap.configurations.cpp = function()
+ utils.fs.select_executable(function(bin)
+ dap.configurations.cpp = {
+ {
+ name = "Launch",
+ type = "lldb",
+ request = "launch",
+ program = bin,
+ cwd = "${workspaceFolder}",
+ stopOnEntry = false,
+ args = {},
+ runInTerminal = false,
+ },
+ }
+ end)
+ end
- -- Disabled in favor of rust-tools
- -- TODO: integrate rust-tools.nvim into this config
- -- dap.configurations.rust = dap.configurations.cpp
+ dap.configurations.c = function()
+ utils.fs.select_executable(function(bin)
+ dap.configurations.c = {
+ {
+ name = "Launch",
+ type = "lldb",
+ request = "launch",
+ program = bin,
+ cwd = "${workspaceFolder}",
+ stopOnEntry = false,
+ args = {},
+ runInTerminal = false,
+ },
+ }
+ end)
+ end
-- Language specific plugins
require("dap-go").setup()
diff --git a/nvim/.config/nvim/lua/tobyvin/utils.lua b/nvim/.config/nvim/lua/tobyvin/utils.lua
new file mode 100644
index 0000000..8aab6f4
--- /dev/null
+++ b/nvim/.config/nvim/lua/tobyvin/utils.lua
@@ -0,0 +1,10 @@
+local M = {}
+
+setmetatable(M, {
+ __index = function(_, k)
+ local _, result = pcall(require, string.format("tobyvin.utils.%s", k))
+ return result
+ end,
+})
+
+return M
diff --git a/nvim/.config/nvim/lua/tobyvin/utils/fs.lua b/nvim/.config/nvim/lua/tobyvin/utils/fs.lua
index d273b17..cef48fe 100644
--- a/nvim/.config/nvim/lua/tobyvin/utils/fs.lua
+++ b/nvim/.config/nvim/lua/tobyvin/utils/fs.lua
@@ -1,30 +1,60 @@
+local Job = require("plenary.job")
local Path = require("plenary.path")
local Reload = require("plenary.reload")
local M = {}
--- TODO: add autocommand/keymap to reload current open file/module
-M.reload = function(module_name)
- local notify_opts = { title = string.format("[utils] reload module: '%s'", module_name) }
- local reload_ok, result = pcall(Reload.reload_module, module_name)
+M.config_path = function(package)
+ local rel_path = Path:new(vim.fn.stdpath("config")):normalize(vim.env.HOME)
+ return Path:new(vim.env.HOME, ".dotfiles", package, rel_path)
+end
- if not reload_ok then
- vim.notify(string.format("Failed to reload module: %s", result), vim.log.levels.ERROR, notify_opts)
- return
+M.module_from_path = function(path)
+ path = vim.F.if_nil(path, vim.api.nvim_buf_get_name(0))
+ path = Path:new(path)
+ local lua_dir = M.config_path("nvim"):joinpath("lua").filename
+ if path:exists() and not path:is_dir() and string.match(path.filename, "^" .. lua_dir) then
+ return path:normalize(lua_dir):gsub(".lua$", ""):gsub("/", ".")
end
+end
- local require_ok, module = pcall(require, module_name)
+M.reload = function(module_name, starts_with_only)
+ module_name = vim.F.if_nil(module_name, M.module_from_path())
+ starts_with_only = vim.F.if_nil(starts_with_only, "tobyvin")
- if not require_ok then
- vim.notify(string.format("Failed to require module: %s", result), vim.log.levels.ERROR, notify_opts)
- return
- end
+ Reload.reload_module(module_name, starts_with_only)
- if vim.tbl_contains(module, "setup") and not pcall(module, "setup") then
- vim.notify(string.format("Failed to require module: %s", result), vim.log.levels.ERROR, notify_opts)
- return
+ local notify_opts = { title = string.format("[utils] reload: '%s'", module_name) }
+ local status_ok, module = pcall(require, module_name)
+ if not status_ok then
+ vim.notify("Failed to require module", vim.log.levels.ERROR, notify_opts)
end
+ vim.notify("Reloaded module", vim.log.levels.INFO, notify_opts)
+ return module
+end
+
+M.select_exe = function(cwd, callback)
+ cwd = vim.fn.expand(vim.F.if_nil(cwd, vim.fn.getcwd()))
+
+ local command_list = (function()
+ if 1 == vim.fn.executable("fd") then
+ return { "fd", "--type", "x", "--hidden" }
+ elseif 1 == vim.fn.executable("fdfind") then
+ return { "fdfind", "--type", "x", "--hidden" }
+ elseif 1 == vim.fn.executable("find") and vim.fn.has("win32") == 0 then
+ return { "find", ".", "-type", "f", "--executable" }
+ end
+ end)()
+
+ local command = table.remove(command_list, 1)
+
+ local items = Job:new({
+ command = command,
+ args = command_list,
+ cwd = cwd,
+ enable_recording = true,
+ }):sync()
- vim.notify("Successfully reloaded module", vim.log.levels.INFO, { title = "[utils]" })
+ vim.ui.select(items, { kind = "executables" }, callback)
end
M.shorten_path = function(filename, len)
diff --git a/nvim/.config/nvim/lua/tobyvin/utils/init.lua b/nvim/.config/nvim/lua/tobyvin/utils/init.lua
deleted file mode 100644
index e2084a2..0000000
--- a/nvim/.config/nvim/lua/tobyvin/utils/init.lua
+++ /dev/null
@@ -1,12 +0,0 @@
-local M = {
- keymap = require("tobyvin.utils.keymap"),
- job = require("tobyvin.utils.job"),
- fs = require("tobyvin.utils.fs"),
- buffer = require("tobyvin.utils.buffer"),
- debug = require("tobyvin.utils.debug"),
- status = require("tobyvin.utils.status"),
- diagnostic = require("tobyvin.utils.diagnostic"),
- documentation = require("tobyvin.utils.documentation"),
-}
-
-return M