config/nvim/lua/maps.lua (view raw)
1local cmd = vim.cmd
2local map = vim.keymap.set
3local u = require 'utils'
4local M = {}
5
6-- map the leader key
7map('n', '<Space>', '', {})
8vim.g.mapleader = ' '
9
10
11local options = { silent = true }
12map('n', '<leader><esc>', ':nohlsearch<cr>', options)
13map('n', '<leader>n', ':bnext<cr>', options)
14map('n', '<leader>p', ':bprev<cr>', options)
15
16-- search matches in the middle of the window
17map('n', 'n', 'nzzzv', options)
18map('n', 'N', 'Nzzzv', options)
19
20-- remain in visual after indenting
21map('v', '<', '<gv', options)
22map('v', '>', '>gv', options)
23
24-- Not an editor command: Wqa
25cmd(':command! WQ wq')
26cmd(':command! WQ wq')
27cmd(':command! Wq wq')
28cmd(':command! Wqa wqa')
29cmd(':command! W w')
30cmd(':command! Q q')
31
32local function fzy_ignore(patterns)
33 pattern_cmd = {}
34 for _, p in ipairs(patterns) do
35 table.insert(pattern_cmd, string.format("! -path '%s'", p))
36 end
37
38 return table.concat(pattern_cmd, ' ')
39end
40
41-- fzy mappings
42if vim.fn.executable('fzy') then
43 _G.fzy_edit = require('fzy.edit').fzy_edit
44 map(
45 '',
46 '<leader>e',
47 string.format(
48 ':call v:lua.fzy_edit("find -L . -type f %s | cut -c3-")<cr>',
49 fzy_ignore{'*.git/*', '*node_modules*', '*.pyc', '*migrations*', '*result*'}
50 ),
51 { noremap=true, silent=true }
52 )
53
54 _G.fzy_buffers = require('fzy.buffers').fzy_buffers
55 map('',
56 '<leader>b',
57 ':call v:lua.fzy_buffers()<cr>',
58 { noremap=true, silent=true }
59 )
60
61 _G.fzy_jmp = require('fzy.jump').fzy_jmp
62 map('',
63 '<leader>f',
64 ':call v:lua.fzy_jmp()<cr>',
65 { noremap=true, silent=true}
66 )
67else
68 print('fzy not in PATH!')
69end
70
71-- lspconfig
72function M.on_attach(client, bufnr)
73 local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
74 local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
75
76 buf_set_keymap('n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', options)
77 buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', options)
78 buf_set_keymap('n', 'ga', '<Cmd>lua vim.lsp.buf.code_action()<CR>', options)
79 buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', options)
80 buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', options)
81 buf_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', options)
82 buf_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', options)
83 buf_set_keymap('n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', options)
84
85 if client.resolved_capabilities.document_formatting then
86 buf_set_keymap('n', 'ff', '<cmd>lua vim.lsp.buf.formatting()<CR>', options)
87 elseif client.resolved_capabilities.document_range_formatting then
88 buf_set_keymap('n', 'ff', '<cmd>lua vim.lsp.buf.range_formatting()<CR>', options)
89 end
90end
91
92-- vim.api.nvim_exec([[
93-- " Use <Tab> and <S-Tab> to navigate through popup menu
94-- inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
95-- inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
96--
97-- ]], false)
98
99-- abbreviations
100local star = '★'
101local stars = {}
102for n = 1, 5
103do
104 table.insert(stars, star)
105 u.iabbrev(n .. '*', table.concat(stars))
106end
107
108return M