Skip to content

Commit

Permalink
Handle for LSP textDocument/formatting request (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
PushUpek committed Feb 13, 2023
1 parent 713d4e6 commit e1925b0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
TEXT_DOCUMENT_DID_CLOSE,
TEXT_DOCUMENT_DID_OPEN,
TEXT_DOCUMENT_DID_SAVE,
TEXT_DOCUMENT_FORMATTING,
TEXT_DOCUMENT_HOVER,
AnnotatedTextEdit,
ClientCapabilities,
Expand All @@ -34,6 +35,7 @@
DidCloseTextDocumentParams,
DidOpenTextDocumentParams,
DidSaveTextDocumentParams,
DocumentFormattingParams,
Hover,
HoverParams,
InitializeParams,
Expand Down Expand Up @@ -451,6 +453,21 @@ def resolve_code_action(params: CodeAction) -> CodeAction:
return params


@LSP_SERVER.feature(TEXT_DOCUMENT_FORMATTING)
def formatting(params: DocumentFormattingParams) -> list[TextEdit] | None:
"""LSP handler for textDocument/formatting request."""
document = LSP_SERVER.workspace.get_document(params.text_document.uri)

# Deep copy, to prevent accidentally updating global settings.
settings = copy.deepcopy(_get_settings_by_document(document))

if settings["fixAll"]:
return _formatting_helper(document)
elif settings["organizeImports"]:
return _formatting_helper(document, only="I001")
return None


@LSP_SERVER.command("ruff.applyAutofix")
def apply_autofix(arguments: tuple[TextDocument]):
uri = arguments[0]["uri"]
Expand Down

0 comments on commit e1925b0

Please sign in to comment.