44import sys
55from pathlib import PurePath
66from subprocess import PIPE , Popen
7- from typing import Dict , List
7+ from typing import Dict , Generator , List , Optional
88
99from lsprotocol .types import (
1010 CodeAction ,
@@ -64,6 +64,34 @@ def pylsp_settings():
6464 return converter .unstructure (settings )
6565
6666
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+
6795@hookimpl
6896def pylsp_lint (workspace : Workspace , document : Document ) -> List [Dict ]:
6997 """
@@ -315,7 +343,23 @@ def run_ruff_fix(workspace: Workspace, document: Document) -> str:
315343 return result
316344
317345
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 :
319363 """
320364 Run ruff on the given document and the given arguments.
321365
@@ -327,14 +371,16 @@ def run_ruff(workspace: Workspace, document: Document, fix: bool = False) -> str
327371 File to run ruff on.
328372 fix : bool
329373 Whether to run fix or no-fix.
374+ extra_arguments : List[str]
375+ Extra arguments to pass to ruff.
330376
331377 Returns
332378 -------
333379 String containing the result in json format.
334380 """
335381 settings = load_settings (workspace , document )
336382 executable = settings .executable
337- arguments = build_arguments (document , settings , fix )
383+ arguments = build_arguments (document , settings , fix , extra_arguments )
338384
339385 log .debug (f"Calling { executable } with args: { arguments } on '{ document .path } '" )
340386 try :
@@ -358,6 +404,7 @@ def build_arguments(
358404 document : Document ,
359405 settings : PluginSettings ,
360406 fix : bool = False ,
407+ extra_arguments : Optional [List [str ]] = None ,
361408) -> List [str ]:
362409 """
363410 Build arguments for ruff.
@@ -368,6 +415,10 @@ def build_arguments(
368415 Document to apply ruff on.
369416 settings : PluginSettings
370417 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.
371422
372423 Returns
373424 -------
@@ -416,6 +467,9 @@ def build_arguments(
416467 continue
417468 args .append (f"--ignore={ ',' .join (errors )} " )
418469
470+ if extra_arguments :
471+ args .extend (extra_arguments )
472+
419473 args .extend (["--" , "-" ])
420474
421475 return args
@@ -456,6 +510,7 @@ def load_settings(workspace: Workspace, document: Document) -> PluginSettings:
456510 executable = plugin_settings .executable ,
457511 extend_ignore = plugin_settings .extend_ignore ,
458512 extend_select = plugin_settings .extend_select ,
513+ format = plugin_settings .format ,
459514 )
460515
461516 return plugin_settings
0 commit comments