44 lib ,
55 ...
66} : let
7- inherit ( builtins ) attrNames ;
87 inherit ( lib . meta ) getExe ;
98 inherit ( lib . modules ) mkIf mkMerge ;
109 inherit ( lib . options ) mkOption mkEnableOption literalMD ;
1110 inherit ( lib . strings ) optionalString ;
12- inherit ( lib . trivial ) boolToString ;
1311 inherit ( lib . lists ) isList ;
12+ inherit ( lib . attrsets ) attrNames ;
1413 inherit ( lib . types ) bool package str listOf either enum ;
15- inherit ( lib . nvim . types ) mkGrammarOption ;
16- inherit ( lib . nvim . lua ) toLuaObject ;
14+ inherit ( lib . nvim . types ) mkGrammarOption mkPluginSetupOption ;
15+ inherit ( lib . nvim . lua ) expToLua toLuaObject ;
1716 inherit ( lib . nvim . dag ) entryAfter entryAnywhere ;
1817
1918 cfg = config . vim . languages . rust ;
3332 package = mkGrammarOption pkgs "rust" ;
3433 } ;
3534
36- crates = {
37- enable = mkEnableOption "crates-nvim, tools for managing dependencies" ;
38- codeActions = mkOption {
39- description = "Enable code actions through null-ls" ;
40- type = bool ;
41- default = true ;
42- } ;
43- } ;
44-
4535 lsp = {
4636 enable = mkEnableOption "Rust LSP support (rust-analyzer with extra tools)" // { default = config . vim . lsp . enable ; } ;
4737 package = mkOption {
@@ -103,25 +93,32 @@ in {
10393 default = pkgs . lldb ;
10494 } ;
10595 } ;
106- } ;
10796
108- config = mkIf cfg . enable ( mkMerge [
109- ( mkIf cfg . crates . enable {
110- vim = {
111- startPlugins = [ "crates-nvim" ] ;
112- lsp . null-ls . enable = mkIf cfg . crates . codeActions true ;
113- autocomplete . nvim-cmp . sources = { crates = "[Crates]" ; } ;
114- pluginRC . rust-crates = entryAnywhere ''
115- require('crates').setup {
116- null_ls = {
117- enabled = ${ boolToString cfg . crates . codeActions } ,
118- name = "crates.nvim",
119- }
120- }
121- '' ;
97+ extensions = {
98+ crates-nvim = {
99+ enable = mkEnableOption "crates.io dependency management [crates-nvim]" ;
100+
101+ setupOpts = mkPluginSetupOption "crates-nvim" {
102+ completion . enable = mkOption {
103+ type = bool ;
104+ default = config . vim . autocomplete . nvim-cmp . enable ;
105+ defaultText = "{option}`config.vim.autocomplete.nvim-cmp.enable`" ;
106+ description = ''
107+ Whether to add crates.nvim as a source for completion plugins. The following
108+ plugins are supported by crates.nvim:
109+
110+ * nvim-cmp
111+ * coq.nvim
112+
113+ However nvf only supports auto-setup for nvim-cmp.
114+ '' ;
115+ } ;
116+ } ;
122117 } ;
123- } )
118+ } ;
119+ } ;
124120
121+ config = mkIf cfg . enable ( mkMerge [
125122 ( mkIf cfg . treesitter . enable {
126123 vim . treesitter . enable = true ;
127124 vim . treesitter . grammars = [ cfg . treesitter . package ] ;
140137 ( mkIf ( cfg . lsp . enable || cfg . dap . enable ) {
141138 vim = {
142139 startPlugins = [ "rustaceanvim" ] ;
143-
144140 pluginRC . rustaceanvim = entryAfter [ "lsp-setup" ] ''
145141 vim.g.rustaceanvim = {
146142 ${ optionalString cfg . lsp . enable ''
@@ -153,13 +149,14 @@ in {
153149 server = {
154150 cmd = ${
155151 if isList cfg . lsp . package
156- then toLuaObject cfg . lsp . package
152+ then expToLua cfg . lsp . package
157153 else ''{"${ cfg . lsp . package } /bin/rust-analyzer"}''
158154 } ,
159155 default_settings = {
160156 ${ cfg . lsp . opts }
161157 },
162158 on_attach = function(client, bufnr)
159+ default_on_attach(client, bufnr)
163160 local opts = { noremap=true, silent=true, buffer = bufnr }
164161 vim.keymap.set("n", "<localleader>rr", ":RustLsp runnables<CR>", opts)
165162 vim.keymap.set("n", "<localleader>rp", ":RustLsp parentModule<CR>", opts)
@@ -198,5 +195,28 @@ in {
198195 '' ;
199196 } ;
200197 } )
198+
199+ ( mkIf cfg . extensions . crates-nvim . enable {
200+ vim = let
201+ withCompletion = cfg . extensions . crates-nvim . withCmpSource ;
202+ in
203+ mkMerge [
204+ {
205+ startPlugins = [ "crates-nvim" ] ;
206+ pluginRC . rust-crates = entryAnywhere ''
207+ require("crates").setup(${ toLuaObject cfg . extensions . crates-nvim . setupOpts } )
208+ '' ;
209+ }
210+
211+ # FIXME: this will not be necessary once crates.nvim creates a new release that
212+ # ships improvements to the in-progress LSP module. If updating > 0.7.1, remember
213+ # to update this section.
214+ # See:
215+ # <https://github.com/saecki/crates.nvim/wiki/Documentation-unstable#auto-completion>
216+ ( mkIf withCompletion {
217+ autocomplete . nvim-cmp . sources = { crates = "[Crates]" ; } ;
218+ } )
219+ ] ;
220+ } )
201221 ] ) ;
202222}
0 commit comments