|
| 1 | +# Copyright 2020 Palantir Technologies, Inc. |
| 2 | +import logging |
| 3 | + |
| 4 | +from pyls import hookimpl, uris, _utils |
| 5 | + |
| 6 | +log = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +@hookimpl |
| 10 | +def pyls_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument |
| 11 | + log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name) |
| 12 | + kwargs = _utils.position_to_jedi_linecolumn(document, position) |
| 13 | + kwargs['new_name'] = new_name |
| 14 | + try: |
| 15 | + refactoring = document.jedi_script().rename(**kwargs) |
| 16 | + except NotImplementedError: |
| 17 | + raise Exception('No support for renaming in Python 2/3.5 with Jedi. ' |
| 18 | + 'Consider using the rope_rename plugin instead') |
| 19 | + 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 | + }, |
| 36 | + }, |
| 37 | + 'newText': changed_file.get_new_code(), |
| 38 | + } |
| 39 | + ], |
| 40 | + } |
| 41 | + for file_path, changed_file in refactoring.get_changed_files().items() |
| 42 | + ], |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +def _num_lines(file_contents): |
| 47 | + 'Count the number of lines in the given string.' |
| 48 | + return len(file_contents.splitlines()) |
0 commit comments