~whynothugo/dotfiles

ref: 38b900dc12ae810eda071e6fd1e4f25db6bb3a2a dotfiles/home/.config/nvim/lua/mappings.lua -rw-r--r-- 6.8 KiB
38b900dc — Hugo Osvaldo Barrera nvim: Add borders to hover float windows 3 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
-- 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, desc = "Stop highlighting search results" }
)

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

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

vim.keymap.set("n", "<Leader>s", function()
  vim.opt_local.spell = not(vim.opt_local.spell:get())
  print("spell: " .. tostring(vim.opt_local.spell:get()))
end, { desc = "Toggle spellchecker" })

-- 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>", { desc = "Echo filepath" })

-- 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, desc = "Shirk window to the left" }
)
vim.keymap.set(
  "n",
  "<C-j>",
  [[:resize +1<CR>]],
  { silent = true, desc = "Grow window downwards" }
)
vim.keymap.set(
  "n",
  "<C-k>",
  [[:resize -1<CR>]],
  { silent = true, desc = "Shrink window upwards" }
)
vim.keymap.set(
  "n",
  "<C-l>",
  [[:vertical resize +1<CR>]],
  { silent = true, desc = "Grow window to the right" }
)
-------------------------------------------------------------------------------

-- 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 files in pwd are weighted higher.
local function fuzzy_finder()
  -- Exclude currently focused file from the list:
  local current = vim.fn.expand("%")

  -- "hidden" includes dotfiles, but not git-ignored files and alike.
  local cmd = "fd --type f --type symlink --hidden"
  if current ~= "" then
    -- TODO: would be great to have an "extra_opts" flag in fzf
    cmd = string.format("%s --exclude %s", cmd, vim.fn.shellescape(current))
  end

  require("fzf-lua").files({ cmd = cmd })
end
vim.keymap.set("n", "<Leader>p", fuzzy_finder, { desc = "Open fuzzy finder" })

-- Inserts breakpoint.
-- Only implemented for Python and Ruby. `i` is a reference to `ipdb`.
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, { desc = "Insert a breakpoint" })

-- LSP mappings ---------------------------------------------------------------
-- These are configured even for buffers with no LSP. Otherwise, the 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, desc = "Show hover details. Use twice to focus hover." }
)
vim.keymap.set("n", "<Leader>r", function()
  require("trouble").toggle("lsp_references")
end, { desc = "Show LSP references" })
vim.keymap.set("n", "<Leader>e", function()
  require("trouble").toggle({ "document_diagnostics", group = false })
end, { desc = "Show LSP errors/diagnostics" })

-- 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, desc = "Jump to previous diagnostic" })

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

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

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

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

-- Show available code actions:
vim.keymap.set({ "n", "v" }, "<Leader>A", function()
  require("fzf-lua").lsp_code_actions()
end, { remap = true, desc = "Apply LSP action" })
-------------------------------------------------------------------------------