{"id":13605638,"url":"https://github.com/hrsh7th/cmp-buffer","last_synced_at":"2025-04-04T10:05:31.716Z","repository":{"id":37531822,"uuid":"392379061","full_name":"hrsh7th/cmp-buffer","owner":"hrsh7th","description":"nvim-cmp source for buffer words","archived":false,"fork":false,"pushed_at":"2024-04-28T15:00:42.000Z","size":46,"stargazers_count":643,"open_issues_count":34,"forks_count":34,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-03-28T09:03:16.655Z","etag":null,"topics":["nvim-cmp"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hrsh7th.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-03T16:19:57.000Z","updated_at":"2025-03-27T11:55:39.000Z","dependencies_parsed_at":"2024-01-15T03:58:54.047Z","dependency_job_id":"c6f67f3e-0fea-4f1b-b483-8da182f8c552","html_url":"https://github.com/hrsh7th/cmp-buffer","commit_stats":{"total_commits":42,"total_committers":7,"mean_commits":6.0,"dds":0.3571428571428571,"last_synced_commit":"3022dbc9166796b644a841a02de8dd1cc1d311fa"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrsh7th%2Fcmp-buffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrsh7th%2Fcmp-buffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrsh7th%2Fcmp-buffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrsh7th%2Fcmp-buffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hrsh7th","download_url":"https://codeload.github.com/hrsh7th/cmp-buffer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247151044,"owners_count":20892244,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nvim-cmp"],"created_at":"2024-08-01T19:01:01.034Z","updated_at":"2025-04-04T10:05:31.686Z","avatar_url":"https://github.com/hrsh7th.png","language":"Lua","readme":"# cmp-buffer\n\nnvim-cmp source for buffer words.\n\n## Setup\n\n```lua\nrequire('cmp').setup({\n  sources = {\n    { name = 'buffer' },\n  },\n})\n```\n\n## Configuration\n\nThe below source configuration are available. To set any of these options, do:\n\n```lua\ncmp.setup({\n  sources = {\n    {\n      name = 'buffer',\n      option = {\n        -- Options go into this table\n      },\n    },\n  },\n})\n```\n\n\n### keyword_length (type: number)\n\n_Default:_ `3`\n\nThe number of characters that need to be typed to trigger auto-completion.\n\n\n### keyword_pattern (type: string)\n\n_Default:_ `[[\\%(-\\?\\d\\+\\%(\\.\\d\\+\\)\\?\\|\\h\\w*\\%([\\-.]\\w*\\)*\\)]]`\n\nA vim's regular expression for creating a word list from buffer content.\n\nYou can set this to `[[\\k\\+]]` if you want to use the `iskeyword` option for recognizing words.\nLua's `[[ ]]` string literals are particularly useful here to avoid escaping all of the backslash\n(`\\`) characters used for writing regular expressions.\n\n**NOTE:** Be careful with where you set this option! You must do this:\n\n```lua\ncmp.setup({\n  sources = {\n    {\n      name = 'buffer',\n      -- Correct:\n      option = {\n        keyword_pattern = [[\\k\\+]],\n      }\n    },\n  },\n})\n```\n\nInstead of this:\n\n```lua\ncmp.setup({\n  sources = {\n    {\n      name = 'buffer',\n      -- Wrong:\n      keyword_pattern = [[\\k\\+]],\n    },\n  },\n})\n```\n\nThe second notation is allowed by nvim-cmp (documented [here](https://github.com/hrsh7th/nvim-cmp#sourcesnumberkeyword_pattern-type-string)), but it is meant for a different purpose and will not be detected by this plugin as the pattern for searching words.\n\n\n### get_bufnrs (type: fun(): number[])\n\n_Default:_ `function() return { vim.api.nvim_get_current_buf() } end`\n\nA function that specifies the buffer numbers to complete.\n\nYou can use the following pre-defined recipes.\n\n##### All buffers\n\n```lua\ncmp.setup {\n  sources = {\n    {\n      name = 'buffer',\n      option = {\n        get_bufnrs = function()\n          return vim.api.nvim_list_bufs()\n        end\n      }\n    }\n  }\n}\n```\n\n##### Visible buffers\n\n```lua\ncmp.setup {\n  sources = {\n    {\n      name = 'buffer',\n      option = {\n        get_bufnrs = function()\n          local bufs = {}\n          for _, win in ipairs(vim.api.nvim_list_wins()) do\n            bufs[vim.api.nvim_win_get_buf(win)] = true\n          end\n          return vim.tbl_keys(bufs)\n        end\n      }\n    }\n  }\n}\n\n```\n\n\n### indexing_interval (type: number)\n\n_Default:_ `100`\n\nOptimization option. See the section [Indexing](#indexing-and-how-to-optimize-it).\n\n\n### indexing_batch_size (type: number)\n\n_Default:_ `1000`\n\nOptimization option. See the section [Indexing](#indexing-and-how-to-optimize-it).\n\n\n### max_indexed_line_length (type: number)\n\n_Default:_ `1024 * 40` (40 Kilobytes)\n\nOptimization option. See the section [Indexing](#indexing-and-how-to-optimize-it).\n\n\n## Locality bonus comparator (distance-based sorting)\n\nThis source also provides a comparator function which uses information from the word indexer\nto sort completion results based on the distance of the word from the cursor line. It will also\nsort completion results coming from other sources, such as Language Servers, which might improve\naccuracy of their suggestions too. The usage is as follows:\n\n```lua\nlocal cmp = require('cmp')\nlocal cmp_buffer = require('cmp_buffer')\n\ncmp.setup({\n  sources = {\n    { name = 'buffer' },\n      -- The rest of your sources...\n  },\n  sorting = {\n    comparators = {\n      function(...) return cmp_buffer:compare_locality(...) end,\n      -- The rest of your comparators...\n    }\n  }\n})\n```\n\n\n## Indexing and how to optimize it\n\nWhen a buffer is opened, this source first has to scan all lines in the buffer, match all words\nand store all of their occurrences. This process is called _indexing_. When actually editing the\ntext in the buffer, the index of words is kept up-to-date with changes to the buffer's contents,\nthis is called _watching_. It is done by re-running the indexer on just the changed lines.\nIndexing happens completely asynchronously in background, unlike watching, which must be performed\nsynchronously to ensure that the index of words is kept perfectly in-sync with the lines in the\nbuffer. However, most of the time this will not be a problem since many typical text edit\noperations affect only one or two lines, unless you are pasting a 1000-line snippet.\n\n_Note that you can freely edit the buffer while it is being indexed_, the underlying algorithm is\nwritten in such a way that your changes will not break the index or cause errors. If a crash does\nhappen - it is a bug, so please report it.\n\nThe speed of indexing is configurable with two options: `indexing_interval` and\n`indexing_batch_size`. Essentially, when indexing, a timer is started, which pulls a batch of\n`indexing_batch_size` lines from the buffer, scans them for words, and repeats after\n`indexing_interval` milliseconds. Decreasing interval and/or increasing the batch size will make\nthe indexer faster, but at the expense of higher CPU usage and more lag when editing the file\nwhile indexing is still in progress. Setting `indexing_batch_size` to a negative value will switch\nthe indexer to the \"synchronous\" mode: this will process all lines in one go, take less time in\ntotal (since no other code will be running on the Lua thread), but with the obvious downside that\nthe editor UI will be blocked.\n\nThe option `max_indexed_line_length` controls plugin's behavior in files with very long lines.\nThis is known to slow this source down significantly (see issue [#13](https://github.com/hrsh7th/cmp-buffer/issues/13)),\nso by default it will take only the first few kilobytes of the line it is currently on. In other\nwords, very long lines are not ignored, but only a part of them is indexed.\n\n### Performance on large text files\n\nThis source has been tested on code files of a few megabytes in size (5-10) and contains\noptimizations for them, however, the indexed words can still take up tens of megabytes of RAM if\nthe file is large. So, if you wish to avoid accidentally running this source on big files, you\ncan tweak `get_bufnrs`, for example like this:\n\n```lua\nget_bufnrs = function()\n  local buf = vim.api.nvim_get_current_buf()\n  local byte_size = vim.api.nvim_buf_get_offset(buf, vim.api.nvim_buf_line_count(buf))\n  if byte_size \u003e 1024 * 1024 then -- 1 Megabyte max\n    return {}\n  end\n  return { buf }\nend\n```\n\nOf course, this snippet can be combined with any other recipes for `get_bufnrs`.\n","funding_links":[],"categories":["Plugins","Lua","Completion"],"sub_categories":["Diagnostics"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhrsh7th%2Fcmp-buffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhrsh7th%2Fcmp-buffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhrsh7th%2Fcmp-buffer/lists"}