Skip to content

Commit 10cd98c

Browse files
authored
Do not create documents from a textDocument/rename (#818)
1 parent 2951e9f commit 10cd98c

File tree

5 files changed

+81
-43
lines changed

5 files changed

+81
-43
lines changed

pyls/plugins/jedi_rename.py

+22-23
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,29 @@ def pyls_rename(config, workspace, document, position, new_name): # pylint: dis
1717
raise Exception('No support for renaming in Python 2/3.5 with Jedi. '
1818
'Consider using the rope_rename plugin instead')
1919
log.debug('Finished rename: %s', refactoring.get_diff())
20-
21-
return {
22-
'documentChanges': [
23-
{
24-
'textDocument': {
25-
'uri': uris.uri_with(document.uri, path=file_path),
26-
'version': workspace.get_document(document.uri).version,
27-
},
28-
'edits': [
29-
{
30-
'range': {
31-
'start': {'line': 0, 'character': 0},
32-
'end': {
33-
'line': _num_lines(changed_file.get_new_code()),
34-
'character': 0,
35-
},
20+
changes = []
21+
for file_path, changed_file in refactoring.get_changed_files().items():
22+
uri = uris.from_fs_path(file_path)
23+
doc = workspace.get_maybe_document(uri)
24+
changes.append({
25+
'textDocument': {
26+
'uri': uri,
27+
'version': doc.version if doc else None
28+
},
29+
'edits': [
30+
{
31+
'range': {
32+
'start': {'line': 0, 'character': 0},
33+
'end': {
34+
'line': _num_lines(changed_file.get_new_code()),
35+
'character': 0,
3636
},
37-
'newText': changed_file.get_new_code(),
38-
}
39-
],
40-
}
41-
for file_path, changed_file in refactoring.get_changed_files().items()
42-
],
43-
}
37+
},
38+
'newText': changed_file.get_new_code(),
39+
}
40+
],
41+
})
42+
return {'documentChanges': changes}
4443

4544

4645
def _num_lines(file_contents):

pyls/plugins/rope_rename.py

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Copyright 2017 Palantir Technologies, Inc.
22
import logging
3-
import os
43

54
from rope.base import libutils
65
from rope.refactor.rename import Rename
@@ -30,23 +29,29 @@ def pyls_rename(config, workspace, document, position, new_name):
3029
log.debug("Executing rename of %s to %s", document.word_at_position(position), new_name)
3130
changeset = rename.get_changes(new_name, in_hierarchy=True, docs=True)
3231
log.debug("Finished rename: %s", changeset.changes)
33-
return {
34-
'documentChanges': [{
32+
changes = []
33+
for change in changeset.changes:
34+
uri = uris.from_fs_path(change.resource.path)
35+
doc = workspace.get_maybe_document(uri)
36+
changes.append({
3537
'textDocument': {
36-
'uri': uris.uri_with(
37-
document.uri, path=os.path.join(workspace.root_path, change.resource.path)
38-
),
39-
'version': workspace.get_document(document.uri).version
38+
'uri': uri,
39+
'version': doc.version if doc else None
4040
},
41-
'edits': [{
42-
'range': {
43-
'start': {'line': 0, 'character': 0},
44-
'end': {'line': _num_lines(change.resource), 'character': 0},
45-
},
46-
'newText': change.new_contents
47-
}]
48-
} for change in changeset.changes]
49-
}
41+
'edits': [
42+
{
43+
'range': {
44+
'start': {'line': 0, 'character': 0},
45+
'end': {
46+
'line': _num_lines(change.resource),
47+
'character': 0,
48+
},
49+
},
50+
'newText': change.new_contents,
51+
}
52+
]
53+
})
54+
return {'documentChanges': changes}
5055

5156

5257
def _num_lines(resource):

pyls/workspace.py

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def get_document(self, doc_uri):
7171
"""
7272
return self._docs.get(doc_uri) or self._create_document(doc_uri)
7373

74+
def get_maybe_document(self, doc_uri):
75+
return self._docs.get(doc_uri)
76+
7477
def put_document(self, doc_uri, source, version=None):
7578
self._docs[doc_uri] = self._create_document(doc_uri, source=source, version=version)
7679

test/plugins/test_jedi_rename.py

+33-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ class Test2(Test1):
1717
pass
1818
'''
1919

20+
DOC_NAME_EXTRA = 'test2.py'
21+
DOC_EXTRA = '''from test1 import Test1
22+
x = Test1()
23+
'''
24+
2025

2126
@pytest.fixture
2227
def tmp_workspace(temp_workspace_factory):
23-
return temp_workspace_factory({DOC_NAME: DOC})
28+
return temp_workspace_factory({
29+
DOC_NAME: DOC,
30+
DOC_NAME_EXTRA: DOC_EXTRA
31+
})
2432

2533

2634
@pytest.mark.skipif(LT_PY36, reason='Jedi refactoring isnt supported on Python 2.x/3.5')
@@ -34,10 +42,11 @@ def test_jedi_rename(tmp_workspace, config): # pylint: disable=redefined-outer-
3442
assert len(result.keys()) == 1
3543

3644
changes = result.get('documentChanges')
37-
assert len(changes) == 1
38-
changes = changes[0]
45+
assert len(changes) == 2
3946

40-
assert changes.get('edits') == [
47+
assert changes[0]['textDocument']['uri'] == doc.uri
48+
assert changes[0]['textDocument']['version'] == doc.version
49+
assert changes[0].get('edits') == [
4150
{
4251
'range': {
4352
'start': {'line': 0, 'character': 0},
@@ -46,3 +55,23 @@ def test_jedi_rename(tmp_workspace, config): # pylint: disable=redefined-outer-
4655
'newText': 'class ShouldBeRenamed():\n pass\n\nclass Test2(ShouldBeRenamed):\n pass\n',
4756
}
4857
]
58+
path = os.path.join(tmp_workspace.root_path, DOC_NAME_EXTRA)
59+
uri_extra = uris.from_fs_path(path)
60+
assert changes[1]['textDocument']['uri'] == uri_extra
61+
# This also checks whether documents not yet added via textDocument/didOpen
62+
# but that do need to be renamed in the project have a `null` version
63+
# number.
64+
assert changes[1]['textDocument']['version'] is None
65+
expected = 'from test1 import ShouldBeRenamed\nx = ShouldBeRenamed()\n'
66+
if os.name == 'nt':
67+
# The .write method in the temp_workspace_factory functions writes
68+
# Windows-style line-endings.
69+
expected = expected.replace('\n', '\r\n')
70+
assert changes[1].get('edits') == [
71+
{
72+
'range': {
73+
'start': {'line': 0, 'character': 0},
74+
'end': {'line': 2, 'character': 0}},
75+
'newText': expected
76+
}
77+
]

test/plugins/test_rope_rename.py

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def test_rope_rename(tmp_workspace, config): # pylint: disable=redefined-outer-
3131
assert len(changes) == 1
3232
changes = changes[0]
3333

34+
# Note that this test differs from test_jedi_rename, because rope does not
35+
# seem to modify files that haven't been opened with textDocument/didOpen.
3436
assert changes.get("edits") == [
3537
{
3638
"range": {

0 commit comments

Comments
 (0)