4
4
import sys
5
5
from pathlib import PurePath
6
6
from subprocess import PIPE , Popen
7
- from typing import Dict , List
7
+ from typing import Dict , Generator , List , Optional
8
8
9
9
from lsprotocol .types import (
10
10
CodeAction ,
@@ -64,6 +64,34 @@ def pylsp_settings():
64
64
return converter .unstructure (settings )
65
65
66
66
67
+ @hookimpl (hookwrapper = True )
68
+ def pylsp_format_document (workspace : Workspace , document : Document ) -> Generator :
69
+ """
70
+ Provide formatting through ruff.
71
+
72
+ Parameters
73
+ ----------
74
+ workspace : pylsp.workspace.Workspace
75
+ Current workspace.
76
+ document : pylsp.workspace.Document
77
+ Document to apply ruff on.
78
+ """
79
+ log .debug (f"textDocument/formatting: { document } " )
80
+ outcome = yield
81
+ results = outcome .get_result ()
82
+ if results :
83
+ document .source = results [0 ]["new_text" ]
84
+
85
+ new_text = run_ruff_format (workspace , document )
86
+ range = Range (
87
+ start = Position (line = 0 , character = 0 ),
88
+ end = Position (line = len (document .lines ), character = 0 ),
89
+ )
90
+ text_edit = TextEdit (range = range , new_text = new_text )
91
+
92
+ outcome .force_result (converter .unstructure ([text_edit ]))
93
+
94
+
67
95
@hookimpl
68
96
def pylsp_lint (workspace : Workspace , document : Document ) -> List [Dict ]:
69
97
"""
@@ -315,7 +343,23 @@ def run_ruff_fix(workspace: Workspace, document: Document) -> str:
315
343
return result
316
344
317
345
318
- def run_ruff (workspace : Workspace , document : Document , fix : bool = False ) -> str :
346
+ def run_ruff_format (workspace : Workspace , document : Document ) -> str :
347
+ settings = load_settings (workspace , document )
348
+ extra_arguments = []
349
+ if settings .format :
350
+ extra_arguments .append (f"--fixable={ ',' .join (settings .format )} " )
351
+ else :
352
+ extra_arguments .append ("--unfixable=ALL" )
353
+ result = run_ruff (workspace , document , fix = True , extra_arguments = extra_arguments )
354
+ return result
355
+
356
+
357
+ def run_ruff (
358
+ workspace : Workspace ,
359
+ document : Document ,
360
+ fix : bool = False ,
361
+ extra_arguments : Optional [List [str ]] = None ,
362
+ ) -> str :
319
363
"""
320
364
Run ruff on the given document and the given arguments.
321
365
@@ -327,14 +371,16 @@ def run_ruff(workspace: Workspace, document: Document, fix: bool = False) -> str
327
371
File to run ruff on.
328
372
fix : bool
329
373
Whether to run fix or no-fix.
374
+ extra_arguments : List[str]
375
+ Extra arguments to pass to ruff.
330
376
331
377
Returns
332
378
-------
333
379
String containing the result in json format.
334
380
"""
335
381
settings = load_settings (workspace , document )
336
382
executable = settings .executable
337
- arguments = build_arguments (document , settings , fix )
383
+ arguments = build_arguments (document , settings , fix , extra_arguments )
338
384
339
385
log .debug (f"Calling { executable } with args: { arguments } on '{ document .path } '" )
340
386
try :
@@ -358,6 +404,7 @@ def build_arguments(
358
404
document : Document ,
359
405
settings : PluginSettings ,
360
406
fix : bool = False ,
407
+ extra_arguments : Optional [List [str ]] = None ,
361
408
) -> List [str ]:
362
409
"""
363
410
Build arguments for ruff.
@@ -368,6 +415,10 @@ def build_arguments(
368
415
Document to apply ruff on.
369
416
settings : PluginSettings
370
417
Settings to use for arguments to pass to ruff.
418
+ fix : bool
419
+ Whether to execute with --fix.
420
+ extra_arguments : List[str]
421
+ Extra arguments to pass to ruff.
371
422
372
423
Returns
373
424
-------
@@ -416,6 +467,9 @@ def build_arguments(
416
467
continue
417
468
args .append (f"--ignore={ ',' .join (errors )} " )
418
469
470
+ if extra_arguments :
471
+ args .extend (extra_arguments )
472
+
419
473
args .extend (["--" , "-" ])
420
474
421
475
return args
@@ -456,6 +510,7 @@ def load_settings(workspace: Workspace, document: Document) -> PluginSettings:
456
510
executable = plugin_settings .executable ,
457
511
extend_ignore = plugin_settings .extend_ignore ,
458
512
extend_select = plugin_settings .extend_select ,
513
+ format = plugin_settings .format ,
459
514
)
460
515
461
516
return plugin_settings
0 commit comments