Skip to content

tzachar/cmp-fuzzy-buffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cmp-fuzzy-buffer

nvim-cmp source for fuzzy matched items from using the current buffer. Can use either fzf or fzy.

Installation

Depends on fuzzy.nvim (which depends either on fzf or on fzy).

Using Packer with fzf:

use {'nvim-telescope/telescope-fzf-native.nvim', run = 'make'}
use "hrsh7th/nvim-cmp"
use {'tzachar/cmp-fuzzy-buffer', requires = {'hrsh7th/nvim-cmp', 'tzachar/fuzzy.nvim'}}

Using Packer with fzy:

use {'romgrk/fzy-lua-native', run = 'make'}
use "hrsh7th/nvim-cmp"
use {'tzachar/cmp-fuzzy-buffer', requires = {'hrsh7th/nvim-cmp', 'tzachar/fuzzy.nvim'}}

Setup

require'cmp'.setup {
  sources = cmp.config.sources({
    { name = 'fuzzy_buffer' },
  })
}

This plugin can also be used to perform / searches with cmdline mode of cmp:

cmp.setup.cmdline('/', {
  sources = cmp.config.sources({
    { name = 'fuzzy_buffer' }
  })
})

In / search mode, the plugin will match the input string as is, without parsing out tokens. This enables fuzzy search containing, for example, spaces.

Note: the plugin's name is fuzzy_buffer in cmp's config.

Sorting and Filtering

By default, nvim-cmp will filter out sequences which we matched. To prevent this, we use the searched-for pattern as an input for filterText, such that all matched strings will be returned. However, this causes nvim-cmp to badly sort our returned results. To solve this issue, and sort cmp-fuzzy-path results by the score returned by the fuzzy matcher, you can use the following:

local compare = require('cmp.config.compare')

cmp.setup {
	sorting = {
		priority_weight = 2,
		comparators = {
			require('cmp_fuzzy_buffer.compare'),
			compare.offset,
			compare.exact,
			compare.score,
			compare.recently_used,
			compare.kind,
			compare.sort_text,
			compare.length,
			compare.order,
		}
	},
}

Configuration

keyword_pattern (type: string)

Default: [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%([\-.]\w*\)*\)]] _ cmdline Default:_ [=[[^[:blank:]].*]=]

A vim's regular expression for detecting the pattern to search with (starting from where the cursor currently is)

indentifier_patter (type: vim regex)

Default:

indentifier_patter = [=[[[:keyword:]]]=]

Used to find the best matched identifier to return based on the fuzzy patter and match results.

non_indentifier_patter (type: vim regex)

Default:

non_indentifier_patter = [=[[^[:keyword:]]]=],

Used to find the best matched identifier to return based on the fuzzy patter and match results.

max_buffer_lines (type: int)

The plugin will not work in buffers with more than max_buffer_lines lines for performance reasons.

Default: 20000

max_match_length (type: int)

Do not show matches longer than max_match_length.

Default: 50

min_match_length (type: int)

Do not show matches shorter than min_match_length.

Default: 1

fuzzy_extra_arg

This has different meaning depending on the fuzzy matching library used. For fzf, this is the case_mode argument: 0 = smart_case, 1 = ignore_case, 2 = respect_case. For fzy, this is the is_case_sensitive argument. Set to false to do case insensitive matching.

Default: 0

get_bufnrs (type: function)

Return a list of buffer numbers from which to get lines.

Default:

get_bufnrs = function()
  return { vim.api.nvim_get_current_buf() }
end

If you want to use all loaded buffer, you can use the following setup:

require'cmp'.setup {
  sources = cmp.config.sources({
    { 
       name = 'fuzzy_buffer' ,
       option = {
          get_bufnrs = function()  
          local bufs = {}
          for _, buf in ipairs(vim.api.nvim_list_bufs()) do
            local buftype = vim.api.nvim_buf_get_option(buf, 'buftype')
            if buftype ~= 'nofile' and buftype ~= 'prompt' then
              bufs[#bufs + 1] = buf
            end
          end
          return bufs
          end
       },
    },
  })
}

max_matches (type: int)

Maximal number of matches to returl.

Default: 15

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages