diff options
| author | srdusr <trevorgray@srdusr.com> | 2022-12-16 23:42:45 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2022-12-16 23:42:45 +0200 |
| commit | af5e62f47fc0c49ff3fb7c4dcf26a438471cf7cf (patch) | |
| tree | d80cae9fd509089a3472aaf355b598fdca1b4f6f /.config/nvim/lua/plugins/cmp-gh-source.lua | |
| parent | 6371e90cc598e52b1d057d4b6d9a7b75d60e2fd0 (diff) | |
| parent | 18e3249ba8bfea656b02308225684e893d0273fa (diff) | |
| download | dotfiles-af5e62f47fc0c49ff3fb7c4dcf26a438471cf7cf.tar.gz dotfiles-af5e62f47fc0c49ff3fb7c4dcf26a438471cf7cf.zip | |
Merge commit 'af4baf569778ef8324ec0c1c6c0f09d6fb79e6a4' as '.config/nvim'
Diffstat (limited to '.config/nvim/lua/plugins/cmp-gh-source.lua')
| -rw-r--r-- | .config/nvim/lua/plugins/cmp-gh-source.lua | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/.config/nvim/lua/plugins/cmp-gh-source.lua b/.config/nvim/lua/plugins/cmp-gh-source.lua new file mode 100644 index 0000000..05bba55 --- /dev/null +++ b/.config/nvim/lua/plugins/cmp-gh-source.lua @@ -0,0 +1,72 @@ +local ok, Job = pcall(require, "plenary.job") +if not ok then + return +end + +local source = {} + +source.new = function() + local self = setmetatable({ cache = {} }, { __index = source }) + + return self +end + +source.complete = function(self, _, callback) + local bufnr = vim.api.nvim_get_current_buf() + + -- This just makes sure that we only hit the GH API once per session. + -- + -- You could remove this if you wanted, but this just makes it so we're + -- good programming citizens. + if not self.cache[bufnr] then + Job + :new({ + -- Uses `gh` executable to request the issues from the remote repository. + "gh", + "issue", + "list", + "--limit", + "1000", + "--json", + "title,number,body", + + on_exit = function(job) + local result = job:result() + local ok, parsed = pcall(vim.json.decode, table.concat(result, "")) + if not ok then + vim.notify "Failed to parse gh result" + return + end + + local items = {} + for _, gh_item in ipairs(parsed) do + gh_item.body = string.gsub(gh_item.body or "", "\r", "") + + table.insert(items, { + label = string.format("#%s", gh_item.number), + documentation = { + kind = "markdown", + value = string.format("# %s\n\n%s", gh_item.title, gh_item.body), + }, + }) + end + + callback { items = items, isIncomplete = false } + self.cache[bufnr] = items + end, + }) + :start() + else + callback { items = self.cache[bufnr], isIncomplete = false } + end +end + +source.get_trigger_characters = function() + return { "#" } +end + +source.is_available = function() + return vim.bo.filetype == "gitcommit" +end + +require("cmp").register_source("gh_issues", source.new()) |
