Repository: hrsh7th/cmp-nvim-lua Branch: main Commit: e3a22cb071eb Files: 4 Total size: 3.7 KB Directory structure: gitextract_csdc37fq/ ├── LICENSE ├── README.md ├── after/ │ └── plugin/ │ └── cmp_nvim_lua.lua └── lua/ └── cmp_nvim_lua/ └── init.lua ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2025 hrsh7th Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # cmp-nvim-lua nvim-cmp source for neovim Lua API. # Setup ```lua require'cmp'.setup { sources = { { name = 'nvim_lua' } } } ``` # Options ### `include_deprecated` (boolean: default false) Specify should includes deprecated things or not. # What is this source? This source will complete neovim's Lua runtime API such `vim.lsp.*`. You can get the `vim.lsp.util.*` API with this source. ================================================ FILE: after/plugin/cmp_nvim_lua.lua ================================================ require'cmp'.register_source('nvim_lua', require'cmp_nvim_lua'.new()) ================================================ FILE: lua/cmp_nvim_lua/init.lua ================================================ local cmp = require('cmp') local source = {} source.new = function() local self = setmetatable({}, { __index = source }) self.regex = vim.regex([[\%(\.\|\w\)\+\ze\.\w*$]]) return self end source.is_available = function() return vim.bo.filetype == 'lua' or vim.bo.filetype == 'vim' end source.get_keyword_pattern = function() return [[\w\+]] end source.get_trigger_characters = function() return { '.' } end source.complete = function(self, request, callback) local s, e = self.regex:match_str(request.context.cursor_before_line) if not s then return callback() end local items = self:items(string.sub(request.context.cursor_before_line, s + 1, e)) if not request.option.include_deprecated then items = vim.tbl_filter(function(item) return not item.label:match('^_') end, items) end callback({ items = items, }) end source.items = function(self, path) local target = _G local target_keys = vim.tbl_keys(_G) for _, name in ipairs(vim.split(path, '.', true)) do if vim.tbl_contains(target_keys, name) and type(target[name]) == 'table' then target = target[name] target_keys = vim.tbl_keys(target) elseif name ~= '' then return {} end end local candidates = {} for _, key in ipairs(target_keys) do if string.match(key, '^%a[%a_]*$') then table.insert(candidates, self:item(key, target[key])) end end for _, key in ipairs(target_keys) do if not string.match(key, '^%a[%a_]*$') then table.insert(candidates, self:item(key, target[key])) end end return candidates end source.item = function(_, key, value) key = tostring(key) local kind = cmp.lsp.CompletionItemKind.Field local t = type(value) if t == 'function' then kind = cmp.lsp.CompletionItemKind.Function elseif t == 'table' then kind = cmp.lsp.CompletionItemKind.Struct elseif t == 'string' then kind = cmp.lsp.CompletionItemKind.Value elseif t == 'boolean' then kind = cmp.lsp.CompletionItemKind.Value elseif t == 'number' then kind = cmp.lsp.CompletionItemKind.Value elseif t == 'nil' then kind = cmp.lsp.CompletionItemKind.Value end return { label = key, kind = kind, detail = t, } end return source