Skip to content

Commit

Permalink
Account for out-of-bounds columnn offsets of text edits
Browse files Browse the repository at this point in the history
This fixes an issue for import edits for the FsAutoComplete server.

See: ionide/FsAutoComplete@48129a5

Co-authored-by: chendesheng <[email protected]>
  • Loading branch information
rwols and chendesheng committed May 17, 2021
1 parent 7fc1f80 commit 4a5785d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 6 additions & 3 deletions plugin/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ def run(self, edit: Any, changes: Optional[List[TextEditTuple]] = None) -> None:
return
with temporary_setting(self.view.settings(), "translate_tabs_to_spaces", False):
view_version = self.view.change_count()
last_row, last_col = self.view.rowcol_utf16(self.view.size())
last_row, _ = self.view.rowcol_utf16(self.view.size())
for start, end, replacement, version in reversed(sort_by_application_order(changes)):
if version is not None and version != view_version:
debug('ignoring edit due to non-matching document version')
continue
region = sublime.Region(self.view.text_point_utf16(*start), self.view.text_point_utf16(*end))
region = sublime.Region(
self.view.text_point_utf16(*start, clamp_column=True),
self.view.text_point_utf16(*end, clamp_column=True)
)
if start[0] > last_row and replacement[0] != '\n':
# Handle when a language server (eg gopls) inserts at a row beyond the document
# some editors create the line automatically, sublime needs to have the newline prepended.
self.apply_change(region, '\n' + replacement, edit)
last_row, last_col = self.view.rowcol(self.view.size())
last_row, _ = self.view.rowcol(self.view.size())
else:
self.apply_change(region, replacement, edit)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_single_document.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from copy import deepcopy
from LSP.plugin import Request
from LSP.plugin.core.url import filename_to_uri
from LSP.plugin.core.views import entire_content
from LSP.plugin.hover import _test_contents
from setup import TextDocumentTestCase
from setup import TIMEOUT_TIME
Expand Down Expand Up @@ -51,6 +52,18 @@ def test_did_open(self) -> 'Generator':
# -> "shutdown" -> client shut down
pass

def test_out_of_bounds_column_for_text_document_edit(self) -> 'Generator':
self.insert_characters("a\nb\nc\n")
self.view.run_command("lsp_apply_document_edit", {"changes": [
(
(1, 0), # start row-col
(1, 10000), # end row-col (the col offset is out of bounds intentionally)
"hello there", # new text
None # version
)
]})
self.assertEqual(entire_content(self.view), "a\nhello there\nc\n")

def test_did_close(self) -> 'Generator':
self.assertTrue(self.view)
self.assertTrue(self.view.is_valid())
Expand Down

0 comments on commit 4a5785d

Please sign in to comment.