Add basic mappings

pull/70/head
Marcelo Jacobus 2024-06-06 15:44:06 -03:00
parent 738f91b5f8
commit a7a17af9c0
5 changed files with 166 additions and 0 deletions

View File

@ -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", "<Esc>", { desc = "Go to normal mode" })
map("n", "<leader>w", "<cmd>write<cr>", { desc = "Save file" })
local alternative_file = require("mj.alternate_file")
map("n", "<leader>ak", function()
alternative_file.open("next", "--exists")
end, { desc = "Open next alternative file" })
map("n", "<leader>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", "<leader>Q", "<cmd>q!<cr>", { desc = "Quit" })
map("n", "<leader>q", "<cmd>bdelete<cr>", { desc = "Close buffer" })
map("n", "<leader>bdd", "<cmd>bdelete!<cr>", { desc = "Close buffer!" })
map("n", "<leader>bda", "<cmd>bufdo %bd!<cr>", { desc = "Close all buffers!" })
-- Notes
map("n", "<leader>et", ":e ~/.tmp/notes/", { desc = "Edit notes" })
map("n", "<leader>on", ":e ~/.tmp/notes/notes.md<cr>", { desc = "Open notes" })
map("n", "<leader>con", ":e ~/.tmp/notes/notes.md<cr>", { desc = "Open notes" })
map("n", "<leader>j", function()
CloseTerminalBuffer(true)
end, { desc = "Close terminal buffer (true)" })
map("n", "<leader>k", function()
CloseTerminalBuffer(false)
end, { desc = "Close terminal buffer (false)" })
-- Search mappings
map("n", "<leader>f", "/", { desc = "Search alias" })
map("n", "<leader>F", "<cmd>nohlsearch<cr>", { desc = "Remove search highlight" })
-- Alternative file
map("n", "<leader>af", "<c-^>", { desc = "Alternative file" })
-- Avoid arrow keys in command mode
map("c", "<C-h>", "<left>", { desc = "Move left" })
map("c", "<C-j>", "<down>", { desc = "Move down" })
map("c", "<C-k>", "<up>", { desc = "Move up" })
map("c", "<C-l>", "<right>", { desc = "Move right" })
map("c", "<C-x>", "<del>", { desc = "Delete" })
-- Alternative file navigation
map("n", "<leader>ak", function()
alternative_file.open("next", "--exists")
end, { desc = "Next alternative file" })
map("n", "<leader>aj", function()
alternative_file.open("prev", "--exists")
end, { desc = "Previous alternative file" })
-- Diagnostic config
vim.diagnostic.config({ virtual_text = false })
-- Running tests
map("n", "<Leader>T", ":lua RunTestFile()<CR>", { desc = "Run test file" })
map("n", "<Leader>t", ":lua RunTestLineByLine()<CR>", { desc = "Run test for current line" })
map("n", "<Leader>at", ":lua RunAllTests()<CR>", { desc = "Run all tests" })
map("n", "s.t", ":lua RunShellTest()<CR>", { desc = "Run ./shell_test" })

View File

@ -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

View File

@ -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

50
lua/mj/globals.lua 100644
View File

@ -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

5
lua/mj/mj.lua 100644
View File

@ -0,0 +1,5 @@
local M = {}
M.alternate_file = require("mj.alternate_file")
return M