Skip to content

languages/python: add ruff as lsp #575

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions docs/release-notes/rl-0.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@
- Add [ruff] as a formatter option in `vim.languages.python.format.type`.
- Add [cue] support under `vim.languages.cue`.

[QuiNzX](https://github.com/QuiNzX):

- Add ruff as lsp alongside other lsp servers in a list as an option. Under
`vim.languages.python.lsp.server`.

[ARCIII](https://github.com/ArmandoCIII):

[leetcode.nvim]: https://github.com/kawre/leetcode.nvim
Expand Down
52 changes: 36 additions & 16 deletions modules/plugins/languages/python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
inherit (lib.options) mkEnableOption mkOption literalExpression;
inherit (lib.meta) getExe;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) isList;
inherit (lib.types) enum either listOf package str bool;
inherit (lib.lists) isList toList;
inherit (lib.types) enum either listOf package str bool attrsOf;
inherit (lib.nvim.lua) expToLua;
inherit (lib) genAttrs;

cfg = config.vim.languages.python;

Expand All @@ -23,9 +24,9 @@
capabilities = capabilities;
on_attach = default_on_attach;
cmd = ${
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ''{"${cfg.lsp.package}/bin/pyright-langserver", "--stdio"}''
if isList cfg.lsp.package.pyright
then expToLua cfg.lsp.package.pyright
else ''{"${cfg.lsp.package.pyright}/bin/pyright-langserver", "--stdio"}''
}
}
'';
Expand All @@ -38,9 +39,24 @@
capabilities = capabilities;
on_attach = default_on_attach;
cmd = ${
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ''{"${cfg.lsp.package}/bin/basedpyright-langserver", "--stdio"}''
if isList cfg.lsp.package.basedpyright
then expToLua cfg.lsp.package.basedpyright
else ''{"${cfg.lsp.package.basedpyright}/bin/basedpyright-langserver", "--stdio"}''
}
}
'';
};

ruff = {
package = pkgs.ruff;
lspConfig = ''
lspconfig.ruff.setup{
capabilities = capabilities;
on_attach = default_on_attach;
cmd = ${
if isList cfg.lsp.package.ruff
then expToLua cfg.lsp.package.ruff
else ''{"${cfg.lsp.package.ruff}/bin/ruff", "server"}''
}
}
'';
Expand All @@ -53,9 +69,9 @@
capabilities = capabilities;
on_attach = default_on_attach;
cmd = ${
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ''{"${cfg.lsp.package}/bin/pylsp"}''
if isList cfg.lsp.package.pylsp
then expToLua cfg.lsp.package.pylsp
else ''{"${cfg.lsp.package.pylsp}/bin/pylsp"}''
}
}
'';
Expand Down Expand Up @@ -205,16 +221,20 @@ in {
enable = mkEnableOption "Python LSP support" // {default = config.vim.languages.enableLSP;};

server = mkOption {
description = "Python LSP server to use";
type = enum (attrNames servers);
description = "Python LSP server to use either as a single server or a list of servers";
example = ''
server = "basedpyright;
server = ["basedpyright" "ruff"];
'';
type = either (enum (attrNames servers)) (listOf (enum (attrNames servers)));
default = defaultServer;
};

package = mkOption {
description = "python LSP server package, or the command to run as a list of strings";
example = ''[lib.getExe pkgs.jdt-language-server "-data" "~/.cache/jdtls/workspace"]'';
type = either package (listOf str);
default = servers.${cfg.lsp.server}.package;
type = attrsOf (either package (listOf str));
default = genAttrs (toList cfg.lsp.server) (name: servers.${name}.package);
};
};

Expand Down Expand Up @@ -268,7 +288,7 @@ in {

(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.python-lsp = servers.${cfg.lsp.server}.lspConfig;
vim.lsp.lspconfig.sources = genAttrs (toList cfg.lsp.server) (name: servers.${name}.lspConfig);
})

(mkIf cfg.format.enable {
Expand Down