• Stars
    star
    616
  • Rank 72,837 (Top 2 %)
  • Language
    Lua
  • License
    MIT License
  • Created over 3 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

nvim-cmp source for buffer words

cmp-buffer

nvim-cmp source for buffer words.

Setup

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

Configuration

The below source configuration are available. To set any of these options, do:

cmp.setup({
  sources = {
    {
      name = 'buffer',
      option = {
        -- Options go into this table
      },
    },
  },
})

keyword_length (type: number)

Default: 3

The number of characters that need to be typed to trigger auto-completion.

keyword_pattern (type: string)

Default: [[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%([\-.]\w*\)*\)]]

A vim's regular expression for creating a word list from buffer content.

You can set this to [[\k\+]] if you want to use the iskeyword option for recognizing words. Lua's [[ ]] string literals are particularly useful here to avoid escaping all of the backslash (\) characters used for writing regular expressions.

NOTE: Be careful with where you set this option! You must do this:

cmp.setup({
  sources = {
    {
      name = 'buffer',
      -- Correct:
      option = {
        keyword_pattern = [[\k\+]],
      }
    },
  },
})

Instead of this:

cmp.setup({
  sources = {
    {
      name = 'buffer',
      -- Wrong:
      keyword_pattern = [[\k\+]],
    },
  },
})

The second notation is allowed by nvim-cmp (documented here), but it is meant for a different purpose and will not be detected by this plugin as the pattern for searching words.

get_bufnrs (type: fun(): number[])

Default: function() return { vim.api.nvim_get_current_buf() } end

A function that specifies the buffer numbers to complete.

You can use the following pre-defined recipes.

All buffers
cmp.setup {
  sources = {
    {
      name = 'buffer',
      option = {
        get_bufnrs = function()
          return vim.api.nvim_list_bufs()
        end
      }
    }
  }
}
Visible buffers
cmp.setup {
  sources = {
    {
      name = 'buffer',
      option = {
        get_bufnrs = function()
          local bufs = {}
          for _, win in ipairs(vim.api.nvim_list_wins()) do
            bufs[vim.api.nvim_win_get_buf(win)] = true
          end
          return vim.tbl_keys(bufs)
        end
      }
    }
  }
}

indexing_interval (type: number)

Default: 100

Optimization option. See the section Indexing.

indexing_batch_size (type: number)

Default: 1000

Optimization option. See the section Indexing.

max_indexed_line_length (type: number)

Default: 1024 * 40 (40 Kilobytes)

Optimization option. See the section Indexing.

Locality bonus comparator (distance-based sorting)

This source also provides a comparator function which uses information from the word indexer to sort completion results based on the distance of the word from the cursor line. It will also sort completion results coming from other sources, such as Language Servers, which might improve accuracy of their suggestions too. The usage is as follows:

local cmp = require('cmp')
local cmp_buffer = require('cmp_buffer')

cmp.setup({
  sources = {
    { name = 'buffer' },
      -- The rest of your sources...
  },
  sorting = {
    comparators = {
      function(...) return cmp_buffer:compare_locality(...) end,
      -- The rest of your comparators...
    }
  }
})

Indexing and how to optimize it

When a buffer is opened, this source first has to scan all lines in the buffer, match all words and store all of their occurrences. This process is called indexing. When actually editing the text in the buffer, the index of words is kept up-to-date with changes to the buffer's contents, this is called watching. It is done by re-running the indexer on just the changed lines. Indexing happens completely asynchronously in background, unlike watching, which must be performed synchronously to ensure that the index of words is kept perfectly in-sync with the lines in the buffer. However, most of the time this will not be a problem since many typical text edit operations affect only one or two lines, unless you are pasting a 1000-line snippet.

Note that you can freely edit the buffer while it is being indexed, the underlying algorithm is written in such a way that your changes will not break the index or cause errors. If a crash does happen - it is a bug, so please report it.

The speed of indexing is configurable with two options: indexing_interval and indexing_batch_size. Essentially, when indexing, a timer is started, which pulls a batch of indexing_batch_size lines from the buffer, scans them for words, and repeats after indexing_interval milliseconds. Decreasing interval and/or increasing the batch size will make the indexer faster, but at the expense of higher CPU usage and more lag when editing the file while indexing is still in progress. Setting indexing_batch_size to a negative value will switch the indexer to the "synchronous" mode: this will process all lines in one go, take less time in total (since no other code will be running on the Lua thread), but with the obvious downside that the editor UI will be blocked.

The option max_indexed_line_length controls plugin's behavior in files with very long lines. This is known to slow this source down significantly (see issue #13), so by default it will take only the first few kilobytes of the line it is currently on. In other words, very long lines are not ignored, but only a part of them is indexed.

Performance on large text files

This source has been tested on code files of a few megabytes in size (5-10) and contains optimizations for them, however, the indexed words can still take up tens of megabytes of RAM if the file is large. So, if you wish to avoid accidentally running this source on big files, you can tweak get_bufnrs, for example like this:

get_bufnrs = function()
  local buf = vim.api.nvim_get_current_buf()
  local byte_size = vim.api.nvim_buf_get_offset(buf, vim.api.nvim_buf_line_count(buf))
  if byte_size > 1024 * 1024 then -- 1 Megabyte max
    return {}
  end
  return { buf }
end

Of course, this snippet can be combined with any other recipes for get_bufnrs.

More Repositories

1

nvim-cmp

A completion plugin for neovim coded in Lua.
Lua
8,005
star
2

cmp-nvim-lsp

nvim-cmp source for neovim builtin LSP client
Lua
1,287
star
3

nvim-compe

Auto completion Lua plugin for nvim
Lua
1,282
star
4

vim-vsnip

Snippet plugin for vim/nvim that supports LSP/VSCode's snippet format.
Vim Script
880
star
5

cmp-nvim-lsp-signature-help

cmp-nvim-lsp-signature-help
Lua
630
star
6

cmp-path

nvim-cmp source for path
Lua
625
star
7

vscode-langservers-extracted

vscode-langservers bin collection.
Shell
595
star
8

cmp-cmdline

nvim-cmp source for vim's cmdline
Lua
557
star
9

cmp-nvim-lua

nvim-cmp source for nvim lua
Lua
317
star
10

cmp-emoji

nvim-cmp source for emoji
Lua
184
star
11

cmp-nvim-lsp-document-symbol

nvim-cmp source for textDocument/documentSymbol via nvim-lsp.
Lua
182
star
12

nvim-insx

Flexible key mapping manager.
Lua
177
star
13

cmp-calc

nvim-cmp source for math calculation
Lua
152
star
14

cmp-copilot

copilot.vim source for nvim-cmp
Lua
129
star
15

vim-vsnip-integ

vim-vsnip integrations to other plugins.
Vim Script
128
star
16

nvim-pasta

The yank/paste enhancement plugin for neovim.
Lua
107
star
17

cmp-vsnip

nvim-cmp source for vim-vsnip
Lua
93
star
18

vim-searchx

The extended search motion.
Vim Script
77
star
19

vim-eft

enhanced f/t
Vim Script
75
star
20

cmp-omni

nvim-cmp source for omnifunc
Lua
46
star
21

nvim-gtd

LSP's Go to definition plugin for neovim.
Lua
36
star
22

vim-seak

search + seek = seak. The plugin that enhances the `/` and `?`.
Vim Script
35
star
23

js-co-on

co based event emitter handling.
JavaScript
33
star
24

vim-lamp

💡Language Server Protocol client for Vim.
Vim Script
32
star
25

nvim-kit

My personal Lua utilities for neovim.
Lua
29
star
26

nvim-automa

Automatic macro recorder for neovim.
Lua
23
star
27

nvim-dansa

Guess the indent from lines of around.
Lua
19
star
28

vim-vital-vs

Vim Script
18
star
29

denops-popup

https://deno.land/x/denops_popup
Vim Script
17
star
30

vim-gindent

General indentexpr plugin for vim and nvim.
Lua
17
star
31

nvim-lua-rpc-example

Lua
16
star
32

cmp-core-example

Lua
14
star
33

nvim-linkedit

Lua
14
star
34

nvim-automa-poc

POC of plugin that will be called `nvim-automa`
Lua
13
star
35

vim-candle

Candidates listing engine for vim/nvim built on yaegi on golang.
Vim Script
12
star
36

nvim-matcha

A fuzzy finding plugin for neovim (for my personal use)
11
star
37

nvim-lua-rpc-example-with-headless

Use nvim --headless for asynchronous processing
Lua
11
star
38

vim-neco-calc

neocomplcache/neocomplete/deoplete calculates plugin.
Vim Script
10
star
39

vim-versions

useful interface for version control.
Vim Script
9
star
40

vim-compete

Auto completion plugin for vim/nvim that supports fuzzy match.
Vim Script
9
star
41

state-use

Simple state manager for React
TypeScript
8
star
42

deno-json-rpc

Strongly typed json-rpc module for deno.
TypeScript
7
star
43

nvim-plugin-template

Makefile
7
star
44

msw-snapshot

Transparently create an API cache for testing.
TypeScript
6
star
45

srimmer

Srimmer provides simple api to use react and immer.
TypeScript
6
star
46

vim-unmatchparen

highlight unmatch parens.
Vim Script
6
star
47

vim-denite-gitto

denite with vim-gitto
Python
6
star
48

deno-nvim-types

The Nvim API type definition for TypeScript.
TypeScript
5
star
49

completion-snippet

The vim-vsnip snippet collection for completion.
5
star
50

vim-foolish-move

Vim Script
5
star
51

fern-mapping-collapse-or-leave.vim

Vim Script
5
star
52

hrsh7th

My profile.
4
star
53

deoplete-vim-lsc

deoplete source for vim-lsc.
Vim Script
4
star
54

fern-mapping-call-function.vim

Vim Script
4
star
55

vim-minx

Extended insert-mode mapping manager.
Vim Script
3
star
56

vim-aim

This plugin provides motion that similar to /. (experimental)
Vim Script
3
star
57

js-gulp-component-build

gulp plugin to build component.
JavaScript
3
star
58

js-modelis

Modelis javascript modeling support.
JavaScript
2
star
59

vim-lamp-extensions

Vim Script
2
star
60

node-monores

monorepo scripts.
TypeScript
2
star
61

nvim-tuil

nvim plugin utilities that can be embed your plugin.
Lua
2
star
62

frontend-testing-demo

https://hrsh7th.github.io/frontend-testing-demo/main/storybook
TypeScript
2
star
63

vim-compete-lamp

Vim Script
2
star
64

vim-gitto

vim-gitto
Vim Script
2
star
65

vim-feeling-move

Vim Script
1
star
66

vsnip-snippet-plugin-demo

Vim Script
1
star
67

deoplete-lamp

deoplete source for vim-lamp.
Vim Script
1
star
68

denite-converter-prioritize-basename

Python
1
star
69

denops-sandbox

TypeScript
1
star
70

vim-neco-snippets

neocomplcache my snippets.
1
star
71

compe-lamp

vim-lamp source for compe for my personal use.
Vim Script
1
star
72

deoplete-calc

calculates at completion.
Python
1
star
73

nvim-lapi

1
star
74

js-modelis-assurance

assucrance plugin for modelis.
JavaScript
1
star
75

refnew-react

refnew react binding.
TypeScript
1
star
76

deoplete-fname

filename completion for deoplete.nvim.
Python
1
star
77

vim-diffkit

Buffer diff management in VimL. Improve performance for getting buffer's diff.
Vim Script
1
star
78

vim-picka

Vim Script
1
star
79

asyncomplete-lamp

asyncomplete source for vim-lamp.
Vim Script
1
star
80

react-inview-hook

react hooks & intersection-observer
TypeScript
1
star
81

vim-locon

local config in vimrc.
Vim Script
1
star
82

vim-compete-omnifunc

Vim Script
1
star
83

vim-vital-deno

vim x deno (experimental)
1
star
84

diagnostics.nvim

Experimental diagnostics service for neovim.
Vim Script
1
star
85

immer-deps

Auto update dependencies, when immer's produce.
TypeScript
1
star
86

vim-effort-gf

Vim Script
1
star
87

sandbox

Lua
1
star
88

slow-lsp-demo

TypeScript
1
star
89

vim-compete-lsp

vim-lsp source for vim-compete.
Vim Script
1
star
90

xhr-snapshot

TypeScript
1
star
91

refnew

proxy based state management utility.
TypeScript
1
star
92

compe-conjure

compe-nvim source for conjure
Lua
1
star
93

babel-plugin-tree-shakable

good manner cjs files to mjs. this is very very experimental. do not use.
JavaScript
1
star