Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions pylsp_ruff/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
Document to apply ruff on.

"""

log.debug(f"textDocument/formatting: {document}")
outcome = yield
result = outcome.get_result()
Expand All @@ -128,34 +129,37 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator
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,
with workspace.report_progress("format: ruff"):
new_text = run_ruff_format(
settings=settings, document_path=document.path, document_source=source
)

# Avoid applying empty text edit
if not new_text or new_text == source:
return
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,
)

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)
# 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]))
outcome.force_result(converter.unstructure([text_edit]))


@hookimpl
Expand All @@ -174,10 +178,12 @@ def pylsp_lint(workspace: Workspace, document: Document) -> List[Dict]:
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)

with workspace.report_progress("lint: ruff"):
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:
Expand Down
Loading