summaryrefslogtreecommitdiffstats
path: root/src/debputy/lsp/text_util.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/debputy/lsp/text_util.py')
-rw-r--r--src/debputy/lsp/text_util.py29
1 files changed, 20 insertions, 9 deletions
diff --git a/src/debputy/lsp/text_util.py b/src/debputy/lsp/text_util.py
index ef4cd0a..e58990f 100644
--- a/src/debputy/lsp/text_util.py
+++ b/src/debputy/lsp/text_util.py
@@ -5,6 +5,7 @@ from lsprotocol.types import (
Position,
Range,
WillSaveTextDocumentParams,
+ DocumentFormattingParams,
)
from debputy.linting.lint_util import LinterPositionCodec
@@ -73,18 +74,24 @@ def normalize_dctrl_field_name(f: str) -> str:
def on_save_trim_end_of_line_whitespace(
ls: "LanguageServer",
- params: WillSaveTextDocumentParams,
+ params: Union[WillSaveTextDocumentParams, DocumentFormattingParams],
) -> Optional[Sequence[TextEdit]]:
doc = ls.workspace.get_text_document(params.text_document.uri)
- return trim_end_of_line_whitespace(doc, doc.lines)
+ return trim_end_of_line_whitespace(doc.position_codec, doc.lines)
def trim_end_of_line_whitespace(
- doc: "TextDocument",
+ position_codec: "LintCapablePositionCodec",
lines: List[str],
+ *,
+ line_range: Optional[Iterable[int]] = None,
+ line_relative_line_no: int = 0,
) -> Optional[Sequence[TextEdit]]:
edits = []
- for line_no, orig_line in enumerate(lines):
+ if line_range is None:
+ line_range = range(0, len(lines))
+ for line_no in line_range:
+ orig_line = lines[line_no]
orig_len = len(orig_line)
if orig_line.endswith("\n"):
orig_len -= 1
@@ -92,16 +99,20 @@ def trim_end_of_line_whitespace(
if stripped_len == orig_len:
continue
- edit_range = doc.position_codec.range_to_client_units(
+ stripped_len_client_off = position_codec.client_num_units(
+ orig_line[:stripped_len]
+ )
+ orig_len_client_off = position_codec.client_num_units(orig_line[:orig_len])
+ edit_range = position_codec.range_to_client_units(
lines,
Range(
Position(
- line_no,
- stripped_len,
+ line_no + line_relative_line_no,
+ stripped_len_client_off,
),
Position(
- line_no,
- orig_len,
+ line_no + line_relative_line_no,
+ orig_len_client_off,
),
),
)