Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

languages: inherit lspconfig options from language options #643

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions lib/languages.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# From home-manager: https://github.com/nix-community/home-manager/blob/master/modules/lib/booleans.nix
{lib}: let
inherit (builtins) isString getAttr;
inherit (lib.options) mkOption;
inherit (lib.types) bool;
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.types) listOf bool str submodule attrsOf anything;
inherit (lib.generators) mkLuaInline;
inherit (lib.meta) getExe;
inherit (lib.nvim.attrsets) mapListToAttrs;
inherit (lib.nvim.types) luaInline;
in {
diagnosticsToLua = {
lang,
Expand Down Expand Up @@ -32,4 +34,31 @@ in {
type = bool;
description = "Turn on ${desc} for enabled languages by default";
};

lspOptions = submodule {
freeformType = attrsOf anything;
options = {
capabilities = mkOption {
type = luaInline;
default = mkLuaInline "default_capabilities";
description = "LSP capabilitiess to pass to lspconfig";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just "capabilities" I'm pretty sure

};

on_attach = mkOption {
type = luaInline;
default = mkLuaInline "default_capabilities";
description = "Function to execute when an LSP server attaches to a buffer";
};

filetypes = mkOption {
type = listOf str;
description = "Filetypes to auto-attach LSP in";
};

cmd = mkOption {
type = listOf str;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think it's necessary to split this into cmd, args? someone might want to override the cmd but keep the args

description = "Command used to start the LSP server";
};
};
};
}
68 changes: 54 additions & 14 deletions modules/plugins/languages/asm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@
lib,
...
}: let
inherit (builtins) attrNames;
inherit (lib.options) mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.types) package;
inherit (lib.types) enum either listOf package str;
inherit (lib.lists) isList;
inherit (lib.meta) getExe;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.lua) expToLua toLuaObject;
inherit (lib.nvim.languages) lspOptions;
inherit (lib.nvim.types) mkGrammarOption;

cfg = config.vim.languages.assembly;

defaultServer = "asm-lsp";
servers = {
asm-lsp = {
package = pkgs.asm-lsp;
options = {
capabilities = mkLuaInline "capabilities";
on_attach = mkLuaInline "attach_keymaps";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not default_on_attach?

filetypes = ["asm" "vasm"];
cmd =
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ["${getExe cfg.lsp.package}"];
};
};
};
in {
options.vim.languages.assembly = {
enable = mkEnableOption "Assembly support";
Expand All @@ -22,28 +44,46 @@ in {
lsp = {
enable = mkEnableOption "Assembly LSP support (asm-lsp)" // {default = config.vim.languages.enableLSP;};

server = mkOption {
description = "Assembly LSP server to use";
type = enum (attrNames servers);
default = defaultServer;
};

package = mkOption {
type = package;
default = pkgs.asm-lsp;
description = "asm-lsp package";
description = "asm-lsp LSP server package, or the command to run as a list of strings";
example = ''[lib.getExe pkgs.asm-lsp "--quiet"]'';
type = either package (listOf str);
default = servers.${cfg.lsp.server}.package;
};

options = mkOption {
type = lspOptions;
default = servers.${cfg.lsp.server}.options;
description = ''
LSP options for Assembly language support.

This option is freeform, you may add options that are not set by default
and they will be merged into the final table passed to lspconfig.
'';
};
};
};
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package];
vim.treesitter = {
enable = true;
grammars = [cfg.treesitter.package];
};
})

(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.asm-lsp = ''
lspconfig.asm_lsp.setup {
capabilities = capabilities,
on_attach = default_on_attach,
cmd = {"${cfg.lsp.package}/bin/asm-lsp"},
}
'';
vim.lsp.lspconfig = {
enable = true;
sources.asm-lsp = ''
lspconfig.("asm_lsp").setup (${toLuaObject cfg.lsp.options})
'';
};
})
]);
}
44 changes: 28 additions & 16 deletions modules/plugins/languages/astro.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
inherit (builtins) attrNames;
inherit (lib.options) mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.types) enum either listOf package str;
inherit (lib.lists) isList;
inherit (lib.meta) getExe;
inherit (lib.types) enum either listOf package str;
inherit (lib.nvim.lua) expToLua;
inherit (lib.nvim.languages) diagnosticsToLua;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.lua) expToLua toLuaObject;
inherit (lib.nvim.languages) diagnosticsToLua lspOptions;
inherit (lib.nvim.types) mkGrammarOption diagnostics;

cfg = config.vim.languages.astro;
Expand All @@ -20,17 +21,16 @@
servers = {
astro = {
package = pkgs.astro-language-server;
lspConfig = ''
lspconfig.astro.setup {
capabilities = capabilities;
on_attach = attach_keymaps,
cmd = ${
options = {
capabilities = mkLuaInline "capabilities";
on_attach = mkLuaInline "attach_keymaps";
filetypes = ["astro"];
init_options = {typescript = {};};
cmd =
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ''{"${cfg.lsp.package}/bin/astro-ls", "--stdio"}''
}
}
'';
else ["${getExe cfg.lsp.package}" "--stdio"];
};
};
};

Expand Down Expand Up @@ -83,8 +83,7 @@ in {

treesitter = {
enable = mkEnableOption "Astro treesitter" // {default = config.vim.languages.enableTreesitter;};

astroPackage = mkGrammarOption pkgs "astro";
package = mkGrammarOption pkgs "astro";
};

lsp = {
Expand All @@ -96,6 +95,17 @@ in {
default = defaultServer;
};

options = mkOption {
type = lspOptions;
default = servers.${cfg.lsp.server}.options;
description = ''
LSP options for Astro language support.

This option is freeform, you may add options that are not set by default
and they will be merged into the final table passed to lspconfig.
'';
};

package = mkOption {
description = "Astro LSP server package, or the command to run as a list of strings";
example = ''[lib.getExe pkgs.astro-language-server "--minify" "--stdio"]'';
Expand Down Expand Up @@ -134,12 +144,14 @@ in {
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.astroPackage];
vim.treesitter.grammars = [cfg.treesitter.package];
})

(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.astro-lsp = servers.${cfg.lsp.server}.lspConfig;
vim.lsp.lspconfig.sources.astro-lsp = ''
lspconfig.("astro").setup (${toLuaObject cfg.lsp.options})
'';
})

(mkIf cfg.format.enable {
Expand Down
41 changes: 27 additions & 14 deletions modules/plugins/languages/bash.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
inherit (builtins) attrNames;
inherit (lib.options) mkOption mkEnableOption literalExpression;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.types) enum either listOf package str bool;
inherit (lib.lists) isList;
inherit (lib.types) enum either package listOf str bool;
inherit (lib.nvim.languages) diagnosticsToLua;
inherit (lib.nvim.types) diagnostics mkGrammarOption;
inherit (lib.nvim.lua) expToLua;
inherit (lib.meta) getExe;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.lua) expToLua toLuaObject;
inherit (lib.nvim.languages) diagnosticsToLua lspOptions;
inherit (lib.nvim.types) mkGrammarOption diagnostics;

cfg = config.vim.languages.bash;

defaultServer = "bash-ls";
servers = {
bash-ls = {
package = pkgs.bash-language-server;
lspConfig = ''
lspconfig.bashls.setup{
capabilities = capabilities;
on_attach = default_on_attach;
cmd = ${
options = {
capabilities = mkLuaInline "capabilities";
on_attach = mkLuaInline "default_on_attach";
filetypes = ["bash" "sh"];
cmd =
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ''{"${cfg.lsp.package}/bin/bash-language-server", "start"}''
};
}
'';
else ["${getExe cfg.lsp.package}" "start"];
};
};
};

Expand Down Expand Up @@ -87,6 +87,17 @@ in {
type = either package (listOf str);
default = pkgs.bash-language-server;
};

options = mkOption {
type = lspOptions;
default = servers.${cfg.lsp.server}.options;
description = ''
LSP options for Bash language support.

This option is freeform, you may add options that are not set by default
and they will be merged into the final table passed to lspconfig.
'';
};
};

format = {
Expand Down Expand Up @@ -126,7 +137,9 @@ in {

(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.bash-lsp = servers.${cfg.lsp.server}.lspConfig;
vim.lsp.lspconfig.sources.bash-lsp = ''
lspconfig.("bashls").setup (${toLuaObject cfg.lsp.options})
'';
})

(mkIf cfg.format.enable {
Expand Down
Loading