From c46fe1cf9448c36457da9812fb6af6dc848291aa Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Sat, 28 Sep 2024 20:33:13 -0300 Subject: [PATCH] wip --- .../DummyMarkerInfoForAnalysis.java | 60 +++++++++ ...erationQuickFixFromMarkersParticipant.java | 4 +- ...=> TddQuickFixFromMarkersParticipant.java} | 4 +- ...ionQuickFixParticipantWithMarkersTest.java | 125 ++++++++++++++++++ .../refactoring/tdd/TddTestWorkbench.java | 16 +-- 5 files changed, 195 insertions(+), 14 deletions(-) create mode 100644 plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/quick_fixes/DummyMarkerInfoForAnalysis.java rename plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/{TddQuickFixParticipant.java => TddQuickFixFromMarkersParticipant.java} (99%) create mode 100644 plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/TddCodeGenerationQuickFixParticipantWithMarkersTest.java diff --git a/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/quick_fixes/DummyMarkerInfoForAnalysis.java b/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/quick_fixes/DummyMarkerInfoForAnalysis.java new file mode 100644 index 0000000000..8d538f8c6c --- /dev/null +++ b/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/quick_fixes/DummyMarkerInfoForAnalysis.java @@ -0,0 +1,60 @@ +package com.python.pydev.analysis.refactoring.quick_fixes; + +import org.python.pydev.core.IMarkerInfoForAnalysis; + +public class DummyMarkerInfoForAnalysis implements IMarkerInfoForAnalysis { + + public Object message; + public Object flake8MessageId; + public int length; + public int offset; + public Integer pyDevAnalysisType; + public Object pyLintMessageId; + + public DummyMarkerInfoForAnalysis(int pyDevAnalysisType, int offset, int length) { + this.pyDevAnalysisType = pyDevAnalysisType; + this.offset = offset; + this.length = length; + } + + @Override + public Object getPyLintMessageIdAttribute() { + return pyLintMessageId; + } + + @Override + public Integer getPyDevAnalisysType() { + return pyDevAnalysisType; + } + + @Override + public boolean hasPosition() { + return true; + } + + @Override + public int getOffset() { + return offset; + } + + @Override + public int getLength() { + return length; + } + + @Override + public void delete() { + + } + + @Override + public Object getFlake8MessageId() { + return flake8MessageId; + } + + @Override + public Object getMessage() { + return message; + } + +} diff --git a/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddCodeGenerationQuickFixFromMarkersParticipant.java b/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddCodeGenerationQuickFixFromMarkersParticipant.java index a0db4b121e..8f1c880662 100644 --- a/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddCodeGenerationQuickFixFromMarkersParticipant.java +++ b/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddCodeGenerationQuickFixFromMarkersParticipant.java @@ -5,11 +5,11 @@ public class TddCodeGenerationQuickFixFromMarkersParticipant extends AbstractAnalysisMarkersParticipants { - private TddQuickFixParticipant tddQuickFixParticipant; + private TddQuickFixFromMarkersParticipant tddQuickFixParticipant; @Override protected void fillParticipants() { - tddQuickFixParticipant = new TddQuickFixParticipant(); + tddQuickFixParticipant = new TddQuickFixFromMarkersParticipant(); participants.add(tddQuickFixParticipant); } diff --git a/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddQuickFixParticipant.java b/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddQuickFixFromMarkersParticipant.java similarity index 99% rename from plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddQuickFixParticipant.java rename to plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddQuickFixFromMarkersParticipant.java index 393bfbdcef..802800a1e1 100644 --- a/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddQuickFixParticipant.java +++ b/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/TddQuickFixFromMarkersParticipant.java @@ -61,13 +61,13 @@ /** * This participant will add a suggestion to create class/methods/attributes when an undefined variable error is found. */ -public class TddQuickFixParticipant implements IAnalysisMarkersParticipant { +public class TddQuickFixFromMarkersParticipant implements IAnalysisMarkersParticipant { /*default*/IImageHandle imageClass; /*default*/IImageHandle imageMethod; /*default*/IImageHandle imageModule; - public TddQuickFixParticipant() { + public TddQuickFixFromMarkersParticipant() { IImageCache imageCache = SharedUiPlugin.getImageCache(); if (imageCache != null) { //making tests imageClass = imageCache.get(UIConstants.CREATE_CLASS_ICON); diff --git a/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/TddCodeGenerationQuickFixParticipantWithMarkersTest.java b/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/TddCodeGenerationQuickFixParticipantWithMarkersTest.java new file mode 100644 index 0000000000..033ffef140 --- /dev/null +++ b/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/TddCodeGenerationQuickFixParticipantWithMarkersTest.java @@ -0,0 +1,125 @@ +/** + * Copyright (c) 2005-2013 by Appcelerator, Inc. All Rights Reserved. + * Licensed under the terms of the Eclipse Public License (EPL). + * Please see the license.txt included with this distribution for details. + * Any modifications to this file must keep this entire header intact. + */ +package com.python.pydev.refactoring.tdd; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jface.text.Document; +import org.python.pydev.ast.codecompletion.PyCodeCompletion; +import org.python.pydev.ast.codecompletion.revisited.CodeCompletionTestsBase; +import org.python.pydev.ast.codecompletion.revisited.modules.CompiledModule; +import org.python.pydev.ast.refactoring.AbstractPyRefactoring; +import org.python.pydev.core.IAnalysisPreferences; +import org.python.pydev.core.IMarkerInfoForAnalysis; +import org.python.pydev.core.TestDependent; +import org.python.pydev.core.docutils.PySelection; +import org.python.pydev.core.structure.CompletionRecursionException; +import org.python.pydev.plugin.nature.PythonNature; +import org.python.pydev.shared_core.callbacks.ICallback; +import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle; + +import com.python.pydev.analysis.AnalysisPreferences; +import com.python.pydev.analysis.refactoring.quick_fixes.DummyMarkerInfoForAnalysis; +import com.python.pydev.analysis.refactoring.refactorer.Refactorer; + +/** + * @author Fabio + * + */ +public class TddCodeGenerationQuickFixParticipantWithMarkersTest extends CodeCompletionTestsBase { + + public static void main(String[] args) { + + try { + //DEBUG_TESTS_BASE = true; + TddCodeGenerationQuickFixParticipantWithMarkersTest test = new TddCodeGenerationQuickFixParticipantWithMarkersTest(); + test.setUp(); + test.testCreateClass(); + test.tearDown(); + System.out.println("Finished"); + + junit.textui.TestRunner.run(TddCodeGenerationQuickFixParticipantWithMarkersTest.class); + } catch (Throwable e) { + e.printStackTrace(); + } + } + + /* + * @see TestCase#setUp() + */ + @Override + public void setUp() throws Exception { + super.setUp(); + AbstractPyRefactoring.setPyRefactoring(new Refactorer()); + CompiledModule.COMPILED_MODULES_ENABLED = false; + this.restorePythonPath(TestDependent.getCompletePythonLib(true, isPython3Test()) + + "|" + TestDependent.PYTHON2_PIL_PACKAGES + + "|" + + TestDependent.TEST_PYSRC_TESTING_LOC + + "configobj-4.6.0-py2.6.egg", false); + + this.restorePythonPath(false); + codeCompletion = new PyCodeCompletion(); + TddCodeGenerationQuickFixWithoutMarkersParticipant.onGetTddPropsError = new ICallback() { + + @Override + public Boolean call(Exception e) { + throw new RuntimeException("Error:" + org.python.pydev.shared_core.log.Log.getExceptionStr(e)); + } + }; + PyCodeCompletion.onCompletionRecursionException = new ICallback() { + + @Override + public Object call(CompletionRecursionException e) { + throw new RuntimeException( + "Recursion error:" + org.python.pydev.shared_core.log.Log.getExceptionStr(e)); + } + + }; + } + + /* + * @see TestCase#tearDown() + */ + @Override + public void tearDown() throws Exception { + CompiledModule.COMPILED_MODULES_ENABLED = true; + super.tearDown(); + AbstractPyRefactoring.setPyRefactoring(null); + PyCodeCompletion.onCompletionRecursionException = null; + } + + public void testCreateClass() throws Exception { + int usedGrammar = GRAMMAR_TO_USE_FOR_PARSING; + GRAMMAR_TO_USE_FOR_PARSING = PythonNature.LATEST_GRAMMAR_PY3_VERSION; + try { + String mod1Contents = "" + + "class Foo(object):\n" + + " def m1(self):\n" + + " NewClass"; + + Document doc = new Document(mod1Contents); + + TddQuickFixFromMarkersParticipant participant = new TddQuickFixFromMarkersParticipant(); + int offset = mod1Contents.length(); + PySelection ps = new PySelection(doc, offset); + int markerStart = mod1Contents.indexOf("NewClass"); + int markerLen = "NewClass".length(); + + IMarkerInfoForAnalysis markerInfo = new DummyMarkerInfoForAnalysis( + IAnalysisPreferences.TYPE_UNDEFINED_VARIABLE, markerStart, markerLen); + IAnalysisPreferences analysisPreferences = new AnalysisPreferences(null); + String line = ps.getLine(); + List props = new ArrayList<>(); + participant.addProps(markerInfo, analysisPreferences, line, ps, offset, nature, null, props); + + } finally { + GRAMMAR_TO_USE_FOR_PARSING = usedGrammar; + } + } +} diff --git a/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/TddTestWorkbench.java b/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/TddTestWorkbench.java index 4cb48fc298..4aaee40f73 100644 --- a/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/TddTestWorkbench.java +++ b/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/TddTestWorkbench.java @@ -448,16 +448,14 @@ private void checkCreateMethod3() throws CoreException, BadLocationException, Mi private void checkCreateMethod4() throws CoreException, BadLocationException, MisconfigurationException { String mod1Contents = "" + - "print i\n" - + //just to have an error + "print i\n" + //just to have an error "class Foo(object):\n" + "\n" + "#comment\n" + "\n" + " def m1(self):\n" + " self.m2" + - "" - + + "" + ""; setContentsAndWaitReparseAndError(mod1Contents); @@ -469,21 +467,18 @@ private void checkCreateMethod4() throws CoreException, BadLocationException, Mi waitForQuickFixProps(quickFix, ps, offset, "Create m2 method at Foo").apply(editor.getISourceViewer(), '\n', 0, offset); assertContentsEqual("" + - "print i\n" - + //just to have an error + "print i\n" + "class Foo(object):\n" + "\n" + "#comment\n" + "\n" + "\n" + - " def m2(self):\n" - + + " def m2(self):\n" + " pass\n" + " \n" + " \n" + " def m1(self):\n" + " self.m2" + - "" + "", editor.getDocument().get()); } finally { @@ -927,7 +922,8 @@ private ICompletionProposalExtension2 waitForQuickFixProps(TddCodeGenerationQuic } - private List waitForQuickFixProps(TddCodeGenerationQuickFixFromMarkersParticipant quickFix, + private List waitForQuickFixProps( + TddCodeGenerationQuickFixFromMarkersParticipant quickFix, PySelection ps, int offset) throws BadLocationException, MisconfigurationException { for (int i = 0; i < 10; i++) { List props = quickFix.getProps(ps, SharedUiPlugin.getImageCache(),