Skip to content

gbroques/cmp-ripgrep-flags

Repository files navigation

cmp-ripgrep-flags

nvim-cmp completion source for ripgrep flags.

Documentation for each flag is derived from ripgrep's man page.

Motivation

telescope-live-grep-args.nvim allows passing flags to the underlying ripgrep (rg) command that powers the search.

Adding a completion source for flags allows them to be more discoverable, and aids with memory in case they're forgot.

It could also be useful a completion source for a more raw :Rg command from plugins such as duane9/nvim-rg.

cmp-ripgrep-demo.mp4

Installation

This project follows semantic versioning so you may pin it to a major version range if your plugin manager supports that.

For example, if you use lazy.nvim as your plugin manager:

{
  'gbroques/cmp-ripgrep-flags',
  version = '^1.0.0',
  dependencies = 'hrsh7th/nvim-cmp'
}

Setup & Configuration

By default the completion source is available everywhere.

It's recommended to pass a function to the enabled option to restrict where the completion source provides suggestions.

Additionally, users may customize the kind, which is the icon displayed to the left of the source on the right-hand side of the completion menu.

Finally, numerous rg flags are filtered out from completion by default as they break the live_grep_args picker, or aren't very useful.

The default excluded flags may be overridden via the exclude option.

The below setup restricts the completion source to the live_grep_args picker, and displays the default values for the kind and exclude options.

local function is_current_picker_live_grep_args()
  local bufnr = vim.api.nvim_get_current_buf()
  local current_picker = require('telescope.actions.state').get_current_picker(bufnr)
  -- You need to update this if you customize the default prompt_title for the picker.
  return current_picker ~= nil and current_picker.prompt_title == 'Live Grep (Args)'
end
local cmp = require('cmp')
cmp.setup({
  -- Enable completion when picker is live_grep_args, and buffer type is prompt.
  -- Completion is disabled by default for the prompt buffer type.
  -- See https://github.com/hrsh7th/nvim-cmp/issues/60
  enabled = function()
    local is_prompt = vim.api.nvim_get_option_value('buftype', { buf = 0 }) == 'prompt'
    return (is_prompt and is_current_picker_live_grep_args()) or not is_prompt
  end,
  sources = {
    {
      name = 'ripgrep_flags',
      option = {
        -- Only enable the completion source when the current picker is live_grep_args
        enabled = is_current_picker_live_grep_args,

        -- Default kind that may be removed or overridden.
        kind = cmp.lsp.CompletionItemKind.Variable,

        -- Exclude flags that break the picker, or are not very useful.
        -- If you're happy with the following, then you may remove it as it's the default.
        exclude = {
          -- INPUT OPTIONS
          '-f', '--file',
          -- SEARCH OPTIONS
          '-v', '--invert-match', -- see http://github.com/nvim-telescope/telescope-live-grep-args.nvim/issues/65
          '--mmap',
          '-j', '--threads',
          -- OUTPUT OPTIONS
          '-A', '--after-context',
          '-B', '--before-context',
          '--color',
          '--colors',
          '--column',
          '-C', '--context',
          '--context-separator',
          '--field-context-separator',
          '--field-match-separator',
          '--heading',
          '-h', '--help',
          '--hostname-bin',
          '--hyperlink-format',
          '--include-zero',
          '-n', '--line-number',
          '-N', '--no-line-number',
          '-0', '--null',
          '--passthru',
          '-p', '--pretty',
          '-q', '--quiet',
          '-r', '--replace',
          '--vimgrep',
          '-H', '--with-filename',
          '-I', '--no-filename',
          -- OUTPUT MODES
          '-c', '--count',
          '--count-matches',
          '-l', '--files-with-matches',
          '--files-without-match',
          '--json', -- see https://github.com/nvim-telescope/telescope-live-grep-args.nvim/issues/4
          -- LOGGING OPTIONS
          '--debug',
          '--stats',
          '--trace',
          -- OTHER BEHAVIORS
          '--files',
          '--generate',
          '--type-list',
          '--pcre2-version',
          '-V', '--version'
        }
      }
    },
  },
})

Additional Configuration

The following is additional suggested configuration.

Displaying Ripgrep as a Source

If you want to customize the menu appearance to show Ripgrep as the source, then you might have the following code:

local cmp = require('cmp')
cmp.setup {
  formatting = {
    format = function(entry, vim_item)
      -- Kind icons
      vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind)
      -- Source
      vim_item.menu = ({
        -- 👇 Show Ripgrep source
        ripgrep_flags = "[Ripgrep]",
        buffer = "[Buffer]",
        nvim_lsp = "[LSP]",
        luasnip = "[LuaSnip]",
        nvim_lua = "[Lua]",
        latex_symbols = "[LaTeX]",
      })[entry.source.name]
      return vim_item
    end
  },
}

Excluding Global Snippets

If you use rafamadriz/friendly-snippets, then exclude global snippets such as time and timeHMS since they show up in the live grep args prompt:

-- sample lazy.nvim configuration
{
  'L3MON4D3/LuaSnip',
  dependencies = { 'rafamadriz/friendly-snippets' },
  config = function()
    require('luasnip').config.setup {}
    require("luasnip.loaders.from_vscode").lazy_load({ exclude = 'global' })
  end
}

Contributing

See CONTRIBUTING.md.

Related Plugins

About

nvim-cmp completion source for ripgrep flags.

Topics

Resources

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages