-- Every key the IDE adds, in one table: the mappings, the hints that appear after , the -- cheat sheet (:LLeMbasIdeKeys) and the table in the README are all made from it. -- The leader is . Neovim's own keys are unchanged unless listed here. -- -- LLeMbas's own keys are not written here a second time: they are the LLeMbas plugin's key table -- (lua/llembas/keys.lua), which maps them, feeds the bottom bar, and is read below for the cheat -- sheet. Each action there has two forms: a chord (Ctrl+Shift+A …) that arrives from a terminal -- with the kitty keyboard protocol, and a key after l that always arrives. local M = {} local function pick(name, opts) return function() require("mini.pick").builtin[name](opts) end end local function extra(name, opts) return function() require("mini.extra").pickers[name](opts) end end local function problem(count) return function() if vim.diagnostic.jump then return vim.diagnostic.jump({ count = count, float = true }) end if count > 0 then vim.diagnostic.goto_next() else vim.diagnostic.goto_prev() end -- Neovim 0.10 end end local function mod(module, fn) return function(...) return require(module)[fn](...) end end M.groups = { { "f", "find" }, { "b", "buffer" }, { "c", "code" }, { "g", "git" }, { "l", "LLeMbas" }, { "s", "session" }, { "t", "terminal" }, { "u", "look" }, { "x", "problems" }, { "i", "IDE" }, } -- { modes, keys, action, what it does, section } M.map = { -- Files and search { "n", "", pick("files"), "Find a file", "Find" }, { "n", "/", pick("grep_live"), "Search in files (needs rg)", "Find" }, { "n", ",", pick("buffers"), "Switch buffer", "Find" }, { "n", "e", function() require("llembas.layout").toggle("tree") end, "File tree (the left bar), show and hide", "Find" }, { "n", "E", function() require("mini.files").open(vim.api.nvim_buf_get_name(0)) end, "Directories side by side, at this file (edit them like text)", "Find" }, { "n", "ff", pick("files"), "Find a file", "Find" }, { "n", "fg", pick("grep_live"), "Search in files (needs rg)", "Find" }, { "n", "fw", function() require("mini.pick").builtin.grep({ pattern = vim.fn.expand("") }) end, "Search the word under the cursor", "Find" }, { "n", "fb", pick("buffers"), "Open buffers", "Find" }, { "n", "fr", extra("oldfiles"), "Recent files", "Find" }, { "n", "fs", extra("lsp", { scope = "document_symbol" }), "Symbols in this file", "Find" }, { "n", "fS", extra("lsp", { scope = "workspace_symbol" }), "Symbols in the project", "Find" }, { "n", "fR", extra("lsp", { scope = "references" }), "References to the symbol under the cursor", "Find" }, { "n", "fd", extra("diagnostic", { scope = "all" }), "Problems in the project", "Find" }, { "n", "fh", pick("help"), "Help pages", "Find" }, { "n", "fk", extra("keymaps"), "All key mappings", "Find" }, { "n", "f.", pick("resume"), "The last search again", "Find" }, -- Buffers and windows { "n", "bd", function() require("mini.bufremove").delete() end, "Close the buffer, keep the window", "Buffers" }, { "n", "bn", "bnext", "Next buffer", "Buffers" }, { "n", "bp", "bprevious", "Previous buffer", "Buffers" }, { "n", "", "bnext", "Next buffer", "Buffers" }, { "n", "", "bprevious", "Previous buffer", "Buffers" }, { "n", "w", "write", "Save", "Buffers" }, { "n", "q", "quit", "Close the window", "Buffers" }, { "n", "", "nohlsearch", "Clear the search highlight", "Buffers" }, -- Code { "n", "ca", function() vim.lsp.buf.code_action() end, "Code action", "Code" }, { "n", "cr", function() vim.lsp.buf.rename() end, "Rename the symbol", "Code" }, { { "n", "x" }, "cf", mod("llembas_ide.plugins", "format"), "Format the file or the selection", "Code" }, { "n", "cd", function() vim.diagnostic.open_float() end, "The problem on this line", "Code" }, { "n", "cl", mod("llembas_ide.lsp", "guide"), "Language servers: found, missing, how to install", "Code" }, -- Problems { "n", "xx", function() vim.diagnostic.setloclist() end, "Problems in this file, as a list", "Problems" }, { "n", "xw", function() vim.diagnostic.setqflist() end, "Problems in the project, as a list", "Problems" }, { "n", "xn", problem(1), "Next problem", "Problems" }, { "n", "xp", problem(-1), "Previous problem", "Problems" }, -- Git { "n", "gs", extra("git_files", { scope = "modified" }), "Changed files", "Git" }, { "n", "gh", extra("git_hunks"), "Changes, hunk by hunk", "Git" }, { "n", "gd", mod("llembas_ide.git", "diff_head"), "This file beside its committed version", "Git" }, { "n", "go", function() require("mini.diff").toggle_overlay(0) end, "Show changes inline, on and off", "Git" }, { "n", "ga", mod("llembas_ide.git", "stage_hunk"), "Stage the change under the cursor", "Git" }, { "n", "gr", mod("llembas_ide.git", "reset_hunk"), "Undo the change under the cursor", "Git" }, { "n", "gb", function() require("mini.git").show_at_cursor() end, "Who changed this line, and the commit", "Git" }, -- (LLeMbas: the plugin's own key table; see the top of this file.) -- Sessions { "n", "sr", mod("llembas_ide.sessions", "restore"), "Restore this directory's session", "Session" }, { "n", "sw", mod("llembas_ide.sessions", "save"), "Save the session now", "Session" }, { "n", "sd", mod("llembas_ide.sessions", "delete"), "Forget this directory's session", "Session" }, -- Terminal { "n", "tt", mod("llembas_ide.terminal", "toggle"), "Terminal, open and close", "Terminal" }, { { "n", "t" }, "", mod("llembas_ide.terminal", "toggle"), "Terminal, open and close", "Terminal" }, { { "n", "t" }, "", mod("llembas_ide.terminal", "toggle"), "The same, as some terminals send it", "Terminal" }, { "t", "", "", "Leave the terminal's typing mode", "Terminal" }, -- Look { "n", "ut", function() vim.o.background = vim.o.background == "dark" and "light" or "dark" end, "Dark and light theme", "Look" }, { "n", "uw", function() vim.wo.wrap = not vim.wo.wrap end, "Wrap long lines, on and off", "Look" }, { "n", "un", function() vim.wo.relativenumber = not vim.wo.relativenumber end, "Relative line numbers, on and off", "Look" }, -- The IDE itself { "n", "ih", mod("llembas_ide.dashboard", "open"), "Start screen", "IDE" }, { "n", "ik", function() M.show() end, "This list of keys", "IDE" }, { "n", "id", "LLeMbasIdeDoctor", "Check the installation", "IDE" }, { "n", "iu", "LLeMbasIdeConfig", "Open your own settings file", "IDE" }, { "n", "iv", "LLeMbasIdeVersion", "Versions", "IDE" }, } -- Added to a buffer when a language server attaches to it. M.lsp = { { "n", "gd", function() vim.lsp.buf.definition() end, "Go to the definition" }, { "n", "gD", function() vim.lsp.buf.declaration() end, "Go to the declaration" }, { "n", "gI", function() vim.lsp.buf.implementation() end, "Go to the implementation" }, { "n", "gy", function() vim.lsp.buf.type_definition() end, "Go to the type's definition" }, } -- Keys that are not the IDE's mappings but belong on the cheat sheet: plugins' and Neovim's own. M.also = { { "i", "", "Ask LLeMbas for a suggestion now (accepting one: under LLeMbas, below)", "Typing" }, { "i", " ", "Move in the completion menu (also )", "Typing" }, { "i", "", "Take the menu entry you moved to; otherwise a new line", "Typing" }, { "i", "", "Take the menu's first entry", "Typing" }, { "i", "", "Close the menu", "Typing" }, { "i", "", "Open the menu, or the entry's documentation", "Typing" }, { "n", "gcc", "Comment the line, on and off (gc with a motion or a selection)", "Editing" }, { "n", "sa sd sr", "Surround: add (saiw\"), delete (sd\"), replace (sr\"')", "Editing" }, { "n", "K", "Documentation of the symbol under the cursor", "Editing" }, { "n", "gd gD gI gy", "Definition, declaration, implementation, type (with a language server)", "Editing" }, { "n", "]g [g", "Next and previous git change (]G [G: last and first)", "Git" }, } --- LLeMbas's keys, from the plugin's table: { keys, where, what it does } for the cheat sheet. --- Both forms of an action are in the one cell, the chord first. local WHERE = { tree = "the tree", settings = "the settings lines", prompt = "the prompt", chat = "the transcript", change = "a proposed change", suggestion = "insert, with a suggestion", } function M.llembas_rows() local ok, keys = pcall(require, "llembas.keys") if not ok then return {} end local out = {} for _, r in ipairs(keys.rows()) do local both = {} if r.chord then both[#both + 1] = r.chord end if r.alt ~= "" then both[#both + 1] = r.alt end local where = WHERE[r.where] if not where then local names = {} for _, m in ipairs(r.modes) do names[#names + 1] = ({ n = "normal", x = "visual", i = "insert" })[m] or m end where = table.concat(names, ", ") -- The chord form is mapped in normal, visual and insert mode, whatever the other form is. if r.chord then local more = {} for _, m in ipairs({ "n", "x", "i" }) do if not vim.tbl_contains(r.modes, m) then more[#more + 1] = ({ n = "normal", x = "visual", i = "insert" })[m] end end if #more > 0 then where = where .. "; the chord also in " .. table.concat(more, ", ") end end end if #both > 0 then out[#out + 1] = { table.concat(both, " or "), where, r.desc } end end return out end local function modes(m) return type(m) == "table" and m or { m } end --- Bindings given more than once (the same mode and keys), as text. Empty when the map is sound. function M.duplicates() local seen, dup = {}, {} -- LLeMbas's keys that are mapped everywhere count too: nothing here may take one of them. local ok, llembas = pcall(require, "llembas.keys") for _, k in ipairs(ok and llembas.table or {}) do if k.run and not k.virtual then local function take(mode, lhs) seen[mode .. " " .. vim.api.nvim_replace_termcodes(llembas.lhs(lhs), true, true, true)] = true end for _, lhs in ipairs(type(k.chord) == "table" and k.chord or { k.chord }) do for _, mode in ipairs({ "n", "x", "i" }) do take(mode, lhs) end end if not k.scope then for _, lhs in ipairs(llembas.alts(k)) do for _, mode in ipairs(k.modes or { "n" }) do take(mode, lhs) end end end end end for _, list in ipairs({ M.map, M.lsp }) do for _, k in ipairs(list) do for _, mode in ipairs(modes(k[1])) do local id = mode .. " " .. vim.api.nvim_replace_termcodes(k[2], true, true, true) if seen[id] then table.insert(dup, mode .. " " .. k[2]) end seen[id] = true end end end return dup end function M.setup() for _, k in ipairs(M.map) do vim.keymap.set(k[1], k[2], k[3], { desc = k[4], silent = type(k[3]) ~= "string" or k[3]:sub(1, 1) ~= ":" }) end end local function shown(lhs) return (lhs:gsub("", "")) end local mode_names = { n = "normal", x = "visual", i = "insert", t = "terminal" } local function mode_text(m) local out = {} for _, mode in ipairs(modes(m)) do table.insert(out, mode_names[mode] or mode) end return table.concat(out, ", ") end --- The sections of the cheat sheet in order: { name, { {keys, modes, text}, … } }. function M.sections() local order, by = {}, {} local function add(section, keys, mode, text) if not by[section] then by[section] = {} table.insert(order, section) end table.insert(by[section], { shown(keys), mode_text(mode), text }) end for _, k in ipairs(M.also) do if k[4] == "Typing" or k[4] == "Editing" then add(k[4], k[2], k[1], k[3]) end end for _, k in ipairs(M.map) do add(k[5], k[2], k[1], k[4]) end for _, k in ipairs(M.also) do if k[4] ~= "Typing" and k[4] ~= "Editing" then add(k[4], k[2], k[1], k[3]) end end local out = {} for _, name in ipairs(order) do table.insert(out, { name, by[name] }) -- LLeMbas's own section stands after Git, where it always stood. if name == "Git" then local rows = {} for _, r in ipairs(M.llembas_rows()) do table.insert(rows, { shown(r[1]), r[2], r[3] }) end if #rows > 0 then table.insert(out, { "LLeMbas", rows }) end end end return out end --- The cheat sheet as the README has it. function M.markdown() local out = {} for _, s in ipairs(M.sections()) do vim.list_extend(out, { "**" .. s[1] .. "**", "", "| Keys | In | What it does |", "|---|---|---|" }) for _, r in ipairs(s[2]) do table.insert(out, ("| `%s` | %s | %s |"):format(r[1]:gsub("|", "\\|"), r[2], (r[3]:gsub("|", "\\|")))) end table.insert(out, "") end return out end --- :LLeMbasIdeKeys function M.show() local lines, titles = { "The leader key is . Press it and wait: the next keys are shown.", "" }, {} for _, s in ipairs(M.sections()) do table.insert(lines, s[1]) table.insert(titles, #lines - 1) -- The keys' column is as wide as this section's longest, so every row of it shares one edge. local kw, ww = 16, 16 for _, r in ipairs(s[2]) do kw, ww = math.max(kw, vim.fn.strdisplaywidth(r[1])), math.max(ww, vim.fn.strdisplaywidth(r[2])) end for _, r in ipairs(s[2]) do table.insert(lines, " " .. r[1] .. (" "):rep(kw - vim.fn.strdisplaywidth(r[1]) + 1) .. r[2] .. (" "):rep(ww - vim.fn.strdisplaywidth(r[2]) + 1) .. r[3]) end table.insert(lines, "") end local buf = require("llembas_ide.ui").page("LLeMbas IDE: keys", lines) for _, l in ipairs(titles) do vim.api.nvim_buf_add_highlight(buf, -1, "Title", l, 0, -1) end for i = 2, #lines - 1 do if lines[i + 1]:sub(1, 2) == " " then local keys_end = lines[i + 1]:find(" ", 3, true) or 18 vim.api.nvim_buf_add_highlight(buf, -1, "LLeMbasIdeKey", i, 2, keys_end) end end end return M