-
Notifications
You must be signed in to change notification settings - Fork 286
Add support for renaming with Jedi #801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b5e1b34
plugins: implement rename with jedi
fsouza dcd4aa9
plugins/rope_rename: default to disabled
fsouza 5779756
Use Python 3.6 in CI
fsouza 720d731
fix pylint
fsouza 25b8521
tests/plugins/test_jedi_rename: address PR feedback
fsouza 5393813
[PR feedback] Implement a helper fixture for temporary workspaces
fsouza 8d6f475
[PR feedback] provide a better error message for unsupported python
fsouza 30be1dd
pylint
fsouza 436ba3b
Apply PR review suggestion
fsouza 533b7ef
Merge branch 'develop' into rename-with-jedi
ccordoba12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Copyright 2020 Palantir Technologies, Inc. | ||
| import logging | ||
|
|
||
| from pyls import hookimpl, uris, _utils | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @hookimpl | ||
| def pyls_rename(config, workspace, document, position, new_name): # pylint: disable=unused-argument | ||
| log.debug('Executing rename of %s to %s', document.word_at_position(position), new_name) | ||
| kwargs = _utils.position_to_jedi_linecolumn(document, position) | ||
| kwargs['new_name'] = new_name | ||
| try: | ||
| refactoring = document.jedi_script().rename(**kwargs) | ||
| except NotImplementedError: | ||
| raise Exception('No support for renaming in Python 2/3.5 with Jedi. ' | ||
| 'Consider using the rope_rename plugin instead') | ||
| log.debug('Finished rename: %s', refactoring.get_diff()) | ||
|
|
||
| return { | ||
| 'documentChanges': [ | ||
| { | ||
| 'textDocument': { | ||
| 'uri': uris.uri_with(document.uri, path=file_path), | ||
| 'version': workspace.get_document(document.uri).version, | ||
| }, | ||
| 'edits': [ | ||
| { | ||
| 'range': { | ||
| 'start': {'line': 0, 'character': 0}, | ||
| 'end': { | ||
| 'line': _num_lines(changed_file.get_new_code()), | ||
| 'character': 0, | ||
| }, | ||
| }, | ||
| 'newText': changed_file.get_new_code(), | ||
| } | ||
| ], | ||
| } | ||
| for file_path, changed_file in refactoring.get_changed_files().items() | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| def _num_lines(file_contents): | ||
| 'Count the number of lines in the given string.' | ||
| return len(file_contents.splitlines()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Copyright 2020 Palantir Technologies, Inc. | ||
| import os | ||
| import sys | ||
|
|
||
| import pytest | ||
| from pyls import uris | ||
| from pyls.plugins.jedi_rename import pyls_rename | ||
| from pyls.workspace import Document | ||
|
|
||
| LT_PY36 = sys.version_info.major < 3 or (sys.version_info.major == 3 and sys.version_info.minor < 6) | ||
|
|
||
| DOC_NAME = 'test1.py' | ||
| DOC = '''class Test1(): | ||
| pass | ||
|
|
||
| class Test2(Test1): | ||
| pass | ||
| ''' | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def tmp_workspace(temp_workspace_factory): | ||
| return temp_workspace_factory({DOC_NAME: DOC}) | ||
|
|
||
|
|
||
| @pytest.mark.skipif(LT_PY36, reason='Jedi refactoring isnt supported on Python 2.x/3.5') | ||
| def test_jedi_rename(tmp_workspace, config): # pylint: disable=redefined-outer-name | ||
| # rename the `Test1` class | ||
| position = {'line': 0, 'character': 6} | ||
| DOC_URI = uris.from_fs_path(os.path.join(tmp_workspace.root_path, DOC_NAME)) | ||
| doc = Document(DOC_URI, tmp_workspace) | ||
|
|
||
| result = pyls_rename(config, tmp_workspace, doc, position, 'ShouldBeRenamed') | ||
| assert len(result.keys()) == 1 | ||
|
|
||
| changes = result.get('documentChanges') | ||
| assert len(changes) == 1 | ||
| changes = changes[0] | ||
|
|
||
| assert changes.get('edits') == [ | ||
| { | ||
| 'range': { | ||
| 'start': {'line': 0, 'character': 0}, | ||
| 'end': {'line': 5, 'character': 0}, | ||
| }, | ||
| 'newText': 'class ShouldBeRenamed():\n pass\n\nclass Test2(ShouldBeRenamed):\n pass\n', | ||
| } | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.