Skip to content

Commit

Permalink
Moving code from UI plugins to core plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Sep 21, 2024
1 parent 1da5193 commit 6f477a4
Show file tree
Hide file tree
Showing 157 changed files with 701 additions and 639 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@

public class UndefinedVariableQuickFixCreator {

public static void createImportQuickProposalsFromMarkerSelectedText(IAdaptable projectAdaptable, PySelection ps,
public static void createImportQuickProposalsFromMarkerSelectedText(
IAdaptable projectAdaptable,
PySelection ps,
int offset,
IPythonNature initialNature,
List<ICompletionProposalHandle> props, ICodeCompletionASTManager astManager, int start, int end,
List<ICompletionProposalHandle> props,
ICodeCompletionASTManager astManager,
int start,
int end,
boolean forceReparseOnApply)
throws BadLocationException {
ps.setSelection(start, end);
Expand Down
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 @@ -17,6 +17,11 @@
import org.eclipse.jface.text.templates.GlobalTemplateVariables;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.python.pydev.ast.adapters.IClassDefAdapter;
import org.python.pydev.ast.adapters.ModuleAdapter;
import org.python.pydev.ast.adapters.offsetstrategy.BeginOffset;
import org.python.pydev.ast.adapters.offsetstrategy.EndOffset;
import org.python.pydev.ast.adapters.offsetstrategy.IOffsetStrategy;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.docutils.PySelection.LineStartingScope;
import org.python.pydev.core.docutils.PyStringUtils;
Expand All @@ -28,11 +33,6 @@
import org.python.pydev.parser.jython.ast.ClassDef;
import org.python.pydev.parser.jython.ast.Pass;
import org.python.pydev.parser.visitors.NodeUtils;
import org.python.pydev.refactoring.ast.adapters.IClassDefAdapter;
import org.python.pydev.refactoring.ast.adapters.ModuleAdapter;
import org.python.pydev.refactoring.ast.adapters.offsetstrategy.BeginOffset;
import org.python.pydev.refactoring.ast.adapters.offsetstrategy.EndOffset;
import org.python.pydev.refactoring.ast.adapters.offsetstrategy.IOffsetStrategy;
import org.python.pydev.refactoring.core.base.RefactoringInfo;
import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle;
import org.python.pydev.shared_core.string.FastStringBuffer;
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 @@ -8,8 +8,8 @@

import java.util.List;

import org.python.pydev.ast.adapters.ModuleAdapter;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.refactoring.ast.adapters.ModuleAdapter;
import org.python.pydev.refactoring.core.base.RefactoringInfo;
import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle;
import org.python.pydev.shared_core.string.FastStringBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.python.pydev.ast.adapters.FunctionDefAdapter;
import org.python.pydev.ast.adapters.IClassDefAdapter;
import org.python.pydev.ast.adapters.ModuleAdapter;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.log.Log;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.ast.FunctionDef;
import org.python.pydev.parser.jython.ast.Pass;
import org.python.pydev.parser.jython.ast.stmtType;
import org.python.pydev.parser.visitors.NodeUtils;
import org.python.pydev.refactoring.ast.adapters.FunctionDefAdapter;
import org.python.pydev.refactoring.ast.adapters.IClassDefAdapter;
import org.python.pydev.refactoring.ast.adapters.ModuleAdapter;
import org.python.pydev.refactoring.core.base.RefactoringInfo;
import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle;
import org.python.pydev.shared_core.string.StringUtils;
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 @@ -46,7 +45,9 @@
import org.python.pydev.shared_core.callbacks.ICallback;
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.DummyImageCache;
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,16 +74,19 @@ 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>();
}
if (imageCache == null) {
imageCache = new DummyImageCache();
}
//Additional option: Generate markers for 'self.' accesses
int lineOfOffset = ps.getLineOfOffset(offset);
String lineContents = ps.getLine(lineOfOffset);
Expand Down Expand Up @@ -158,15 +162,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 +192,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 +215,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 +247,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 +264,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 +302,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 +334,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 +361,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 +398,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 +410,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 +426,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 +459,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
Loading

0 comments on commit 6f477a4

Please sign in to comment.