From eb0e094de0eb327362d453a89ae0ef31c9b681b7 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Sun, 29 Sep 2024 10:12:03 -0300 Subject: [PATCH] wip --- .../AbstractPyCreateClassOrMethodOrField.java | 10 ++- .../refactoring/tdd/ExecutePyCreate.java | 5 +- .../refactoring/tdd/PyCreateMethodTest.java | 87 ++++++++++--------- .../templates/PyDocumentTemplateContext.java | 22 +++-- 4 files changed, 76 insertions(+), 48 deletions(-) diff --git a/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/tdd/AbstractPyCreateClassOrMethodOrField.java b/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/tdd/AbstractPyCreateClassOrMethodOrField.java index ce39360505..e1be76a5c8 100644 --- a/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/tdd/AbstractPyCreateClassOrMethodOrField.java +++ b/plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/tdd/AbstractPyCreateClassOrMethodOrField.java @@ -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); diff --git a/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/ExecutePyCreate.java b/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/ExecutePyCreate.java index 5550220497..149b614263 100644 --- a/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/ExecutePyCreate.java +++ b/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/ExecutePyCreate.java @@ -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; @@ -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 { diff --git a/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/PyCreateMethodTest.java b/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/PyCreateMethodTest.java index 6175de1561..c50a310f40 100644 --- a/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/PyCreateMethodTest.java +++ b/plugins/com.python.pydev.refactoring/tests/com/python/pydev/refactoring/tdd/PyCreateMethodTest.java @@ -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; @@ -74,7 +76,7 @@ public void testPyCreateMethodGlobal() { assertContentsEqual("" + "def MyMethod():\n" + - " ${pass}${cursor}\n" + + " pass\n" + "\n" + "\n" + "MyMethod()" + @@ -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" + @@ -116,7 +118,7 @@ public void testPyCreateMethodGlobal1() { "a = MyMethod()\n" + "\n" + "def MyMethod():\n" + - " ${pass}${cursor}\n" + + " pass\n" + "\n" + "\n" + @@ -136,7 +138,7 @@ public void testPyCreateMethodInEmptyDoc() { assertContentsEqual("" + "def MyMethod():\n" + - " ${pass}${cursor}\n" + + " pass\n" + "\n" + "\n" + "", document.get()); @@ -147,7 +149,7 @@ public void testPyCreateMethodInEmptyDoc() { assertContentsEqual("" + "def MyMethod2():\n" + - " ${pass}${cursor}\n" + + " pass\n" + "\n" + "\n" + "", document.get()); @@ -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" + @@ -210,7 +212,7 @@ public void testPyCreateMethodInSelfWithDecorator() { " \n" + " def m2(self):\n" + - " ${pass}${cursor}\n" + + " pass\n" + " \n" + " \n" + " @decorator\n" + @@ -246,7 +248,7 @@ public void testPyCreateMethod() { "\n" + " def m2(self):\n" + - " ${pass}${cursor}\n" + + " pass\n" + " \n" + " \n" + " def m1(self):\n" + @@ -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()); } diff --git a/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyDocumentTemplateContext.java b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyDocumentTemplateContext.java index 292e0e026f..ce633b7288 100644 --- a/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyDocumentTemplateContext.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyDocumentTemplateContext.java @@ -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; @@ -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; } @@ -145,9 +147,8 @@ public String getModuleName() { * @return a template context that can handle template insertion at the given location, or null */ 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); } @@ -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; } @@ -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); } } \ No newline at end of file