-
-
Notifications
You must be signed in to change notification settings - Fork 203
/
pylsp.nix
611 lines (511 loc) · 21.2 KB
/
pylsp.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
{
lib,
helpers,
config,
pkgs,
...
}:
with lib;
let
cfg = config.plugins.lsp.servers.pylsp;
in
{
# All settings are documented here:
# https://github.com/python-lsp/python-lsp-server/blob/develop/CONFIGURATION.md
options.plugins.lsp.servers.pylsp.settings = {
configurationSources = mkOption {
type = lib.types.nullOr (
types.enum [
"pycodestyle"
"flake8"
]
);
description = "List of configuration sources to use.";
default = null;
apply = value: if (value != null) then [ value ] else null;
};
plugins = {
autopep8 = {
enabled = helpers.defaultNullOpts.mkBool true ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin (autopep8).
'';
};
flake8 = {
config = helpers.mkNullOrOption types.str ''
Path to the config file that will be the authoritative config source.
'';
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin (flake8).
'';
exclude = helpers.defaultNullOpts.mkListOf types.str [ ] ''
List of files or directories to exclude.
'';
executable = helpers.mkNullOrOption types.str ''
Path to the flake8 executable.
'';
filename = helpers.mkNullOrOption types.str ''
Only check for filenames matching the patterns in this list.
'';
hangClosing = helpers.mkNullOrOption types.bool ''
Hang closing bracket instead of matching indentation of opening bracket's line.
'';
ignore = helpers.defaultNullOpts.mkListOf types.str [ ] ''
List of errors and warnings to ignore (or skip).
'';
maxComplexity = helpers.mkNullOrOption types.int ''
Maximum allowed complexity threshold.
'';
maxLineLength = helpers.mkNullOrOption types.int ''
Maximum allowed line length for the entirety of this run.
'';
indentSize = helpers.mkNullOrOption types.int ''
Set indentation spaces.
'';
perFileIgnores = helpers.defaultNullOpts.mkListOf types.str [ ] ''
A pairing of filenames and violation codes that defines which violations to ignore in a
particular file.
For example: `["file_path.py:W305,W304"]`.
'';
select = helpers.mkNullOrOption (types.listOf types.str) ''
List of errors and warnings to enable.
'';
};
jedi = {
auto_import_modules = helpers.defaultNullOpts.mkListOf types.str [
"numpy"
] "List of module names for `jedi.settings.auto_import_modules`.";
extra_paths = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Define extra paths for jedi.Script.
'';
environment = helpers.mkNullOrOption types.str ''
Define environment for jedi.Script and Jedi.names.
'';
};
jedi_completion = {
enabled = helpers.defaultNullOpts.mkBool true "Enable or disable the plugin.";
include_params = helpers.defaultNullOpts.mkBool true ''
Auto-completes methods and classes with tabstops for each parameter.
'';
include_class_objects = helpers.defaultNullOpts.mkBool false ''
Adds class objects as a separate completion item.
'';
include_function_objects = helpers.defaultNullOpts.mkBool false ''
Adds function objects as a separate completion item.
'';
fuzzy = helpers.defaultNullOpts.mkBool false ''
Enable fuzzy when requesting autocomplete.
'';
eager = helpers.defaultNullOpts.mkBool false ''
Resolve documentation and detail eagerly.
'';
resolve_at_most = helpers.defaultNullOpts.mkInt 25 ''
How many labels and snippets (at most) should be resolved.
'';
cache_for = helpers.defaultNullOpts.mkListOf types.str [
"pandas"
"numpy"
"tensorflow"
"matplotlib"
] "Modules for which labels and snippets should be cached.";
};
jedi_definition = {
enabled = helpers.defaultNullOpts.mkBool true "Enable or disable the plugin.";
follow_imports = helpers.defaultNullOpts.mkBool true ''
The goto call will follow imports.
'';
follow_builtin_imports = helpers.defaultNullOpts.mkBool true ''
If follow_imports is true will decide if it follow builtin imports.
'';
follow_builtin_definitions = helpers.defaultNullOpts.mkBool true ''
Follow builtin and extension definitions to stubs.
'';
};
jedi_hover = {
enabled = helpers.defaultNullOpts.mkBool true "Enable or disable the plugin.";
};
jedi_references = {
enabled = helpers.defaultNullOpts.mkBool true "Enable or disable the plugin.";
};
jedi_signature_help = {
enabled = helpers.defaultNullOpts.mkBool true "Enable or disable the plugin.";
};
jedi_symbols = {
enabled = helpers.defaultNullOpts.mkBool true "Enable or disable the plugin.";
all_scopes = helpers.defaultNullOpts.mkBool true ''
If true lists the names of all scopes instead of only the module namespace.
'';
include_import_symbols = helpers.defaultNullOpts.mkBool true ''
If true includes symbols imported from other libraries.
'';
};
mccabe = {
enabled = helpers.defaultNullOpts.mkBool true ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin (mccabe).
'';
threshold = helpers.defaultNullOpts.mkInt 15 ''
The minimum threshold that triggers warnings about cyclomatic complexity.
'';
};
preload = {
enabled = helpers.defaultNullOpts.mkBool true "Enable or disable the plugin.";
modules = helpers.defaultNullOpts.mkListOf types.str [ ] ''
List of modules to import on startup.
'';
};
pycodestyle = {
enabled = helpers.defaultNullOpts.mkBool true ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin
(pycodestyle).
'';
exclude = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Exclude files or directories which match these patterns.
'';
filename = helpers.defaultNullOpts.mkListOf types.str [ ] ''
When parsing directories, only check filenames matching these patterns.
'';
ropeFolder = helpers.mkNullOrOption (types.listOf types.str) ''
Select errors and warnings.
'';
ignore = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Ignore errors and warnings.
'';
hangClosing = helpers.mkNullOrOption types.bool ''
Hang closing bracket instead of matching indentation of opening bracket's line.
'';
maxLineLength = helpers.mkNullOrOption types.int ''
Set maximum allowed line length.
'';
indentSize = helpers.mkNullOrOption types.int ''
Set indentation spaces.
'';
};
pydocstyle = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin
(pydocstyle).
'';
convention = helpers.mkNullOrOption (types.enum [
"pep257"
"numpy"
"google"
"None"
]) "Choose the basic list of checked errors by specifying an existing convention.";
addIgnore = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Ignore errors and warnings in addition to the specified convention.
'';
addSelect = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Select errors and warnings in addition to the specified convention.
'';
ignore = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Ignore errors and warnings.
'';
select = helpers.mkNullOrOption (types.listOf types.str) ''
Select errors and warnings.
'';
match = helpers.defaultNullOpts.mkStr "(?!test_).*\\.py" ''
Check only files that exactly match the given regular expression;
default is to match files that don't start with 'test_' but end with '.py'.
'';
matchDir = helpers.defaultNullOpts.mkStr "[^\\.].*" ''
Search only dirs that exactly match the given regular expression;
default is to match dirs which do not begin with a dot.
'';
};
pyflakes = {
enabled = helpers.defaultNullOpts.mkBool true ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin (pyflakes).
'';
};
pylint = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin (pylint).
'';
args = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Arguments to pass to pylint.
'';
executable = helpers.mkNullOrOption types.str ''
Executable to run pylint with.
Enabling this will run pylint on unsaved files via stdin.
Can slow down workflow. Only works with python3.
'';
};
rope_autoimport = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin (rope).
'';
memory = helpers.defaultNullOpts.mkBool false ''
Make the autoimport database memory only.
Drastically increases startup time.
'';
};
rope_completion = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin (rope).
'';
eager = helpers.defaultNullOpts.mkBool false ''
Resolve documentation and detail eagerly.
'';
};
yapf = {
enabled = helpers.defaultNullOpts.mkBool true ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin (yapf).
'';
};
### THIRD-PARTY PLUGINS
pylsp_mypy = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin
(pylsp-mypy).
'';
live_mode = helpers.defaultNullOpts.mkBool true ''
Provides type checking as you type.
This writes to a tempfile every time a check is done.
Turning off live_mode means you must save your changes for mypy diagnostics to update
correctly.
'';
dmypy = helpers.defaultNullOpts.mkBool false ''
Executes via dmypy run rather than mypy.
This uses the dmypy daemon and may dramatically improve the responsiveness of the pylsp
server, however this currently does not work in live_mode.
Enabling this disables live_mode, even for conflicting configs.
'';
strict = helpers.defaultNullOpts.mkBool false ''
Refers to the strict option of mypy.
This option often is too strict to be useful.
'';
overrides = helpers.defaultNullOpts.mkListOf (with types; either bool str) [ true ] ''
Specifies a list of alternate or supplemental command-line options.
This modifies the options passed to mypy or the mypy-specific ones passed to dmypy run.
When present, the special boolean member true is replaced with the command-line options that
would've been passed had overrides not been specified.
Later options take precedence, which allows for replacing or negating individual default
options (see `mypy.main:process_options` and `mypy --help | grep inverse`).
'';
dmypy_status_file = helpers.defaultNullOpts.mkStr ".dmypy.json" ''
Specifies which status file dmypy should use.
This modifies the --status-file option passed to dmypy given dmypy is active.
'';
config_sub_paths = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Specifies sub paths under which the mypy configuration file may be found.
For each directory searched for the mypy config file, this also searches the sub paths
specified here.
'';
report_progress = helpers.defaultNullOpts.mkBool false ''
Report basic progress to the LSP client.
With this option, pylsp-mypy will report when mypy is running, given your editor supports
LSP progress reporting.
For small files this might produce annoying flashing in your editor, especially in with
live_mode.
For large projects, enabling this can be helpful to assure yourself whether mypy is still
running.
'';
};
isort = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin
(pyls-isort).
'';
};
black = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin
(python-lsp-black).
'';
cache_config = helpers.defaultNullOpts.mkBool false ''
Whether to enable black configuration caching.
'';
line_length = helpers.defaultNullOpts.mkInt 88 ''
An integer that maps to black's max-line-length setting.
Defaults to 88 (same as black's default).
This can also be set through black's configuration files, which should be preferred for
multi-user projects.
'';
preview = helpers.defaultNullOpts.mkBool false ''
Enable or disable black's --preview setting.
'';
};
memestra = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin
(pyls-memestra).
'';
};
rope = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin
(pylsp-rope).
'';
};
ruff = {
enabled = helpers.defaultNullOpts.mkBool false ''
Enable or disable the plugin.
Setting this explicitly to `true` will install the dependency for this plugin
(python-lsp-ruff).
'';
config = helpers.mkNullOrOption types.str "Path to optional pyproject.toml file.";
exclude = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Exclude files from being checked by ruff.
'';
executable = helpers.mkNullOrOption types.str ''
Path to the ruff executable. Assumed to be in PATH by default.
'';
ignore = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Error codes to ignore.
'';
extendIgnore = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Same as ignore, but append to existing ignores.
'';
lineLength = helpers.mkNullOrOption types.int "Set the line-length for length checks.";
perFileIgnores = helpers.mkNullOrOption (with types; attrsOf (listOf str)) ''
File-specific error codes to be ignored.
'';
select = helpers.defaultNullOpts.mkListOf types.str [ ] ''
List of error codes to enable.
'';
extendSelect = helpers.defaultNullOpts.mkListOf types.str [ ] ''
Same as select, but append to existing error codes.
'';
format = helpers.defaultNullOpts.mkListOf types.str [ ] ''
List of error codes to fix during formatting.
The default is ["I"], any additional codes are appended to this list.
'';
};
### END OF THIRD-PARTY PLUGINS
};
rope = {
extensionModules = helpers.mkNullOrOption types.str ''
Builtin and c-extension modules that are allowed to be imported and inspected by rope.
'';
ropeFolder = helpers.mkNullOrOption (types.listOf types.str) ''
The name of the folder in which rope stores project configurations and data.
Pass null for not using such a folder at all.
'';
};
};
config = mkIf cfg.enable {
# WARNING: tricky stuff below:
# We need to fix the `python-lsp-server` derivation by adding all of the (user enabled)
# plugins to its `propagatedBuildInputs`.
# See https://github.com/NixOS/nixpkgs/issues/229337
plugins.lsp.servers.pylsp.package =
let
isEnabled = x: (x.enabled != null && x.enabled);
inherit (cfg.settings) plugins;
nativePlugins =
(map
(
pluginName:
(optionals (isEnabled plugins.${pluginName}) cfg.package.optional-dependencies.${pluginName})
)
[
"autopep8"
"flake8"
"mccabe"
"pycodestyle"
"pydocstyle"
"pyflakes"
"pylint"
"yapf"
]
)
++ (optionals (
(isEnabled plugins.rope_autoimport) || (isEnabled plugins.rope_completion)
) cfg.package.optional-dependencies.rope);
# All of those plugins have `python-lsp-server` as a dependency.
# We need to get rid of it to add them to the `python-lsp-server` derivation itself.
thirdPartyPlugins = lists.flatten (
mapAttrsToList
(
pluginName: nixPackage:
(optional (isEnabled plugins.${pluginName}) (
nixPackage.overridePythonAttrs (old: {
# Get rid of the python-lsp-server dependency
propagatedBuildInputs = filter (dep: dep.pname != "python-lsp-server") old.propagatedBuildInputs;
# Skip testing because those naked dependencies will complain about missing pylsp
doCheck = false;
})
))
)
(
with pkgs.python3Packages;
{
pylsp_mypy = pylsp-mypy.overridePythonAttrs (old: {
postPatch =
old.postPatch or ''''
+ ''
substituteInPlace setup.cfg \
--replace-fail "python-lsp-server >=1.7.0" ""
'';
});
isort = pyls-isort.overridePythonAttrs (old: {
postPatch =
old.postPatch or ''''
+ ''
substituteInPlace setup.py \
--replace-fail 'install_requires=["python-lsp-server", "isort"],' 'install_requires=["isort"],'
'';
});
black = python-lsp-black.overridePythonAttrs (old: {
postPatch =
old.postPatch or ''''
+ ''
substituteInPlace setup.cfg \
--replace-fail "python-lsp-server>=1.4.0" ""
'';
});
memestra = pyls-memestra.overridePythonAttrs (old: {
postPatch =
old.postPatch or ''''
+ ''
sed -i '/python-lsp-server/d' requirements.txt
'';
});
rope = pylsp-rope.overridePythonAttrs (old: {
postPatch =
old.postPatch or ''''
+ ''
sed -i '/python-lsp-server/d' setup.cfg
'';
});
ruff = python-lsp-ruff.overridePythonAttrs (old: {
postPatch =
old.postPatch or ''''
+ ''
sed -i '/python-lsp-server/d' pyproject.toml
'';
build-system = [ setuptools ] ++ (old.build-system or [ ]);
});
}
)
);
# Final list of pylsp plugins to install
pylspPlugins = nativePlugins ++ thirdPartyPlugins;
in
mkDefault (
# This is the final default package for pylsp
pkgs.python3Packages.python-lsp-server.overridePythonAttrs (old: {
propagatedBuildInputs = pylspPlugins ++ old.dependencies;
disabledTests = (old.disabledTests or [ ]) ++ [
# Those tests fail when third-party plugins are loaded
"test_notebook_document__did_open"
"test_notebook_document__did_change"
];
})
);
};
}