nix-config

My personal nixos and home-manager configuration
Log | Files | Refs | README

init.lua (8239B)


      1 -- vimrc but lua
      2 
      3 -- Set leader to space
      4 vim.g.mapleader = ' '
      5 
      6 -- Line numbers
      7 -- See :h number_relativenumber
      8 vim.wo.number = true
      9 vim.wo.relativenumber = true
     10 
     11 -- Tab configuration
     12 -- See :h tabstop for tab configuration stuff
     13 vim.bo.tabstop = 4
     14 vim.bo.softtabstop = 4
     15 vim.bo.shiftwidth = 4
     16 vim.bo.expandtab = true
     17 vim.bo.autoindent = true
     18 vim.o.tabstop = 4
     19 vim.o.softtabstop = 4
     20 vim.o.shiftwidth = 4
     21 vim.o.expandtab = true
     22 vim.o.autoindent = true
     23 vim.o.smarttab = true
     24 vim.o.hlsearch = false
     25 vim.o.hidden = true
     26 
     27 -- Use Australian spelling
     28 vim.bo.spelllang = 'en_au'
     29 
     30 -- Ruler shows the line and column number at the bottom right
     31 vim.o.ruler = true
     32 
     33 -- Always show the tab bar for consistency
     34 vim.o.showtabline = 2
     35 
     36 -- Make underscore separated words separate words
     37 --vim.bo.iskeyword = vim.bo.iskeyword:gsub(",_","")
     38 --vim.o.iskeyword = vim.o.iskeyword:gsub(",_","")
     39 
     40 -- Dont wrap long lines
     41 vim.wo.wrap = false
     42 
     43 -- Dont highlight searches
     44 vim.o.hlsearch = false
     45 
     46 -- Make CTRL-L clear echo and search
     47 vim.api.nvim_set_keymap('n', '<C-L>', ':noh<CR>:mode<CR>',
     48                         { noremap = true, silent = true })
     49 
     50 -- Make leader+a insert one from the end of the line because semicolon
     51 vim.api.nvim_set_keymap('n', '<leader>a', '$i',
     52                         { noremap = true, silent = true })
     53 -- Make leader+x delete char at the end of the line
     54 vim.api.nvim_set_keymap('n', '<leader>x', '$x',
     55                         { noremap = true, silent = true })
     56 -- Make leader+f fix the spelling of the current word
     57 vim.api.nvim_set_keymap('n', '<leader>f', '1z=',
     58                         { noremap = true, silent = true })
     59 -- Make leader+C open the colour scheme menu
     60 vim.api.nvim_set_keymap('n', '<leader>C', ':Telescope colorscheme<CR>',
     61                         { noremap = true, silent = true })
     62 -- Make leader+F fuzzyfind files
     63 vim.api.nvim_set_keymap('n', '<leader>F', ':Telescope find_files<CR>',
     64                         { noremap = true, silent = true })
     65  
     66 -- Make Shift-Delete do nothing (my keyboard is weird so I press it a lot)
     67 vim.api.nvim_set_keymap('i', '<S-Del>', '', { noremap = true, silent = true })
     68 
     69 -- Colour column shows the text width of a file
     70 vim.wo.colorcolumn = vim.wo.colorcolumn .. '+' .. 1
     71 for i = 2,255 do vim.wo.colorcolumn = vim.wo.colorcolumn .. ',+' .. i end
     72 
     73 -- Funky commands
     74 vim.api.nvim_command('command! W w')
     75 
     76 -- Command that opens fzf for colour schemes
     77 -- This is obsolete because I use telescope now
     78 function colourscheme()
     79     vim.api.nvim_eval('fzf#run(fzf#wrap({"source":luaeval("{'..string.gsub(vim.api.nvim_eval("globpath(&rtp, 'colors/*.vim')") .. '\n','.-/colors/(.-).vim\n',"'%1',")..'}"),"sink":"colorscheme"}))')
     80 end
     81 
     82 -- LSP
     83 
     84 require'lspconfig'.ccls.setup{}
     85 require'lspconfig'.hls.setup{}
     86 require'lspconfig'.texlab.setup{}
     87 
     88 -- Always have the sign column so that code doesnt move around on error
     89 vim.wo.signcolumn = "yes"
     90 
     91 -- nvim-compe
     92 
     93 vim.g.vimtex_view_method = "zathura"
     94 
     95 function t(str)
     96     return vim.api.nvim_replace_termcodes(str, true, true, true)
     97 end
     98 
     99 function check_back_space()
    100     local col = vim.fn.col('.') - 1
    101     if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
    102         return true
    103     else
    104         return false
    105     end
    106 end
    107 
    108 _G.tab_complete = function()
    109     if vim.fn.pumvisible() == 1 then
    110         return t "<C-n>"
    111     elseif require'luasnip'.expand_or_jumpable() then
    112         return t "<Plug>luasnip-expand-or-jump"
    113     elseif check_back_space() then
    114         return t "<Tab>"
    115     else
    116         return vim.fn['compe#complete']()
    117     end
    118 end
    119 _G.s_tab_complete = function()
    120     if vim.fn.pumvisible() == 1 then
    121         return t "<C-p>"
    122     elseif require'luasnip'.expand_or_jumpable() then
    123         return t "<Plug>luasnip-expand-or-jump"
    124     else
    125         return t "<S-Tab>"
    126     end
    127 end
    128 
    129 vim.api.nvim_set_keymap("i", "<Tab>", "v:lua.tab_complete()", {expr = true})
    130 vim.api.nvim_set_keymap("s", "<Tab>", "v:lua.tab_complete()", {expr = true})
    131 vim.api.nvim_set_keymap("i", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
    132 vim.api.nvim_set_keymap("s", "<S-Tab>", "v:lua.s_tab_complete()", {expr = true})
    133 
    134 -- Vimwiki
    135 vim.g.vimwiki_list = {{
    136     path = "~/.local/share/vimwiki/",
    137     path_html = "~/.local/share/vimwiki-html/",
    138     auto_toc = 1,
    139     index = "main"
    140 }}
    141 
    142 vim.g.calendar_open = 0
    143 
    144 _G.open_calendar = function()
    145     if vim.g.calendar_open == 1 then
    146         vim.g.calendar_open = 0
    147         return t "<Plug>CalendarVq"
    148     end
    149     vim.g.calendar_open = 1
    150     return t "<Plug>CalendarV"
    151 end
    152 
    153 vim.api.nvim_set_keymap("n", "<leader>wc", "v:lua.open_calendar()", {expr = true})
    154 
    155 
    156 
    157 -- The very big idris2-lsp config that they give you
    158 
    159 local lspconfig = require('lspconfig')
    160 local configs = require('lspconfig/configs')
    161 if not lspconfig.idris2_lsp then
    162   configs.idris2_lsp = {
    163     default_config = {
    164       cmd = {'idris2-lsp'}; -- if not available in PATH, provide the absolute path
    165       filetypes = {'idris2'};
    166       on_new_config = function(new_config, new_root_dir)
    167         new_config.cmd = {'idris2-lsp'}
    168         new_config.capabilities['workspace']['semanticTokens'] = {refreshSupport = true}
    169       end;
    170       root_dir = function(fname)
    171         local scandir = require('plenary.scandir')
    172         local find_ipkg_ancestor = function(fname)
    173           return lspconfig.util.search_ancestors(fname, function(path)
    174             local res = scandir.scan_dir(path, {depth=1; search_pattern='.+%.ipkg'})
    175             if not vim.tbl_isempty(res) then
    176               return path
    177             end
    178           end)
    179         end
    180         return find_ipkg_ancestor(fname) or lspconfig.util.find_git_ancestor(fname) or vim.loop.os_homedir()
    181       end;
    182       settings = {};
    183     };
    184   }
    185 end
    186 -- Flag to enable semantic highlightning on start, if false you have to issue a first command manually
    187 local autostart_semantic_highlightning = true
    188 lspconfig.idris2_lsp.setup {
    189   on_init = custom_init,
    190   on_attach = function(client)
    191     if autostart_semantic_highlightning then
    192       vim.lsp.buf_request(0, 'textDocument/semanticTokens/full',
    193         { textDocument = vim.lsp.util.make_text_document_params() }, nil)
    194     end
    195     --custom_attach(client) -- remove this line if you don't have a customized attach function
    196   end,
    197   autostart = true,
    198   handlers = {
    199     ['workspace/semanticTokens/refresh'] = function(err, method, params, client_id, bufnr, config)
    200       if autostart_semantic_highlightning then
    201         vim.lsp.buf_request(0, 'textDocument/semanticTokens/full',
    202           { textDocument = vim.lsp.util.make_text_document_params() }, nil)
    203       end
    204       return vim.NIL
    205     end,
    206     ['textDocument/semanticTokens/full'] = function(err, method, result, client_id, bufnr, config)
    207       -- temporary handler until native support lands
    208       local client = vim.lsp.get_client_by_id(client_id)
    209       local legend = client.server_capabilities.semanticTokensProvider.legend
    210       local token_types = legend.tokenTypes
    211       local data = result.data
    212 
    213       local ns = vim.api.nvim_create_namespace('nvim-lsp-semantic')
    214       vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
    215       local tokens = {}
    216       local prev_line, prev_start = nil, 0
    217       for i = 1, #data, 5 do
    218         local delta_line = data[i]
    219         prev_line = prev_line and prev_line + delta_line or delta_line
    220         local delta_start = data[i + 1]
    221         prev_start = delta_line == 0 and prev_start + delta_start or delta_start
    222         local token_type = token_types[data[i + 3] + 1]
    223         vim.api.nvim_buf_add_highlight(bufnr, ns, 'LspSemantic_' .. token_type, prev_line, prev_start, prev_start + data[i + 2])
    224       end
    225     end
    226   },
    227 }
    228 
    229 -- Set here your preferred colors for semantic values
    230 vim.cmd [[highlight link LspSemantic_type Include]]   -- Type constructors
    231 vim.cmd [[highlight link LspSemantic_function Identifier]] -- Functions names
    232 vim.cmd [[highlight link LspSemantic_enumMember Number]]   -- Data constructors
    233 vim.cmd [[highlight LspSemantic_variable guifg=gray]] -- Bound variables
    234 vim.cmd [[highlight link LspSemantic_keyword Structure]]  -- Keywords
    235 
    236 -- Add the following command to a mapping if you want to send a manual request for semantic highlight
    237 -- :lua vim.lsp.buf_request(0, 'textDocument/semanticTokens/full', {textDocument = vim.lsp.util.make_text_document_params()}, nil)