neovim

Personal neovim configuration files
git clone git://gtms.dev/neovim
Log | Files | Refs

glab_source.lua (1448B)


      1 local source = {}
      2 
      3 local glab_issues = function(callback)
      4   require('plenary.job'):new({
      5     command = 'glab_all_issues',
      6     args = {},
      7     on_exit = function(j, code)
      8       if code > 0 then callback({}) end
      9       local issues = {}
     10       for _, item in ipairs(j:result()) do
     11         local id, info = string.match(item, '#(%d+)\t(.-)\t')
     12         table.insert(issues, {
     13           --
     14           label = '#' .. id .. ' ' .. info,
     15           insertText = id,
     16           kind = require('cmp').lsp.CompletionItemKind.Text,
     17         })
     18       end
     19       callback(issues)
     20     end,
     21   }):start()
     22 end
     23 
     24 ---Source constructor.
     25 source.new = function()
     26   local self = setmetatable({}, {__index = source})
     27   self.issues = {}
     28   return self
     29 end
     30 
     31 ---Return trigger characters.
     32 ---@param params cmp.SourceBaseApiParams
     33 ---@return string[]
     34 function source:get_trigger_characters(params) return {'#'} end
     35 
     36 ---Invoke completion (required).
     37 ---  If you want to abort completion, just call the callback without arguments.
     38 ---@param params cmp.SourceCompletionApiParams
     39 ---@param callback fun(response: lsp.CompletionResponse|nil)
     40 function source:complete(params, callback)
     41   if not params.context.cursor_before_line:find('dpgw#$') then return end
     42   if #self.issues == 0 then
     43     glab_issues(function(issues)
     44       self.issues = issues
     45       callback(self.issues)
     46     end)
     47   end
     48 
     49   if #self.issues > 0 then
     50     callback(self.issues)
     51   else
     52     callback()
     53   end
     54 end
     55 
     56 return source