Git source for hrsh7th/nvim-cmp and Saghen/blink.cmp.
| Git | Trigger |
|---|---|
| Commits | : |
| GitHub | Trigger |
|---|---|
| Issues | # |
Mentions (curl only) |
@ |
| Pull Requests | # |
| GitLab | Trigger |
|---|---|
| Issues | # |
| Mentions | @ |
| Merge Requests | ! |
- 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)
curl: Generate token withreposcope. SetGITHUB_API_TOKENenvironment variable.GitHub CLI: Run gh auth login
curlGenerate token withapiscope. SetGITLAB_TOKENenvironment variable.GitLab CLI: Run glab auth login
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",
}require("cmp").setup({
sources = {
{ name = "git" },
-- more sources
}
})
require("cmp_git").setup()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.
Run the tests with:
nvim --headless -u NONE -l tests/run.lualocal 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
sort_by = sort.github.pull_requests,
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
sort_by = sort.gitlab.merge_requests,
format = format.gitlab.merge_requests,
},
},
trigger_actions = {
{
trigger_character = ":",
actions = { "git_commits" },
},
{
trigger_character = "#",
actions = { "gitlab_issues", "github_issues_and_change_requests" },
},
{
trigger_character = "@",
actions = { "gitlab_mentions", "github_mentions" },
},
{
trigger_character = "!",
actions = { "gitlab_change_requests" },
},
},
}
)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 trigger_character and actions. trigger_character 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 git_commits, gitlab_issues, gitlab_mentions, gitlab_change_requests,
github_issues, github_change_requests, github_issues_and_change_requests, and github_mentions.
Compatibility aliases are also available: gitlab_mrs routes to gitlab_change_requests, and
github_issues_and_prs routes to github_issues_and_change_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,
},
},
})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", }
}
}Special thanks to tjdevries for their informative video and starting code.