Skip to content

Commit

Permalink
wip3
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Sep 21, 2024
1 parent 0769038 commit a76255a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public abstract class AbstractPyCreateAction extends Action implements IEditorAc

protected IPyEdit targetEditor;

public void setActiveEditor(IAction action, IPyEdit edit) {
this.targetEditor = edit;
}

@Override
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
if (targetEditor instanceof ITextEditor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.python.pydev.ast.codecompletion.ProposalsComparator;
import org.python.pydev.core.IPyEdit;
import org.python.pydev.editor.PyEdit;
import org.python.pydev.editor.codecompletion.proposals.PyCompletionProposal;
import org.python.pydev.parser.PyParser;
Expand All @@ -24,22 +25,22 @@
public abstract class AbstractTddRefactorCompletion extends PyCompletionProposal implements
ICompletionProposalExtension2 {

protected PyEdit edit;
protected IPyEdit edit;

public AbstractTddRefactorCompletion(PyEdit edit, String replacementString, int replacementOffset,
public AbstractTddRefactorCompletion(IPyEdit edit, String replacementString, int replacementOffset,
int replacementLength, int cursorPosition, int priority) {
this(edit, replacementString, replacementOffset, replacementLength, cursorPosition, null, null, null, null,
priority);
}

public AbstractTddRefactorCompletion(PyEdit edit, String replacementString, int replacementOffset,
public AbstractTddRefactorCompletion(IPyEdit edit, String replacementString, int replacementOffset,
int replacementLength, int cursorPosition, IImageHandle image, String displayString,
IContextInformation contextInformation, String additionalProposalInfo, int priority) {
this(edit, replacementString, replacementOffset, replacementLength, cursorPosition, image, displayString,
contextInformation, additionalProposalInfo, priority, IPyCompletionProposal.ON_APPLY_DEFAULT, "");
}

public AbstractTddRefactorCompletion(PyEdit edit, String replacementString, int replacementOffset,
public AbstractTddRefactorCompletion(IPyEdit edit, String replacementString, int replacementOffset,
int replacementLength, int cursorPosition, IImageHandle image, String displayString,
IContextInformation contextInformation, String additionalProposalInfo, int priority, int onApplyAction,
String args) {
Expand All @@ -50,13 +51,13 @@ public AbstractTddRefactorCompletion(PyEdit edit, String replacementString, int

protected void forceReparseInBaseEditorAnd(PyEdit... others) {
if (edit != null) {
PyParser parser = edit.getParser();
PyParser parser = (PyParser) edit.getParser();
parser.forceReparse(
new Tuple<String, Boolean>(IMiscConstants.ANALYSIS_PARSER_OBSERVER_FORCE, true));
}

for (PyEdit e : others) {
PyParser parser = e.getParser();
for (IPyEdit e : others) {
PyParser parser = (PyParser) e.getParser();
parser.forceReparse(
new Tuple<String, Boolean>(IMiscConstants.ANALYSIS_PARSER_OBSERVER_FORCE, true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.python.pydev.core.docutils.PySelection.TddPossibleMatches;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.structure.CompletionRecursionException;
import org.python.pydev.editor.PyEdit;
import org.python.pydev.parser.jython.ast.ClassDef;
import org.python.pydev.parser.jython.ast.FunctionDef;
import org.python.pydev.parser.jython.ast.Return;
Expand All @@ -47,6 +46,7 @@
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;
import org.python.pydev.shared_core.image.UIConstants;
import org.python.pydev.shared_core.string.FullRepIterable;
import org.python.pydev.shared_core.string.StringUtils;

Expand All @@ -73,13 +73,13 @@ public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imag
IPythonNature nature,
IPyEdit edit, int offset) throws BadLocationException {
List<ICompletionProposalHandle> ret = super.getProps(ps, imageCache, f, nature, edit, offset);
this.getTddProps(ps, imageCache, f, nature, (PyEdit) edit, offset, ret);
TddCodeGenerationQuickFixParticipant.getTddProps(ps, imageCache, f, nature, edit, offset, ret);
return ret;
}

public List<ICompletionProposalHandle> getTddProps(PySelection ps, IImageCache imageCache, File f,
public static List<ICompletionProposalHandle> getTddProps(PySelection ps, IImageCache imageCache, File f,
IPythonNature nature,
PyEdit edit, int offset, List<ICompletionProposalHandle> ret) {
IPyEdit edit, int offset, List<ICompletionProposalHandle> ret) {
if (ret == null) {
ret = new ArrayList<ICompletionProposalHandle>();
}
Expand Down Expand Up @@ -158,15 +158,15 @@ public List<ICompletionProposalHandle> getTddProps(PySelection ps, IImageCache i
PySelection newSelection = new PySelection(callPs.getDoc(), absoluteCursorOffset);

checkCreationBasedOnFoundPointers(edit, callPs, ret, possibleMatch, pointers, methodToCreate,
newSelection, nature);
newSelection, nature, imageCache);
continue CONTINUE_FOR;
}

if (pointers.length >= 1) {

//Ok, we found whatever was there, so, we don't need to create anything (except maybe do
//the __init__ or something at the class level).
if (!checkInitCreation(edit, callPs, pointers, ret)) {
if (!checkInitCreation(edit, callPs, pointers, ret, imageCache)) {
//This was called only when isCall == false
//Ok, if it's not a call and we found a field, it's still possible that we may want to create
//a field if it wasn't found in the __init__
Expand All @@ -188,13 +188,13 @@ public List<ICompletionProposalHandle> getTddProps(PySelection ps, IImageCache i
}
if (!foundInInit) {
checkMethodCreationAtClass(edit, pyRefactoring, callWithoutParens, callPs, ret,
lineContents, possibleMatch, f, nature);
lineContents, possibleMatch, f, nature, imageCache);
}
}

} else if (pointers.length == 0) {
checkMethodCreationAtClass(edit, pyRefactoring, callWithoutParens, callPs, ret, lineContents,
possibleMatch, f, nature);
possibleMatch, f, nature, imageCache);

}
} catch (Exception e) {
Expand All @@ -211,10 +211,11 @@ public List<ICompletionProposalHandle> getTddProps(PySelection ps, IImageCache i

public static ICallback<Boolean, Exception> onGetTddPropsError;

private boolean checkMethodCreationAtClass(PyEdit edit, IPyRefactoring pyRefactoring, String callWithoutParens,
private static boolean checkMethodCreationAtClass(IPyEdit edit, IPyRefactoring pyRefactoring,
String callWithoutParens,
PySelection callPs, List<ICompletionProposalHandle> ret, String lineContents,
TddPossibleMatches possibleMatch,
File f, IPythonNature nature) throws MisconfigurationException, Exception {
File f, IPythonNature nature, IImageCache imageCache) throws MisconfigurationException, Exception {
RefactoringRequest request;
ItemPointer[] pointers;
//Ok, no definition found for the full string, so, check if we have a dot there and check
Expand Down Expand Up @@ -242,7 +243,7 @@ private boolean checkMethodCreationAtClass(PyEdit edit, IPyRefactoring pyRefacto
pyCreateMethod.setCreateInClass(classNameInLine);

addCreateMethodOption(callPs, edit, ret, methodToCreate, parametersAfterCall,
pyCreateMethod, classNameInLine);
pyCreateMethod, classNameInLine, imageCache);
}
}
}
Expand All @@ -259,15 +260,15 @@ private boolean checkMethodCreationAtClass(PyEdit edit, IPyRefactoring pyRefacto
pointers = pyRefactoring.findDefinition(request);
if (pointers.length == 1) {
if (checkCreationBasedOnFoundPointers(edit, callPs, ret, possibleMatch, pointers, methodToCreate,
newSelection, nature)) {
newSelection, nature, imageCache)) {
return true;
}
}
}
return false;
}

public Definition rebaseAssignDefinition(AssignDefinition assignDef, IPythonNature nature,
public static Definition rebaseAssignDefinition(AssignDefinition assignDef, IPythonNature nature,
ICompletionCache completionCache) throws Exception {
IDefinition[] definitions2;
if ("None".equals(assignDef.type)) {
Expand Down Expand Up @@ -297,7 +298,8 @@ public Definition rebaseAssignDefinition(AssignDefinition assignDef, IPythonNatu
return assignDef;
}

public Definition rebaseFunctionDef(Definition definition, IPythonNature nature, ICompletionCache completionCache)
public static Definition rebaseFunctionDef(Definition definition, IPythonNature nature,
ICompletionCache completionCache)
throws Exception {
ITypeInfo type = NodeUtils.getReturnTypeFromFuncDefAST(definition.ast);
if (type != null) {
Expand Down Expand Up @@ -328,7 +330,7 @@ public Definition rebaseFunctionDef(Definition definition, IPythonNature nature,
return definition;
}

private Definition rebaseToClassDefDefinition(IPythonNature nature, CompletionCache completionCache,
private static Definition rebaseToClassDefDefinition(IPythonNature nature, CompletionCache completionCache,
Definition definition, CompletionState completionState) throws CompletionRecursionException, Exception {

if (completionState == null) {
Expand All @@ -355,10 +357,10 @@ private Definition rebaseToClassDefDefinition(IPythonNature nature, CompletionCa
return definition;
}

public boolean checkCreationBasedOnFoundPointers(PyEdit edit, PySelection callPs,
public static boolean checkCreationBasedOnFoundPointers(IPyEdit edit, PySelection callPs,
List<ICompletionProposalHandle> ret,
TddPossibleMatches possibleMatch, ItemPointer[] pointers, String methodToCreate, PySelection newSelection,
IPythonNature nature) throws MisconfigurationException, Exception {
IPythonNature nature, IImageCache imageCache) throws MisconfigurationException, Exception {
CompletionCache completionCache = new CompletionCache();
for (ItemPointer pointer : pointers) {
Definition definition = pointer.definition;
Expand Down Expand Up @@ -392,7 +394,7 @@ public boolean checkCreationBasedOnFoundPointers(PyEdit edit, PySelection callPs
pyCreateMethod.getCreationStr(), className, definition.module.getName());

TddRefactorCompletionInModule completion = new TddRefactorCompletionInModule(methodToCreate,
tddQuickFixParticipant != null ? tddQuickFixParticipant.imageMethod : null, displayString,
imageCache.get(UIConstants.CREATE_METHOD_ICON), displayString,
null, displayString, IPyCompletionProposal.PRIORITY_CREATE, edit,
definition.module.getFile(), parametersAfterCall, pyCreateMethod, newSelection);
completion.locationStrategy = AbstractPyCreateAction.LOCATION_STRATEGY_END;
Expand All @@ -404,7 +406,7 @@ public boolean checkCreationBasedOnFoundPointers(PyEdit edit, PySelection callPs
return false;
}

private List<String> configCreateAsAndReturnParametersAfterCall(PySelection callPs, boolean isCall,
private static List<String> configCreateAsAndReturnParametersAfterCall(PySelection callPs, boolean isCall,
PyCreateMethodOrField pyCreateMethod, List<String> parametersAfterCall, String methodToCreate) {
if (isCall) {
pyCreateMethod.setCreateAs(PyCreateMethodOrField.BOUND_METHOD);
Expand All @@ -420,20 +422,21 @@ private List<String> configCreateAsAndReturnParametersAfterCall(PySelection call
return parametersAfterCall;
}

private void addCreateMethodOption(PySelection ps, PyEdit edit, List<ICompletionProposalHandle> props,
private static void addCreateMethodOption(PySelection ps, IPyEdit edit, List<ICompletionProposalHandle> props,
String markerContents, List<String> parametersAfterCall, PyCreateMethodOrField pyCreateMethod,
String classNameInLine) {
String classNameInLine, IImageCache imageCache) {
String displayString = StringUtils.format("Create %s %s at %s",
markerContents,
pyCreateMethod.getCreationStr(), classNameInLine);
TddRefactorCompletion tddRefactorCompletion = new TddRefactorCompletion(markerContents,
tddQuickFixParticipant.imageMethod, displayString, null, null, IPyCompletionProposal.PRIORITY_CREATE,
imageCache.get(UIConstants.CREATE_METHOD_ICON), displayString, null, null,
IPyCompletionProposal.PRIORITY_CREATE,
edit, PyCreateClass.LOCATION_STRATEGY_BEFORE_CURRENT, parametersAfterCall, pyCreateMethod, ps);
props.add(tddRefactorCompletion);
}

private boolean checkInitCreation(PyEdit edit, PySelection callPs, ItemPointer[] pointers,
List<ICompletionProposalHandle> ret) {
private static boolean checkInitCreation(IPyEdit edit, PySelection callPs, ItemPointer[] pointers,
List<ICompletionProposalHandle> ret, IImageCache imageCache) {
for (ItemPointer pointer : pointers) {
Definition definition = pointer.definition;
if (definition != null && definition.ast instanceof ClassDef) {
Expand All @@ -452,7 +455,7 @@ private boolean checkInitCreation(PyEdit edit, PySelection callPs, ItemPointer[]
"Create %s __init__ (%s)", className,
definition.module.getName());
TddRefactorCompletionInModule completion = new TddRefactorCompletionInModule("__init__",
tddQuickFixParticipant.imageMethod, displayString, null, displayString,
imageCache.get(UIConstants.CREATE_METHOD_ICON), displayString, null, displayString,
IPyCompletionProposal.PRIORITY_CREATE, edit, definition.module.getFile(),
parametersAfterCall, pyCreateMethod, callPs);
completion.locationStrategy = AbstractPyCreateAction.LOCATION_STRATEGY_FIRST_METHOD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.templates.TemplateProposal;
import org.eclipse.swt.graphics.Point;
import org.python.pydev.core.IPyEdit;
import org.python.pydev.core.MisconfigurationException;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.log.Log;
import org.python.pydev.editor.PyEdit;
import org.python.pydev.parser.PyParser;
import org.python.pydev.refactoring.core.base.RefactoringInfo;
import org.python.pydev.shared_core.image.IImageHandle;

Expand All @@ -33,7 +34,7 @@ public final class TddRefactorCompletion extends AbstractTddRefactorCompletion {
private PySelection ps;

TddRefactorCompletion(String replacementString, IImageHandle image, String displayString,
IContextInformation contextInformation, String additionalProposalInfo, int priority, PyEdit edit,
IContextInformation contextInformation, String additionalProposalInfo, int priority, IPyEdit edit,
int locationStrategy, List<String> parametersAfterCall, AbstractPyCreateAction pyCreateAction,
PySelection ps) {

Expand Down Expand Up @@ -68,7 +69,8 @@ public Point getSelection(IDocument document) {
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
if (edit != null) {
//We have to reparse to make sure that we'll have an accurate AST.
edit.getParser().reparseDocument();
PyParser parser = (PyParser) edit.getParser();
parser.reparseDocument();
}
TemplateProposal executed2 = getExecuted();
if (executed2 != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.swt.graphics.Point;
import org.python.pydev.ast.item_pointer.ItemPointer;
import org.python.pydev.core.IPyEdit;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.log.Log;
import org.python.pydev.editor.PyEdit;
Expand All @@ -34,7 +35,7 @@ public final class TddRefactorCompletionInModule extends AbstractTddRefactorComp
public int locationStrategy = AbstractPyCreateAction.LOCATION_STRATEGY_END;

public TddRefactorCompletionInModule(String replacementString, IImageHandle image, String displayString,
IContextInformation contextInformation, String additionalProposalInfo, int priority, PyEdit edit,
IContextInformation contextInformation, String additionalProposalInfo, int priority, IPyEdit edit,
File module, List<String> parametersAfterCall, AbstractPyCreateAction pyCreateAction, PySelection ps) {

super(edit, replacementString, 0, 0, 0, image, displayString, contextInformation, additionalProposalInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public void addProps(MarkerAnnotationAndPosition markerAnnotation, IAnalysisPref
int start = markerAnnotation.position.offset;
int end = start + markerAnnotation.position.length;
UndefinedVariableQuickFixCreator.createImportQuickProposalsFromMarkerSelectedText(edit, ps, offset,
initialNature,
props, astManager, start, end, forceReparseOnApply);
initialNature, props, astManager, start, end, forceReparseOnApply);
}

}

0 comments on commit a76255a

Please sign in to comment.