From 1bc4e2f71d0599beb0985d9c1cd3a354e4174561 Mon Sep 17 00:00:00 2001 From: anticipate218 <3481402509@qq.com> Date: Fri, 13 Nov 2026 02:01:15 +0800 Subject: [PATCH] Preserve physical line boundaries in document edits Recognize only CR, LF and CRLF as document line endings. Keep form feeds and other non-line-break characters on their physical line so edits and position lookups use the right offsets. Add regression coverage for incremental and batch edits, position lookups and existing line-ending behavior. Assisted-by: OpenAI Codex --- pylsp/workspace.py | 2 +- test/test_document.py | 38 ++++++++++++++++++++++++++++++++++++++ test/test_text_edit.py | 26 ++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/pylsp/workspace.py b/pylsp/workspace.py index 290b95ee..86c47769 100644 --- a/pylsp/workspace.py +++ b/pylsp/workspace.py @@ -431,7 +431,7 @@ def _rope_resource(self, rope_config): @property @lock def lines(self): - return self.source.splitlines(True) + return io.StringIO(self.source, newline="").readlines() @property @lock diff --git a/test/test_document.py b/test/test_document.py index f31d446e..de6e7b90 100644 --- a/test/test_document.py +++ b/test/test_document.py @@ -1,6 +1,8 @@ # Copyright 2017-2020 Palantir Technologies, Inc. # Copyright 2021- Python Language Server Contributors. +import pytest + from pylsp.workspace import Document from test.fixtures import DOC, DOC_URI @@ -103,3 +105,39 @@ def test_document_end_of_file_edit(workspace) -> None: "print 'b'\n", "o", ] + + +@pytest.mark.parametrize( + "separator", ["\f", "\v", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"] +) +def test_document_non_line_break_characters(workspace, separator) -> None: + first_line = f'label = "a{separator}b"\n' + document = Document(DOC_URI, workspace, first_line + "value = 1\n") + + assert document.lines == [first_line, "value = 1\n"] + assert document.offset_at_position({"line": 1, "character": 0}) == len(first_line) + assert document.word_at_position({"line": 1, "character": 2}) == "value" + + +@pytest.mark.parametrize("line_ending", ["\n", "\r", "\r\n"]) +@pytest.mark.parametrize("final_newline", [False, True]) +def test_document_preserves_line_endings(workspace, line_ending, final_newline) -> None: + lines = [f"first{line_ending}", "second" + (line_ending if final_newline else "")] + document = Document(DOC_URI, workspace, "".join(lines)) + + assert document.lines == lines + + +def test_document_edit_after_form_feed(workspace) -> None: + document = Document(DOC_URI, workspace, "def foo():\n\f return 1\n") + document.apply_change( + { + "range": { + "start": {"line": 1, "character": 12}, + "end": {"line": 1, "character": 13}, + }, + "text": "2", + } + ) + + assert document.source == "def foo():\n\f return 2\n" diff --git a/test/test_text_edit.py b/test/test_text_edit.py index 1d9115bf..48c1c058 100644 --- a/test/test_text_edit.py +++ b/test/test_text_edit.py @@ -1,6 +1,8 @@ # Copyright 2017-2020 Palantir Technologies, Inc. # Copyright 2021- Python Language Server Contributors. +import pytest + from pylsp import uris from pylsp.text_edit import OverLappingTextEditException, apply_text_edits @@ -301,3 +303,27 @@ def test_apply_text_edits_multiline(pylsp) -> None: ) == "0\n1World\nHello3\n4" ) + + +@pytest.mark.parametrize( + "separator", ["\f", "\v", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"] +) +def test_apply_text_edits_after_non_line_break(pylsp, separator) -> None: + first_line = f'label = "a{separator}b"\n' + pylsp.workspace.put_document(DOC_URI, first_line + "value = 1\n") + document = pylsp.workspace.get_document(DOC_URI) + + result = apply_text_edits( + document, + [ + { + "range": { + "start": {"line": 1, "character": 8}, + "end": {"line": 1, "character": 9}, + }, + "newText": "2", + } + ], + ) + + assert result == first_line + "value = 2\n"