Skip to content

Commit

Permalink
fix!: nvim-cmp source registration
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the nvim-cmp source isn't automatically registered
anymore. Instead enable it in setup.
require("crates").setup {
    src = {
        cmp = {
            enabled = true
        },
    },
}
  • Loading branch information
saecki committed Oct 5, 2023
1 parent 86b31c9 commit 71fc6ae
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 158 deletions.
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,21 @@ lua require('crates').setup()

### Auto completion
#### [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) source
Just add it to your list of sources.

Enable it in the setup.
```lua
require('crates').setup {
...
src = {
...
cmp = {
enabled = true,
},
},
}
```

And add it to your list of sources.
```lua
require('cmp').setup {
...
Expand Down Expand Up @@ -300,11 +314,8 @@ require('crates').setup {
prerelease = "  pre-release ",
yanked = "  yanked ",
},
coq = {
enabled = false,
name = "Crates",
},
cmp = {
enabled = false,
use_custom_kind = true,
kind_text = {
version = "Version",
Expand All @@ -315,6 +326,10 @@ require('crates').setup {
feature = "CmpItemKindFeature",
},
},
coq = {
enabled = false,
name = "Crates",
},
},
null_ls = {
enabled = false,
Expand Down
4 changes: 0 additions & 4 deletions after/plugin/cmp_crates.lua

This file was deleted.

54 changes: 33 additions & 21 deletions doc/crates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,8 @@ For more information about individual config options see |crates-config|.
prerelease = "  pre-release ",
yanked = "  yanked ",
},
coq = {
enabled = false,
name = "Crates",
},
cmp = {
enabled = false,
use_custom_kind = true,
kind_text = {
version = "Version",
Expand All @@ -222,6 +219,10 @@ For more information about individual config options see |crates-config|.
feature = "CmpItemKindFeature",
},
},
coq = {
enabled = false,
name = "Crates",
},
},
null_ls = {
enabled = false,
Expand Down Expand Up @@ -1287,28 +1288,21 @@ src.text.yanked *crates-config-src-text-yanked*
Text shown in the completion source documentation preview for yanked versions.


src.coq *crates-config-src-coq*
src.cmp *crates-config-src-cmp*
Type: `section`

Configuration options for the |coq_nvim| completion source.
Configuration options for the |nvim-cmp| completion source.


src.coq.enabled *crates-config-src-coq-enabled*
src.cmp.enabled *crates-config-src-cmp-enabled*
Type: `boolean`, Default: `false`

Whether to load and register the |coq_nvim| source.


src.coq.name *crates-config-src-coq-name*
Type: `string`, Default: `"Crates"`

The source name displayed by |coq_nvim|.


src.cmp *crates-config-src-cmp*
Type: `section`

Configuration options for the |nvim-cmp| completion source.
Whether to load and register the |nvim-cmp| source.
Note Ignored if |crates-config-autoload| is disabled.
You may manually register it, after |nvim-cmp| has been loaded.
>
require("crates.src.cmp").setup()
<
src.cmp.use_custom_kind *crates-config-src-cmp-use_custom_kind*
Expand Down Expand Up @@ -1353,6 +1347,24 @@ src.cmp.kind_highlight.feature *crates-config-src-cmp-kind_highlight-feature*
Highlight group used for the feature kind text in the |nvim-cmp| completion menu.


src.coq *crates-config-src-coq*
Type: `section`

Configuration options for the |coq_nvim| completion source.


src.coq.enabled *crates-config-src-coq-enabled*
Type: `boolean`, Default: `false`

Whether to load and register the |coq_nvim| source.


src.coq.name *crates-config-src-coq-name*
Type: `string`, Default: `"Crates"`

The source name displayed by |coq_nvim|.


null_ls *crates-config-null_ls*
Type: `section`

Expand All @@ -1375,7 +1387,7 @@ on_attach *crates-config-on_attach*
Type: `function`, Default: `function(bufnr) end`

Callback to run when Cargo.toml is opened.
Ignored if `autoload` is disabled.
Note Ignored if |crates-config-autoload| is disabled.



Expand Down
58 changes: 35 additions & 23 deletions lua/crates/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ local M = {Config = {TextConfig = {}, HighlightConfig = {}, DiagnosticConfig = {






local Config = M.Config
Expand Down Expand Up @@ -1343,38 +1344,26 @@ entry(schema_src_text, "yanked", {
]],
})


entry(schema_src, "coq", {
entry(schema_src, "cmp", {
type = "section",
description = [[
Configuration options for the |coq_nvim| completion source.
Configuration options for the |nvim-cmp| completion source.
]],
fields = {},
})
local schema_src_coq = schema_src.coq.fields
entry(schema_src_coq, "enabled", {
local schema_src_cmp = schema_src.cmp.fields
entry(schema_src_cmp, "enabled", {
type = "boolean",
default = false,
description = [[
Whether to load and register the |coq_nvim| source.
Whether to load and register the |nvim-cmp| source.
Note Ignored if |crates-config-autoload| is disabled.
You may manually register it, after |nvim-cmp| has been loaded.
>
require("crates.src.cmp").setup()
<
]],
})
entry(schema_src_coq, "name", {
type = "string",
default = "Crates",
description = [[
The source name displayed by |coq_nvim|.
]],
})

entry(schema_src, "cmp", {
type = "section",
description = [[
Configuration options for the |nvim-cmp| completion source.
]],
fields = {},
})
local schema_src_cmp = schema_src.cmp.fields
entry(schema_src_cmp, "use_custom_kind", {
type = "boolean",
default = true,
Expand Down Expand Up @@ -1429,6 +1418,29 @@ entry(schema_src_cmp_kind_hi, "feature", {
]],
})

entry(schema_src, "coq", {
type = "section",
description = [[
Configuration options for the |coq_nvim| completion source.
]],
fields = {},
})
local schema_src_coq = schema_src.coq.fields
entry(schema_src_coq, "enabled", {
type = "boolean",
default = false,
description = [[
Whether to load and register the |coq_nvim| source.
]],
})
entry(schema_src_coq, "name", {
type = "string",
default = "Crates",
description = [[
The source name displayed by |coq_nvim|.
]],
})


entry(M.schema, "null_ls", {
type = "section",
Expand Down Expand Up @@ -1459,7 +1471,7 @@ entry(M.schema, "on_attach", {
default_text = "function(bufnr) end",
description = [[
Callback to run when Cargo.toml is opened.
Ignored if `autoload` is disabled.
Note Ignored if |crates-config-autoload| is disabled.
]],
})

Expand Down
8 changes: 8 additions & 0 deletions lua/crates/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ function M.setup(cfg)
local group = vim.api.nvim_create_augroup("Crates", {})
if state.cfg.autoload then
if vim.fn.expand("%:t") == "Cargo.toml" then
if state.cfg.src.cmp.enabled then
require("crates.src.cmp").setup()
end

core.update(nil, false)
state.cfg.on_attach(vim.api.nvim_get_current_buf())
end
Expand All @@ -99,6 +103,10 @@ function M.setup(cfg)
group = group,
pattern = "Cargo.toml",
callback = function(info)
if state.cfg.src.cmp.enabled then
require("crates.src.cmp").setup()
end

core.update(nil, false)
state.cfg.on_attach(info.buf)
end,
Expand Down
93 changes: 90 additions & 3 deletions lua/crates/src/cmp.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,81 @@
local M = {}
local M = {lsp = {CompletionItemKind = {}, MarkupKind = {}, MarkupContent = {}, CompletionItem = {}, CompletionList = {}, }, cmp = {SourceBaseApiParams = {}, SourceCompletionApiParams = {}, }, }











































































local cmp = require("cmp")
local lsp = cmp.lsp
local src = require("crates.src.common")
local util = require("crates.util")


function M.new()
Expand Down Expand Up @@ -38,4 +111,18 @@ function M:complete(_, callback)
src.complete(callback)
end

function M.setup()
if M.registered_source then
return
end

local cmp = package.loaded["cmp"]
if not cmp then
return
end

cmp.register_source("crates", M.new())
M.registered_source = true
end

return M
16 changes: 15 additions & 1 deletion scripts/README.md.in
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,21 @@ lua require('crates').setup()

### Auto completion
#### [nvim-cmp](https://github.com/hrsh7th/nvim-cmp) source
Just add it to your list of sources.

Enable it in the setup.
```lua
require('crates').setup {
...
src = {
...
cmp = {
enabled = true,
},
},
}
```

And add it to your list of sources.
```lua
require('cmp').setup {
...
Expand Down
Loading

0 comments on commit 71fc6ae

Please sign in to comment.