~whynothugo/dotfiles

ref: f5ee7f9b7135aa34372c78f7e17738db33eaea25 dotfiles/home/.config/nvim/lua/mappings.lua -rw-r--r-- 5.6 KiB
f5ee7f9b — Hugo Osvaldo Barrera nvim: Drop usage of `vim.cmd` for highlight 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
-- Leader is recommended for user-defined bindings. See `:h leader`.
vim.g.mapleader = " "

-- Mouse ----------------------------------------------------------------------
-- Enable mouse support (which I really only use for scrolling).
vim.opt.mouse = "a"

-- Copy to PRIMARY selection on mouse selection.
vim.keymap.set("v", "<LeftRelease>", '"*ygv')

-- Yank to PRIMARY selection by default (PRIMARY = mouse2, CLIPBOARD = ctrl+v).
vim.opt.clipboard = "unnamed"
-------------------------------------------------------------------------------

-- Make < > shifts keep selection.
vim.keymap.set("v", "<", "<gv")
vim.keymap.set("v", ">", ">gv")

-- Disable arrow keys and PageDown/PageUp
vim.keymap.set("n", "<left>", "<nop>")
vim.keymap.set("n", "<right>", "<nop>")
vim.keymap.set("n", "<up>", "<nop>")
vim.keymap.set("n", "<down>", "<nop>")
vim.keymap.set("n", "<PageDown>", "<nop>")
vim.keymap.set("n", "<PageUp>", "<nop>")

-- Hide search result highlight:
vim.keymap.set("", "<Leader>f", ":nohlsearch<CR>", { remap = true })

-- Comment / uncomment blocks of code.
vim.keymap.set("", "<Leader>c", ":TComment<CR>", { remap = true })

-- Open the tree-sitter playground. Used for debugging its queries.
vim.keymap.set("", "<Leader>t", ":TSPlaygroundToggle<CR>", { remap = true })

vim.keymap.set("n", "<Leader>s", function()
  vim.o.spell = not vim.o.spell
  print("spell: " .. tostring(vim.o.spell))
end)

-- XXX: Add git root name here?
-- use  system('git rev-parse --show-toplevel 2> /dev/null') and some awk
vim.keymap.set("n", "<Leader>.", ":echo @%<CR>")

-- Switch split orientations --------------------------------------------------

-- From vertical split into horizontal split
vim.keymap.set("n", "<Leader>th", "<C-w>t<C-w>K")

-- From horitonzal split into vertical split
vim.keymap.set("n", "<Leader>tv", "<C-w>t<C-w>H")

-- Ctrl-w followed by H, J, K or L will move the current window to the far
-- left, bottom, top or right respectively (same directions as usual).
-------------------------------------------------------------------------------

-- Switch between autocompletion options with Tab (or Shift+Tab) --------------
vim.keymap.set("i", "<TAB>", [[pumvisible() ? "\<C-n>" : "\<TAB>"]], { silent = true, expr = true })
vim.keymap.set("i", "<S-TAB>", [[pumvisible() ? "\<C-p>" : "\<TAB>"]], { silent = true, expr = true })
-------------------------------------------------------------------------------

-- Helper keys to resize splits -----------------------------------------------
-- They Ctrl+<directional keys> (hjkl).
vim.keymap.set("n", "<C-h>", [[:vertical resize -1<CR>]], { silent = true })
vim.keymap.set("n", "<C-j>", [[:resize +1<CR>]], { silent = true })
vim.keymap.set("n", "<C-k>", [[:resize -1<CR>]], { silent = true })
vim.keymap.set("n", "<C-l>", [[:vertical resize +1<CR>]], { silent = true })
-------------------------------------------------------------------------------

-- FZF mapping
--
-- If in a git repo, shows a file-picker for all non-ignored files. Untracked
-- failes are included too. If outside a git repo, just all files. Dotfiles are
-- always included.
--
-- TODO: It'd be great if matches in pwd are weighted higher.
vim.keymap.set("n", "<Leader>p", require("_fzf").show_fuzzy_finder, { silent = true })

local function breakPoint()
  local ft = vim.bo.filetype
  local esc = vim.api.nvim_replace_termcodes("<ESC>", true, false, true)

  if ft == "python" then
    vim.fn.feedkeys("oimport ipdb; ipdb.set_trace()  # fmt: skip" .. esc)
  elseif ft == "ruby" then
    vim.fn.feedkeys("obinding.pry" .. esc)
  end
end

vim.keymap.set("n", "<Leader>i", breakPoint)

-- LSP mappings ---------------------------------------------------------------
-- These are configured even for buffers with no LSP. Otherwise, they mappings
-- are inactive when there's no LSP, and pressing them yields unexpected
-- results. Having the mapping results in an error, which is less unexpected
-- than keys not being mapped and having a different meaning.

vim.keymap.set("n", "gd", vim.lsp.buf.definition, { remap = true })
vim.keymap.set("n", "gD", vim.lsp.buf.declaration, { remap = true })
vim.keymap.set("n", "<Leader>h", vim.lsp.buf.hover, { remap = true })
-- TODO: focus the hover float after creating it.
-- TODO: Also map <Esc> to closing it. The goal is to be able to easily scroll AND dismiss it.
vim.keymap.set("n", "<Leader>r", "<cmd>Trouble lsp_references<CR>", { remap = true })
vim.keymap.set("n", "<Leader>e", "<cmd>TroubleToggle document_diagnostics group=false<CR>", { remap = true })

-- TODO: goto_next calls vim.diagnostic.show_line_diagnostics
-- Overrride that to make it wrap and also maybe use a split.
-- https://www.reddit.com/r/neovim/comments/mxiltm/why_are_so_many_plugins_tied_to_using_floating/gvqrdjl/?context=3

-- Should be using these, which match the defaults...
vim.keymap.set({ "n", "v" }, "[d", function()
  vim.diagnostic.goto_prev({ float = false })
end, { remap = true })

vim.keymap.set({ "n", "v" }, "]d", function()
  vim.diagnostic.goto_next({ float = false })
end, { remap = true })

-- ... actually use these due to memory muscle:
vim.keymap.set({ "n", "v" }, "<Leader>n", function()
  vim.diagnostic.goto_next({ float = false })
end, { remap = true })

vim.keymap.set({ "n", "v" }, "<Leader>N", function()
  vim.diagnostic.goto_prev({ float = false })
end, { remap = true })

-- Autoformatting
vim.keymap.set({ "n", "v" }, "<Leader>a", function()
  vim.lsp.buf.formatting()
end, { remap = true })

-- Show available code actions:
vim.keymap.set({ "n", "v" }, "<Leader>A", function()
  vim.lsp.buf.code_action()
end, { remap = true })
-------------------------------------------------------------------------------