From 3c02ff0d785c5a6b4fe089d12afb4539d807b0f7 Mon Sep 17 00:00:00 2001 From: KMohZaid <68484509+KMohZaid@users.noreply.github.com> Date: Mon, 19 Aug 2024 09:33:49 +0000 Subject: [PATCH 1/3] notfy: give ack. about ruff linter and formatter --- pylsp_ruff/plugin.py | 78 +++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index 833f16b..c7e9b7c 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -116,46 +116,48 @@ 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() - 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 + with workspace.report_progress("format: ruff"): + log.debug(f"textDocument/formatting: {document}") + outcome = yield + result = outcome.get_result() + if result: + source = result[0]["newText"] + else: + source = document.source - new_text = run_ruff_format( - settings=settings, document_path=document.path, document_source=source - ) + settings = load_settings(workspace=workspace, document_path=document.path) + if not settings.format_enabled: + 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, + 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 @@ -174,10 +176,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: From e5236cbdfe83679816142ff6e238005cff704c62 Mon Sep 17 00:00:00 2001 From: KMohZaid <68484509+KMohZaid@users.noreply.github.com> Date: Mon, 19 Aug 2024 10:01:14 +0000 Subject: [PATCH 2/3] fix: ruff format message is shown even when it is disabled (my bad) --- pylsp_ruff/plugin.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index c7e9b7c..d494af1 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -117,19 +117,19 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator """ - with workspace.report_progress("format: ruff"): - log.debug(f"textDocument/formatting: {document}") - outcome = yield - result = outcome.get_result() - if result: - source = result[0]["newText"] - else: - source = document.source + 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 + settings = load_settings(workspace=workspace, document_path=document.path) + if not settings.format_enabled: + return + with workspace.report_progress("format: ruff"): new_text = run_ruff_format( settings=settings, document_path=document.path, document_source=source ) From 0ce805e7b1058a2e59256a0984e90db82f470fce Mon Sep 17 00:00:00 2001 From: KMohZaid <68484509+KMohZaid@users.noreply.github.com> Date: Mon, 19 Aug 2024 10:31:00 +0000 Subject: [PATCH 3/3] format: formatted to fix pre commit error (line length exceed format) --- pylsp_ruff/plugin.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pylsp_ruff/plugin.py b/pylsp_ruff/plugin.py index d494af1..7b95212 100644 --- a/pylsp_ruff/plugin.py +++ b/pylsp_ruff/plugin.py @@ -140,7 +140,9 @@ def pylsp_format_document(workspace: Workspace, document: Document) -> Generator # 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 + ignore=["ALL"], + select=settings.format, + executable=settings.executable, ), document_path=document.path, document_source=new_text,