-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathrope_rename.py
59 lines (48 loc) · 1.79 KB
/
rope_rename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Copyright 2017 Palantir Technologies, Inc.
import logging
from rope.base import libutils
from rope.refactor.rename import Rename
from pyls import hookimpl, uris
log = logging.getLogger(__name__)
@hookimpl
def pyls_settings():
# Default rope_rename to disabled
return {'plugins': {'rope_rename': {'enabled': False}}}
@hookimpl
def pyls_rename(config, workspace, document, position, new_name):
rope_config = config.settings(document_path=document.path).get('rope', {})
rope_project = workspace._rope_project_builder(rope_config)
rename = Rename(
rope_project,
libutils.path_to_resource(rope_project, document.path),
document.offset_at_position(position)
)
log.debug("Executing rename of %s to %s", document.word_at_position(position), new_name)
changeset = rename.get_changes(new_name, in_hierarchy=True, docs=True)
log.debug("Finished rename: %s", changeset.changes)
changes = []
for change in changeset.changes:
uri = uris.from_fs_path(change.resource.path)
doc = workspace.get_maybe_document(uri)
changes.append({
'textDocument': {
'uri': uri,
'version': doc.version if doc else None
},
'edits': [
{
'range': {
'start': {'line': 0, 'character': 0},
'end': {
'line': _num_lines(change.resource),
'character': 0,
},
},
'newText': change.new_contents,
}
]
})
return {'documentChanges': changes}
def _num_lines(resource):
"Count the number of lines in a `File` resource."
return len(resource.read().splitlines())