Fix buffer-specific binds

This commit is contained in:
Smaug123
2024-03-24 23:30:52 +00:00
parent 75cebeaa7a
commit 4554ea1a90
4 changed files with 138 additions and 113 deletions

View File

@@ -166,19 +166,35 @@ if status then
local mappings = {} local mappings = {}
local commands = {} -- Store commands keyed by the display string local commands = {} -- Store commands keyed by the display string
require("which-key.keys").get_tree("n").tree:walk(function(node) function accumulate(tree)
if node.mapping then tree:walk(function(node)
local mapping = node.mapping -- Note: we could (if desired) view all groups, because the `node.mapping` table looks like this:
local description = mapping.desc or mapping.label or mapping.cmd -- { prefix = "g", group = true, keys = {...}}
-- Some actions are just there for which-key to hook into to display prefixes; they don't have a description. if node.mapping then
if description then local mapping = node.mapping
local displayString = description .. " | " .. mapping.prefix if not mapping.group then
commands[displayString] = mapping.prefix local description = mapping.desc or mapping.label or mapping.cmd
mappings[#mappings + 1] = displayString -- 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 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 pickers
.new({}, { .new({}, {
@@ -208,8 +224,12 @@ if status then
vim.cmd("setlocal spell!") vim.cmd("setlocal spell!")
end end
vim.api.nvim_set_keymap("n", "<localleader><localleader>", ":lua DisplayAllMappingsWithTelescope()<CR>", {})
whichkey.register({ 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"] = { ["mp"] = {
MarkdownPreview, MarkdownPreview,
"Preview Markdown in Lynx", "Preview Markdown in Lynx",
@@ -226,11 +246,6 @@ if status then
ChangeToCurrentDirectory, ChangeToCurrentDirectory,
"Switch CWD to the directory of the open buffer", "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"] = { ["ss"] = {
ToggleSpell, ToggleSpell,
"Toggle spell-checker on or off", "Toggle spell-checker on or off",

View File

@@ -2,8 +2,6 @@ vim.g["fsharp#fsautocomplete_command"] = { "fsautocomplete" }
vim.g["fsharp#show_signature_on_cursor_move"] = 1 vim.g["fsharp#show_signature_on_cursor_move"] = 1
vim.g["fsharp#fsi_keymap"] = "none" vim.g["fsharp#fsi_keymap"] = "none"
vim.api.nvim_create_augroup("FSharpGroup", {})
-- MASSIVE HACK - raised https://github.com/ionide/Ionide-vim/pull/78 -- MASSIVE HACK - raised https://github.com/ionide/Ionide-vim/pull/78
local function captureLoadedProjects() local function captureLoadedProjects()
vim.fn.execute("redir => g:massive_hack_patrick_capture") vim.fn.execute("redir => g:massive_hack_patrick_capture")
@@ -236,33 +234,30 @@ vim.api.nvim_create_user_command("BuildFSharpProject", function(opts)
end end
end, { nargs = "?", complete = "file" }) end, { nargs = "?", complete = "file" })
local function SetupFSharpKeyBindings()
local status, whichkey = pcall(require, "which-key")
if status then
whichkey.register({
["f"] = {
t = { ":call fsharp#showTooltip()<CR>", "Show F# Tooltip" },
["si"] = { ":call fsharp#toggleFsi()<CR>", "Toggle FSI (F# Interactive)" },
["sl"] = { ":call fsharp#sendLineToFsi()<cr>", "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", "<localleader>ft", ":call fsharp#showTooltip()<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>fsi", ":call fsharp#toggleFsi()<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>fsl", ":call fsharp#sendLineToFsi()<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>bpa", BuildFSharpProjects, { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>bps", ":BuildFSharpProject", { noremap = true })
end
end
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd("FileType", {
group = "FSharpGroup",
pattern = "fsharp", pattern = "fsharp",
callback = SetupFSharpKeyBindings, callback = function()
local status, whichkey = pcall(require, "which-key")
if status then
whichkey.register({
f = {
t = { ":call fsharp#showTooltip()<CR>", "Show F# Tooltip" },
["si"] = { ":call fsharp#toggleFsi()<CR>", "Toggle FSI (F# Interactive)" },
["sl"] = { ":call fsharp#sendLineToFsi()<cr>", "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", "<localleader>ft", ":call fsharp#showTooltip()<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>fsi", ":call fsharp#toggleFsi()<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>fsl", ":call fsharp#sendLineToFsi()<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>bpa", BuildFSharpProjects, { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>bps", ":BuildFSharpProject", { noremap = true })
end
end,
}) })

View File

@@ -67,21 +67,23 @@ function ToggleLocList()
end end
end end
local whichkey_status, whichkey = pcall(require, "which-key") do
if whichkey_status then local whichkey_status, whichkey = pcall(require, "which-key")
whichkey.register({ if whichkey_status then
l = { whichkey.register({
p = { vim.diagnostic.goto_prev, "Go to previous entry in loclist" }, l = {
n = { vim.diagnostic.goto_next, "Go to next entry in loclist" }, p = { vim.diagnostic.goto_prev, "Go to previous entry in loclist" },
l = { ToggleLocList, "Toggle loclist" }, n = { vim.diagnostic.goto_next, "Go to next entry in loclist" },
f = { vim.diagnostic.open_float, "Open current loclist entry in floating window" }, l = { ToggleLocList, "Toggle loclist" },
}, f = { vim.diagnostic.open_float, "Open current loclist entry in floating window" },
}, { prefix = vim.api.nvim_get_var("maplocalleader") }) },
else }, { prefix = vim.api.nvim_get_var("maplocalleader") })
vim.keymap.set("n", "<localleader>lp", vim.diagnostic.goto_prev) else
vim.keymap.set("n", "<localleader>ln", vim.diagnostic.goto_next) vim.keymap.set("n", "<localleader>lp", vim.diagnostic.goto_prev)
vim.keymap.set("n", "<localleader>ll", ToggleLocList) vim.keymap.set("n", "<localleader>ln", vim.diagnostic.goto_next)
vim.keymap.set("n", "<localleader>lf", vim.diagnostic.open_float) vim.keymap.set("n", "<localleader>ll", ToggleLocList)
vim.keymap.set("n", "<localleader>lf", vim.diagnostic.open_float)
end
end end
-- Use LspAttach autocommand to only map the following keys -- Use LspAttach autocommand to only map the following keys
@@ -89,6 +91,7 @@ end
vim.api.nvim_create_autocmd("LspAttach", { vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("UserLspConfig", {}), group = vim.api.nvim_create_augroup("UserLspConfig", {}),
callback = function(ev) callback = function(ev)
local whichkey_status, whichkey = pcall(require, "which-key")
-- Enable completion triggered by <c-x><c-o> -- Enable completion triggered by <c-x><c-o>
vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc" vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc"

View File

@@ -17,58 +17,70 @@ dap.configurations.fsharp = {
}, },
} }
local status, whichkey = pcall(require, "which-key") do
if status then local status, whichkey = pcall(require, "which-key")
whichkey.register({ if status then
d = { whichkey.register({
o = { dap.step_over, "Step over" }, d = {
i = { dap.step_into, "Step into" }, o = { dap.step_over, "Step over" },
c = { dap.continue, "Continue" }, i = { dap.step_into, "Step into" },
C = { dap.run_last, "Run with last debug configuration" }, c = { dap.continue, "Continue" },
b = { dap.toggle_breakpoint, "Toggle breakpoint" }, C = { dap.run_last, "Run with last debug configuration" },
r = { dap.repl.open, "Open debug repl" }, b = { dap.toggle_breakpoint, "Toggle breakpoint" },
v = { r = { dap.repl.open, "Open debug repl" },
v = { v = {
function() v = {
dap_ui.hover() function()
end, dap_ui.hover()
"View value of expression under cursor", end,
}, "View value of expression under cursor",
s = { },
function() s = {
dap_ui.sidebar(dap_ui.scopes).open() function()
end, dap_ui.sidebar(dap_ui.scopes).open()
"View values of all variables in all scopes", end,
}, "View values of all variables in all scopes",
f = { },
function() f = {
dap_ui.sidebar(dap_ui.frames).open() function()
end, dap_ui.sidebar(dap_ui.frames).open()
"View stack frames", 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
}, { prefix = vim.api.nvim_get_var("maplocalleader") }) vim.api.nvim_set_keymap("n", "<localleader>do", ":lua require('dap').step_over()<CR>", { noremap = true })
else vim.api.nvim_set_keymap("n", "<localleader>di", ":lua require('dap').step_into()<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>do", ":lua require('dap').step_over()<CR>", { noremap = true }) vim.api.nvim_set_keymap("n", "<localleader>dc", ":lua require('dap').continue()<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>di", ":lua require('dap').step_into()<CR>", { noremap = true }) vim.api.nvim_set_keymap("n", "<localleader>dC", ":lua require('dap').run_last()<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<localleader>dc", ":lua require('dap').continue()<CR>", { noremap = true }) vim.api.nvim_set_keymap(
vim.api.nvim_set_keymap("n", "<localleader>dC", ":lua require('dap').run_last()<CR>", { noremap = true }) "n",
vim.api.nvim_set_keymap("n", "<localleader>db", ":lua require('dap').toggle_breakpoint()<CR>", { noremap = true }) "<localleader>db",
vim.api.nvim_set_keymap("n", "<localleader>dr", ":lua require('dap').repl.open()<CR>", { noremap = true }) ":lua require('dap').toggle_breakpoint()<CR>",
vim.api.nvim_set_keymap("n", "<localleader>dvv", ":lua require('dap.ui.widgets').hover()<CR>", { noremap = true }) { noremap = true }
vim.api.nvim_set_keymap( )
"n", vim.api.nvim_set_keymap("n", "<localleader>dr", ":lua require('dap').repl.open()<CR>", { noremap = true })
"<localleader>dvs", vim.api.nvim_set_keymap(
":lua require('dap.ui.widgets').sidebar(require('dap.ui.widgets').scopes).open()<CR>", "n",
{ noremap = true } "<localleader>dvv",
) ":lua require('dap.ui.widgets').hover()<CR>",
vim.api.nvim_set_keymap( { noremap = true }
"n", )
"<localleader>dvf", vim.api.nvim_set_keymap(
":lua require('dap.ui.widgets').sidebar(require('dap.ui.widgets').frames).open()<CR>", "n",
{ noremap = true } "<localleader>dvs",
) ":lua require('dap.ui.widgets').sidebar(require('dap.ui.widgets').scopes).open()<CR>",
vim.api.nvim_set_keymap("n", "<localleader>dt", ":lua require('dap').terminate()<CR>", { noremap = true }) { noremap = true }
)
vim.api.nvim_set_keymap(
"n",
"<localleader>dvf",
":lua require('dap.ui.widgets').sidebar(require('dap.ui.widgets').frames).open()<CR>",
{ noremap = true }
)
vim.api.nvim_set_keymap("n", "<localleader>dt", ":lua require('dap').terminate()<CR>", { noremap = true })
end
end end