nvim-config

Log | Files | Refs | README

mappings.lua (3881B)


      1 local M = {}
      2 local comp = require "complementree"
      3 local s = require "complementree.sources"
      4 local cdef = require 'complementree.defaults'
      5 local cc = require "complementree.combinators"
      6 local ccomp = require 'complementree.comparators'
      7 
      8 local fuzzy_lsp = cc.pipeline(s.lsp_matches {}, ccomp.fzy)
      9 
     10 local lsp_completion = {
     11   default = fuzzy_lsp,
     12   string = cdef.ins_completion "C-F",
     13   comment = cdef.dummy,
     14   -- comment = cdef.snippy,
     15 }
     16 
     17 comp.setup {
     18   default = cdef.ins_completion "C-N",
     19   vim = cdef.ins_completion "C-V",
     20   dockerfile = cdef.ins_completion "C-F",
     21   query = cdef.ins_completion 'C-O',
     22   org = cdef.ins_completion 'C-O',
     23   c = lsp_completion,
     24   cpp = lsp_completion,
     25   lua = {
     26     default = fuzzy_lsp,
     27     string = cdef.ins_completion "C-F",
     28   },
     29   rust = lsp_completion,
     30   ocaml = lsp_completion,
     31   python = lsp_completion,
     32   ql = lsp_completion,
     33   cmake = lsp_completion,
     34   nix = { default = fuzzy_lsp, path_expression = cdef.ins_completion "C-F", path_fragment = cdef.ins_completion "C-F" },
     35   tex = lsp_completion,
     36   haskell = lsp_completion,
     37   go = lsp_completion,
     38   javascript = lsp_completion,
     39   typescript = lsp_completion,
     40   zig = lsp_completion,
     41   xml = lsp_completion,
     42   html = lsp_completion,
     43 
     44   lean = lsp_completion,
     45 
     46   -- For git commit and text only do luasnip
     47   markdown = cdef.ins_completion("C-F")
     48 }
     49 
     50 local mpairs = require 'mini.pairs'
     51 local rt = function(codes)
     52   return vim.api.nvim_replace_termcodes(codes, true, true, true)
     53 end
     54 
     55 local function feed(codes)
     56   local keys = rt(codes)
     57   vim.api.nvim_feedkeys(keys, 'n', true)
     58 end
     59 
     60 local function sjump()
     61   vim.schedule(function()
     62     if vim.snippet.active { direction = 1 } then
     63       vim.snippet.jump(1)
     64     end
     65     -- if luasnip.jumpable(1) and luasnip.in_snippet() then
     66     --   luasnip.jump(1)
     67     -- end
     68   end)
     69 end
     70 
     71 local function tab()
     72   if vim.fn.pumvisible() == 1 then
     73     return feed '<C-N>'
     74   elseif vim.api.nvim_get_current_line():sub(1, vim.fn.getcurpos()[3] - 1):match("^%s*$") then
     75     return feed '<Tab>'
     76   end
     77 
     78   if not comp.complete() then
     79     -- Maybe we did not complete, and I actually wanted to jump
     80     sjump()
     81   end
     82 end
     83 
     84 local function cr()
     85   if vim.fn.pumvisible() == 1 then
     86     feed '<C-Y>'
     87     -- sjump()
     88   else
     89     feed(mpairs.cr())
     90   end
     91 end
     92 
     93 local function bs()
     94   local pumvisible = vim.fn.pumvisible()
     95   -- feed(mpairs.bs())
     96   vim.api.nvim_feedkeys(mpairs.bs(), 'n', true)
     97   vim.schedule(function()
     98     if pumvisible == 1 then
     99       comp.complete(true)
    100     end
    101   end)
    102 end
    103 
    104 function M.set_mappings()
    105   vim.keymap.set('i', '<CR>', cr, { noremap = true })
    106   vim.keymap.set({ 'i', 's' }, '<Tab>', tab)
    107   vim.keymap.set({ 'i', 's' }, '<BS>', bs)
    108   vim.keymap.set({ 'i', 's' }, '<C-l>', function() vim.snippet.jump(1) end)
    109 
    110   --- Tiny mapping to select the node at cursor and input it
    111   vim.keymap.set('c', '<C-R><C-N>', function()
    112     local node = vim.treesitter.get_node()
    113     if not node then return end
    114 
    115     local types = {} ---@type string[]
    116     repeat
    117       types[#types + 1] = node:type()
    118       node = node:parent()
    119     until not node
    120     vim.api.nvim_input(types[1])
    121   end)
    122 
    123   vim.keymap.set('n', '<Leader>q', function() vim.diagnostic.setqflist{severity = vim.diagnostic.severity.ERROR} end)
    124   vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { remap = false, silent = true })
    125   vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { remap = false, silent = true })
    126 
    127   require'azy'.setup {
    128     -- preview = true,
    129     debug = true,
    130   }
    131   local ab = require'azy.builtins'
    132 
    133   vim.keymap.set("n", "<Leader>e", ab.files(), {})
    134   vim.keymap.set("n", "<Leader>r", ab.files_contents(), {})
    135   vim.keymap.set('n', '<Leader>oc', ab.files { vim.fn.stdpath'config' }, {})
    136   vim.keymap.set('n', '<Leader>h', ab.help(), {})
    137   vim.keymap.set('n', '<Leader>b', ab.buffers(), {})
    138   vim.keymap.set('n', '<Leader>q', ab.quickfix(), {})
    139 
    140   vim.ui.select = ab.select
    141 end
    142 
    143 return M