Script 'mail_helper' called by obssrc Hello community, here is the log from the commit of package python-pylsp-rope for openSUSE:Factory checked in at 2021-12-25 22:41:40 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/python-pylsp-rope (Old) and /work/SRC/openSUSE:Factory/.python-pylsp-rope.new.2520 (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "python-pylsp-rope" Sat Dec 25 22:41:40 2021 rev:5 rq:942470 version:0.1.8 Changes: -------- --- /work/SRC/openSUSE:Factory/python-pylsp-rope/python-pylsp-rope.changes 2021-11-23 22:13:22.494321411 +0100 +++ /work/SRC/openSUSE:Factory/.python-pylsp-rope.new.2520/python-pylsp-rope.changes 2021-12-25 22:41:41.154638556 +0100 @@ -1,0 +2,8 @@ +Sat Dec 18 17:55:06 UTC 2021 - Matej Cepl <mc...@suse.com> + +- Upgrade to 0.1.8: + - Add refactor extract method/variable including similar statements + variant + - Add refactor extract global method/variable variant + +------------------------------------------------------------------- Old: ---- pylsp-rope-0.1.7.tar.gz New: ---- pylsp-rope-0.1.8.tar.gz ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ python-pylsp-rope.spec ++++++ --- /var/tmp/diff_new_pack.Y8ipWk/_old 2021-12-25 22:41:41.634638843 +0100 +++ /var/tmp/diff_new_pack.Y8ipWk/_new 2021-12-25 22:41:41.638638845 +0100 @@ -19,7 +19,7 @@ %{?!python_module:%define python_module() python-%{**} python3-%{**}} %define skip_python2 1 Name: python-pylsp-rope -Version: 0.1.7 +Version: 0.1.8 Release: 0 Summary: Extended refactoring capabilities for Python LSP Server using Rope License: MIT ++++++ pylsp-rope-0.1.7.tar.gz -> pylsp-rope-0.1.8.tar.gz ++++++ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/PKG-INFO new/pylsp-rope-0.1.8/PKG-INFO --- old/pylsp-rope-0.1.7/PKG-INFO 2021-11-05 08:10:35.458753300 +0100 +++ new/pylsp-rope-0.1.8/PKG-INFO 2021-12-17 10:11:14.431807500 +0100 @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: pylsp-rope -Version: 0.1.7 +Version: 0.1.8 Summary: Extended refactoring capabilities for Python LSP Server using Rope. Home-page: https://github.com/python-rope/pylsp-rope Author: Lie Ryan @@ -76,12 +76,25 @@ ### Extract method +Variants: + +- Extract method +- Extract global method +- Extract method including similar statements +- Extract global method including similar statements + This refactoring works by triggering CodeAction when selecting a block of code. +Optionally, similar statements can also be extracted. ### Extract variable +- Extract variable +- Extract global variable +- Extract variable including similar statements +- Extract global variable including similar statements + This refactoring works by triggering CodeAction when selecting a Python -expression. +expression. Optionally, similar statements can also be extracted. ### Inline diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/README.md new/pylsp-rope-0.1.8/README.md --- old/pylsp-rope-0.1.7/README.md 2021-11-04 23:39:22.000000000 +0100 +++ new/pylsp-rope-0.1.8/README.md 2021-12-13 20:50:04.000000000 +0100 @@ -53,12 +53,25 @@ ### Extract method +Variants: + +- Extract method +- Extract global method +- Extract method including similar statements +- Extract global method including similar statements + This refactoring works by triggering CodeAction when selecting a block of code. +Optionally, similar statements can also be extracted. ### Extract variable +- Extract variable +- Extract global variable +- Extract variable including similar statements +- Extract global variable including similar statements + This refactoring works by triggering CodeAction when selecting a Python -expression. +expression. Optionally, similar statements can also be extracted. ### Inline diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/pylsp_rope/plugin.py new/pylsp-rope-0.1.8/pylsp_rope/plugin.py --- old/pylsp-rope-0.1.7/pylsp_rope/plugin.py 2021-11-04 23:24:08.000000000 +0100 +++ new/pylsp-rope-0.1.8/pylsp_rope/plugin.py 2021-12-14 08:04:04.000000000 +0100 @@ -79,48 +79,55 @@ for resource in get_resources(workspace, workspace.documents.keys()): project.pycore._invalidate_resource_cache(resource) - commands = { - "Extract method": CommandRefactorExtractMethod( + commands = {} + commands.update( + CommandRefactorExtractMethod.get_code_actions( workspace, - document_uri=document.uri, + document=document, range=range, ), - "Extract variable": CommandRefactorExtractVariable( + ) + commands.update( + CommandRefactorExtractVariable.get_code_actions( workspace, - document_uri=document.uri, + document=document, range=range, ), - "Inline method/variable/parameter": CommandRefactorInline( - workspace, - document_uri=document.uri, - position=info.position, - ), - "Use function": CommandRefactorUseFunction( - workspace, - document_uri=document.uri, - position=info.position, - ), - "Use function for current file only": CommandRefactorUseFunction( - workspace, - document_uri=document.uri, - position=info.position, - documents=[document.uri], - ), - "To method object": CommandRefactorMethodToMethodObject( - workspace, - document_uri=document.uri, - position=info.position, - ), - "Convert local variable to field": CommandRefactorLocalToField( - workspace, - document_uri=document.uri, - position=info.position, - ), - "Organize import": CommandSourceOrganizeImport( - workspace, - document_uri=document.uri, - ), - } + ) + commands.update( + { + "Inline method/variable/parameter": CommandRefactorInline( + workspace, + document_uri=document.uri, + position=info.position, + ), + "Use function": CommandRefactorUseFunction( + workspace, + document_uri=document.uri, + position=info.position, + ), + "Use function for current file only": CommandRefactorUseFunction( + workspace, + document_uri=document.uri, + position=info.position, + documents=[document.uri], + ), + "To method object": CommandRefactorMethodToMethodObject( + workspace, + document_uri=document.uri, + position=info.position, + ), + "Convert local variable to field": CommandRefactorLocalToField( + workspace, + document_uri=document.uri, + position=info.position, + ), + "Organize import": CommandSourceOrganizeImport( + workspace, + document_uri=document.uri, + ), + } + ) return [ cmd.get_code_action(title=title) @@ -161,13 +168,19 @@ def __call__(self): rope_changeset = self.get_changes() - apply_rope_changeset(self.workspace, rope_changeset) + if rope_changeset is not None: + apply_rope_changeset(self.workspace, rope_changeset) def get_changes(self): - pass - - def validate(self, info): - pass + """ + Calculate the rope changeset to perform this refactoring. + """ + + def validate(self, info) -> None: + """ + Override this method to raise an exception if this refactoring command + cannot be performed + """ def is_valid(self, info): try: @@ -202,6 +215,8 @@ document_uri: DocumentUri range: typing.Range + similar: bool + global_: bool # FIXME: requires rope.refactor.extract._ExceptionalConditionChecker for proper checking # def _is_valid(self, info): @@ -218,9 +233,44 @@ ) rope_changeset = refactoring.get_changes( extracted_name="extracted_method", + similar=self.similar, + global_=self.global_, ) return rope_changeset + @classmethod + def get_code_actions(cls, workspace, document, range): + return { + "Extract method including similar statements": cls( + workspace, + document_uri=document.uri, + range=range, + global_=False, + similar=True, + ), + "Extract method": cls( + workspace, + document_uri=document.uri, + range=range, + global_=False, + similar=False, + ), + "Extract global method including similar statements": cls( + workspace, + document_uri=document.uri, + range=range, + global_=True, + similar=True, + ), + "Extract global method": cls( + workspace, + document_uri=document.uri, + range=range, + global_=True, + similar=False, + ), + } + class CommandRefactorExtractVariable(Command): name = commands.COMMAND_REFACTOR_EXTRACT_VARIABLE @@ -228,6 +278,8 @@ document_uri: DocumentUri range: typing.Range + similar: bool + global_: bool def validate(self, info): # FIXME: requires rope.refactor.extract._ExceptionalConditionChecker for proper checking @@ -244,9 +296,43 @@ ) rope_changeset = refactoring.get_changes( extracted_name="extracted_variable", + similar=self.similar, + global_=self.global_, ) return rope_changeset + @classmethod + def get_code_actions(cls, workspace, document, range): + return { + "Extract variable including similar statements": cls( + workspace, + document_uri=document.uri, + range=range, + global_=False, + similar=True, + ), + "Extract variable": cls( + workspace, + document_uri=document.uri, + range=range, + global_=False, + similar=False, + ), + "Extract global variable including similar statements": cls( + workspace, + document_uri=document.uri, + range=range, + global_=True, + similar=True, + ), + "Extract global variable": cls( + workspace, + document_uri=document.uri, + range=range, + global_=True, + similar=False, + ), + } class CommandRefactorInline(Command): name = commands.COMMAND_REFACTOR_INLINE diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/pylsp_rope.egg-info/PKG-INFO new/pylsp-rope-0.1.8/pylsp_rope.egg-info/PKG-INFO --- old/pylsp-rope-0.1.7/pylsp_rope.egg-info/PKG-INFO 2021-11-05 08:10:35.000000000 +0100 +++ new/pylsp-rope-0.1.8/pylsp_rope.egg-info/PKG-INFO 2021-12-17 10:11:14.000000000 +0100 @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: pylsp-rope -Version: 0.1.7 +Version: 0.1.8 Summary: Extended refactoring capabilities for Python LSP Server using Rope. Home-page: https://github.com/python-rope/pylsp-rope Author: Lie Ryan @@ -76,12 +76,25 @@ ### Extract method +Variants: + +- Extract method +- Extract global method +- Extract method including similar statements +- Extract global method including similar statements + This refactoring works by triggering CodeAction when selecting a block of code. +Optionally, similar statements can also be extracted. ### Extract variable +- Extract variable +- Extract global variable +- Extract variable including similar statements +- Extract global variable including similar statements + This refactoring works by triggering CodeAction when selecting a Python -expression. +expression. Optionally, similar statements can also be extracted. ### Inline diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/pylsp_rope.egg-info/SOURCES.txt new/pylsp-rope-0.1.8/pylsp_rope.egg-info/SOURCES.txt --- old/pylsp-rope-0.1.7/pylsp_rope.egg-info/SOURCES.txt 2021-11-05 08:10:35.000000000 +0100 +++ new/pylsp-rope-0.1.8/pylsp_rope.egg-info/SOURCES.txt 2021-12-17 10:11:14.000000000 +0100 @@ -38,8 +38,14 @@ test/fixtures/method_local_to_field.py test/fixtures/method_object.py test/fixtures/method_object_use_function.py +test/fixtures/method_with_global_function.py +test/fixtures/method_with_global_variable.py +test/fixtures/method_with_similar_global_function.py +test/fixtures/method_with_similar_global_variable.py test/fixtures/redundant_import.py test/fixtures/simple.py test/fixtures/simple_extract_method.py +test/fixtures/simple_extract_method_with_similar.py test/fixtures/simple_extract_variable.py +test/fixtures/simple_extract_variable_with_similar.py test/fixtures/use_function.py \ No newline at end of file diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/setup.cfg new/pylsp-rope-0.1.8/setup.cfg --- old/pylsp-rope-0.1.7/setup.cfg 2021-11-05 08:10:35.458753300 +0100 +++ new/pylsp-rope-0.1.8/setup.cfg 2021-12-17 10:11:14.435807500 +0100 @@ -1,6 +1,6 @@ [metadata] name = pylsp-rope -version = 0.1.7 +version = 0.1.8 author = Lie Ryan author_email = lie.1...@gmail.com url = https://github.com/python-rope/pylsp-rope @@ -36,6 +36,9 @@ test = pytest +[pycodestyle] +max-line-length = 88 + [egg_info] tag_build = tag_date = 0 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/method.py new/pylsp-rope-0.1.8/test/fixtures/method.py --- old/pylsp-rope-0.1.7/test/fixtures/method.py 2021-11-03 03:33:16.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/fixtures/method.py 2021-12-13 10:47:33.000000000 +0100 @@ -6,3 +6,4 @@ local_var = 10 print(sys.stdin.read()) print(local_var) + print(sys.stdin.read()) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/method_with_global_function.py new/pylsp-rope-0.1.8/test/fixtures/method_with_global_function.py --- old/pylsp-rope-0.1.7/test/fixtures/method_with_global_function.py 1970-01-01 01:00:00.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/fixtures/method_with_global_function.py 2021-12-12 06:01:58.000000000 +0100 @@ -0,0 +1,12 @@ +import sys + + +class MyClass: + def my_method(self): + local_var = 10 + print(extracted_method()) + print(local_var) + print(sys.stdin.read()) + +def extracted_method(): + return sys.stdin.read() diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/method_with_global_variable.py new/pylsp-rope-0.1.8/test/fixtures/method_with_global_variable.py --- old/pylsp-rope-0.1.7/test/fixtures/method_with_global_variable.py 1970-01-01 01:00:00.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/fixtures/method_with_global_variable.py 2021-12-13 10:47:23.000000000 +0100 @@ -0,0 +1,10 @@ +import sys + + +class MyClass: + def my_method(self): + local_var = 10 + print(extracted_variable) + print(local_var) + print(sys.stdin.read()) +extracted_variable = sys.stdin.read() diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/method_with_similar_global_function.py new/pylsp-rope-0.1.8/test/fixtures/method_with_similar_global_function.py --- old/pylsp-rope-0.1.7/test/fixtures/method_with_similar_global_function.py 1970-01-01 01:00:00.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/fixtures/method_with_similar_global_function.py 2021-12-13 10:48:16.000000000 +0100 @@ -0,0 +1,12 @@ +import sys + + +class MyClass: + def my_method(self): + local_var = 10 + print(extracted_method()) + print(local_var) + print(extracted_method()) + +def extracted_method(): + return sys.stdin.read() diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/method_with_similar_global_variable.py new/pylsp-rope-0.1.8/test/fixtures/method_with_similar_global_variable.py --- old/pylsp-rope-0.1.7/test/fixtures/method_with_similar_global_variable.py 1970-01-01 01:00:00.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/fixtures/method_with_similar_global_variable.py 2021-12-13 10:48:55.000000000 +0100 @@ -0,0 +1,10 @@ +import sys + + +class MyClass: + def my_method(self): + local_var = 10 + print(extracted_variable) + print(local_var) + print(extracted_variable) +extracted_variable = sys.stdin.read() diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/redundant_import.py new/pylsp-rope-0.1.8/test/fixtures/redundant_import.py --- old/pylsp-rope-0.1.7/test/fixtures/redundant_import.py 2021-11-04 23:21:12.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/fixtures/redundant_import.py 2021-11-06 09:23:20.000000000 +0100 @@ -4,4 +4,11 @@ def main(): - print(sys.stdin.read()) + a = sys.stdin.read() + b = 20 + print(a + b) + c = a + b + + +a, b = 30, 40 +print(a + b) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/simple.py new/pylsp-rope-0.1.8/test/fixtures/simple.py --- old/pylsp-rope-0.1.7/test/fixtures/simple.py 2021-11-03 03:35:10.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/fixtures/simple.py 2021-11-06 21:25:50.000000000 +0100 @@ -2,4 +2,11 @@ def main(): - print(sys.stdin.read()) + a = int(sys.stdin.read()) + b = 20 + print(a + b) + c = a + b + + +a, b = 30, 40 +print(a + b) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/simple_extract_method.py new/pylsp-rope-0.1.8/test/fixtures/simple_extract_method.py --- old/pylsp-rope-0.1.7/test/fixtures/simple_extract_method.py 2021-10-05 05:04:03.000000000 +0200 +++ new/pylsp-rope-0.1.8/test/fixtures/simple_extract_method.py 2021-11-06 21:24:23.000000000 +0100 @@ -2,7 +2,14 @@ def main(): - extracted_method() + a = int(sys.stdin.read()) + b = 20 + extracted_method(a, b) + c = a + b -def extracted_method(): - print(sys.stdin.read()) +def extracted_method(a, b): + print(a + b) + + +a, b = 30, 40 +print(a + b) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/simple_extract_method_with_similar.py new/pylsp-rope-0.1.8/test/fixtures/simple_extract_method_with_similar.py --- old/pylsp-rope-0.1.7/test/fixtures/simple_extract_method_with_similar.py 1970-01-01 01:00:00.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/fixtures/simple_extract_method_with_similar.py 2021-11-06 20:48:40.000000000 +0100 @@ -0,0 +1,15 @@ +import sys + + +def main(): + a = int(sys.stdin.read()) + b = 20 + extracted_method(a, b) + c = a + b + +def extracted_method(a, b): + print(a + b) + + +a, b = 30, 40 +extracted_method(a, b) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/simple_extract_variable.py new/pylsp-rope-0.1.8/test/fixtures/simple_extract_variable.py --- old/pylsp-rope-0.1.7/test/fixtures/simple_extract_variable.py 2021-10-05 05:04:03.000000000 +0200 +++ new/pylsp-rope-0.1.8/test/fixtures/simple_extract_variable.py 2021-11-06 21:24:23.000000000 +0100 @@ -2,5 +2,12 @@ def main(): - extracted_variable = sys.stdin.read() + a = int(sys.stdin.read()) + b = 20 + extracted_variable = a + b print(extracted_variable) + c = a + b + + +a, b = 30, 40 +print(a + b) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/fixtures/simple_extract_variable_with_similar.py new/pylsp-rope-0.1.8/test/fixtures/simple_extract_variable_with_similar.py --- old/pylsp-rope-0.1.7/test/fixtures/simple_extract_variable_with_similar.py 1970-01-01 01:00:00.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/fixtures/simple_extract_variable_with_similar.py 2021-11-06 20:50:23.000000000 +0100 @@ -0,0 +1,13 @@ +import sys + + +def main(): + a = int(sys.stdin.read()) + b = 20 + extracted_variable = a + b + print(extracted_variable) + c = extracted_variable + + +a, b = 30, 40 +print(a + b) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/helpers.py new/pylsp-rope-0.1.8/test/helpers.py --- old/pylsp-rope-0.1.7/test/helpers.py 2021-10-08 13:30:16.000000000 +0200 +++ new/pylsp-rope-0.1.8/test/helpers.py 2021-11-06 08:59:23.000000000 +0100 @@ -1,14 +1,14 @@ from typing import ( Any, Collection, - Dict, List, ) from unittest.mock import ANY, call -from pylsp.workspace import Document +from pylsp.workspace import Workspace, Document from pylsp_rope.typing import ( + CodeAction, DocumentContent, DocumentUri, SimpleWorkspaceEdit, @@ -17,9 +17,12 @@ from test.conftest import read_fixture_file -def assert_code_actions_do_not_offer(response: Dict, command: str) -> None: +def assert_code_actions_do_not_offer(response: List[CodeAction], command: str) -> None: for action in response: - assert action["command"] != command, f"CodeAction should not offer {action}" + assert action["command"] is not None + assert ( + action["command"]["command"] != command + ), f"CodeAction should not offer {action}" def assert_text_edits(document_edits: List[TextEdit], target: str) -> DocumentContent: @@ -88,3 +91,7 @@ ) -> None: assert is_document_uri(document_uri) assert document_uri not in workspace_edit["changes"] + + +def assert_no_execute_command(workspace: Workspace) -> None: + workspace._endpoint.request.assert_not_called() diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/test_commands.py new/pylsp-rope-0.1.8/test/test_commands.py --- old/pylsp-rope-0.1.7/test/test_commands.py 2021-10-07 19:25:17.000000000 +0200 +++ new/pylsp-rope-0.1.8/test/test_commands.py 2021-11-06 08:59:22.000000000 +0100 @@ -2,9 +2,11 @@ from pylsp.lsp import MessageType -from pylsp_rope import commands +from pylsp_rope import commands, plugin from pylsp_rope.plugin import pylsp_commands, pylsp_execute_command from pylsp_rope.text import Position +from test.conftest import create_document +from test.helpers import assert_no_execute_command def test_command_registration(config, workspace): @@ -44,7 +46,23 @@ "window/showMessage", params={ "type": MessageType.Error, - "message": f"pylsp-rope: some unexpected exception", + "message": "pylsp-rope: some unexpected exception", }, ) assert "Traceback (most recent call last):" in caplog.text + + +def test_command_nothing_to_modify(config, workspace, document, code_action_context): + document = create_document(workspace, "simple.py") + + command = commands.COMMAND_SOURCE_ORGANIZE_IMPORT + arguments = [{"document_uri": document.uri}] + + response = plugin.pylsp_execute_command( + config=config, + workspace=workspace, + command=command, + arguments=arguments, + ) + + assert_no_execute_command(workspace) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/test_extract.py new/pylsp-rope-0.1.8/test/test_extract.py --- old/pylsp-rope-0.1.7/test/test_extract.py 2021-10-24 20:21:37.000000000 +0200 +++ new/pylsp-rope-0.1.8/test/test_extract.py 2021-12-13 10:51:26.000000000 +0100 @@ -1,15 +1,17 @@ from pylsp_rope import commands, plugin, typing from pylsp_rope.text import Range +from test.conftest import create_document from test.helpers import ( - assert_text_edits, assert_code_actions_do_not_offer, assert_single_document_edit, + assert_text_edits, ) -def test_extract_variable(config, workspace, document, code_action_context): - line = 4 - start_col = document.lines[line].index("sys") +def test_extract_variable(config, workspace, code_action_context): + document = create_document(workspace, "simple.py") + line = 6 + start_col = document.lines[line].index("a + b") end_col = document.lines[line].index(")\n") selection = Range((line, start_col), (line, end_col)) @@ -31,6 +33,8 @@ { "document_uri": document.uri, "range": selection, + "global_": False, + "similar": False, } ], }, @@ -54,14 +58,182 @@ document_edits = assert_single_document_edit(edit_request, document) new_text = assert_text_edits(document_edits, target="simple_extract_variable.py") assert "extracted_variable = " in new_text + assert new_text.count("a + b") == 3 + + +def test_extract_variable_with_similar(config, workspace, code_action_context): + document = create_document(workspace, "simple.py") + line = 6 + start_col = document.lines[line].index("a + b") + end_col = document.lines[line].index(")\n") + selection = Range((line, start_col), (line, end_col)) + + response = plugin.pylsp_code_actions( + config=config, + workspace=workspace, + document=document, + range=selection, + context=code_action_context, + ) + + expected: typing.CodeAction = { + "title": "Extract variable including similar statements", + "kind": "refactor.extract", + "command": { + "title": "Extract variable including similar statements", + "command": commands.COMMAND_REFACTOR_EXTRACT_VARIABLE, + "arguments": [ + { + "document_uri": document.uri, + "range": selection, + "global_": False, + "similar": True, + } + ], + }, + } + + assert expected in response + + assert expected["command"] is not None + command = expected["command"]["command"] + arguments = expected["command"]["arguments"] + + response = plugin.pylsp_execute_command( + config=config, + workspace=workspace, + command=command, + arguments=arguments, + ) + + edit_request = workspace._endpoint.request.call_args + + document_edits = assert_single_document_edit(edit_request, document) + new_text = assert_text_edits( + document_edits, target="simple_extract_variable_with_similar.py" + ) + assert "extracted_variable = " in new_text + assert new_text.count("a + b") == 2 + + +def test_extract_global_variable(config, workspace, code_action_context): + document = create_document(workspace, "method.py") + line = 6 + start_col = document.lines[line].index("sys.stdin.read()") + end_col = document.lines[line].index(")\n") + selection = Range((line, start_col), (line, end_col)) + + response = plugin.pylsp_code_actions( + config=config, + workspace=workspace, + document=document, + range=selection, + context=code_action_context, + ) + + expected: typing.CodeAction = { + "title": "Extract global variable", + "kind": "refactor.extract", + "command": { + "title": "Extract global variable", + "command": commands.COMMAND_REFACTOR_EXTRACT_VARIABLE, + "arguments": [ + { + "document_uri": document.uri, + "range": selection, + "global_": True, + "similar": False, + } + ], + }, + } + + assert expected in response + + assert expected["command"] is not None + command = expected["command"]["command"] + arguments = expected["command"]["arguments"] + + response = plugin.pylsp_execute_command( + config=config, + workspace=workspace, + command=command, + arguments=arguments, + ) + + edit_request = workspace._endpoint.request.call_args + + document_edits = assert_single_document_edit(edit_request, document) + new_text = assert_text_edits( + document_edits, target="method_with_global_variable.py" + ) + assert "extracted_variable = " in new_text + assert new_text.count("extracted_variable = sys.stdin.read()") == 1 + assert new_text.count("sys.stdin.read()") == 2 + + +def test_extract_global_variable_with_similar(config, workspace, code_action_context): + document = create_document(workspace, "method.py") + line = 6 + start_col = document.lines[line].index("sys.stdin.read()") + end_col = document.lines[line].index(")\n") + selection = Range((line, start_col), (line, end_col)) + + response = plugin.pylsp_code_actions( + config=config, + workspace=workspace, + document=document, + range=selection, + context=code_action_context, + ) + + expected: typing.CodeAction = { + "title": "Extract global variable including similar statements", + "kind": "refactor.extract", + "command": { + "title": "Extract global variable including similar statements", + "command": commands.COMMAND_REFACTOR_EXTRACT_VARIABLE, + "arguments": [ + { + "document_uri": document.uri, + "range": selection, + "global_": True, + "similar": True, + } + ], + }, + } + + assert expected in response + + assert expected["command"] is not None + command = expected["command"]["command"] + arguments = expected["command"]["arguments"] + + response = plugin.pylsp_execute_command( + config=config, + workspace=workspace, + command=command, + arguments=arguments, + ) + + edit_request = workspace._endpoint.request.call_args + + document_edits = assert_single_document_edit(edit_request, document) + new_text = assert_text_edits( + document_edits, target="method_with_similar_global_variable.py" + ) + assert "extracted_variable = " in new_text + assert new_text.count("extracted_variable = sys.stdin.read()") == 1 + assert new_text.count("sys.stdin.read()") == 1 def test_extract_variable_not_offered_when_selecting_non_expression( config, workspace, document, code_action_context ): - line = 4 + line = 6 start_col = document.lines[line].index("print") - end_col = document.lines[line].index(".read())\n") + end_col = document.lines[line].index("+") selection = Range((line, start_col), (line, end_col)) response = plugin.pylsp_code_actions( @@ -78,8 +250,9 @@ ) -def test_extract_method(config, workspace, document, code_action_context): - selection = Range(4) +def test_extract_method(config, workspace, code_action_context): + document = create_document(workspace, "simple.py") + selection = Range(6) response = plugin.pylsp_code_actions( config=config, @@ -99,6 +272,8 @@ { "document_uri": document.uri, "range": selection, + "global_": False, + "similar": False, } ], }, @@ -122,3 +297,172 @@ document_edits = assert_single_document_edit(edit_request, document) new_text = assert_text_edits(document_edits, target="simple_extract_method.py") assert "def extracted_method(" in new_text + assert new_text.count("print(a + b)") == 2 + assert new_text.count("extracted_method(a, b)\n") == 1 + + +def test_extract_method_with_similar(config, workspace, code_action_context): + document = create_document(workspace, "simple.py") + selection = Range(6) + + response = plugin.pylsp_code_actions( + config=config, + workspace=workspace, + document=document, + range=selection, + context=code_action_context, + ) + + expected: typing.CodeAction = { + "title": "Extract method including similar statements", + "kind": "refactor.extract", + "command": { + "title": "Extract method including similar statements", + "command": commands.COMMAND_REFACTOR_EXTRACT_METHOD, + "arguments": [ + { + "document_uri": document.uri, + "range": selection, + "global_": False, + "similar": True, + } + ], + }, + } + + assert expected in response + + assert expected["command"] is not None + command = expected["command"]["command"] + arguments = expected["command"]["arguments"] + + response = plugin.pylsp_execute_command( + config=config, + workspace=workspace, + command=command, + arguments=arguments, + ) + + edit_request = workspace._endpoint.request.call_args + + document_edits = assert_single_document_edit(edit_request, document) + new_text = assert_text_edits( + document_edits, target="simple_extract_method_with_similar.py" + ) + assert "def extracted_method(" in new_text + assert new_text.count("print(a + b)") == 1 + assert new_text.count("extracted_method(a, b)\n") == 2 + + +def test_extract_global_method(config, workspace, code_action_context): + document = create_document(workspace, "method.py") + line = 6 + start_col = document.lines[line].index("sys.stdin.read()") + end_col = document.lines[line].index(")\n") + selection = Range((line, start_col), (line, end_col)) + + response = plugin.pylsp_code_actions( + config=config, + workspace=workspace, + document=document, + range=selection, + context=code_action_context, + ) + + expected: typing.CodeAction = { + "title": "Extract global method", + "kind": "refactor.extract", + "command": { + "title": "Extract global method", + "command": commands.COMMAND_REFACTOR_EXTRACT_METHOD, + "arguments": [ + { + "document_uri": document.uri, + "range": selection, + "global_": True, + "similar": False, + } + ], + }, + } + + assert expected in response + + assert expected["command"] is not None + command = expected["command"]["command"] + arguments = expected["command"]["arguments"] + + response = plugin.pylsp_execute_command( + config=config, + workspace=workspace, + command=command, + arguments=arguments, + ) + + edit_request = workspace._endpoint.request.call_args + + document_edits = assert_single_document_edit(edit_request, document) + new_text = assert_text_edits( + document_edits, target="method_with_global_function.py" + ) + assert "def extracted_method(" in new_text + assert new_text.count("return sys.stdin.read()") == 1 + assert new_text.count("sys.stdin.read()") == 2 + assert new_text.count("extracted_method()") == 2 + + +def test_extract_method_global_with_similar(config, workspace, code_action_context): + document = create_document(workspace, "method.py") + line = 6 + start_col = document.lines[line].index("sys.stdin.read()") + end_col = document.lines[line].index(")\n") + selection = Range((line, start_col), (line, end_col)) + + response = plugin.pylsp_code_actions( + config=config, + workspace=workspace, + document=document, + range=selection, + context=code_action_context, + ) + + expected: typing.CodeAction = { + "title": "Extract global method including similar statements", + "kind": "refactor.extract", + "command": { + "title": "Extract global method including similar statements", + "command": commands.COMMAND_REFACTOR_EXTRACT_METHOD, + "arguments": [ + { + "document_uri": document.uri, + "range": selection, + "global_": True, + "similar": True, + } + ], + }, + } + + assert expected in response + + assert expected["command"] is not None + command = expected["command"]["command"] + arguments = expected["command"]["arguments"] + + response = plugin.pylsp_execute_command( + config=config, + workspace=workspace, + command=command, + arguments=arguments, + ) + + edit_request = workspace._endpoint.request.call_args + + document_edits = assert_single_document_edit(edit_request, document) + new_text = assert_text_edits( + document_edits, target="method_with_similar_global_function.py" + ) + assert "def extracted_method(" in new_text + assert new_text.count("return sys.stdin.read()") == 1 + assert new_text.count("sys.stdin.read()") == 1 + assert new_text.count("extracted_method()") == 3 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pylsp-rope-0.1.7/test/test_import_utils.py new/pylsp-rope-0.1.8/test/test_import_utils.py --- old/pylsp-rope-0.1.7/test/test_import_utils.py 2021-11-04 23:23:52.000000000 +0100 +++ new/pylsp-rope-0.1.8/test/test_import_utils.py 2021-11-06 09:23:54.000000000 +0100 @@ -3,7 +3,6 @@ from test.conftest import create_document from test.helpers import ( assert_text_edits, - assert_code_actions_do_not_offer, assert_single_document_edit, ) @@ -54,5 +53,5 @@ document_edits = assert_single_document_edit(edit_request, document) new_text = assert_text_edits(document_edits, target="simple.py") - assert document.source.count('import sys') == 2 - assert new_text.count('import sys') == 1 + assert document.source.count("import sys") == 2 + assert new_text.count("import sys") == 1