Skip to content

Commit

Permalink
Move code related to TDD actions to core plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Oct 2, 2024
1 parent 4cfd990 commit b6be0da
Show file tree
Hide file tree
Showing 15 changed files with 143 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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;
package com.python.pydev.analysis.marker_quick_fixes;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -38,10 +38,12 @@
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.preferences.FileTypesPreferences;
import org.python.pydev.core.proposals.CompletionProposalFactory;
import org.python.pydev.core.structure.CompletionRecursionException;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.ast.ClassDef;
import org.python.pydev.parser.visitors.NodeUtils;
import org.python.pydev.shared_core.SharedCorePlugin;
import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle;
import org.python.pydev.shared_core.code_completion.IPyCompletionProposal;
import org.python.pydev.shared_core.image.IImageCache;
Expand All @@ -52,7 +54,6 @@
import org.python.pydev.shared_core.string.FullRepIterable;
import org.python.pydev.shared_core.string.StringUtils;
import org.python.pydev.shared_core.structure.Tuple;
import org.python.pydev.shared_ui.SharedUiPlugin;

import com.python.pydev.analysis.refactoring.tdd.AbstractPyCreateAction;
import com.python.pydev.analysis.refactoring.tdd.NullPyCreateAction;
Expand All @@ -69,7 +70,7 @@ public class TddQuickFixFromMarkersParticipant implements IAnalysisMarkersPartic
/*default*/IImageHandle imageModule;

public TddQuickFixFromMarkersParticipant() {
IImageCache imageCache = SharedUiPlugin.getImageCache();
IImageCache imageCache = SharedCorePlugin.getImageCache();
if (imageCache != null) { //making tests
imageClass = imageCache.get(UIConstants.CREATE_CLASS_ICON);
imageMethod = imageCache.get(UIConstants.CREATE_METHOD_ICON);
Expand Down Expand Up @@ -343,9 +344,12 @@ protected File getFileStructure(File file, String withoutLastPart) {
private void addCreateClassmethodOption(PySelection ps, IPyEdit edit, List<ICompletionProposalHandle> props,
String markerContents, List<String> parametersAfterCall, PyCreateMethodOrField pyCreateMethod, File file,
String className) {
props.add(new TddRefactorCompletionInModule(markerContents, imageMethod, "Create " + markerContents +
" classmethod at " + className +
" in " + file.getName(), null,

props.add(CompletionProposalFactory.get().createTddRefactorCompletionInModule(markerContents, imageMethod,
"Create " + markerContents +
" classmethod at " + className +
" in " + file.getName(),
null,
"Create " + markerContents +
" classmethod at class: " + className +
" in " + file,
Expand All @@ -355,8 +359,10 @@ private void addCreateClassmethodOption(PySelection ps, IPyEdit edit, List<IComp

private void addCreateMethodOption(PySelection ps, IPyEdit edit, List<ICompletionProposalHandle> props,
String markerContents, List<String> parametersAfterCall, File file) {
props.add(new TddRefactorCompletionInModule(markerContents, imageMethod, "Create " + markerContents +
" method at " + file.getName(), null,
props.add(CompletionProposalFactory.get().createTddRefactorCompletionInModule(markerContents, imageMethod,
"Create " + markerContents +
" method at " + file.getName(),
null,
"Create " + markerContents +
" method at " + file,
IPyCompletionProposal.PRIORITY_CREATE, edit, file, parametersAfterCall, new PyCreateMethodOrField(),
Expand All @@ -365,8 +371,10 @@ IPyCompletionProposal.PRIORITY_CREATE, edit, file, parametersAfterCall, new PyCr

private void addCreateClassOption(PySelection ps, IPyEdit edit, List<ICompletionProposalHandle> props,
String markerContents, List<String> parametersAfterCall, File file) {
props.add(new TddRefactorCompletionInModule(markerContents, imageClass, "Create " + markerContents +
" class at " + file.getName(), null,
props.add(CompletionProposalFactory.get().createTddRefactorCompletionInModule(markerContents, imageClass,
"Create " + markerContents +
" class at " + file.getName(),
null,
"Create " + markerContents +
" class at " + file,
IPyCompletionProposal.PRIORITY_CREATE, edit, file, parametersAfterCall, new PyCreateClass(), ps,
Expand All @@ -375,8 +383,10 @@ IPyCompletionProposal.PRIORITY_CREATE, edit, file, parametersAfterCall, new PyCr

private void addCreateClassInNewModuleOption(PySelection ps, IPyEdit edit, List<ICompletionProposalHandle> props,
String markerContents, String moduleName, List<String> parametersAfterCall, File file) {
props.add(new TddRefactorCompletionInInexistentModule(markerContents, imageClass, "Create " + markerContents +
" class at new module " + moduleName, null,
props.add(CompletionProposalFactory.get().createTddRefactorCompletionInInexistentModule(markerContents,
imageClass, "Create " + markerContents +
" class at new module " + moduleName,
null,
"Create " + markerContents +
" class at new module "
+ file,
Expand All @@ -386,8 +396,10 @@ private void addCreateClassInNewModuleOption(PySelection ps, IPyEdit edit, List<

private void addCreateMethodInNewModuleOption(PySelection ps, IPyEdit edit, List<ICompletionProposalHandle> props,
String markerContents, String moduleName, List<String> parametersAfterCall, File file) {
props.add(new TddRefactorCompletionInInexistentModule(markerContents, imageMethod, "Create " + markerContents +
" method at new module " + moduleName, null,
props.add(CompletionProposalFactory.get().createTddRefactorCompletionInInexistentModule(markerContents,
imageMethod, "Create " + markerContents +
" method at new module " + moduleName,
null,
"Create " + markerContents +
" method at new module "
+ file,
Expand All @@ -397,8 +409,10 @@ private void addCreateMethodInNewModuleOption(PySelection ps, IPyEdit edit, List

private void addCreateModuleOption(PySelection ps, IPyEdit edit, List<ICompletionProposalHandle> props,
String markerContents, File file) {
props.add(new TddRefactorCompletionInInexistentModule(markerContents, imageModule, "Create " + markerContents +
" module", null,
props.add(CompletionProposalFactory.get().createTddRefactorCompletionInInexistentModule(markerContents,
imageModule, "Create " + markerContents +
" module",
null,
"Create " + markerContents +
" module (" + file +
")",
Expand All @@ -408,16 +422,20 @@ private void addCreateModuleOption(PySelection ps, IPyEdit edit, List<ICompletio

private void addCreateMethodOption(PySelection ps, IPyEdit edit, List<ICompletionProposalHandle> props,
String markerContents, List<String> parametersAfterCall) {
props.add(new TddRefactorCompletion(markerContents, imageMethod, "Create " + markerContents +
" method", null,
props.add(CompletionProposalFactory.get().createTddRefactorCompletion(markerContents, imageMethod,
"Create " + markerContents +
" method",
null,
null, IPyCompletionProposal.PRIORITY_CREATE, edit, PyCreateClass.LOCATION_STRATEGY_BEFORE_CURRENT,
parametersAfterCall, new PyCreateMethodOrField(), ps));
}

private void addCreateClassOption(PySelection ps, IPyEdit edit, List<ICompletionProposalHandle> props,
String markerContents, List<String> parametersAfterCall) {
props.add(new TddRefactorCompletion(markerContents, imageClass, "Create " + markerContents +
" class", null,
props.add(CompletionProposalFactory.get().createTddRefactorCompletion(markerContents, imageClass,
"Create " + markerContents +
" class",
null,
null, IPyCompletionProposal.PRIORITY_CREATE, edit, PyCreateClass.LOCATION_STRATEGY_BEFORE_CURRENT,
parametersAfterCall, new PyCreateClass(), ps));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
public class PyCreateClass extends AbstractPyCreateClassOrMethodOrField {

private final static String baseClassStr = "" +
"class %s(${object}):\n" +
"class %s:\n" +
" ${pass}${cursor}\n" +
"\n" +
"\n" +
"";

private final static String baseClassWithInitStr = "" +
"class %s(${object}):\n" +
"class %s:\n" +
" \n"
+
" def __init__(self, %s):\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.python.pydev.refactoring.tdd;
package com.python.pydev.analysis.refactoring.tdd;

import java.io.File;
import java.util.ArrayList;
Expand Down Expand Up @@ -29,6 +29,7 @@
import org.python.pydev.core.docutils.PySelection.LineStartingScope;
import org.python.pydev.core.docutils.PySelection.TddPossibleMatches;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.proposals.CompletionProposalFactory;
import org.python.pydev.core.structure.CompletionRecursionException;
import org.python.pydev.parser.jython.ast.ClassDef;
import org.python.pydev.parser.jython.ast.FunctionDef;
Expand All @@ -46,10 +47,6 @@
import org.python.pydev.shared_core.string.FullRepIterable;
import org.python.pydev.shared_core.string.StringUtils;

import com.python.pydev.analysis.refactoring.tdd.AbstractPyCreateAction;
import com.python.pydev.analysis.refactoring.tdd.PyCreateClass;
import com.python.pydev.analysis.refactoring.tdd.PyCreateMethodOrField;

public class TddCodeGenerationQuickFixWithoutMarkersParticipant implements IAssistProps {

/**
Expand Down Expand Up @@ -104,11 +101,12 @@ static boolean checkInitCreation(IPyEdit edit, PySelection callPs, ItemPointer[]
String displayString = StringUtils.format(
"Create %s __init__ (%s)", className,
definition.module.getName());
TddRefactorCompletionInModule completion = new TddRefactorCompletionInModule("__init__",
imageCache.get(UIConstants.CREATE_METHOD_ICON), displayString, null, displayString,
IPyCompletionProposal.PRIORITY_CREATE, edit, definition.module.getFile(),
parametersAfterCall, pyCreateMethod, callPs,
AbstractPyCreateAction.LOCATION_STRATEGY_FIRST_METHOD);
ICompletionProposalHandle completion = CompletionProposalFactory.get()
.createTddRefactorCompletionInModule("__init__",
imageCache.get(UIConstants.CREATE_METHOD_ICON), displayString, null, displayString,
IPyCompletionProposal.PRIORITY_CREATE, edit, definition.module.getFile(),
parametersAfterCall, pyCreateMethod, callPs,
AbstractPyCreateAction.LOCATION_STRATEGY_FIRST_METHOD);
ret.add(completion);
return true;
}
Expand All @@ -123,7 +121,8 @@ static void addCreateMethodOption(PySelection ps, IPyEdit edit, List<ICompletion
String displayString = StringUtils.format("Create %s %s at %s",
markerContents,
pyCreateMethod.getCreationStr(), classNameInLine);
TddRefactorCompletion tddRefactorCompletion = new TddRefactorCompletion(markerContents,
ICompletionProposalHandle tddRefactorCompletion = CompletionProposalFactory.get().createTddRefactorCompletion(
markerContents,
imageCache.get(UIConstants.CREATE_METHOD_ICON), displayString, null, null,
IPyCompletionProposal.PRIORITY_CREATE,
edit, PyCreateClass.LOCATION_STRATEGY_BEFORE_CURRENT, parametersAfterCall, pyCreateMethod, ps);
Expand Down Expand Up @@ -183,11 +182,12 @@ public static boolean checkCreationBasedOnFoundPointers(IPyEdit edit, PySelectio
"Create %s %s at %s (%s)", methodToCreate,
pyCreateMethod.getCreationStr(), className, definition.module.getName());

TddRefactorCompletionInModule completion = new TddRefactorCompletionInModule(methodToCreate,
imageCache.get(UIConstants.CREATE_METHOD_ICON), displayString,
null, displayString, IPyCompletionProposal.PRIORITY_CREATE, edit,
definition.module.getFile(), parametersAfterCall, pyCreateMethod, newSelection,
AbstractPyCreateAction.LOCATION_STRATEGY_END);
ICompletionProposalHandle completion = CompletionProposalFactory.get()
.createTddRefactorCompletionInModule(methodToCreate,
imageCache.get(UIConstants.CREATE_METHOD_ICON), displayString,
null, displayString, IPyCompletionProposal.PRIORITY_CREATE, edit,
definition.module.getFile(), parametersAfterCall, pyCreateMethod, newSelection,
AbstractPyCreateAction.LOCATION_STRATEGY_END);
ret.add(completion);
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion plugins/com.python.pydev.refactoring/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<ctrl_1_participant class="com.python.pydev.refactoring.tdd.TddCodeGenerationQuickFixFromMarkersParticipant"/>
</extension>
<extension point="org.python.pydev.pydev_ctrl_1">
<ctrl_1_participant class="com.python.pydev.refactoring.tdd.TddCodeGenerationQuickFixWithoutMarkersParticipant"/>
<ctrl_1_participant class="com.python.pydev.analysis.refactoring.tdd.TddCodeGenerationQuickFixWithoutMarkersParticipant"/>
</extension>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.python.pydev.analysis.additionalinfo.builders.AnalysisRunner;
import com.python.pydev.analysis.ctrl_1.AbstractAnalysisMarkersParticipants;
import com.python.pydev.analysis.marker_quick_fixes.TddQuickFixFromMarkersParticipant;

public class TddCodeGenerationQuickFixFromMarkersParticipant extends AbstractAnalysisMarkersParticipants {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@

import com.python.pydev.analysis.refactoring.refactorer.Refactorer;
import com.python.pydev.analysis.refactoring.tdd.AbstractPyCreateClassOrMethodOrField;
import com.python.pydev.analysis.refactoring.tdd.TddCodeGenerationQuickFixWithoutMarkersParticipant;
import com.python.pydev.analysis.refactoring.tdd.TemplateInfo;
import com.python.pydev.refactoring.tdd.completions.TddRefactorCompletion;
import com.python.pydev.refactoring.tdd.completions.TddRefactorCompletionInModule;

/**
* @author Fabio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@
import com.python.pydev.analysis.AnalysisPreferences;
import com.python.pydev.analysis.AnalysisTestsBase;
import com.python.pydev.analysis.additionalinfo.builders.AnalysisRunner;
import com.python.pydev.analysis.marker_quick_fixes.TddQuickFixFromMarkersParticipant;
import com.python.pydev.analysis.refactoring.quick_fixes.DummyMarkerInfoForAnalysis;
import com.python.pydev.analysis.refactoring.refactorer.Refactorer;
import com.python.pydev.analysis.refactoring.tdd.TddCodeGenerationQuickFixWithoutMarkersParticipant;
import com.python.pydev.analysis.refactoring.tdd.TemplateInfo;
import com.python.pydev.refactoring.tdd.completions.TddRefactorCompletion;
import com.python.pydev.refactoring.tdd.completions.TddRefactorCompletionInModule;

/**
* @author Fabio
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.python.pydev.core.proposals;

import java.io.File;
import java.util.List;

import org.eclipse.jface.text.IRegion;
Expand Down Expand Up @@ -135,4 +136,25 @@ ICompletionProposalHandle createMoveImportsToLocalCompletionProposal(

ShellId getCythonShellId();

ICompletionProposalHandle createTddRefactorCompletion(String replacementString, IImageHandle image,
String displayString,
/*IContextInformation*/Object contextInformation, String additionalProposalInfo, int priority, IPyEdit edit,
int locationStrategy, List<String> parametersAfterCall, /*PyCreateMethodOrField*/Object pyCreateAction,
PySelection ps);

ICompletionProposalHandle createTddRefactorCompletionInModule(String replacementString, IImageHandle image,
String displayString,
/*IContextInformation*/ Object contextInformation, String additionalProposalInfo, int priority,
IPyEdit edit,
File module, List<String> parametersAfterCall, /*AbstractPyCreateAction*/ Object pyCreateAction,
PySelection ps,
int locationStrategy);

ICompletionProposalHandle createTddRefactorCompletionInInexistentModule(String replacementString,
IImageHandle image, String displayString,
/*IContextInformation*/ Object contextInformation, String additionalProposalInfo, int priority,
IPyEdit edit,
File module, List<String> parametersAfterCall, /*AbstractPyCreateAction*/ Object pyCreateAction,
PySelection ps);

}
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,11 @@ public static int getFirstCharRelativePosition(IDocument doc, int cursorOffset)
*/
public static int getFirstCharPosition(IDocument doc, int cursorOffset) throws BadLocationException {
IRegion region;
region = doc.getLineInformationOfOffset(cursorOffset);
try {
region = doc.getLineInformationOfOffset(cursorOffset);
} catch (BadLocationException e) {
throw new BadLocationException("Error: bad offset: " + cursorOffset + " doc len: " + doc.getLength());
}
int offset = region.getOffset();
return offset + getFirstCharRelativePosition(doc, cursorOffset);
}
Expand Down Expand Up @@ -1219,7 +1223,7 @@ public String getContentsFromLineRange(int startLine, int endLine) {
*/
public static boolean stillInTok(String string, int j) {
char c = string.charAt(j);

return c != '\n' && c != '\r' && c != ' ' && c != '.' && c != '(' && c != ')' && c != ',' && c != ']'
&& c != '[' && c != '#' && c != '\'' && c != '"';
}
Expand Down
1 change: 1 addition & 0 deletions plugins/org.python.pydev/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Export-Package: com.python.pydev.actions,
com.python.pydev.analysis.ctrl_1,
com.python.pydev.analysis.indexview,
com.python.pydev.analysis.ui,
com.python.pydev.refactoring.tdd.completions,
com.python.pydev.ui.hierarchy,
org.python.copiedfromeclipsesrc,
org.python.pydev.ast.codecompletion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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;
package com.python.pydev.refactoring.tdd.completions;

import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
import org.eclipse.jface.text.contentassist.IContextInformation;
Expand Down
Loading

0 comments on commit b6be0da

Please sign in to comment.