Skip to content

Commit

Permalink
feat(integrations): added blink integration. Closes #71
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 1, 2024
1 parent 2c09c87 commit c4cd076
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ workspace libraries.
- will update your workspace libraries for:
- **require** statements: `require("nvim-treesitter")`
- **module annotations**: `---@module "nvim-treesitter"`
- [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) and [nvim_coq](https://github.com/ms-jpq/coq_nvim) completion source for the above
- [nvim-cmp](https://github.com/hrsh7th/nvim-cmp), [blink.cmp](https://github.com/Saghen/blink.cmp) and [nvim_coq](https://github.com/ms-jpq/coq_nvim) completion source for the above

![2024-06-01_21-02-40](https://github.com/folke/lazydev.nvim/assets/292349/c5f23225-88eb-454d-9b4e-1bf9183f7ff8)

Expand All @@ -26,7 +26,7 @@ workspace libraries.
will only return loaded modules in your workspace.
- To get around the above, you can:
- pre-load those plugins with the `library` option.
- use the **nvim-cmp** or **coq_nvim** completion source to get all available modules.
- use the **nvim-cmp**, **blink.cmp** or **coq_nvim** completion source to get all available modules.
- Neovim types are **NOT** included and also no longer needed
on **Neovim >= 0.10**

Expand Down Expand Up @@ -54,7 +54,7 @@ return {
},
},
{ "Bilal2453/luvit-meta", lazy = true }, -- optional `vim.uv` typings
{ -- optional completion source for require statements and module annotations
{ -- optional cmp completion source for require statements and module annotations
"hrsh7th/nvim-cmp",
opts = function(_, opts)
opts.sources = opts.sources or {}
Expand All @@ -64,6 +64,22 @@ return {
})
end,
},
{ -- optional blink completion source for require statements and module annotations
"saghen/blink.cmp",
opts = {
sources = {
-- add lazydev to your completion providers
completion = {
enabled_providers = { "lsp", "path", "snippets", "buffer", "lazydev" },
},
providers = {
-- dont show LuaLS require statements when lazydev has items
lsp = { fallback_for = { "lazydev" } },
lazydev = { name = "LazyDev", module = "lazydev.integrations.blink" },
},
},
},
}
-- { "folke/neodev.nvim", enabled = false }, -- make sure to uninstall or disable neodev.nvim
}
```
Expand Down
85 changes: 85 additions & 0 deletions lua/lazydev/integrations/blink.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---@module 'blink.cmp'

local Buf = require("lazydev.buf")
local Config = require("lazydev.config")
local Pkg = require("lazydev.pkg")

--- @type blink.cmp.Source
local M = {}

function M.new()
return setmetatable({}, { __index = M })
end

function M:get_trigger_characters()
return { '"', "'", ".", "/" }
end

function M:enabled()
return Buf.attached[vim.api.nvim_get_current_buf()] and true or false
end

function M:get_completions(ctx, callback)
local before = string.sub(ctx.line, 1, ctx.cursor[2])

local transformed_callback = function(items)
callback({
context = ctx,
is_incomplete_forward = false,
is_incomplete_backward = false,
items = items,
})
end

---@type string?
local req, forward_slash = Pkg.get_module(before, { before = true })
if not req then
return transformed_callback({})
end
local items = {} ---@type table<string,lsp.CompletionItem>

---@param modname string
---@param modpath string
local function add(modname, modpath)
local word = forward_slash and modname:gsub("%.", "/") or modname
items[modname] = items[modname]
or {
label = word,
kind = vim.lsp.protocol.CompletionItemKind.Module,
insertTextFormat = vim.lsp.protocol.InsertTextFormat.PlainText,
insertText = word,
}
local item = items[modname]
-- item.label = "test + " .. item.label

local plugin = Pkg.get_plugin_name(modpath)
if plugin then
if item.documentation then
item.documentation.value = item.documentation.value .. "\n- `" .. plugin .. "`"
else
item.documentation = {
kind = vim.lsp.protocol.MarkupKind.Markdown,
value = "# Plugins:\n" .. "- `" .. plugin .. "`",
}
end
end
end

if not req:find(".", 1, true) then
Pkg.topmods(add)
for _, lib in ipairs(Config.libs) do
for _, mod in ipairs(lib.mods) do
add(mod, lib.path)
end
end
else
Pkg.lsmod(req:gsub("%.[^%.]*$", ""), add)
end

transformed_callback(vim.tbl_values(items))

-- TODO: cancel run_async
return function() end
end

return M

0 comments on commit c4cd076

Please sign in to comment.