robin/ivy
my custom neovim config
git clone https://git.robinwobin.dev/robin/ivy
b848d06
main
1if vim . g . loaded_ember then 2return 3end 4 5vim . g . loaded_ember =true 6 7---@type table<vim.lsp.protocol.Method, fun(params: table, callback:fun(err: lsp.ResponseError?, result: any))> 8local handlers ={} 9local ms =vim . lsp . protocol . Methods 10 11local chars ={} 12for i =32 , 126 do 13table . insert ( chars , string . char ( i )) 14end 15 16---@type lsp.InitializeResult 17local initializeResult ={ 18capabilities ={ 19hoverProvider =true , 20definitionProvider =true , 21referencesProvider =true , 22completionProvider ={ 23triggerCharacters =chars , 24} , 25} , 26serverInfo ={ 27name ="ember_ls" , 28version ="0.0.1" , 29} , 30} 31 32handlers [ ms . initialize ] =function ( _ , callback ) 33callback ( nil , initializeResult ) 34end 35 36-- hover ====================================================================== 37 38---@param _ lsp.HoverParams 39---@param callback fun(err?: lsp.ResponseError, result: lsp.Hover) 40handlers [ ms . textDocument_hover ] =function ( _ , callback ) 41local word =vim . fn . expand ( "<cword>" ) 42local url_format ="https://api.dictionaryapi.dev/api/v2/entries/en/%s" 43 44vim . system ( 45{ "curl" , url_format : format ( word ) } , 46vim . schedule_wrap ( function ( out ) 47local contents 48if out . code ~= 0 then 49contents ="word fetch failed" 50else 51local ok , decoded =pcall ( vim . json . decode , out . stdout ) 52if ok and decoded and decoded [ 1 ] then 53contents =decoded [ 1 ]. meanings [ 1 ]. definitions [ 1 ]. definition 54else 55contents =decoded . message -- this api gives a nice message if no result 56end 57end 58callback ( nil , { contents =contents } ) 59end ) 60) 61end 62 63-- completion ================================================================= 64 65---@param params lsp.CompletionParams 66---@return string word_to_complete 67local get_word_to_complete =function ( params ) 68local col =params . position . character + 1 69local line =vim . api . nvim_get_current_line () 70local line_to_cursor =line : sub ( 1 , col ) 71local regex =vim . regex ( "\\k*$" ) 72 73return line : sub ( regex : match_str ( line_to_cursor ) + 1 , col ) 74end 75 76---@return vim.lsp.inline_completion.Item[] 77local function completion_buffer ( params ) 78local word =get_word_to_complete ( params ) 79 80local lines =vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , false ) 81local matches =vim . fn . matchstrlist ( lines , [[\<\k\+\>]] ) 82vim . list . unique ( matches , function ( x ) 83---@diagnostic disable-next-line: undefined-field 84return x . text 85end ) 86matches =vim . fn . matchfuzzy ( matches , word , { key ="text" } ) 87return vim 88. iter ( matches ) 89: map ( function ( v ) 90-- dont match current word 91if v . text == word then 92return 93end 94return { 95label =v . text , 96kind =vim . lsp . protocol . CompletionItemKind [ "Text" ], 97} 98end ) 99: totable () 100end 101 102local get_spell_candidates =vim . func . _memoize ( "concat" , function ( w ) 103local entries =vim . fn . spellsuggest ( w ) 104entries =vim . fn . matchfuzzy ( entries , w , { limit =5 } ) 105 106return vim 107. iter ( entries ) 108: map ( function ( v ) 109if string . find ( v , "[ ']" ) ~= nil then 110return 111end 112return { 113label =v , 114kind =vim . lsp . protocol . CompletionItemKind [ "Text" ], 115} 116end ) 117: totable () 118end ) 119 120---@return vim.lsp.inline_completion.Item[] 121local function completion_spell ( params ) 122local word =get_word_to_complete ( params ) 123 124return get_spell_candidates ( word ) 125end 126 127---@param params lsp.CompletionParams 128---@param callback fun(err?: lsp.ResponseError, result: lsp.CompletionItem[]) 129handlers [ ms . textDocument_completion ] =function ( params , callback ) 130local items ={} 131if vim . wo . spell then 132vim . list_extend ( items , completion_spell ( params )) 133end 134vim . list_extend ( items , completion_buffer ( params )) 135 136callback ( nil , { 137items =items , 138isIncomplete =# items > 0 , 139} ) 140end 141 142-- init ======================================================================= 143 144---@type vim.lsp.ClientConfig 145local cfg ={ 146name ="ember_ls" , 147cmd =function () 148return { 149request =function ( method , params , callback ) 150if handlers [ method ] then 151handlers [ method ]( params , callback ) 152end 153end , 154notify =function () end , 155is_closing =function () end , 156terminate =function () end , 157} 158end , 159} 160 161vim . lsp . config ( "ember_ls" , cfg )