-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathplugin.py
737 lines (607 loc) · 21.2 KB
/
plugin.py
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import enum
import json
import logging
import re
import sys
from pathlib import PurePath
from subprocess import PIPE, Popen
from typing import Dict, Generator, List, Optional
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
from lsprotocol.types import (
CodeAction,
CodeActionContext,
CodeActionKind,
Diagnostic,
DiagnosticSeverity,
DiagnosticTag,
Position,
Range,
TextEdit,
WorkspaceEdit,
)
from pylsp import hookimpl
from pylsp._utils import find_parents
from pylsp.config.config import Config
from pylsp.workspace import Document, Workspace
from pylsp_ruff.ruff import Check as RuffCheck
from pylsp_ruff.ruff import Fix as RuffFix
from pylsp_ruff.settings import PluginSettings, get_converter
log = logging.getLogger(__name__)
logging.getLogger("blib2to3").setLevel(logging.ERROR)
converter = get_converter()
DIAGNOSTIC_SOURCE = "ruff"
# shamelessly borrowed from:
# https://github.com/charliermarsh/ruff-lsp/blob/2a0e2ea3afefdbf00810b8df91030c1c6b59d103/ruff_lsp/server.py#L214
NOQA_REGEX = re.compile(
r"(?i:# (?:(?:ruff|flake8): )?(?P<noqa>noqa))"
r"(?::\s?(?P<codes>([A-Z]+[0-9]+(?:[,\s]+)?)+))?"
)
UNNECESSITY_CODES = {
"F401", # `module` imported but unused
"F504", # % format unused named arguments
"F522", # .format(...) unused named arguments
"F523", # .format(...) unused positional arguments
"F841", # local variable `name` is assigned to but never used
}
DIAGNOSTIC_SEVERITIES = {
"E": DiagnosticSeverity.Error,
"W": DiagnosticSeverity.Warning,
"I": DiagnosticSeverity.Information,
"H": DiagnosticSeverity.Hint,
}
class Subcommand(str, enum.Enum):
CHECK = "check"
FORMAT = "format"
def __str__(self) -> str:
return self.value
def build_args(
self,
document_path: str,
settings: PluginSettings,
fix: bool = False,
extra_arguments: Optional[List[str]] = None,
) -> List[str]:
if self == Subcommand.CHECK:
return build_check_arguments(document_path, settings, fix, extra_arguments)
elif self == Subcommand.FORMAT:
return build_format_arguments(document_path, settings, extra_arguments)
else:
logging.warn(f"subcommand without argument builder '{self}'")
return []
@hookimpl
def pylsp_settings():
log.debug("Initializing pylsp_ruff")
# This plugin disables some enabled-by-default plugins that duplicate Ruff
# functionality
settings = {
"plugins": {
"ruff": PluginSettings(),
"pyflakes": {"enabled": False},
"mccabe": {"enabled": False},
"pycodestyle": {"enabled": False},
"yapf": {"enabled": False},
"autopep8": {"enabled": False},
}
}
return converter.unstructure(settings)
@hookimpl(hookwrapper=True)
def pylsp_format_document(workspace: Workspace, document: Document) -> Generator:
"""Provide formatting through ruff.
Parameters
----------
workspace : pylsp.workspace.Workspace
Current workspace.
document : pylsp.workspace.Document
Document to apply ruff on.
"""
log.debug(f"textDocument/formatting: {document}")
outcome = yield
result = outcome.get_result()
if result:
source = result[0]["newText"]
else:
source = document.source
settings = load_settings(workspace=workspace, document_path=document.path)
if not settings.format_enabled:
return
new_text = run_ruff_format(
settings=settings, document_path=document.path, document_source=source
)
if settings.format:
# A second pass through the document with `ruff check` and only the rules
# enabled via the format config property. This allows for things like
# specifying `format = ["I"]` to get import sorting as part of formatting.
new_text = run_ruff(
settings=PluginSettings(
ignore=["ALL"], select=settings.format, executable=settings.executable
),
document_path=document.path,
document_source=new_text,
fix=True,
)
# Avoid applying empty text edit
if not new_text or new_text == source:
return
range = Range(
start=Position(line=0, character=0),
end=Position(line=len(document.lines), character=0),
)
text_edit = TextEdit(range=range, new_text=new_text)
outcome.force_result(converter.unstructure([text_edit]))
@hookimpl
def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
"""Register ruff as the linter.
Parameters
----------
workspace : pylsp.workspace.Workspace
Current workspace.
document : pylsp.workspace.Document
Document to apply ruff on.
Returns
-------
List of dicts containing the diagnostics.
"""
settings = load_settings(workspace, document.path)
checks = run_ruff_check(document=document, settings=settings)
diagnostics = [create_diagnostic(check=c, settings=settings) for c in checks]
return converter.unstructure(diagnostics)
def create_diagnostic(check: RuffCheck, settings: PluginSettings) -> Diagnostic:
"""Create a LSP diagnostic based on the given RuffCheck object.
Parameters
----------
check : RuffCheck
RuffCheck object to convert.
settings : PluginSettings
Current settings.
Returns
-------
Diagnostic
"""
# Adapt range to LSP specification (zero-based)
range = Range(
start=Position(
line=check.location.row - 1,
character=check.location.column - 1,
),
end=Position(
line=check.end_location.row - 1,
character=check.end_location.column - 1,
),
)
# Ruff intends to implement severity codes in the future,
# see https://github.com/charliermarsh/ruff/issues/645.
severity = DiagnosticSeverity.Warning
if check.code == "E999" or check.code[0] == "F":
severity = DiagnosticSeverity.Error
# Check if check.code starts contained in given severities
if settings.severities is not None:
_custom_sev = [
sev
for pat, sev in sorted(
settings.severities.items(), key=lambda key: (len(key), key)
)
if check.code.startswith(pat)
]
if _custom_sev:
custom_sev = _custom_sev[-1]
severity = DIAGNOSTIC_SEVERITIES.get(custom_sev, severity)
tags = []
if check.code in UNNECESSITY_CODES:
tags = [DiagnosticTag.Unnecessary]
return Diagnostic(
source=DIAGNOSTIC_SOURCE,
code=check.code,
range=range,
message=check.message,
severity=severity,
tags=tags,
data=check.fix,
)
@hookimpl
def pylsp_code_actions(
config: Config,
workspace: Workspace,
document: Document,
range: Dict,
context: Dict,
) -> List[Dict]:
"""Provide code actions through ruff.
Parameters
----------
config : pylsp.config.config.Config
Current workspace.
workspace : pylsp.workspace.Workspace
Current workspace.
document : pylsp.workspace.Document
Document to apply ruff on.
range : Dict
Range argument given by pylsp. Not used here.
context : Dict
CodeActionContext given as dict.
Returns
-------
List of dicts containing the code actions.
"""
log.debug(f"textDocument/codeAction: {document} {range} {context}")
_context = converter.structure(context, CodeActionContext)
diagnostics = _context.diagnostics
code_actions = []
has_organize_imports = False
settings = load_settings(workspace=workspace, document_path=document.path)
for diagnostic in diagnostics:
code_actions.append(
create_disable_code_action(document=document, diagnostic=diagnostic)
)
if diagnostic.data: # Has fix
fix = converter.structure(diagnostic.data, RuffFix)
if fix.applicability == "unsafe":
if not settings.unsafe_fixes:
continue
fix.message += " (unsafe)"
if diagnostic.code == "I001":
code_actions.append(
create_organize_imports_code_action(
document=document, diagnostic=diagnostic, fix=fix
)
)
has_organize_imports = True
else:
code_actions.append(
create_fix_code_action(
document=document, diagnostic=diagnostic, fix=fix
),
)
checks = run_ruff_check(document=document, settings=settings)
checks_with_fixes = [c for c in checks if c.fix]
checks_organize_imports = [c for c in checks_with_fixes if c.code == "I001"]
if not has_organize_imports and checks_organize_imports:
check = checks_organize_imports[0]
fix = check.fix # type: ignore
diagnostic = create_diagnostic(check=check, settings=settings)
code_actions.extend(
[
create_organize_imports_code_action(
document=document, diagnostic=diagnostic, fix=fix
),
create_disable_code_action(document=document, diagnostic=diagnostic),
]
)
if any([c.fix.applicability == "safe" for c in checks_with_fixes]): # type: ignore
code_actions.append(
create_fix_all_code_action(document=document, settings=settings),
)
return converter.unstructure(code_actions)
def create_fix_code_action(
document: Document,
diagnostic: Diagnostic,
fix: RuffFix,
) -> CodeAction:
title = f"Ruff ({diagnostic.code}): {fix.message}"
kind = CodeActionKind.QuickFix
text_edits = create_text_edits(fix)
workspace_edit = WorkspaceEdit(changes={document.uri: text_edits})
return CodeAction(
title=title,
kind=kind,
diagnostics=[diagnostic],
edit=workspace_edit,
)
def create_disable_code_action(
document: Document,
diagnostic: Diagnostic,
) -> CodeAction:
title = f"Ruff ({diagnostic.code}): Disable for this line"
kind = CodeActionKind.QuickFix
line = document.lines[diagnostic.range.start.line].rstrip("\r\n")
match = NOQA_REGEX.search(line)
has_noqa = match is not None
has_codes = match and match.group("codes") is not None
# `foo # noqa: OLD` -> `foo # noqa: OLD,NEW`
if has_noqa and has_codes:
new_line = f"{line},{diagnostic.code}"
# `foo # noqa` -> `foo # noqa: NEW`
elif has_noqa:
new_line = f"{line}: {diagnostic.code}"
# `foo` -> `foo # noqa: NEW`
else:
new_line = f"{line} # noqa: {diagnostic.code}"
range = Range(
start=Position(line=diagnostic.range.start.line, character=0),
end=Position(line=diagnostic.range.start.line, character=len(line)),
)
text_edit = TextEdit(range=range, new_text=new_line)
workspace_edit = WorkspaceEdit(changes={document.uri: [text_edit]})
return CodeAction(
title=title,
kind=kind,
diagnostics=[diagnostic],
edit=workspace_edit,
)
def create_organize_imports_code_action(
document: Document,
diagnostic: Diagnostic,
fix: RuffFix,
) -> CodeAction:
title = f"Ruff: {fix.message}"
kind = CodeActionKind.SourceOrganizeImports
text_edits = create_text_edits(fix)
workspace_edit = WorkspaceEdit(changes={document.uri: text_edits})
return CodeAction(
title=title,
kind=kind,
diagnostics=[diagnostic],
edit=workspace_edit,
)
def create_fix_all_code_action(
document: Document,
settings: PluginSettings,
) -> CodeAction:
title = "Ruff: Fix All (safe fixes)"
kind = CodeActionKind.SourceFixAll
# No unsafe fixes for 'Fix all', see https://github.com/python-lsp/python-lsp-ruff/issues/55
settings.unsafe_fixes = False
new_text = run_ruff_fix(document=document, settings=settings)
range = Range(
start=Position(line=0, character=0),
end=Position(line=len(document.lines), character=0),
)
text_edit = TextEdit(range=range, new_text=new_text)
workspace_edit = WorkspaceEdit(changes={document.uri: [text_edit]})
return CodeAction(
title=title,
kind=kind,
edit=workspace_edit,
)
def create_text_edits(fix: RuffFix) -> List[TextEdit]:
edits = []
for edit in fix.edits:
range = Range(
start=Position(
line=edit.location.row - 1,
character=edit.location.column - 1,
),
end=Position(
line=edit.end_location.row - 1,
character=edit.end_location.column - 1,
),
)
edits.append(TextEdit(range=range, new_text=edit.content))
return edits
def run_ruff_check(document: Document, settings: PluginSettings) -> List[RuffCheck]:
result = run_ruff(
document_path=document.path,
document_source=document.source,
settings=settings,
subcommand=Subcommand.CHECK,
)
try:
result = json.loads(result)
except json.JSONDecodeError:
result = [] # type: ignore
return converter.structure(result, List[RuffCheck])
def run_ruff_fix(document: Document, settings: PluginSettings) -> str:
result = run_ruff(
document_path=document.path,
document_source=document.source,
fix=True,
settings=settings,
)
return result
def run_ruff_format(
settings: PluginSettings,
document_path: str,
document_source: str,
) -> str:
return run_ruff(
settings=settings,
document_path=document_path,
document_source=document_source,
subcommand=Subcommand.FORMAT,
)
def run_ruff(
settings: PluginSettings,
document_path: str,
document_source: str,
subcommand: Subcommand = Subcommand.CHECK,
fix: bool = False,
extra_arguments: Optional[List[str]] = None,
) -> str:
"""Run ruff on the given document and the given arguments.
Parameters
----------
settings : PluginSettings
Settings to use.
document_path : str
Path to file to run ruff on.
document_source : str
Document source or to apply ruff on.
Needed when the source differs from the file source, e.g. during formatting.
subcommand: Subcommand
The ruff subcommand to run. Default = Subcommand.CHECK.
fix : bool
Whether to run fix or no-fix.
extra_arguments : List[str]
Extra arguments to pass to ruff.
Returns
-------
String containing the result in json format.
"""
executable = settings.executable
arguments = subcommand.build_args(document_path, settings, fix, extra_arguments)
p = None
if executable is not None:
log.debug(f"Calling {executable} with args: {arguments} on '{document_path}'")
try:
cmd = [executable, str(subcommand)]
cmd.extend(arguments)
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
except Exception:
log.error(f"Can't execute ruff with given executable '{executable}'.")
if p is None:
log.debug(
f"Calling ruff via '{sys.executable} -m ruff'"
f" with args: {arguments} on '{document_path}'"
)
cmd = [sys.executable, "-m", "ruff", str(subcommand)]
cmd.extend(arguments)
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
(stdout, stderr) = p.communicate(document_source.encode())
if p.returncode != 0:
log.error(f"Error running ruff: {stderr.decode()}")
return stdout.decode()
def build_check_arguments(
document_path: str,
settings: PluginSettings,
fix: bool = False,
extra_arguments: Optional[List[str]] = None,
) -> List[str]:
"""Build arguments for ruff check.
Parameters
----------
document : pylsp.workspace.Document
Document to apply ruff on.
settings : PluginSettings
Settings to use for arguments to pass to ruff.
fix : bool
Whether to execute with --fix.
extra_arguments : List[str]
Extra arguments to pass to ruff.
Returns
-------
List containing the arguments.
"""
args = []
# Suppress update announcements
args.append("--quiet")
# Suppress exit 1 when violations were found
args.append("--exit-zero")
# Use the json formatting for easier evaluation
args.append("--output-format=json")
args.append("--extension=ipynb:python")
if fix:
args.append("--fix")
else:
# Do not attempt to fix -> returns file instead of diagnostics
args.append("--no-fix")
# Always force excludes
args.append("--force-exclude")
# Pass filename to ruff for per-file-ignores, catch unsaved
if document_path != "":
args.append(f"--stdin-filename={document_path}")
if settings.config:
args.append(f"--config={settings.config}")
if settings.line_length:
args.append(f"--line-length={settings.line_length}")
if settings.preview:
args.append("--preview")
if settings.unsafe_fixes:
args.append("--unsafe-fixes")
if settings.exclude:
args.append(f"--exclude={','.join(settings.exclude)}")
if settings.select:
args.append(f"--select={','.join(settings.select)}")
if settings.extend_select:
args.append(f"--extend-select={','.join(settings.extend_select)}")
if settings.ignore:
args.append(f"--ignore={','.join(settings.ignore)}")
if settings.extend_ignore:
args.append(f"--extend-ignore={','.join(settings.extend_ignore)}")
if settings.target_version:
args.append(f"--target-version={settings.target_version}")
if settings.per_file_ignores:
for path, errors in settings.per_file_ignores.items():
if not PurePath(document_path).match(path):
continue
args.append(f"--ignore={','.join(errors)}")
if extra_arguments:
args.extend(extra_arguments)
args.extend(["--", "-"])
return args
def build_format_arguments(
document_path: str,
settings: PluginSettings,
extra_arguments: Optional[List[str]] = None,
) -> List[str]:
"""Build arguments for ruff format.
Parameters
----------
document : pylsp.workspace.Document
Document to apply ruff on.
settings : PluginSettings
Settings to use for arguments to pass to ruff.
extra_arguments : List[str]
Extra arguments to pass to ruff.
Returns
-------
List containing the arguments.
"""
args = []
# Suppress update announcements
args.append("--quiet")
# Always force excludes
args.append("--force-exclude")
# Pass filename to ruff for per-file-ignores, catch unsaved
if document_path != "":
args.append(f"--stdin-filename={document_path}")
if settings.config:
args.append(f"--config={settings.config}")
if settings.exclude:
args.append(f"--exclude={','.join(settings.exclude)}")
if settings.preview:
args.append("--preview")
if settings.line_length:
args.append(f"--line-length={settings.line_length}")
if settings.target_version:
args.append(f"--target-version={settings.target_version}")
if extra_arguments:
args.extend(extra_arguments)
args.extend(["--", "-"])
return args
def load_settings(workspace: Workspace, document_path: str) -> PluginSettings:
"""Load settings from pyproject.toml file in the project path.
Parameters
----------
workspace : pylsp.workspace.Workspace
Current workspace.
document_path : str
Path to the document to apply ruff on.
Returns
-------
PluginSettings read via lsp.
"""
config = workspace._config
_plugin_settings = config.plugin_settings("ruff", document_path=document_path)
plugin_settings = converter.structure(_plugin_settings, PluginSettings)
pyproject_file = find_parents(
workspace.root_path, document_path, ["pyproject.toml"]
)
config_in_pyproject = False
if pyproject_file:
try:
with open(pyproject_file[0], "rb") as f:
toml_dict = tomllib.load(f)
if "tool" in toml_dict and "ruff" in toml_dict["tool"]:
config_in_pyproject = True
except tomllib.TOMLDecodeError:
log.warn("Error while parsing toml file, ignoring config.")
ruff_toml = find_parents(
workspace.root_path, document_path, ["ruff.toml", ".ruff.toml"]
)
# Check if pyproject is present, ignore user settings if toml exists
if config_in_pyproject or ruff_toml:
log.debug("Found existing configuration for ruff, skipping pylsp config.")
# Leave config to pyproject.toml
return PluginSettings(
enabled=plugin_settings.enabled,
format_enabled=plugin_settings.format_enabled,
executable=plugin_settings.executable,
unsafe_fixes=plugin_settings.unsafe_fixes,
extend_ignore=plugin_settings.extend_ignore,
extend_select=plugin_settings.extend_select,
format=plugin_settings.format,
severities=plugin_settings.severities,
)
return plugin_settings