diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 2c134f7..df6937a 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -1,3 +1,71 @@ -- Keymaps are automatically loaded on the VeryLazy event -- Default keymaps that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/keymaps.lua -- Add any additional keymaps here + +local map = LazyVim.safe_keymap_set + +-- better up/down +map("i", "jj", "", { desc = "Go to normal mode" }) +map("n", "w", "write", { desc = "Save file" }) + +local alternative_file = require("mj.alternate_file") + +map("n", "ak", function() + alternative_file.open("next", "--exists") +end, { desc = "Open next alternative file" }) + +map("n", "aj", function() + alternative_file.open("prev", "--exists") +end, { desc = "Open previous alternative file" }) + +-- Import the map function from LazyVim +local map = LazyVim.safe_keymap_set + +-- Key mappings +map("n", "Q", "q!", { desc = "Quit" }) +map("n", "q", "bdelete", { desc = "Close buffer" }) +map("n", "bdd", "bdelete!", { desc = "Close buffer!" }) +map("n", "bda", "bufdo %bd!", { desc = "Close all buffers!" }) + +-- Notes +map("n", "et", ":e ~/.tmp/notes/", { desc = "Edit notes" }) +map("n", "on", ":e ~/.tmp/notes/notes.md", { desc = "Open notes" }) +map("n", "con", ":e ~/.tmp/notes/notes.md", { desc = "Open notes" }) + +map("n", "j", function() + CloseTerminalBuffer(true) +end, { desc = "Close terminal buffer (true)" }) +map("n", "k", function() + CloseTerminalBuffer(false) +end, { desc = "Close terminal buffer (false)" }) + +-- Search mappings +map("n", "f", "/", { desc = "Search alias" }) +map("n", "F", "nohlsearch", { desc = "Remove search highlight" }) + +-- Alternative file +map("n", "af", "", { desc = "Alternative file" }) + +-- Avoid arrow keys in command mode +map("c", "", "", { desc = "Move left" }) +map("c", "", "", { desc = "Move down" }) +map("c", "", "", { desc = "Move up" }) +map("c", "", "", { desc = "Move right" }) +map("c", "", "", { desc = "Delete" }) + +-- Alternative file navigation +map("n", "ak", function() + alternative_file.open("next", "--exists") +end, { desc = "Next alternative file" }) +map("n", "aj", function() + alternative_file.open("prev", "--exists") +end, { desc = "Previous alternative file" }) + +-- Diagnostic config +vim.diagnostic.config({ virtual_text = false }) + +-- Running tests +map("n", "T", ":lua RunTestFile()", { desc = "Run test file" }) +map("n", "t", ":lua RunTestLineByLine()", { desc = "Run test for current line" }) +map("n", "at", ":lua RunAllTests()", { desc = "Run all tests" }) +map("n", "s.t", ":lua RunShellTest()", { desc = "Run ./shell_test" }) diff --git a/lua/mj/alternate_file.lua b/lua/mj/alternate_file.lua new file mode 100644 index 0000000..878b6b1 --- /dev/null +++ b/lua/mj/alternate_file.lua @@ -0,0 +1,21 @@ +local runner = require("mj.command_runner") +M = {} + +M.open = function(subcommand, options) + local file_path = vim.fn.expand("%") + file_path = vim.fn.fnamemodify(file_path, ":~:.") + local files = mj_alternative_file(file_path, subcommand, options) + files = vim.split(files, " ") + local file = files[1] + + if file ~= "" then + vim.api.nvim_command("e " .. file) + end +end + +function mj_alternative_file(file, subcommand, options) + local cmd = "mj alternative_file " .. subcommand .. " " .. file .. " " .. options + return runner.run(cmd) +end + +return M diff --git a/lua/mj/command_runner.lua b/lua/mj/command_runner.lua new file mode 100644 index 0000000..79d6353 --- /dev/null +++ b/lua/mj/command_runner.lua @@ -0,0 +1,22 @@ +M = {} + +M.run = function(cmd) + print("cmd: " .. cmd) + local f = io.popen(cmd) + local s = f:read('*a') + f:close() + return s +end + + +M.run_in_terminal = function(cmd, open_new_tab) + if open_new_tab then + vim.cmd('tabnew') + -- vim.cmd('tabnext') + end + + print("cmd: " .. cmd) + vim.api.nvim_command(":terminal time " .. cmd) +end + +return M diff --git a/lua/mj/globals.lua b/lua/mj/globals.lua new file mode 100644 index 0000000..bfb6c6f --- /dev/null +++ b/lua/mj/globals.lua @@ -0,0 +1,50 @@ +local M = {} + +local runner = require("mj.command_runner") + +function RunInTerminal(cmd, open_new_tab) + runner.run_in_terminal(cmd, open_new_tab) +end + +function RunTestFile() + local current_path = vim.fn.expand("%") + local cmd = "run_test " .. current_path + RunInTerminal(cmd, true) +end + +function RunTestLineByLine() + local current_line, _ = unpack(vim.api.nvim_win_get_cursor(0)) + local current_path = vim.fn.expand("%") + local cmd = "run_test " .. current_path .. " --line=" .. current_line + RunInTerminal(cmd, true) +end + +function RunAllTests() + local current_path = vim.fn.expand("%") + local cmd = "run_test " .. current_path .. " --all" + RunInTerminal(cmd, true) +end + +function CloseTerminalBuffer(new_tab) + local name = vim.api.nvim_buf_get_name(0) + + if string.find(name, "term://") then + if not new_tab then + local number = vim.api.nvim_buf_get_number(0) + vim.api.nvim_exec("buffer #", true) + vim.api.nvim_buf_delete(number, { force = true }) + return + end + + vim.api.nvim_command("bdelete!") + end +end + +function RunShellTest() + RunInTerminal("./shell_test", true) +end + +function RunRubocop() + local file = vim.fn.expand("%") + RunInTerminal("bundle exec rubocop -A " .. file, false) +end diff --git a/lua/mj/mj.lua b/lua/mj/mj.lua new file mode 100644 index 0000000..3ad4036 --- /dev/null +++ b/lua/mj/mj.lua @@ -0,0 +1,5 @@ +local M = {} + +M.alternate_file = require("mj.alternate_file") + +return M