Skip to content

Commit 37b8f34

Browse files
committed
languages/ruby: add ruby support
1 parent dc5fc8c commit 37b8f34

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

configuration.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ isMaximal: {
6565
enable = isMaximal;
6666
crates.enable = isMaximal;
6767
};
68+
ruby.enable = isMaximal;
6869

6970
# Language modules that are not as common.
7071
assembly.enable = false;

docs/release-notes/rl-0.8.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@
5757
[kaktu5](https://github.com/kaktu5):
5858

5959
- Add WGSL support under `vim.languages.wgsl`.
60+
61+
[tomasguinzburg](https://github.com/tomasguinzburg):
62+
63+
[solargraph]: https://github.com/castwide/solargraph
64+
65+
- Add Ruby support under `vim.languages.ruby` using [solargraph].

modules/plugins/languages/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ in {
3939
./nu.nix
4040
./odin.nix
4141
./wgsl.nix
42+
./ruby.nix
4243
];
4344

4445
options.vim.languages = {

modules/plugins/languages/ruby.nix

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
{
2+
config,
3+
pkgs,
4+
lib,
5+
...
6+
}: let
7+
inherit (builtins) attrNames;
8+
inherit (lib.options) mkEnableOption mkOption;
9+
inherit (lib.modules) mkIf mkMerge;
10+
inherit (lib.nvim.types) mkGrammarOption;
11+
inherit (lib.types) either listOf package str enum;
12+
inherit (lib.nvim.languages) diagnosticsToLua;
13+
14+
cfg = config.vim.languages.ruby;
15+
16+
defaultServer = "rubyserver";
17+
servers = {
18+
rubyserver = {
19+
package = pkgs.rubyPackages.solargraph;
20+
lspConfig = ''
21+
lspconfig.solargraph.setup {
22+
capabilities = capabilities,
23+
on_attach = attach_keymaps,
24+
flags = {
25+
debounce_text_changes = 150,
26+
},
27+
cmd = { "${pkgs.solargraph}/bin/solargraph", "stdio" }
28+
}
29+
'';
30+
};
31+
};
32+
33+
# testing
34+
35+
defaultFormat = "rubocop";
36+
formats = {
37+
rubocop = {
38+
package = pkgs.rubyPackages.rubocop;
39+
nullConfig = ''
40+
41+
local conditional = function(fn)
42+
local utils = require("null-ls.utils").make_conditional_utils()
43+
return fn(utils)
44+
end
45+
46+
table.insert(
47+
ls_sources,
48+
null_ls.builtins.formatting.rubocop.with({
49+
command="${pkgs.bundler}/bin/bundle",
50+
args = vim.list_extend(
51+
{"exec", "rubocop", "-a" },
52+
null_ls.builtins.formatting.rubocop._opts.args
53+
),
54+
})
55+
)
56+
'';
57+
};
58+
};
59+
60+
defaultDiagnosticsProvider = ["rubocop"];
61+
diagnosticsProviders = {
62+
rubocop = {
63+
package = pkgs.rubyPackages.rubocop;
64+
nullConfig = pkg: ''
65+
table.insert(
66+
ls_sources,
67+
null_ls.builtins.diagnostics.rubocop.with({
68+
command = "${lib.getExe pkg}",
69+
})
70+
)
71+
'';
72+
};
73+
};
74+
in {
75+
options.vim.languages.ruby = {
76+
enable = mkEnableOption "Ruby language support";
77+
78+
treesitter = {
79+
enable = mkEnableOption "Ruby treesitter" // {default = config.vim.languages.enableTreesitter;};
80+
package = mkGrammarOption pkgs "ruby";
81+
};
82+
83+
lsp = {
84+
enable = mkEnableOption "Ruby LSP support" // {default = config.vim.languages.enableLSP;};
85+
86+
server = mkOption {
87+
type = enum (attrNames servers);
88+
default = defaultServer;
89+
description = "Ruby LSP server to use";
90+
};
91+
92+
package = mkOption {
93+
description = "Ruby LSP server package, or the command to run as a list of strings";
94+
type = either package (listOf str);
95+
default = servers.${cfg.lsp.server}.package;
96+
example = ''TODO'';
97+
};
98+
};
99+
100+
format = {
101+
enable = mkEnableOption "Ruby formatter support" // {default = config.vim.languages.enableFormat;};
102+
103+
type = mkOption {
104+
description = "Ruby formatter to use";
105+
type = enum (attrNames formats);
106+
default = defaultFormat;
107+
};
108+
109+
package = mkOption {
110+
description = "Ruby formatter package";
111+
type = package;
112+
default = formats.${cfg.format.type}.package;
113+
};
114+
};
115+
116+
extraDiagnostics = {
117+
enable =
118+
mkEnableOption "Ruby extra diagnostics support"
119+
// {default = config.vim.languages.enableExtraDiagnostics;};
120+
121+
types = lib.nvim.types.diagnostics {
122+
langDesc = "Ruby";
123+
inherit diagnosticsProviders;
124+
inherit defaultDiagnosticsProvider;
125+
};
126+
};
127+
};
128+
129+
config = mkIf cfg.enable (mkMerge [
130+
(mkIf cfg.treesitter.enable {
131+
vim.treesitter.enable = true;
132+
vim.treesitter.grammars = [cfg.treesitter.package];
133+
})
134+
135+
(mkIf cfg.lsp.enable {
136+
vim.lsp.lspconfig.enable = true;
137+
vim.lsp.lspconfig.sources.ruby-lsp = servers.${cfg.lsp.server}.lspConfig;
138+
})
139+
140+
(mkIf cfg.format.enable {
141+
vim.lsp.null-ls.enable = true;
142+
vim.lsp.null-ls.sources.ruby-format = formats.${cfg.format.type}.nullConfig;
143+
})
144+
145+
(mkIf cfg.extraDiagnostics.enable {
146+
vim.lsp.null-ls.enable = true;
147+
vim.lsp.null-ls.sources = diagnosticsToLua {
148+
lang = "ruby";
149+
config = cfg.extraDiagnostics.types;
150+
inherit diagnosticsProviders;
151+
};
152+
})
153+
]);
154+
}

0 commit comments

Comments
 (0)