Git source for nvim-cmp
cmp-git
Git source for hrsh7th/nvim-cmp and Saghen/blink.cmp.
Features
| Git | Trigger | | ------- | ------- | | Commits | : |
| GitHub | Trigger | | ---------------------- | ------- | | Issues | # | | Mentions (curl only) | @ | | Pull Requests | # |
| GitLab | Trigger | | -------------- | ------- | | Issues | # | | Mentions | @ | | Merge Requests | ! |
Requirements
- Neovim >= 0.12
- git
- curl
- GitHub CLI (optional, will use curl instead if not avaliable)
- GitLab CLI (optional, will use curl instead if not avaliable)
GitHub Private Repositories
curl: Generate token
repo scope. Set GITHUBAPITOKEN environment variable.
GitHub CLI: Run gh auth login
GitLab Private Repositories
curlGenerate token
api scope. Set GITLAB_TOKEN environment variable.
GitLab CLI: Run glab auth login
Installation
Plug 'petertriho/cmp-git'
use("petertriho/cmp-git")
With nvim-cmp:
return {
"petertriho/cmp-git",
dependencies = { 'hrsh7th/nvim-cmp' },
opts = {
-- options go here
},
init = function()
table.insert(require("cmp").get_config().sources, { name = "git" })
end
}
With blink.cmp:
return {
"petertriho/cmp-git",
}
Setup
nvim-cmp
require("cmp").setup({
sources = {
{ name = "git" },
-- more sources
}
})
require("cmp_git").setup()
blink.cmp
blink.cmp support is optional and configured from your blink.cmp setup. Do not call require("cmp_git").setup() for blink; that function registers the nvim-cmp source.
require("blink.cmp").setup({
sources = {
default = { "git" },
providers = {
git = {
name = "git",
module = "cmp_git.blink",
opts = {
-- cmp-git options go here
},
},
},
},
})
To enable cmp-git only for commit-related filetypes while keeping your normal blink sources elsewhere, add it through blink's sources.per_filetype configuration for filetypes such as gitcommit, octo, or NeogitCommitMessage.
Development
Run the tests with:
nvim --headless -u NONE -l tests/run.lua
Config
local format = require("cmp_git.format")
local sort = require("cmp_git.sort")
require("cmp_git").setup({ -- defaults filetypes = { "gitcommit", "octo", "NeogitCommitMessage" }, remotes = { "upstream", "origin" }, -- in order of most to least prioritized enableRemoteUrlRewrites = false, -- enable git url rewrites, see https://git-scm.com/docs/git-config#Documentation/git-config.txt-urlltbasegtinsteadOf git = { commits = { limit = 100, sort_by = sort.git.commits, format = format.git.commits, sha_length = 7, }, }, github = { hosts = {}, -- list of private instances of github issues = { fields = { "title", "number", "body", "updatedAt", "state" }, filter = "all", -- assigned, created, mentioned, subscribed, all, repos limit = 100, state = "open", -- open, closed, all sort_by = sort.github.issues, format = format.github.issues, }, mentions = { limit = 100, sort_by = sort.github.mentions, format = format.github.mentions, }, pull_requests = { fields = { "title", "number", "body", "updatedAt", "state" }, limit = 100, state = "open", -- open, closed, merged, all sortby = sort.github.pullrequests, format = format.github.pull_requests, }, }, gitlab = { hosts = {}, -- list of private instances of gitlab issues = { limit = 100, state = "opened", -- opened, closed, all sort_by = sort.gitlab.issues, format = format.gitlab.issues, }, mentions = { limit = 100, sort_by = sort.gitlab.mentions, format = format.gitlab.mentions, }, merge_requests = { limit = 100, state = "opened", -- opened, closed, locked, merged sortby = sort.gitlab.mergerequests, format = format.gitlab.merge_requests, }, }, trigger_actions = { { trigger_character = ":", actions = { "git_commits" }, }, { trigger_character = "#", actions = { "gitlabissues", "githubissuesandchange_requests" }, }, { trigger_character = "@", actions = { "gitlabmentions", "githubmentions" }, }, { trigger_character = "!", actions = { "gitlabchangerequests" }, }, }, } )
Capability-level format.filterText is the preferred way to customize completion filtering. Provider-level filter_fn is supported as a compatibility alias and is applied to every capability for that provider.
NOTE
If you want specific behaviour for a trigger, add an entry in the trigger_actions table of the config. The preferred fields are triggercharacter and actions. triggercharacter has to be a single character, and actions is an ordered list of named behaviours. Multiple actions can be used for the same character; they run in order until one handles the request.
Built-in actions are gitcommits, gitlabissues, gitlabmentions, gitlabchange_requests, githubissues, githubchangerequests, githubissuesandchangerequests, and githubmentions. Compatibility aliases are also available: gitlabmrs routes to gitlabchange_requests, and githubissuesandprs routes to githubissuesandchange_requests.
Legacy callback-style trigger actions are still supported for compatibility. These entries use trigger_character and action, where action receives the different sources (currently git, gitlab and github), the trigger character, the completion callback, the parameters passed to complete from nvim-cmp, and the current git info. New configuration should prefer named actions instead.
All source functions take an optional config table as last argument, with which the configuration set in setup can be overwritten for a specific call.
NOTE on sorting
The default sorting order is last updated (for PRs, MRs and issues) and latest (for commits). To make nvim-cmp sort in this order, move cmp.config.compare.sort_text closer to the top of (lower index) in sorting.comparators. E.g.
require("cmp").setup({
-- As above
sorting = {
comparators = {
cmp.config.compare.offset,
cmp.config.compare.exact,
cmp.config.compare.sort_text,
cmp.config.compare.score,
cmp.config.compare.recently_used,
cmp.config.compare.kind,
cmp.config.compare.length,
cmp.config.compare.order,
},
},
})
Working with hosted instances of GitHub or GitLab
You can add hosted instances of Github Enterprise or GitLab to the corresponding hosts list as such:
require("cmp_git").setup({ github = { hosts = { "github.mycompany.com", }, }, gitlab = { hosts = { "gitlab.mycompany.com", } } }
Acknowledgements
Special thanks to tjdevries for their informative video and starting code.