Skip to content

Commit

Permalink
Support one-based column indices for Edit's (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored May 10, 2023
1 parent feb23eb commit 376e009
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def _parse_fix(content: Fix | LegacyFix | None) -> Fix | None:
# Prior to v0.0.260, Ruff returned a single edit.
legacy_fix = cast(LegacyFix, content)
return {
"applicability": None,
"message": legacy_fix.get("message"),
"edits": [
{
Expand All @@ -147,19 +148,20 @@ def _parse_fix(content: Fix | LegacyFix | None) -> Fix | None:
else:
# Since v0.0.260, Ruff returns a list of edits.
fix = cast(Fix, content)

# Since v0.0.266, Ruff returns one based column indices
if fix.get("applicability") is not None:
for edit in fix["edits"]:
edit["location"]["column"] = edit["location"]["column"] - 1
edit["end_location"]["column"] = edit["end_location"]["column"] - 1

return fix


def _parse_output(content: str) -> list[Diagnostic]:
"""Parse Ruff's JSON output."""
diagnostics: list[Diagnostic] = []

line_at_1 = True
column_at_1 = True

line_offset = 1 if line_at_1 else 0
col_offset = 1 if column_at_1 else 0

# Ruff's output looks like:
# [
# {
Expand Down Expand Up @@ -194,12 +196,12 @@ def _parse_output(content: str) -> list[Diagnostic]:
# ]
for check in json.loads(content):
start = Position(
line=max([int(check["location"]["row"]) - line_offset, 0]),
character=int(check["location"]["column"]) - col_offset,
line=max([int(check["location"]["row"]) - 1, 0]),
character=int(check["location"]["column"]) - 1,
)
end = Position(
line=max([int(check["end_location"]["row"]) - line_offset, 0]),
character=int(check["end_location"]["column"]) - col_offset,
line=max([int(check["end_location"]["row"]) - 1, 0]),
character=int(check["end_location"]["column"]) - 1,
)
diagnostic = Diagnostic(
range=Range(start=start, end=end),
Expand Down Expand Up @@ -301,6 +303,7 @@ class Edit(TypedDict):
class Fix(TypedDict):
"""A fix for a diagnostic, represented as a list of edits."""

applicability: str | None
message: str | None
edits: list[Edit]

Expand Down Expand Up @@ -513,6 +516,7 @@ def code_action(params: CodeActionParams) -> list[CodeAction] | None:
new_line = f"{line} # noqa: {diagnostic.code}"
fix = Fix(
message=None,
applicability=None,
edits=[
Edit(
content=new_line,
Expand Down

0 comments on commit 376e009

Please sign in to comment.