Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pylsp/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions test/test_document.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"
26 changes: 26 additions & 0 deletions test/test_text_edit.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"