diff --git a/home-manager/nvim/init.lua b/home-manager/nvim/init.lua index 4efee70..be0341b 100644 --- a/home-manager/nvim/init.lua +++ b/home-manager/nvim/init.lua @@ -166,19 +166,35 @@ if status then local mappings = {} local commands = {} -- Store commands keyed by the display string - require("which-key.keys").get_tree("n").tree:walk(function(node) - if node.mapping then - local mapping = node.mapping - local description = mapping.desc or mapping.label or mapping.cmd - -- Some actions are just there for which-key to hook into to display prefixes; they don't have a description. - if description then - local displayString = description .. " | " .. mapping.prefix - commands[displayString] = mapping.prefix - mappings[#mappings + 1] = displayString + function accumulate(tree) + tree:walk(function(node) + -- Note: we could (if desired) view all groups, because the `node.mapping` table looks like this: + -- { prefix = "g", group = true, keys = {...}} + if node.mapping then + local mapping = node.mapping + if not mapping.group then + local description = mapping.desc or mapping.label or mapping.cmd + -- Some actions are just there for which-key to hook into to display prefixes; they don't have a description. + if description then + local displayString = description .. " | " .. mapping.prefix + commands[displayString] = mapping.prefix + mappings[#mappings + 1] = displayString + else + for k, v in pairs(mapping) do + print("Nothing: " .. k .. " : " .. tostring(v) .. " (type: " .. type(v) .. ")") + end + print("-----") + end + end + -- TODO: If a command is a prefix of an existing command, prepend its description to those commands' descriptions, and append a '...' to the parent's description. end - -- TODO: If a command is a prefix of an existing command, prepend its description to those commands' descriptions, and append a '...' to the parent's description. - end - end) + end) + end + + local cur_buf = vim.api.nvim_win_get_buf(0) + + accumulate(require("which-key.keys").get_tree("n").tree) + accumulate(require("which-key.keys").get_tree("n", cur_buf).tree) pickers .new({}, { @@ -208,8 +224,12 @@ if status then vim.cmd("setlocal spell!") end - vim.api.nvim_set_keymap("n", "", ":lua DisplayAllMappingsWithTelescope()", {}) whichkey.register({ + -- TODO: this isn't working for the FSI ones - maybe we've moved to a different buffer by the time we ask for the keymap? + [vim.api.nvim_get_var("maplocalleader")] = { + DisplayAllMappingsWithTelescope, + "View all mappings", + }, ["mp"] = { MarkdownPreview, "Preview Markdown in Lynx", @@ -226,11 +246,6 @@ if status then ChangeToCurrentDirectory, "Switch CWD to the directory of the open buffer", }, - -- For some reason the command doesn't work at all if I map it in here, - -- whereas if we map it separately and *document* it in here then only the documentation doesn't work. - [vim.api.nvim_get_var("maplocalleader")] = { - "View all mappings", - }, ["ss"] = { ToggleSpell, "Toggle spell-checker on or off", diff --git a/home-manager/nvim/ionide-vim.lua b/home-manager/nvim/ionide-vim.lua index ae3006b..43fef16 100644 --- a/home-manager/nvim/ionide-vim.lua +++ b/home-manager/nvim/ionide-vim.lua @@ -2,8 +2,6 @@ vim.g["fsharp#fsautocomplete_command"] = { "fsautocomplete" } vim.g["fsharp#show_signature_on_cursor_move"] = 1 vim.g["fsharp#fsi_keymap"] = "none" -vim.api.nvim_create_augroup("FSharpGroup", {}) - -- MASSIVE HACK - raised https://github.com/ionide/Ionide-vim/pull/78 local function captureLoadedProjects() vim.fn.execute("redir => g:massive_hack_patrick_capture") @@ -236,33 +234,30 @@ vim.api.nvim_create_user_command("BuildFSharpProject", function(opts) end end, { nargs = "?", complete = "file" }) -local function SetupFSharpKeyBindings() - local status, whichkey = pcall(require, "which-key") - if status then - whichkey.register({ - ["f"] = { - t = { ":call fsharp#showTooltip()", "Show F# Tooltip" }, - ["si"] = { ":call fsharp#toggleFsi()", "Toggle FSI (F# Interactive)" }, - ["sl"] = { ":call fsharp#sendLineToFsi()", "Send line to FSI (F# Interactive)" }, - }, - ["b"] = { - p = { - a = { BuildFSharpProjects, "Build all projects" }, - s = { ":BuildFSharpProject", "Build specified project" }, - }, - }, - }, { prefix = vim.api.nvim_get_var("maplocalleader"), buffer = vim.api.nvim_get_current_buf() }) - else - vim.api.nvim_set_keymap("n", "ft", ":call fsharp#showTooltip()", { noremap = true }) - vim.api.nvim_set_keymap("n", "fsi", ":call fsharp#toggleFsi()", { noremap = true }) - vim.api.nvim_set_keymap("n", "fsl", ":call fsharp#sendLineToFsi()", { noremap = true }) - vim.api.nvim_set_keymap("n", "bpa", BuildFSharpProjects, { noremap = true }) - vim.api.nvim_set_keymap("n", "bps", ":BuildFSharpProject", { noremap = true }) - end -end - vim.api.nvim_create_autocmd("FileType", { - group = "FSharpGroup", pattern = "fsharp", - callback = SetupFSharpKeyBindings, + callback = function() + local status, whichkey = pcall(require, "which-key") + if status then + whichkey.register({ + f = { + t = { ":call fsharp#showTooltip()", "Show F# Tooltip" }, + ["si"] = { ":call fsharp#toggleFsi()", "Toggle FSI (F# Interactive)" }, + ["sl"] = { ":call fsharp#sendLineToFsi()", "Send line to FSI (F# Interactive)" }, + }, + b = { + p = { + a = { BuildFSharpProjects, "Build all projects" }, + s = { ":BuildFSharpProject", "Build specified project" }, + }, + }, + }, { prefix = vim.api.nvim_get_var("maplocalleader"), buffer = vim.api.nvim_get_current_buf() }) + else + vim.api.nvim_set_keymap("n", "ft", ":call fsharp#showTooltip()", { noremap = true }) + vim.api.nvim_set_keymap("n", "fsi", ":call fsharp#toggleFsi()", { noremap = true }) + vim.api.nvim_set_keymap("n", "fsl", ":call fsharp#sendLineToFsi()", { noremap = true }) + vim.api.nvim_set_keymap("n", "bpa", BuildFSharpProjects, { noremap = true }) + vim.api.nvim_set_keymap("n", "bps", ":BuildFSharpProject", { noremap = true }) + end + end, }) diff --git a/home-manager/nvim/lspconfig.lua b/home-manager/nvim/lspconfig.lua index 12340bd..0550ccb 100644 --- a/home-manager/nvim/lspconfig.lua +++ b/home-manager/nvim/lspconfig.lua @@ -67,21 +67,23 @@ function ToggleLocList() end end -local whichkey_status, whichkey = pcall(require, "which-key") -if whichkey_status then - whichkey.register({ - l = { - p = { vim.diagnostic.goto_prev, "Go to previous entry in loclist" }, - n = { vim.diagnostic.goto_next, "Go to next entry in loclist" }, - l = { ToggleLocList, "Toggle loclist" }, - f = { vim.diagnostic.open_float, "Open current loclist entry in floating window" }, - }, - }, { prefix = vim.api.nvim_get_var("maplocalleader") }) -else - vim.keymap.set("n", "lp", vim.diagnostic.goto_prev) - vim.keymap.set("n", "ln", vim.diagnostic.goto_next) - vim.keymap.set("n", "ll", ToggleLocList) - vim.keymap.set("n", "lf", vim.diagnostic.open_float) +do + local whichkey_status, whichkey = pcall(require, "which-key") + if whichkey_status then + whichkey.register({ + l = { + p = { vim.diagnostic.goto_prev, "Go to previous entry in loclist" }, + n = { vim.diagnostic.goto_next, "Go to next entry in loclist" }, + l = { ToggleLocList, "Toggle loclist" }, + f = { vim.diagnostic.open_float, "Open current loclist entry in floating window" }, + }, + }, { prefix = vim.api.nvim_get_var("maplocalleader") }) + else + vim.keymap.set("n", "lp", vim.diagnostic.goto_prev) + vim.keymap.set("n", "ln", vim.diagnostic.goto_next) + vim.keymap.set("n", "ll", ToggleLocList) + vim.keymap.set("n", "lf", vim.diagnostic.open_float) + end end -- Use LspAttach autocommand to only map the following keys @@ -89,6 +91,7 @@ end vim.api.nvim_create_autocmd("LspAttach", { group = vim.api.nvim_create_augroup("UserLspConfig", {}), callback = function(ev) + local whichkey_status, whichkey = pcall(require, "which-key") -- Enable completion triggered by vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc" diff --git a/home-manager/nvim/nvim-dap.lua b/home-manager/nvim/nvim-dap.lua index 269a962..5bf2ccd 100644 --- a/home-manager/nvim/nvim-dap.lua +++ b/home-manager/nvim/nvim-dap.lua @@ -17,58 +17,70 @@ dap.configurations.fsharp = { }, } -local status, whichkey = pcall(require, "which-key") -if status then - whichkey.register({ - d = { - o = { dap.step_over, "Step over" }, - i = { dap.step_into, "Step into" }, - c = { dap.continue, "Continue" }, - C = { dap.run_last, "Run with last debug configuration" }, - b = { dap.toggle_breakpoint, "Toggle breakpoint" }, - r = { dap.repl.open, "Open debug repl" }, - v = { +do + local status, whichkey = pcall(require, "which-key") + if status then + whichkey.register({ + d = { + o = { dap.step_over, "Step over" }, + i = { dap.step_into, "Step into" }, + c = { dap.continue, "Continue" }, + C = { dap.run_last, "Run with last debug configuration" }, + b = { dap.toggle_breakpoint, "Toggle breakpoint" }, + r = { dap.repl.open, "Open debug repl" }, v = { - function() - dap_ui.hover() - end, - "View value of expression under cursor", - }, - s = { - function() - dap_ui.sidebar(dap_ui.scopes).open() - end, - "View values of all variables in all scopes", - }, - f = { - function() - dap_ui.sidebar(dap_ui.frames).open() - end, - "View stack frames", + v = { + function() + dap_ui.hover() + end, + "View value of expression under cursor", + }, + s = { + function() + dap_ui.sidebar(dap_ui.scopes).open() + end, + "View values of all variables in all scopes", + }, + f = { + function() + dap_ui.sidebar(dap_ui.frames).open() + end, + "View stack frames", + }, }, + t = { dap.terminate, "Terminate/stop/end debug session" }, }, - t = { dap.terminate, "Terminate/stop/end debug session" }, - }, - }, { prefix = vim.api.nvim_get_var("maplocalleader") }) -else - vim.api.nvim_set_keymap("n", "do", ":lua require('dap').step_over()", { noremap = true }) - vim.api.nvim_set_keymap("n", "di", ":lua require('dap').step_into()", { noremap = true }) - vim.api.nvim_set_keymap("n", "dc", ":lua require('dap').continue()", { noremap = true }) - vim.api.nvim_set_keymap("n", "dC", ":lua require('dap').run_last()", { noremap = true }) - vim.api.nvim_set_keymap("n", "db", ":lua require('dap').toggle_breakpoint()", { noremap = true }) - vim.api.nvim_set_keymap("n", "dr", ":lua require('dap').repl.open()", { noremap = true }) - vim.api.nvim_set_keymap("n", "dvv", ":lua require('dap.ui.widgets').hover()", { noremap = true }) - vim.api.nvim_set_keymap( - "n", - "dvs", - ":lua require('dap.ui.widgets').sidebar(require('dap.ui.widgets').scopes).open()", - { noremap = true } - ) - vim.api.nvim_set_keymap( - "n", - "dvf", - ":lua require('dap.ui.widgets').sidebar(require('dap.ui.widgets').frames).open()", - { noremap = true } - ) - vim.api.nvim_set_keymap("n", "dt", ":lua require('dap').terminate()", { noremap = true }) + }, { prefix = vim.api.nvim_get_var("maplocalleader") }) + else + vim.api.nvim_set_keymap("n", "do", ":lua require('dap').step_over()", { noremap = true }) + vim.api.nvim_set_keymap("n", "di", ":lua require('dap').step_into()", { noremap = true }) + vim.api.nvim_set_keymap("n", "dc", ":lua require('dap').continue()", { noremap = true }) + vim.api.nvim_set_keymap("n", "dC", ":lua require('dap').run_last()", { noremap = true }) + vim.api.nvim_set_keymap( + "n", + "db", + ":lua require('dap').toggle_breakpoint()", + { noremap = true } + ) + vim.api.nvim_set_keymap("n", "dr", ":lua require('dap').repl.open()", { noremap = true }) + vim.api.nvim_set_keymap( + "n", + "dvv", + ":lua require('dap.ui.widgets').hover()", + { noremap = true } + ) + vim.api.nvim_set_keymap( + "n", + "dvs", + ":lua require('dap.ui.widgets').sidebar(require('dap.ui.widgets').scopes).open()", + { noremap = true } + ) + vim.api.nvim_set_keymap( + "n", + "dvf", + ":lua require('dap.ui.widgets').sidebar(require('dap.ui.widgets').frames).open()", + { noremap = true } + ) + vim.api.nvim_set_keymap("n", "dt", ":lua require('dap').terminate()", { noremap = true }) + end end