grace/dots
dotfiles/nix configs
git clone https://git.gae.moe/grace/dots
ddf0c79
idk
1---@brief 2--- 3--- https://github.com/golang/tools/tree/master/gopls 4--- 5--- Google's lsp server for golang. 6 7--- @class go_dir_custom_args 8--- 9--- @field envvar_id string 10--- 11--- @field custom_subdir string? 12 13local mod_cache =nil 14local std_lib =nil 15 16---@param custom_args go_dir_custom_args 17---@param on_complete fun(dir: string | nil) 18local function identify_go_dir ( custom_args , on_complete ) 19local cmd ={ 'go' , 'env' , custom_args . envvar_id } 20vim . system ( cmd , { text =true } , function ( output ) 21local res =vim . trim ( output . stdout or '' ) 22if output . code == 0 and res ~= '' then 23if custom_args . custom_subdir and custom_args . custom_subdir ~= '' then 24res =res .. custom_args . custom_subdir 25end 26on_complete ( res ) 27else 28vim . schedule ( function () 29vim . notify ( 30( '[gopls] identify ' .. custom_args . envvar_id .. ' dir cmd failed with code %d: %s\n%s' ): format ( 31output . code , 32vim . inspect ( cmd ), 33output . stderr 34) 35) 36end ) 37on_complete ( nil ) 38end 39end ) 40end 41 42---@return string? 43local function get_std_lib_dir () 44if std_lib and std_lib ~= '' then 45return std_lib 46end 47 48identify_go_dir ( { envvar_id ='GOROOT' , custom_subdir ='/src' } , function ( dir ) 49if dir then 50std_lib =dir 51end 52end ) 53return std_lib 54end 55 56---@return string? 57local function get_mod_cache_dir () 58if mod_cache and mod_cache ~= '' then 59return mod_cache 60end 61 62identify_go_dir ( { envvar_id ='GOMODCACHE' } , function ( dir ) 63if dir then 64mod_cache =dir 65end 66end ) 67return mod_cache 68end 69 70---@param fname string 71---@return string? 72local function get_root_dir ( fname ) 73if mod_cache and fname : sub ( 1 , # mod_cache ) == mod_cache then 74local clients =vim . lsp . get_clients ( { name ='gopls' } ) 75if # clients > 0 then 76return clients [ # clients ]. config . root_dir 77end 78end 79if std_lib and fname : sub ( 1 , # std_lib ) == std_lib then 80local clients =vim . lsp . get_clients ( { name ='gopls' } ) 81if # clients > 0 then 82return clients [ # clients ]. config . root_dir 83end 84end 85return vim . fs . root ( fname , 'go.work' ) or vim . fs . root ( fname , 'go.mod' ) or vim . fs . root ( fname , '.git' ) or vim . fs . root ( fname , '.jj' ) 86end 87 88return { 89cmd ={ 'gopls' } , 90filetypes ={ 'go' , 'gomod' , 'gowork' , 'gotmpl' } , 91root_dir =function ( bufnr , on_dir ) 92local fname =vim . api . nvim_buf_get_name ( bufnr ) 93get_mod_cache_dir () 94get_std_lib_dir () 95-- see: https://github.com/neovim/nvim-lspconfig/issues/804 96on_dir ( get_root_dir ( fname )) 97end , 98}