Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Sep 28, 2024
1 parent 7890d9d commit c46fe1c
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Boolean, Exception>() {

@Override
public Boolean call(Exception e) {
throw new RuntimeException("Error:" + org.python.pydev.shared_core.log.Log.getExceptionStr(e));
}
};
PyCodeCompletion.onCompletionRecursionException = new ICallback<Object, CompletionRecursionException>() {

@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<ICompletionProposalHandle> props = new ArrayList<>();
participant.addProps(markerInfo, analysisPreferences, line, ps, offset, nature, null, props);

} finally {
GRAMMAR_TO_USE_FOR_PARSING = usedGrammar;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 {
Expand Down Expand Up @@ -927,7 +922,8 @@ private ICompletionProposalExtension2 waitForQuickFixProps(TddCodeGenerationQuic

}

private List<ICompletionProposalHandle> waitForQuickFixProps(TddCodeGenerationQuickFixFromMarkersParticipant quickFix,
private List<ICompletionProposalHandle> waitForQuickFixProps(
TddCodeGenerationQuickFixFromMarkersParticipant quickFix,
PySelection ps, int offset) throws BadLocationException, MisconfigurationException {
for (int i = 0; i < 10; i++) {
List<ICompletionProposalHandle> props = quickFix.getProps(ps, SharedUiPlugin.getImageCache(),
Expand Down

0 comments on commit c46fe1c

Please sign in to comment.