From 1904e1e2c0e9f029f7fbc174d1391c8c83081fb8 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Sat, 20 Apr 2024 10:55:37 -0300 Subject: [PATCH] Moving template-related classes which are not ui-related to org.python.pydev.ast. --- .../AbstractPyCreateClassOrMethodOrField.java | 4 ++-- .../org.python.pydev.ast/META-INF/MANIFEST.MF | 1 + ...ractDocumentTemplateContextWithIndent.java | 2 +- .../DocumentTemplateContextWithIndent.java | 3 +-- .../templates/PyDocumentTemplateContext.java | 23 +++++++++---------- .../PyTemplateCompletionProcessor.java | 14 +++++++++-- .../templates/PyContextTypeVariables.java | 2 +- .../editor/templates/PyTemplatesDefault.java | 2 +- .../ui/wizards/files/PythonModuleWizard.java | 16 ++++++------- .../PyDocumentTemplateContextTest.java | 2 +- 10 files changed, 39 insertions(+), 30 deletions(-) rename plugins/{org.python.pydev.shared_ui/src/org/python/pydev/shared_ui => org.python.pydev.ast/src/org/python/pydev/core}/templates/AbstractDocumentTemplateContextWithIndent.java (99%) rename plugins/{org.python.pydev/src/org/python/pydev/editor/codecompletion => org.python.pydev.ast/src/org/python/pydev/core}/templates/DocumentTemplateContextWithIndent.java (88%) rename plugins/{org.python.pydev/src/org/python/pydev/editor/codecompletion => org.python.pydev.ast/src/org/python/pydev/core}/templates/PyDocumentTemplateContext.java (88%) diff --git a/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/AbstractPyCreateClassOrMethodOrField.java b/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/AbstractPyCreateClassOrMethodOrField.java index 6f619b2b51..446e3d3c79 100644 --- a/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/AbstractPyCreateClassOrMethodOrField.java +++ b/plugins/com.python.pydev.refactoring/src/com/python/pydev/refactoring/tdd/AbstractPyCreateClassOrMethodOrField.java @@ -22,8 +22,8 @@ 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.editor.PyEdit; -import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext; import org.python.pydev.editor.correctionassist.heuristics.AssistAssign; import org.python.pydev.parser.jython.ast.ClassDef; import org.python.pydev.parser.jython.ast.Pass; @@ -179,7 +179,7 @@ protected ICompletionProposalHandle createProposal(PySelection pySelection, Stri TemplateContextType contextType = new TemplateContextType(); contextType.addResolver(new GlobalTemplateVariables.Cursor()); //We do want the cursor thought. PyDocumentTemplateContext context = PyDocumentTemplateContext.createContext(contextType, - ((PyEdit) targetEditor).getPySourceViewer(), region, indent); + targetEditor, region, indent); Template template = new Template("Create " + creationStr, "Create " + creationStr, "", source, true); ICompletionProposalHandle templateProposal = CompletionProposalFactory.get() diff --git a/plugins/org.python.pydev.ast/META-INF/MANIFEST.MF b/plugins/org.python.pydev.ast/META-INF/MANIFEST.MF index 1918b03b74..02225f3a89 100644 --- a/plugins/org.python.pydev.ast/META-INF/MANIFEST.MF +++ b/plugins/org.python.pydev.ast/META-INF/MANIFEST.MF @@ -33,6 +33,7 @@ Export-Package: org.python.pydev.ast, org.python.pydev.ast.runners, org.python.pydev.ast.simpleassist, org.python.pydev.core.pep8, + org.python.pydev.core.templates, org.python.pydev.plugin, org.python.pydev.plugin.nature Bundle-Vendor: Brainwy Software Ltda diff --git a/plugins/org.python.pydev.shared_ui/src/org/python/pydev/shared_ui/templates/AbstractDocumentTemplateContextWithIndent.java b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/AbstractDocumentTemplateContextWithIndent.java similarity index 99% rename from plugins/org.python.pydev.shared_ui/src/org/python/pydev/shared_ui/templates/AbstractDocumentTemplateContextWithIndent.java rename to plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/AbstractDocumentTemplateContextWithIndent.java index 02234a993e..7e1971ede2 100644 --- a/plugins/org.python.pydev.shared_ui/src/org/python/pydev/shared_ui/templates/AbstractDocumentTemplateContextWithIndent.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/AbstractDocumentTemplateContextWithIndent.java @@ -9,7 +9,7 @@ * Contributors: * Fabio Zadrozny - initial API and implementation ******************************************************************************/ -package org.python.pydev.shared_ui.templates; +package org.python.pydev.core.templates; import java.util.List; diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/templates/DocumentTemplateContextWithIndent.java b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/DocumentTemplateContextWithIndent.java similarity index 88% rename from plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/templates/DocumentTemplateContextWithIndent.java rename to plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/DocumentTemplateContextWithIndent.java index 52d64e6ea6..f92bd2dc88 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/templates/DocumentTemplateContextWithIndent.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/DocumentTemplateContextWithIndent.java @@ -4,12 +4,11 @@ * Please see the license.txt included with this distribution for details. * Any modifications to this file must keep this entire header intact. */ -package org.python.pydev.editor.codecompletion.templates; +package org.python.pydev.core.templates; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.templates.TemplateContextType; import org.python.pydev.core.IIndentPrefs; -import org.python.pydev.shared_ui.templates.AbstractDocumentTemplateContextWithIndent; public class DocumentTemplateContextWithIndent extends AbstractDocumentTemplateContextWithIndent { diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/templates/PyDocumentTemplateContext.java b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyDocumentTemplateContext.java similarity index 88% rename from plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/templates/PyDocumentTemplateContext.java rename to plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyDocumentTemplateContext.java index 470fd173a9..218c1c87aa 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/templates/PyDocumentTemplateContext.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyDocumentTemplateContext.java @@ -4,15 +4,13 @@ * Please see the license.txt included with this distribution for details. * Any modifications to this file must keep this entire header intact. */ -package org.python.pydev.editor.codecompletion.templates; +package org.python.pydev.core.templates; import java.io.File; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; -import org.eclipse.jface.text.ITextSelection; -import org.eclipse.jface.text.ITextViewer; import org.eclipse.jface.text.templates.TemplateContextType; import org.python.pydev.core.IGrammarVersionProvider; import org.python.pydev.core.IIndentPrefs; @@ -28,6 +26,7 @@ import org.python.pydev.parser.jython.ast.ClassDef; import org.python.pydev.parser.jython.ast.FunctionDef; import org.python.pydev.parser.visitors.NodeUtils; +import org.python.pydev.shared_core.string.ICoreTextSelection; /** * Makes a custom evaluation of the template buffer to be created (to put it in the correct indentation and @@ -37,18 +36,18 @@ */ public final class PyDocumentTemplateContext extends DocumentTemplateContextWithIndent { - public ITextViewer viewer; //May be null + public IPyEdit viewer; //May be null /** - * Note that it's in the default context because it should be used on subclasses. + * This constructor is meant for tests! */ - /*default*/ PyDocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length, + public PyDocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length, String indentTo, IIndentPrefs indentPrefs) { super(type, document, offset, length, indentTo, indentPrefs); } public PyDocumentTemplateContext(TemplateContextType type, IDocument document, int offset, int length, - String indentTo, ITextViewer viewer) { + String indentTo, IPyEdit viewer) { this(type, document, offset, length, indentTo, getIndentPrefs(viewer)); this.viewer = viewer; } @@ -145,7 +144,7 @@ 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 ITextViewer viewer, final IRegion region, String indent) { + final IPyEdit viewer, final IRegion region, String indent) { if (contextType != null) { IDocument document = viewer.getDocument(); final String indentTo = indent; @@ -156,11 +155,11 @@ public static PyDocumentTemplateContext createContext(final TemplateContextType } public static PyDocumentTemplateContext createContext(final TemplateContextType contextType, - final ITextViewer viewer, final IRegion region) { + final IPyEdit viewer, final IRegion region) { if (contextType != null) { IDocument document = viewer.getDocument(); - PySelection selection = new PySelection(document, - ((ITextSelection) viewer.getSelectionProvider().getSelection()).getOffset()); + ICoreTextSelection textSelection = viewer.getTextSelection(); + PySelection selection = new PySelection(document, textSelection); String indent = selection.getIndentationFromLine(); return PyDocumentTemplateContext.createContext(contextType, viewer, region, indent); } @@ -170,7 +169,7 @@ public static PyDocumentTemplateContext createContext(final TemplateContextType /** * @return the indent preferences to be used. */ - private static IIndentPrefs getIndentPrefs(ITextViewer viewer) { + private static IIndentPrefs getIndentPrefs(IPyEdit viewer) { if (viewer instanceof IPySourceViewer) { IPySourceViewer pyViewer = (IPySourceViewer) viewer; return pyViewer.getEdit().getIndentPrefs(); diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/PyTemplateCompletionProcessor.java b/plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/PyTemplateCompletionProcessor.java index ae8042c0bb..8938067f6b 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/PyTemplateCompletionProcessor.java +++ b/plugins/org.python.pydev/src/org/python/pydev/editor/codecompletion/PyTemplateCompletionProcessor.java @@ -11,8 +11,11 @@ import org.eclipse.jface.text.templates.TemplateContext; import org.eclipse.jface.text.templates.TemplateContextType; import org.eclipse.swt.graphics.Image; +import org.python.pydev.core.IPySourceViewer; +import org.python.pydev.core.log.Log; import org.python.pydev.core.proposals.CompletionProposalFactory; -import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext; +import org.python.pydev.core.templates.PyDocumentTemplateContext; +import org.python.pydev.editor.codefolding.PySourceViewer; import org.python.pydev.editor.templates.PyContextType; import org.python.pydev.editor.templates.TemplateHelper; import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle; @@ -105,7 +108,14 @@ protected ICompletionProposal createProposal(Template template, TemplateContext @Override protected TemplateContext createContext(final ITextViewer viewer, final IRegion region) { TemplateContextType contextType = getContextType(viewer, region); - return PyDocumentTemplateContext.createContext(contextType, viewer, region); + if (viewer instanceof IPySourceViewer) { + PySourceViewer pySourceViewer = (PySourceViewer) viewer; + + return PyDocumentTemplateContext.createContext(contextType, pySourceViewer.getEdit(), region); + } + Log.log("Expected an IPySourceViewer. Found: " + viewer); + return PyDocumentTemplateContext.createContext(contextType, null, region); + } } diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyContextTypeVariables.java b/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyContextTypeVariables.java index fcd402ce8b..f014ee9245 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyContextTypeVariables.java +++ b/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyContextTypeVariables.java @@ -2,7 +2,7 @@ import org.eclipse.jface.text.templates.TemplateContext; import org.python.pydev.core.IGrammarVersionProvider; -import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext; +import org.python.pydev.core.templates.PyDocumentTemplateContext; import org.python.pydev.shared_core.callbacks.ICallback; class CallableTemplateVariableResolver extends PyTemplateVariableResolver { diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyTemplatesDefault.java b/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyTemplatesDefault.java index d4c73b68b1..153854fdfe 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyTemplatesDefault.java +++ b/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyTemplatesDefault.java @@ -14,7 +14,7 @@ import org.python.pydev.core.CorePlugin; import org.python.pydev.core.docutils.PySelection; import org.python.pydev.core.log.Log; -import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext; +import org.python.pydev.core.templates.PyDocumentTemplateContext; import org.python.pydev.parser.fastparser.FastParser; import org.python.pydev.parser.jython.ast.ClassDef; import org.python.pydev.parser.jython.ast.FunctionDef; diff --git a/plugins/org.python.pydev/src/org/python/pydev/ui/wizards/files/PythonModuleWizard.java b/plugins/org.python.pydev/src/org/python/pydev/ui/wizards/files/PythonModuleWizard.java index ff5510952b..054f4af7ae 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/ui/wizards/files/PythonModuleWizard.java +++ b/plugins/org.python.pydev/src/org/python/pydev/ui/wizards/files/PythonModuleWizard.java @@ -21,18 +21,18 @@ import org.eclipse.ui.IEditorPart; import org.python.pydev.core.log.Log; import org.python.pydev.core.preferences.FileTypesPreferences; +import org.python.pydev.core.templates.PyDocumentTemplateContext; import org.python.pydev.editor.PyEdit; -import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext; import org.python.pydev.editor.templates.PyContextType; import org.python.pydev.shared_ui.utils.RunInUiThread; /** * Python module creation wizard - * + * * TODO: Create initial file content from a comment templates - * + * * @author Mikko Ohtamaa - * + * */ public class PythonModuleWizard extends AbstractPythonWizard { @@ -70,9 +70,9 @@ protected String checkNameText(String text) { /** * We will create a new module (file) here given the source folder and the package specified (which - * are currently validated in the page) - * @param monitor - * @throws CoreException + * are currently validated in the page) + * @param monitor + * @throws CoreException */ @Override protected IFile doCreateNew(IProgressMonitor monitor) throws CoreException { @@ -133,7 +133,7 @@ public void run() { Region region = new Region(0, 0); PyDocumentTemplateContext context = PyDocumentTemplateContext.createContext(new PyContextType(), - pyEdit.getPySourceViewer(), region); + pyEdit, region); TemplateProposal templateProposal = new TemplateProposal(template, context, region, null); templateProposal.apply(pyEdit.getPySourceViewer(), '\n', 0, 0); diff --git a/plugins/org.python.pydev/tests_completions/org/python/pydev/editor/codecompletion/templates/PyDocumentTemplateContextTest.java b/plugins/org.python.pydev/tests_completions/org/python/pydev/editor/codecompletion/templates/PyDocumentTemplateContextTest.java index 3fea2207ca..e5b86c5357 100644 --- a/plugins/org.python.pydev/tests_completions/org/python/pydev/editor/codecompletion/templates/PyDocumentTemplateContextTest.java +++ b/plugins/org.python.pydev/tests_completions/org/python/pydev/editor/codecompletion/templates/PyDocumentTemplateContextTest.java @@ -13,7 +13,7 @@ import org.eclipse.jface.text.templates.TemplateBuffer; import org.eclipse.jface.text.templates.TemplateContextType; import org.python.pydev.core.autoedit.TestIndentPrefs; -import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext; +import org.python.pydev.core.templates.PyDocumentTemplateContext; public class PyDocumentTemplateContextTest extends TestCase {