Help us learn about your current experience with the documentation. Take the survey.

安装并设置 Neovim 的 GitLab 插件

前置条件:

  • 对于 GitLab.com 和 GitLab 自托管版,您需要 GitLab 16.1 或更高版本。 尽管许多扩展功能在早期版本中可能也能用,但它们不受支持。
    • GitLab Duo 代码建议功能需要 GitLab 16.8 或更高版本。
  • 您需要 Neovim 0.9 或更高版本。
  • 您已安装 NPM。安装代码建议需要 NPM。

要安装此扩展,请按照您选择的插件管理器的安装步骤操作:

运行以下命令,在启动时使用 packadd 包含此项目:

git clone https://gitlab.com/gitlab-org/editor-extensions/gitlab.vim.git ~/.local/share/nvim/site/pack/gitlab/start/gitlab.vim

将此插件添加到您的 lazy.nvim 配置中:

{
  'https://gitlab.com/gitlab-org/editor-extensions/gitlab.vim.git',
  -- 在创建/打开文件时激活
  event = { 'BufReadPre', 'BufNewFile' },
  -- 在打开支持的文件类型时激活
  ft = { 'go', 'javascript', 'python', 'ruby' },
  cond = function()
    -- 仅当环境变量中存在 token 时才激活。
    -- 删除此行以使用交互式工作流。
    return vim.env.GITLAB_TOKEN ~= nil and vim.env.GITLAB_TOKEN ~= ''
  end,
  opts = {
    statusline = {
      -- 内置状态栏中挂钩,以指示 GitLab Duo 代码建议集成的状态
      enabled = true,
    },
  },
}

在您的 packer.nvim 配置中声明此插件:

use {
  "[email protected]:gitlab-org/editor-extensions/gitlab.vim.git",
}

连接 GitLab

要将此扩展连接到您的 GitLab 账户,请配置您的环境变量:

环境变量 默认值 描述
GITLAB_TOKEN 不适用 用于认证请求的默认 GitLab 个人访问令牌。如果提供,则跳过交互式认证。
GITLAB_VIM_URL https://gitlab.com 覆盖要连接的 GitLab 实例。默认为 https://gitlab.com

完整的环境变量列表可在扩展的帮助文本中找到,位于 doc/gitlab.txt

配置扩展

要配置此扩展:

  1. 配置您想要的文件类型。例如,由于此插件支持 Ruby,它会添加一个 FileType ruby 自动命令。 要为更多文件类型配置此行为,请将更多文件类型添加到 code_suggestions.auto_filetypes 设置选项中:

    require('gitlab').setup({
      statusline = {
        enabled = false
      },
      code_suggestions = {
        -- 默认语言的完整列表,请参见 'auto_filetypes' 数组
        -- https://gitlab.com/gitlab-org/editor-extensions/gitlab.vim/-/blob/main/lua/gitlab/config/defaults.lua
        auto_filetypes = { 'ruby', 'javascript' }, -- 默认为 { 'ruby' }
        ghost_text = {
          enabled = false, -- ghost text 是一个实验性功能
          toggle_enabled = "<C-h>",
          accept_suggestion = "<C-l>",
          clear_suggestions = "<C-k>",
          stream = true,
        },
      }
    })
  2. 配置 Omni Completion 以设置触发代码建议的键映射。

  3. 可选。配置 <Plug> 键映射

  4. 可选。使用 :helptags ALL 设置帮助标签,以访问 :help gitlab.txt

配置 Omni Completion

要启用带有代码建议的 Omni Completion

  1. 创建一个具有 api 范围的 个人访问令牌

  2. 将该令牌作为 GITLAB_TOKEN 环境变量添加到您的 shell 中。

  3. 通过运行 :GitLabCodeSuggestionsInstallLanguageServer vim 命令来安装代码建议的 语言服务器

  4. 通过运行 :GitLabCodeSuggestionsStart vim 命令来启动语言服务器。或者,配置 <Plug> 键映射 来切换语言服务器。

  5. 可选。考虑为单个建议也配置 Omni Completion 的对话框:

    vim.o.completeopt = 'menu,menuone'

在支持的文件类型中工作时,按 Ctrl+x 然后按 Ctrl+o 打开 Omni Completion 菜单。

配置 <Plug> 键映射

为方便起见,此插件提供了 <Plug> 键映射。要使用 <Plug>(GitLab...) 键映射,您必须包含一个引用它的自定义键映射:

-- 在普通模式下用 CTRL-g 切换代码建议的开关:
vim.keymap.set('n', '<C-g>', '<Plug>(GitLabToggleCodeSuggestions)')

卸载扩展

要卸载此扩展,请使用以下命令移除此插件和任何语言服务器二进制文件:

rm -r ~/.local/share/nvim/site/pack/gitlab/start/gitlab.vim
rm ~/.local/share/nvim/gitlab-code-suggestions-language-server-*