From 1b3153b6f8fc56c8b2c17aca812f3e6d2444b257 Mon Sep 17 00:00:00 2001 From: bingcoke <248677315@qq.com> Date: Sun, 14 May 2023 11:33:15 +0800 Subject: [PATCH 1/2] add:feature choice some module to use --- lua/rust-tools/config.lua | 20 +++- lua/rust-tools/init.lua | 83 +++++++++++------ lua/rust-tools/lsp.lua | 189 ++++++++++++++++++++++---------------- 3 files changed, 186 insertions(+), 106 deletions(-) diff --git a/lua/rust-tools/config.lua b/lua/rust-tools/config.lua index d4ce0ac..1334da1 100644 --- a/lua/rust-tools/config.lua +++ b/lua/rust-tools/config.lua @@ -8,7 +8,8 @@ _G.rust_tools_get_graphviz_backends = function() end local defaults = { - tools = { -- rust-tools options + tools = { + -- rust-tools options -- how to execute terminal commands -- options right now: termopen / quickfix @@ -60,7 +61,6 @@ local defaults = { -- options same as lsp hover / vim.lsp.util.open_floating_preview() hover_actions = { - -- the border that is used for the hover window -- see vim.api.nvim_open_win() border = { @@ -180,12 +180,28 @@ local defaults = { name = "rt_lldb", }, }, + + -- choice module you use + open = { + crate_graph = true, + expand_macro = true, + external_docs = true, + debuggables = true, + hover_range = true, + workspace_refresh = true, + move_item = true, + standalone = true, + dap = true, + parent_module = true, + runnables = true, + }, } M.options = { tools = {}, server = {}, dap = {}, + open = {}, } function M.setup(options) diff --git a/lua/rust-tools/init.lua b/lua/rust-tools/init.lua index ca07557..5044846 100644 --- a/lua/rust-tools/init.lua +++ b/lua/rust-tools/init.lua @@ -29,6 +29,10 @@ local M = { } function M.setup(opts) + if opts.open == nil then + opts.open = {} + end + local code_action_group = require("rust-tools.code_action_group") M.code_action_group = code_action_group @@ -39,27 +43,37 @@ function M.setup(opts) local config = require("rust-tools.config") M.config = config + config.setup(opts) - local crate_graph = require("rust-tools.crate_graph") - M.crate_graph = crate_graph + local open = M.config.options.open - local rt_dap = require("rust-tools.dap") - M.dap = rt_dap + if open.crate_graph ~= false then + local crate_graph = require("rust-tools.crate_graph") + M.crate_graph = crate_graph + end - local debuggables = require("rust-tools.debuggables") - M.debuggables = debuggables + if open.debuggables ~= false then + local debuggables = require("rust-tools.debuggables") + M.debuggables = debuggables + end - local expand_macro = require("rust-tools.expand_macro") - M.expand_macro = expand_macro + if open.expand_macro ~= false then + local expand_macro = require("rust-tools.expand_macro") + M.expand_macro = expand_macro + end - local external_docs = require("rust-tools.external_docs") - M.external_docs = external_docs + if open.external_docs ~= false then + local external_docs = require("rust-tools.external_docs") + M.external_docs = external_docs + end local hover_actions = require("rust-tools.hover_actions") M.hover_actions = hover_actions - local hover_range = require("rust-tools.hover_range") - M.hover_range = hover_range + if open.hover_actions ~= false then + local hover_range = require("rust-tools.hover_range") + M.hover_range = hover_range + end local inlay = require("rust-tools.inlay_hints") local hints = inlay.new() @@ -84,23 +98,31 @@ function M.setup(opts) end, } - local join_lines = require("rust-tools.join_lines") - M.join_lines = join_lines + if open.join_lines ~= false then + local join_lines = require("rust-tools.join_lines") + M.join_lines = join_lines + end local lsp = require("rust-tools.lsp") M.lsp = lsp - local move_item = require("rust-tools.move_item") - M.move_item = move_item + if open.move_item ~= false then + local move_item = require("rust-tools.move_item") + M.move_item = move_item + end local open_cargo_toml = require("rust-tools.open_cargo_toml") M.open_cargo_toml = open_cargo_toml - local parent_module = require("rust-tools.parent_module") - M.parent_module = parent_module + if open.parent_module ~= false then + local parent_module = require("rust-tools.parent_module") + M.parent_module = parent_module + end - local runnables = require("rust-tools.runnables") - M.runnables = runnables + if open.runnables ~= false then + local runnables = require("rust-tools.runnables") + M.runnables = runnables + end local server_status = require("rust-tools.server_status") M.server_status = server_status @@ -108,21 +130,28 @@ function M.setup(opts) local ssr = require("rust-tools.ssr") M.ssr = ssr - local standalone = require("rust-tools.standalone") - M.standalone = standalone + if open.standalone ~= false then + local standalone = require("rust-tools.standalone") + M.standalone = standalone + end - local workspace_refresh = require("rust-tools.workspace_refresh") - M.workspace_refresh = workspace_refresh + if open.workspace_refresh ~= false then + local workspace_refresh = require("rust-tools.workspace_refresh") + M.workspace_refresh = workspace_refresh + end local utils = require("rust-tools.utils.utils") M.utils = utils - config.setup(opts) lsp.setup() commands.setup_lsp_commands() - if pcall(require, "dap") then - rt_dap.setup_adapter() + if opts.open.dap ~= false then + local rt_dap = require("rust-tools.dap") + M.dap = rt_dap + if pcall(require, "dap") then + rt_dap.setup_adapter() + end end end diff --git a/lua/rust-tools/lsp.lua b/lua/rust-tools/lsp.lua index 3288eeb..06e1336 100644 --- a/lua/rust-tools/lsp.lua +++ b/lua/rust-tools/lsp.lua @@ -6,12 +6,13 @@ local server_status = require("rust-tools.server_status") local M = {} local function setup_autocmds() - local group = vim.api.nvim_create_augroup("RustToolsAutocmds", { clear = true }) + local group = + vim.api.nvim_create_augroup("RustToolsAutocmds", { clear = true }) if rt.config.options.tools.reload_workspace_from_cargo_toml then vim.api.nvim_create_autocmd("BufWritePost", { pattern = "*/Cargo.toml", - callback = require('rust-tools.workspace_refresh')._reload_workspace_from_cargo_toml, + callback = require("rust-tools.workspace_refresh")._reload_workspace_from_cargo_toml, group = group, }) end @@ -20,75 +21,116 @@ local function setup_autocmds() pattern = "*.rs", callback = rt.lsp.start_standalone_if_required, group = group, - }); + }) end local function setup_commands() local lsp_opts = rt.config.options.server + local open = rt.config.options.open + local table_list = {} - lsp_opts.commands = vim.tbl_deep_extend("force", lsp_opts.commands or {}, { - RustCodeAction = { - rt.code_action_group.code_action_group, - }, - RustViewCrateGraph = { - function(backend, output, pipe) - rt.crate_graph.view_crate_graph(backend, output, pipe) - end, - "-nargs=* -complete=customlist,v:lua.rust_tools_get_graphviz_backends", - description = "`:RustViewCrateGraph [ []]` Show the crate graph", - }, - RustDebuggables = { + if open.crate_graph then + table_list.RustDebuggables = { rt.debuggables.debuggables, - }, - RustExpandMacro = { rt.expand_macro.expand_macro }, - RustOpenExternalDocs = { - rt.external_docs.open_external_docs, - }, - RustHoverActions = { rt.hover_actions.hover_actions }, - RustHoverRange = { rt.hover_range.hover_range }, - RustEnableInlayHints = { - rt.inlay_hints.enable, - }, - RustDisableInlayHints = { - rt.inlay_hints.disable, - }, - RustLastDebug = { + } + end + + if open.RustExpandMacro then + table_list.RustExpandMacro = { rt.expand_macro.expand_macro } + end + + if open.hover_range then + table_list.RustHoverRange = { rt.hover_range.hover_range } + end + + if open.dap then + table_list.RustLastDebug = { rt.cached_commands.execute_last_debuggable, - }, - RustLastRun = { - rt.cached_commands.execute_last_runnable, - }, - RustSetInlayHints = { - rt.inlay_hints.set, - }, - RustUnsetInlayHints = { - rt.inlay_hints.unset, - }, - RustJoinLines = { rt.join_lines.join_lines }, - RustMoveItemDown = { + } + end + + if open.move_item then + table_list.RustMoveItemDown = { rt.move_item.move_item, - }, - RustMoveItemUp = { + } + table_list.RustMoveItemUp = { function() require("rust-tools.move_item").move_item(true) end, - }, - RustOpenCargo = { rt.open_cargo_toml.open_cargo_toml }, - RustParentModule = { rt.parent_module.parent_module }, - RustRunnables = { + } + end + + if open.parent_module then + table_list.RustParentModule = { rt.parent_module.parent_module } + end + + if open.runnables then + table_list.RustRunnables = { rt.runnables.runnables, - }, - RustSSR = { - function(query) - require("rust-tools.ssr").ssr(query) - end, - "-nargs=?", - description = "`:RustSSR [query]` Structural Search Replace", - }, - RustReloadWorkspace = { + } + end + + if open.workspace_refresh then + table_list.RustReloadWorkspace = { rt.workspace_refresh.reload_workspace, - }, - }) + } + end + + if open.hints then + table_list.RustSetInlayHints = { + rt.inlay_hints.set, + } + table_list.RustUnsetInlayHints = { + rt.inlay_hints.unset, + } + table_list.RustJoinLines = { rt.join_lines.join_lines } + end + + table_list.RustCodeAction = { + rt.code_action_group.code_action_group, + } + + table_list.RustViewCrateGraph = { + function(backend, output, pipe) + rt.crate_graph.view_crate_graph(backend, output, pipe) + end, + "-nargs=* -complete=customlist,v:lua.rust_tools_get_graphviz_backends", + description = "`:RustViewCrateGraph [ []]` Show the crate graph", + } + + + table_list.RustOpenExternalDocs = { + rt.external_docs.open_external_docs, + } + + table_list.RustHoverActions = { rt.hover_actions.hover_actions } + + table_list.RustEnableInlayHints = { + rt.inlay_hints.enable, + } + + table_list.RustDisableInlayHints = { + rt.inlay_hints.disable, + } + + + + table_list.RustLastRun = { + rt.cached_commands.execute_last_runnable, + } + + + table_list.RustOpenCargo = { rt.open_cargo_toml.open_cargo_toml } + + table_list.RustSSR = { + function(query) + require("rust-tools.ssr").ssr(query) + end, + "-nargs=?", + description = "`:RustSSR [query]` Structural Search Replace", + } + + lsp_opts.commands = vim.tbl_deep_extend("force", lsp_opts.commands or {}, table_list) end local function setup_handlers() @@ -103,15 +145,11 @@ local function setup_handlers() ) end - custom_handlers["experimental/serverStatus"] = rt.utils.mk_handler( - server_status.handler - ) + custom_handlers["experimental/serverStatus"] = + rt.utils.mk_handler(server_status.handler) - lsp_opts.handlers = vim.tbl_deep_extend( - "force", - custom_handlers, - lsp_opts.handlers or {} - ) + lsp_opts.handlers = + vim.tbl_deep_extend("force", custom_handlers, lsp_opts.handlers or {}) end local function setup_on_init() @@ -159,11 +197,8 @@ local function setup_capabilities() }, } - lsp_opts.capabilities = vim.tbl_deep_extend( - "force", - capabilities, - lsp_opts.capabilities or {} - ) + lsp_opts.capabilities = + vim.tbl_deep_extend("force", capabilities, lsp_opts.capabilities or {}) end local function setup_lsp() @@ -195,9 +230,9 @@ local function get_root_dir(filename) cargo_workspace_dir = vim.fn.json_decode(cargo_metadata)["workspace_root"] end return cargo_workspace_dir - or cargo_crate_dir - or lspconfig_utils.root_pattern("rust-project.json")(fname) - or lspconfig_utils.find_git_ancestor(fname) + or cargo_crate_dir + or lspconfig_utils.root_pattern("rust-project.json")(fname) + or lspconfig_utils.find_git_ancestor(fname) end local function setup_root_dir() @@ -212,9 +247,9 @@ function M.start_standalone_if_required() local current_buf = vim.api.nvim_get_current_buf() if - lsp_opts.standalone - and rt.utils.is_bufnr_rust(current_buf) - and (get_root_dir() == nil) + lsp_opts.standalone + and rt.utils.is_bufnr_rust(current_buf) + and (get_root_dir() == nil) then require("rust-tools.standalone").start_standalone_client() end From d817cef370aa3f99f477b013c979598bdf5fcf5a Mon Sep 17 00:00:00 2001 From: bingcoke <248677315@qq.com> Date: Sun, 14 May 2023 13:02:26 +0800 Subject: [PATCH 2/2] add: add doc and support float notify window --- README.md | 15 ++++++ lua/rust-tools/utils/utils.lua | 73 ++++++++++++++++++++++++---- lua/rust-tools/workspace_refresh.lua | 16 ++++-- 3 files changed, 92 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 72f67a8..b54fddf 100644 --- a/README.md +++ b/README.md @@ -396,6 +396,21 @@ local opts = { name = "rt_lldb", }, }, + + -- choice module you use to speed up setuptime + open = { + crate_graph = true, + expand_macro = true, + external_docs = true, + debuggables = true, + hover_range = true, + workspace_refresh = true, + move_item = true, + standalone = true, + dap = true, + parent_module = true, + runnables = true, + }, } require('rust-tools').setup(opts) diff --git a/lua/rust-tools/utils/utils.lua b/lua/rust-tools/utils/utils.lua index 091f386..a0f1dbd 100644 --- a/lua/rust-tools/utils/utils.lua +++ b/lua/rust-tools/utils/utils.lua @@ -6,10 +6,10 @@ function M.is_windows() end function M.is_nushell() - local shell = vim.loop.os_getenv("SHELL") - local nu = "nu" - -- Check if $SHELL ends in "nu" - return shell:sub(-string.len(nu)) == nu + local shell = vim.loop.os_getenv("SHELL") + local nu = "nu" + -- Check if $SHELL ends in "nu" + return shell:sub(-string.len(nu)) == nu end ---comment @@ -29,9 +29,7 @@ end ---Note that a space is not added at the end of the returned command string ---@param commands table function M.chain_commands(commands) - local separator = M.is_windows() and " | " - or M.is_nushell() and ";" - or " && " + local separator = M.is_windows() and " | " or M.is_nushell() and ";" or " && " local ret = "" for i, value in ipairs(commands) do @@ -138,10 +136,9 @@ end function M.is_ra_server(client) local name = client.name return client.name == "rust_analyzer" - or client.name == "rust_analyzer-standalone" + or client.name == "rust_analyzer-standalone" end - -- sanitize_command_for_debugging substitutes the command arguments so it can be used to run a -- debugger. -- @@ -158,4 +155,62 @@ function M.sanitize_command_for_debugging(command) end end +-- create floating window +-- content = {"str1","str2"} +-- opt = { +-- title = { { "title", "TitleString" } } +-- title_pos = "center" +-- col = vim.api.nvim_win_get_width(0) - width - 1 +-- row = vim.api.nvim_win_get_height(0) - height - 1 +-- timeout = 1000 +-- } +function M.create_notify_floating_window(content, opt) + local bufnr = vim.api.nvim_create_buf(false, true) + local height = #content + 2 + + local maxLen = 0 + + for _, str in ipairs(content) do + local len = string.len(str) + if len > maxLen then + maxLen = len + end + end + + local width = maxLen + local default_opt = { + col = vim.api.nvim_win_get_width(0) - width - 1, + row = vim.api.nvim_win_get_height(0) - height - 1, + width = width, + height = height, + timeout = 1000, + title = { { "rust-tools", "TitleString" } }, + } + opt = vim.tbl_deep_extend("force", {}, default_opt, opt or {}) + + local win_opts = { + relative = "editor", + width = opt.width, + height = opt.height, + col = opt.col, + row = opt.row, + style = "minimal", + border = "single", + title = opt.title, + } + + win_opts.title_pos = opt.title_pos or "center" + + local winnr = vim.api.nvim_open_win(bufnr, false, win_opts) + + vim.api.nvim_buf_set_option(bufnr, "modifiable", true) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, content) + vim.api.nvim_buf_set_option(bufnr, "modifiable", false) + + -- autoclose + vim.defer_fn(function() + vim.api.nvim_win_close(winnr, true) + end, opt.timeout) +end + return M diff --git a/lua/rust-tools/workspace_refresh.lua b/lua/rust-tools/workspace_refresh.lua index 9b023bf..12fd174 100644 --- a/lua/rust-tools/workspace_refresh.lua +++ b/lua/rust-tools/workspace_refresh.lua @@ -1,10 +1,14 @@ +local util = require("rust-tools.utils.utils") local M = {} local function handler(err) if err then error(tostring(err)) end - vim.notify("Cargo workspace reloaded") + util.create_notify_floating_window( + { "Cargo Workspace reloaded" }, + { width = 30 } + ) end function M._reload_workspace_from_cargo_toml() @@ -12,14 +16,20 @@ function M._reload_workspace_from_cargo_toml() for _, client in ipairs(clients) do if client.name == "rust_analyzer" then - vim.notify("Reloading Cargo Workspace") + util.create_notify_floating_window( + { "Reloading Cargo Workspace" }, + { width = 30 } + ) client.request("rust-analyzer/reloadWorkspace", nil, handler, 0) end end end function M.reload_workspace() - vim.notify("Reloading Cargo Workspace") + util.create_notify_floating_window({ + "Reloading Cargo Workspace", + { width = 30 }, + }) vim.lsp.buf_request(0, "rust-analyzer/reloadWorkspace", nil, handler) end