1 local function map(mode, lhs, rhs, opts) |
1 local function map(mode, lhs, rhs, desc, opts) |
2 opts = opts or {} |
2 opts = opts or {} |
3 opts.silent = opts.silent ~= false |
3 opts.silent = opts.silent ~= false |
|
4 opts.desc = desc |
4 vim.keymap.set(mode, lhs, rhs, opts) |
5 vim.keymap.set(mode, lhs, rhs, opts) |
5 end |
6 end |
6 |
7 |
7 map("n", "<F2>", ":Neotree reveal<CR>") |
8 map("n", "<F2>", ":Neotree reveal<CR>") |
8 map("n", "<F3>", "n") |
9 map("n", "<F3>", "n") |
9 map("n", "<S-F3>", "N") |
10 map("n", "<S-F3>", "N") |
10 map("", "<F4>", ":let v:hlsearch = !v:hlsearch<CR>") |
11 map("", "<F4>", ":let v:hlsearch = !v:hlsearch<CR>", "Toggle Search Highlight") |
11 map("n", "<F5>", ":UndotreeToggle<CR>") |
12 map("n", "<F5>", ":UndotreeToggle<CR>") |
12 map("n", "<F8>", ":TagbarToggle<CR>") |
13 map("n", "<F8>", ":TagbarToggle<CR>") |
13 |
14 |
14 -- Allow :noh even in insert mode |
15 -- Allow :noh even in insert mode |
15 map("i", "<F4>", "<C-O><F4>") |
16 map("i", "<F4>", "<C-O><F4>") |
16 |
17 |
17 -- cover for search habit |
18 -- cover for search habit |
18 map("c", "<F3>", "<CR>") |
19 map("c", "<F3>", "<CR>") |
19 |
20 |
20 -- change to file's directory |
21 -- change to file's directory |
21 map("n", "<leader>cd", ":cd %:p:h<CR>:pwd<CR>") |
22 map("n", "<leader>cd", ":cd %:p:h<CR>:pwd<CR>", "Chdir to buffer dir") |
22 |
23 |
23 -- window switching |
24 -- window switching |
24 map("n", "<C-h>", "<C-w>h") |
25 map("n", "<C-h>", "<C-w>h") |
25 map("n", "<C-j>", "<C-w>j") |
26 map("n", "<C-j>", "<C-w>j") |
26 map("n", "<C-k>", "<C-w>k") |
27 map("n", "<C-k>", "<C-w>k") |
32 map("n", "<C-Down>", "<cmd>resize -2<cr>") |
33 map("n", "<C-Down>", "<cmd>resize -2<cr>") |
33 map("n", "<C-Left>", "<cmd>vertical resize -2<cr>") |
34 map("n", "<C-Left>", "<cmd>vertical resize -2<cr>") |
34 map("n", "<C-Right>", "<cmd>vertical resize +2<cr>") |
35 map("n", "<C-Right>", "<cmd>vertical resize +2<cr>") |
35 |
36 |
36 -- buffer switching |
37 -- buffer switching |
37 map("n", "gb", "<C-^>") |
38 map("n", "gb", "<C-^>", "Jump to # buffer") |
38 map("n", "gB", ":ls<CR>:b ", { silent = false }) |
39 map("n", "gB", ":ls<CR>:b ", "List and switch buffers", { silent = false }) |
39 |
40 |
40 -- Select last paste, in the same mode it was pasted in |
41 -- Select last paste, in the same mode it was pasted in |
41 map("n", "gV", "'`[' . strpart(getregtype(), 0, 1) . '`]'", { expr = true }) |
42 map("n", "gV", "'`[' . strpart(getregtype(), 0, 1) . '`]'", "", { expr = true }) |
42 |
43 |
43 -- Add undo break-points |
44 -- Add undo break-points |
44 map("i", ",", ",<c-g>u") |
45 map("i", ",", ",<c-g>u") |
45 map("i", ".", ".<c-g>u") |
46 map("i", ".", ".<c-g>u") |
46 map("i", ";", ";<c-g>u") |
47 map("i", ";", ";<c-g>u") |
47 |
48 |
48 map("n", "j", "v:count == 0 ? 'gj' : 'j'", { expr = true }) |
49 map("n", "j", "v:count == 0 ? 'gj' : 'j'", "", { expr = true }) |
49 map("n", "k", "v:count == 0 ? 'gk' : 'k'", { expr = true }) |
50 map("n", "k", "v:count == 0 ? 'gk' : 'k'", "", { expr = true }) |
50 |
51 |
51 -- Use ltag over tselect |
52 -- Use ltag over tselect |
52 map("n", "g<C-]>", ":exe 'ltag ' . expand('<cword>') | lopen<CR>") |
53 map("n", "g<C-]>", ":exe 'ltag ' . expand('<cword>') | lopen<CR>", "List and next Tag") |
53 |
54 |
54 -- clear all interestingwords with \\k since \K is ri.vim |
55 -- clear all interestingwords with \\k since \K is ri.vim |
55 map("n", "<leader><leader>k", ":call UncolorAllWords()<CR>") |
56 map("n", "<leader><leader>k", ":call UncolorAllWords()<CR>") |
56 |
57 |
57 -- use Grep for a recursive * |
58 -- use Grep for a recursive * |
58 map("n", "g*", ":Grep<CR>") |
59 map("n", "g*", ":Grep<CR>", "Recursive keyword search") |
59 |
60 |
60 -- K: doc, gKK: doc current filename |
61 -- K: doc, gKK: doc current filename |
61 map("n", "gKK", ":call ViewDoc('doc', expand('%:p'))<CR>") |
62 map("n", "gKK", ":call ViewDoc('doc', expand('%:p'))<CR>", "ViewDoc current buffer") |
62 |
63 |
63 -- Tabular shortcuts |
64 -- Tabular shortcuts |
64 map("n", "<leader>ta", ":Tabularize first_arrow<CR>") |
65 map("n", "<leader>ta", ":Tabularize first_arrow<CR>", "Align =>") |
65 map("n", "<leader>te", ":Tabularize first_eq<CR>") |
66 map("n", "<leader>te", ":Tabularize first_eq<CR>", "Align =") |
66 map("n", "<leader>tc", ":Tabularize first_colon<CR>") |
67 map("n", "<leader>tc", ":Tabularize first_colon<CR>", "Align :") |
67 map("n", "<leader>tm", ":Tabularize methods<CR>") |
68 map("n", "<leader>tm", ":Tabularize methods<CR>", "Align -> or .") |
68 |
69 |
69 map("n", "<leader>a", function() |
70 map("n", "<leader>a", function() |
70 local fo = vim.bo.formatoptions |
71 local fo = vim.bo.formatoptions |
71 if fo:find("a") then |
72 if fo:find("a") then |
72 vim.bo.formatoptions = fo:gsub("a", "") |
73 vim.bo.formatoptions = fo:gsub("a", "") |
73 vim.print("-a") |
74 vim.print("-a") |
74 else |
75 else |
75 vim.bo.formatoptions = fo .. "a" |
76 vim.bo.formatoptions = fo .. "a" |
76 vim.print("+a") |
77 vim.print("+a") |
77 end |
78 end |
78 end) |
79 end, "Toggle autowrap") |
79 |
80 |
80 -- LSP features |
81 -- LSP features |
81 |
82 |
82 vim.api.nvim_create_autocmd("LspAttach", { |
83 vim.api.nvim_create_autocmd("LspAttach", { |
83 group = vim.api.nvim_create_augroup("lsp_attach", { clear = true }), |
84 group = vim.api.nvim_create_augroup("lsp_attach", { clear = true }), |
84 callback = function(args) |
85 callback = function(args) |
85 local bufopts = { buffer = args.buf } |
86 local function bmap(mode, lhs, rhs, desc, opts) |
|
87 opts = opts or {} |
|
88 opts.buffer = args.buf |
|
89 map(mode, lhs, rhs, desc, opts) |
|
90 end |
86 |
91 |
87 map("n", "<leader>d", vim.diagnostic.open_float, bufopts) |
92 bmap("n", "<leader>d", vim.diagnostic.open_float, "Toggle Diag Float") |
88 map("n", "<leader>ld", vim.diagnostic.setqflist, bufopts) |
93 bmap("n", "<leader>ld", vim.diagnostic.setqflist, "List Diagnostics") |
89 map("n", "[d", function() |
94 bmap("n", "[d", function() |
90 vim.diagnostic.goto_prev({ float = false }) |
95 vim.diagnostic.goto_prev({ float = false }) |
91 end, bufopts) |
96 end, "Next Diagnostic") |
92 map("n", "]d", function() |
97 bmap("n", "]d", function() |
93 vim.diagnostic.goto_next({ float = false }) |
98 vim.diagnostic.goto_next({ float = false }) |
94 end, bufopts) |
99 end, "Previous Diagnostic") |
95 |
100 |
96 map("n", "gD", vim.lsp.buf.declaration, bufopts) |
101 bmap("n", "gD", vim.lsp.buf.declaration, "Go to Declaration") |
97 map("n", "gd", vim.lsp.buf.definition, bufopts) |
102 bmap("n", "gd", vim.lsp.buf.definition, "Go to Definition") |
98 map("n", "K", vim.lsp.buf.hover, bufopts) |
103 bmap("n", "K", vim.lsp.buf.hover, "LSP Hover") |
99 map("n", "gi", vim.lsp.buf.implementation, bufopts) |
104 bmap("n", "gi", vim.lsp.buf.implementation, "Go to Implementation") |
100 map("i", "<C-S>", vim.lsp.buf.signature_help, bufopts) |
105 bmap("i", "<C-S>", vim.lsp.buf.signature_help, "Toggle Signature Help") |
101 map("n", "<leader>wa", vim.lsp.buf.add_workspace_folder, bufopts) |
106 bmap("n", "<leader>wa", vim.lsp.buf.add_workspace_folder, "Add Workspace Folder") |
102 map("n", "<leader>wr", vim.lsp.buf.remove_workspace_folder, bufopts) |
107 bmap("n", "<leader>wr", vim.lsp.buf.remove_workspace_folder, "Remove Workspace Folder") |
103 map("n", "<leader>wl", function() |
108 bmap("n", "<leader>wl", function() |
104 print(vim.inspect(vim.lsp.buf.list_workspace_folders())) |
109 print(vim.inspect(vim.lsp.buf.list_workspace_folders())) |
105 end, bufopts) |
110 end, "List Workspace Folders") |
106 map("n", "<leader>D", vim.lsp.buf.type_definition, bufopts) |
111 bmap("n", "<leader>D", vim.lsp.buf.type_definition, "Go to Type Definition") |
107 map("n", "crn", vim.lsp.buf.rename, bufopts) |
112 bmap("n", "crn", vim.lsp.buf.rename, "LSP Rename") |
108 map("n", "gr", vim.lsp.buf.references, bufopts) |
113 bmap("n", "<leader>lr", vim.lsp.buf.references, "List References") |
109 map({ "n", "x" }, "<leader>f", function() |
114 bmap({ "n", "x" }, "<leader>f", function() |
110 vim.lsp.buf.format({ async = true }) |
115 vim.lsp.buf.format({ async = true }) |
111 end, bufopts) |
116 end, "LSP Format") |
112 |
117 |
113 local code_actions = require("actions-preview").code_actions |
118 local code_actions = require("actions-preview").code_actions |
114 map("n", "crr", code_actions, bufopts) |
119 bmap("n", "crr", code_actions, "Code Actions") |
115 map("x", "<C-R><C-R>", code_actions, bufopts) |
120 bmap("x", "<C-R><C-R>", code_actions, "Code Actions") |
116 map("x", "<C-R>", code_actions, bufopts) |
121 bmap("x", "<C-R>", code_actions, "Code Actions") |
117 end, |
122 end, |
118 }) |
123 }) |