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 028f450 commit eb0e094
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,14 @@ protected TemplateInfo createProposal(PySelection pySelection, String source,
//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);
PyDocumentTemplateContext context;
if (targetEditor != null) {
context = PyDocumentTemplateContext.createContextWithCursor(targetEditor, region,
indent);
} else {
context = PyDocumentTemplateContext.createContextWithCursor(targetEditor, pySelection.getDoc(), region,
indent);
}

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 @@ -8,6 +8,7 @@
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.shared_core.code_completion.ICompletionProposalHandle;
import org.python.pydev.shared_core.structure.Tuple;

Expand Down Expand Up @@ -69,7 +70,9 @@ public static void execute(AbstractPyCreateClassOrMethodOrField action, Refactor
ICompletionProposalHandle proposal = CompletionProposalFactory.get()
.createPyTemplateProposal(templateInfo.fTemplate, templateInfo.fContext, templateInfo.fRegion,
null, 0);
if (proposal instanceof ICompletionProposalExtension2) {
if (proposal instanceof PyTemplateProposal) {
((PyTemplateProposal) proposal).getAsTemplateInfo().apply(refactoringInfo.getDocument());
} else if (proposal instanceof ICompletionProposalExtension2) {
ICompletionProposalExtension2 extension2 = (ICompletionProposalExtension2) proposal;
extension2.apply(null, '\n', 0, 0);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.python.pydev.core.IGrammarVersionProvider;
import org.python.pydev.core.MisconfigurationException;
import org.python.pydev.core.TestCaseUtils;
import org.python.pydev.core.autoedit.DefaultIndentPrefs;
import org.python.pydev.core.autoedit.TestIndentPrefs;
import org.python.pydev.core.proposals.CompletionProposalFactory;
import org.python.pydev.editor.codecompletion.proposals.DefaultCompletionProposalFactory;
import org.python.pydev.shared_core.string.CoreTextSelection;
Expand Down Expand Up @@ -74,7 +76,7 @@ public void testPyCreateMethodGlobal() {

assertContentsEqual("" +
"def MyMethod():\n" +
" ${pass}${cursor}\n" +
" pass\n" +
"\n" +
"\n" +
"MyMethod()" +
Expand All @@ -93,8 +95,8 @@ public void testPyCreateMethodGlobalParams() {
ExecutePyCreate.execute(pyCreateMethod, info, AbstractPyCreateAction.LOCATION_STRATEGY_BEFORE_CURRENT);

assertContentsEqual("" +
"def MyMethod(${a}, ${b}):\n" +
" ${pass}${cursor}\n" +
"def MyMethod(a, b):\n" +
" pass\n" +
"\n" +
"\n"
+
Expand All @@ -116,7 +118,7 @@ public void testPyCreateMethodGlobal1() {
"a = MyMethod()\n" +
"\n" +
"def MyMethod():\n" +
" ${pass}${cursor}\n" +
" pass\n" +
"\n"
+
"\n" +
Expand All @@ -136,7 +138,7 @@ public void testPyCreateMethodInEmptyDoc() {

assertContentsEqual("" +
"def MyMethod():\n" +
" ${pass}${cursor}\n" +
" pass\n" +
"\n" +
"\n" +
"", document.get());
Expand All @@ -147,7 +149,7 @@ public void testPyCreateMethodInEmptyDoc() {

assertContentsEqual("" +
"def MyMethod2():\n" +
" ${pass}${cursor}\n" +
" pass\n" +
"\n" +
"\n" +
"", document.get());
Expand Down Expand Up @@ -178,8 +180,8 @@ public void testPyCreateMethodInClass() {
" \n"
+
" @classmethod\n" +
" def MyMethod(cls, ${a}, ${b}):\n" +
" ${pass}${cursor}\n"
" def MyMethod(cls, a, b):\n" +
" pass\n"
+
" \n" +
" \n" +
Expand Down Expand Up @@ -210,7 +212,7 @@ public void testPyCreateMethodInSelfWithDecorator() {
" \n" +
" def m2(self):\n"
+
" ${pass}${cursor}\n" +
" pass\n" +
" \n" +
" \n" +
" @decorator\n" +
Expand Down Expand Up @@ -246,7 +248,7 @@ public void testPyCreateMethod() {
"\n" +
" def m2(self):\n"
+
" ${pass}${cursor}\n" +
" pass\n" +
" \n" +
" \n" +
" def m1(self):\n" +
Expand All @@ -256,35 +258,42 @@ public void testPyCreateMethod() {
}

public void testPyCreateMethodWithTabs() {
PyCreateMethodOrField pyCreateMethod = new PyCreateMethodOrField();

String source = "" +
"class A(object):\n" +
"\n" +
"\n" +
"\n" +
"\tdef m1(self):\n" +
"\t\tself.m2()";
IDocument document = new Document(source);
ICoreTextSelection selection = new CoreTextSelection(document, document.getLength() - "2()".length(), 0);
RefactoringInfo info = new RefactoringInfo(document, selection, PY_27_ONLY_GRAMMAR_VERSION_PROVIDER);

pyCreateMethod.setCreateInClass("A");
pyCreateMethod.setCreateAs(PyCreateMethodOrField.BOUND_METHOD);
ExecutePyCreate.execute(pyCreateMethod, info, AbstractPyCreateAction.LOCATION_STRATEGY_BEFORE_CURRENT);

String expected = "" +
"class A(object):\n" +
"\n" +
"\n" +
"\n" +
"\tdef m2(self):\n"
+
"\t\t${pass}${cursor}\n" +
"\t\n" +
"\t\n" +
"\tdef m1(self):\n" +
"\t\tself.m2()";
DefaultIndentPrefs.set(new TestIndentPrefs(false, 4));
IDocument document;
String expected;
try {
PyCreateMethodOrField pyCreateMethod = new PyCreateMethodOrField();

String source = "" +
"class A(object):\n" +
"\n" +
"\n" +
"\n" +
"\tdef m1(self):\n" +
"\t\tself.m2()";
document = new Document(source);
ICoreTextSelection selection = new CoreTextSelection(document, document.getLength() - "2()".length(), 0);
RefactoringInfo info = new RefactoringInfo(document, selection, PY_27_ONLY_GRAMMAR_VERSION_PROVIDER);

pyCreateMethod.setCreateInClass("A");
pyCreateMethod.setCreateAs(PyCreateMethodOrField.BOUND_METHOD);
ExecutePyCreate.execute(pyCreateMethod, info, AbstractPyCreateAction.LOCATION_STRATEGY_BEFORE_CURRENT);

expected = "" +
"class A(object):\n" +
"\n" +
"\n" +
"\n" +
"\tdef m2(self):\n"
+
"\t\tpass\n" +
"\t\n" +
"\t\n" +
"\tdef m1(self):\n" +
"\t\tself.m2()";
} finally {
DefaultIndentPrefs.set(null);
}

assertContentsEqual(expected, document.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.python.pydev.core.IPythonNature;
import org.python.pydev.core.ISourceViewerForTemplates;
import org.python.pydev.core.MisconfigurationException;
import org.python.pydev.core.autoedit.DefaultIndentPrefs;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.interactive_console.IScriptConsoleViewer;
import org.python.pydev.parser.fastparser.FastParser;
Expand Down Expand Up @@ -49,7 +50,8 @@ public PyDocumentTemplateContext(TemplateContextType type, IDocument document, i

public PyDocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length,
String indentTo, ISourceViewerForTemplates viewer) {
this(type, document, offset, length, indentTo, viewer.getIndentPrefs());
this(type, document, offset, length, indentTo,
viewer != null ? viewer.getIndentPrefs() : DefaultIndentPrefs.get(null));
this.edit = viewer;
}

Expand Down Expand Up @@ -145,9 +147,8 @@ public String getModuleName() {
* @return a template context that can handle template insertion at the given location, or <code>null</code>
*/
public static PyDocumentTemplateContext createContext(final TemplateContextType contextType,
final ISourceViewerForTemplates edit, final IRegion region, String indentTo) {
final ISourceViewerForTemplates edit, IDocument document, final IRegion region, String indentTo) {
if (contextType != null) {
IDocument document = edit.getDocument();
return new PyDocumentTemplateContext(contextType, document, region.getOffset(), region.getLength(),
indentTo, edit);
}
Expand All @@ -161,7 +162,7 @@ public static PyDocumentTemplateContext createContext(final TemplateContextType
ICoreTextSelection textSelection = edit.getTextSelection();
PySelection selection = new PySelection(document, textSelection);
String indent = selection.getIndentationFromLine();
return PyDocumentTemplateContext.createContext(contextType, edit, region, indent);
return PyDocumentTemplateContext.createContext(contextType, edit, edit.getDocument(), region, indent);
}
return null;
}
Expand All @@ -170,14 +171,23 @@ public static PyDocumentTemplateContext createContextWithCursor(ISourceViewerFor
IRegion region, String indent) {
TemplateContextType contextType = new TemplateContextType();
contextType.addResolver(new GlobalTemplateVariables.Cursor()); //We do want the cursor thought.
return createContext(contextType, targetEditor, region, indent);
return createContext(contextType, targetEditor, targetEditor != null ? targetEditor.getDocument() : null,
region, indent);
}

public static PyDocumentTemplateContext createContextWithCursor(ISourceViewerForTemplates targetEditor,
IDocument document,
IRegion region, String indent) {
TemplateContextType contextType = new TemplateContextType();
contextType.addResolver(new GlobalTemplateVariables.Cursor()); //We do want the cursor thought.
return createContext(contextType, targetEditor, document, region, indent);
}

public static PyDocumentTemplateContext createContextWithDefaultResolvers(ISourceViewerForTemplates edit,
IRegion region, String indentTo) {
TemplateContextType contextType = new TemplateContextType();
PyAddTemplateResolvers.addDefaultResolvers(contextType);
return createContext(contextType, edit, region, indentTo);
return createContext(contextType, edit, edit != null ? edit.getDocument() : null, region, indentTo);
}

}

0 comments on commit eb0e094

Please sign in to comment.