Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Sep 29, 2024
1 parent 40ca1eb commit 356fab6
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.python.pydev.ast.refactoring.RefactoringInfo;
import org.python.pydev.core.IPyEdit;
import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle;

public abstract class AbstractPyCreateAction {

Expand All @@ -24,7 +23,7 @@ public void setActiveEditor(IPyEdit edit) {
this.targetEditor = edit;
}

public abstract ICompletionProposalHandle createProposal(RefactoringInfo refactoringInfo, String actTok,
public abstract TemplateInfo createProposal(RefactoringInfo refactoringInfo, String actTok,
int locationStrategy, List<String> parametersAfterCall);

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
import org.python.pydev.core.docutils.PySelection.LineStartingScope;
import org.python.pydev.core.docutils.PyStringUtils;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.proposals.CompletionProposalFactory;
import org.python.pydev.core.templates.PyDocumentTemplateContext;
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.shared_core.code_completion.ICompletionProposalHandle;
import org.python.pydev.shared_core.string.FastStringBuffer;
import org.python.pydev.shared_core.string.StringUtils;
import org.python.pydev.shared_core.structure.Tuple;
Expand All @@ -38,12 +36,12 @@ public abstract class AbstractPyCreateClassOrMethodOrField extends AbstractPyCre

public abstract String getDefaultActTok();

protected ICompletionProposalHandle createProposal(PySelection pySelection, String source,
protected TemplateInfo createProposal(PySelection pySelection, String source,
Tuple<Integer, String> offsetAndIndent) {
return createProposal(pySelection, source, offsetAndIndent, true, null);
}

protected ICompletionProposalHandle createProposal(PySelection pySelection, String source,
protected TemplateInfo createProposal(PySelection pySelection, String source,
Tuple<Integer, String> offsetAndIndent, boolean requireEmptyLines, Pass replacePassStatement) {
int offset;
int len;
Expand Down Expand Up @@ -102,25 +100,16 @@ protected ICompletionProposalHandle createProposal(PySelection pySelection, Stri
}
}

if (targetEditor != null) {
String creationStr = getCreationStr();
Region region = new Region(offset, len);
//Note: was using new PyContextType(), but when we had something as ${user} it
//would end up replacing it with the actual name of the user, which is not what
//we want!
PyDocumentTemplateContext context = PyDocumentTemplateContext.createContextWithCursor(targetEditor, region,
indent);
String creationStr = getCreationStr();
Region region = new Region(offset, len);
//Note: was using new PyContextType(), but when we had something as ${user} it
//would end up replacing it with the actual name of the user, which is not what
//we want!
PyDocumentTemplateContext context = PyDocumentTemplateContext.createContextWithCursor(targetEditor, region,
indent);

Template template = new Template("Create " + creationStr, "Create " + creationStr, "", source, true);
ICompletionProposalHandle templateProposal = CompletionProposalFactory.get()
.createPyTemplateProposal(template, context, region, null, 0);
return templateProposal;

} else {
//This should only happen in tests.
source = StringUtils.indentTo(source, indent, false);
return CompletionProposalFactory.get().createPyCompletionProposal(source, offset, len, 0, 0);
}
Template template = new Template("Create " + creationStr, "Create " + creationStr, "", source, true);
return new TemplateInfo(template, context, region);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import java.util.List;

import org.python.pydev.ast.refactoring.RefactoringInfo;
import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle;

public class NullPyCreateAction extends AbstractPyCreateAction {

@Override
public ICompletionProposalHandle createProposal(RefactoringInfo refactoringInfo, String actTok,
public TemplateInfo createProposal(RefactoringInfo refactoringInfo, String actTok,
int locationStrategy,
List<String> parametersAfterCall) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.python.pydev.ast.adapters.ModuleAdapter;
import org.python.pydev.ast.refactoring.RefactoringInfo;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle;
import org.python.pydev.shared_core.string.FastStringBuffer;
import org.python.pydev.shared_core.string.StringUtils;
import org.python.pydev.shared_core.structure.Tuple;
Expand Down Expand Up @@ -52,7 +51,7 @@ public String getDefaultActTok() {
* Returns a proposal that can be used to generate the code.
*/
@Override
public ICompletionProposalHandle createProposal(RefactoringInfo refactoringInfo, String actTok,
public TemplateInfo createProposal(RefactoringInfo refactoringInfo, String actTok,
int locationStrategy,
List<String> parametersAfterCall) {
PySelection pySelection = refactoringInfo.getPySelection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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.shared_core.code_completion.ICompletionProposalHandle;
import org.python.pydev.shared_core.string.StringUtils;
import org.python.pydev.shared_core.structure.Tuple;

Expand Down Expand Up @@ -63,7 +62,7 @@ public String getDefaultActTok() {
* Returns a proposal that can be used to generate the code.
*/
@Override
public ICompletionProposalHandle createProposal(RefactoringInfo refactoringInfo, String actTok,
public TemplateInfo createProposal(RefactoringInfo refactoringInfo, String actTok,
int locationStrategy,
List<String> parametersAfterCall) {
PySelection pySelection = refactoringInfo.getPySelection();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.python.pydev.analysis.refactoring.tdd;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.templates.DocumentTemplateContext;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.text.templates.TemplateBuffer;
import org.eclipse.jface.text.templates.TemplateException;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.templates.PyDocumentTemplateContext;

public class TemplateInfo {

public Template fTemplate;
public PyDocumentTemplateContext fContext;
public Region fRegion;

public TemplateInfo(Template template, PyDocumentTemplateContext context, Region region) {
this.fTemplate = template;
this.fContext = context;
this.fRegion = region;
}

/**
* Returns the offset of the range in the document that will be replaced by
* applying this template.
*
* @return the offset of the range in the document that will be replaced by
* applying this template
* @since 3.1
*/
protected final int getReplaceOffset() {
int start;
if (fContext instanceof DocumentTemplateContext) {
DocumentTemplateContext docContext = fContext;
start = docContext.getStart();
} else {
start = fRegion.getOffset();
}
return start;
}

/**
* Returns the end offset of the range in the document that will be replaced
* by applying this template.
*
* @return the end offset of the range in the document that will be replaced
* by applying this template
* @since 3.1
*/
protected final int getReplaceEndOffset() {
int end;
if (fContext instanceof DocumentTemplateContext) {
DocumentTemplateContext docContext = fContext;
end = docContext.getEnd();
} else {
end = fRegion.getOffset() + fRegion.getLength();
}
return end;
}

public void apply(IDocument document) {
try {
fContext.setReadOnly(false);
int start;
TemplateBuffer templateBuffer;
try {
// this may already modify the document (e.g. add imports)
templateBuffer = fContext.evaluate(fTemplate);
} catch (TemplateException e1) {
return;
}

start = getReplaceOffset();
int end = getReplaceEndOffset();

// insert template string
String templateString = templateBuffer.getString();
document.replace(start, end - start, templateString);
} catch (Exception e) {
Log.log(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@
import org.python.pydev.core.MisconfigurationException;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.proposals.CompletionProposalFactory;
import org.python.pydev.editor.codecompletion.PyTemplateProposal;
import org.python.pydev.parser.PyParser;
import org.python.pydev.shared_core.image.IImageHandle;

import com.python.pydev.analysis.refactoring.tdd.AbstractPyCreateAction;
import com.python.pydev.analysis.refactoring.tdd.TemplateInfo;

/**
* This is the proposal that goes outside. It only creates the proposal that'll actually do something later, as
* creating that proposal may be slower.
*/
public final class TddRefactorCompletion extends AbstractTddRefactorCompletion {
private TemplateProposal executed;
private PyTemplateProposal executed;
private int locationStrategy;
private List<String> parametersAfterCall;
private AbstractPyCreateAction pyCreateAction;
private PySelection ps;
private TemplateInfo templateInfo;

TddRefactorCompletion(String replacementString, IImageHandle image, String displayString,
IContextInformation contextInformation, String additionalProposalInfo, int priority, IPyEdit edit,
Expand Down Expand Up @@ -67,6 +71,12 @@ public Point getSelection(IDocument document) {
return null;
}

public PyTemplateProposal convertToTemplateProposal(TemplateInfo templateInfo) {
return (PyTemplateProposal) CompletionProposalFactory.get().createPyTemplateProposal(templateInfo.fTemplate,
templateInfo.fContext,
templateInfo.fRegion, null, 0);
}

@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
if (edit != null) {
Expand All @@ -76,27 +86,35 @@ public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
parser.reparseDocument();
}
}
TemplateProposal executed2 = getAsTemplateProposal();
PyTemplateProposal executed2 = getAsTemplateProposal();
if (executed2 != null) {
executed2.apply(viewer, trigger, stateMask, 0);
forceReparseInBaseEditorAnd();
}
}

public TemplateProposal getAsTemplateProposal() {
public PyTemplateProposal getAsTemplateProposal() {
if (executed == null) {
pyCreateAction.setActiveEditor(edit);
try {
RefactoringInfo refactoringInfo = new RefactoringInfo(edit, ps.getTextSelection());
executed = (TemplateProposal) pyCreateAction.createProposal(refactoringInfo, this.fReplacementString,
this.locationStrategy, parametersAfterCall);
TemplateInfo templateInfo = getAsTemplateInfo();
executed = convertToTemplateProposal(templateInfo);
} catch (MisconfigurationException e) {
Log.log(e);
}
}
return executed;
}

public TemplateInfo getAsTemplateInfo() throws MisconfigurationException {
if (templateInfo == null) {
pyCreateAction.setActiveEditor(edit);
RefactoringInfo refactoringInfo = new RefactoringInfo(edit, ps.getTextSelection());
templateInfo = pyCreateAction.createProposal(refactoringInfo, this.fReplacementString,
this.locationStrategy, parametersAfterCall);
}
return templateInfo;
}

@Override
public void selected(ITextViewer viewer, boolean smartToggle) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import org.python.pydev.ast.refactoring.RefactoringInfo;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.log.Log;
import org.python.pydev.core.proposals.CompletionProposalFactory;
import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle;
import org.python.pydev.shared_core.structure.Tuple;

import com.python.pydev.analysis.refactoring.tdd.AbstractPyCreateClassOrMethodOrField;
import com.python.pydev.analysis.refactoring.tdd.TemplateInfo;

public class ExecutePyCreate {

Expand Down Expand Up @@ -61,9 +63,12 @@ public static void execute(AbstractPyCreateClassOrMethodOrField action, Refactor
String actTok, List<String> parametersAfterCall,
int locationStrategy) {
try {
ICompletionProposalHandle proposal = action.createProposal(refactoringInfo, actTok, locationStrategy,
TemplateInfo templateInfo = action.createProposal(refactoringInfo, actTok, locationStrategy,
parametersAfterCall);
if (proposal != null) {
if (templateInfo != null) {
ICompletionProposalHandle proposal = CompletionProposalFactory.get()
.createPyTemplateProposal(templateInfo.fTemplate, templateInfo.fContext, templateInfo.fRegion,
null, 0);
if (proposal instanceof ICompletionProposalExtension2) {
ICompletionProposalExtension2 extension2 = (ICompletionProposalExtension2) proposal;
extension2.apply(null, '\n', 0, 0);
Expand Down
Loading

0 comments on commit 356fab6

Please sign in to comment.