From ae01bc4ebd72ebe42364ac2fa78109760e4071fb Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Thu, 18 Apr 2024 20:40:45 -0300 Subject: [PATCH] Starting to move templates from pytemplate_defaults.py to java code. --- .../jysrc/pytemplate_defaults.py | 22 +--- .../pydev/editor/templates/PyContextType.java | 7 +- .../editor/templates/PyTemplatesDefault.java | 105 ++++++++++++++++++ .../templates/TemplatesTest.java | 23 ++++ 4 files changed, 138 insertions(+), 19 deletions(-) create mode 100644 plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyTemplatesDefault.java create mode 100644 plugins/org.python.pydev/tests_completions/org/python/pydev/ast/codecompletion/templates/TemplatesTest.java diff --git a/plugins/org.python.pydev.jython/jysrc/pytemplate_defaults.py b/plugins/org.python.pydev.jython/jysrc/pytemplate_defaults.py index 7229579de7..4cecaab1b7 100644 --- a/plugins/org.python.pydev.jython/jysrc/pytemplate_defaults.py +++ b/plugins/org.python.pydev.jython/jysrc/pytemplate_defaults.py @@ -59,27 +59,18 @@ def _IsGrammar3(context): def GetISODate(context): return time.strftime("%Y-%m-%d") -template_helper.AddTemplateVariable(py_context_type, 'isodate', 'ISO-8601 Ymd date', GetISODate) - def GetISODateString1(context): return time.strftime("%Y-%m-%d %H:%M") -template_helper.AddTemplateVariable(py_context_type, 'isodatestr', 'ISO-8601 Ymd HM date', GetISODateString1) - def GetISODateString2(context): return time.strftime("%Y-%m-%d %H:%M:%S") -template_helper.AddTemplateVariable(py_context_type, 'isodatestr2', 'ISO-8601 Ymd HMS date', GetISODateString2) - - #=================================================================================================== # GetModuleName #=================================================================================================== def GetModuleName(context): return context.getModuleName() -template_helper.AddTemplateVariable(py_context_type, 'module', 'Current module', GetModuleName) - #=================================================================================================== # _GetCurrentASTPath @@ -113,11 +104,6 @@ def GetQualifiedNameScope(context): return ret -template_helper.AddTemplateVariable( - py_context_type, 'current_qualified_scope', 'Current qualified scope.', GetQualifiedNameScope) - - - #=================================================================================================== # _GetCurrentClassStmt #=================================================================================================== @@ -153,18 +139,18 @@ def GetCurrentClass(context): #=================================================================================================== def GetSelfOrCls(context): FunctionDef = context.getFunctionDefClass() # from org.python.pydev.parser.jython.ast import ClassDef - + FastParser = context.getFastParserClass() # from org.python.pydev.parser.fastparser import FastParser selection = _CreateSelection(context) - + node = FastParser.firstClassOrFunction(context.getDocument(), selection.getStartLineIndex(), False, False) if isinstance(node, FunctionDef): firstToken = selection.getFirstInsideParentesisTok(node.beginLine-1) if firstToken == 'cls': return 'cls' - + return 'self' - + template_helper.AddTemplateVariable(py_context_type, 'self_or_cls', 'Get `self` or `cls`', GetSelfOrCls) diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyContextType.java b/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyContextType.java index fc7d49575c..90d91a392e 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyContextType.java +++ b/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyContextType.java @@ -36,7 +36,7 @@ public class PyContextType extends TemplateContextType { private IPythonInterpreter interpreter; /** - * Creates a new XML context type. + * Creates a new XML context type. */ public PyContextType() { //Note: created twice because we have 2 registries: @@ -57,6 +57,11 @@ private void addGlobalResolvers() { addResolver(new GlobalTemplateVariables.Year()); addResolver(new GlobalTemplateVariables.Time()); addResolver(new GlobalTemplateVariables.User()); + addResolver(PyTemplatesDefault.IsoDate()); + addResolver(PyTemplatesDefault.IsoDate1()); + addResolver(PyTemplatesDefault.IsoDate2()); + addResolver(PyTemplatesDefault.ModuleName()); + addResolver(PyTemplatesDefault.QualifiedNameScope()); PyContextTypeVariables.addResolvers(this); HashMap locals = new HashMap(); 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 new file mode 100644 index 0000000000..35e2e3b1e4 --- /dev/null +++ b/plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyTemplatesDefault.java @@ -0,0 +1,105 @@ +package org.python.pydev.editor.templates; + +import java.text.SimpleDateFormat; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import org.eclipse.jface.text.templates.TemplateContext; +import org.python.pydev.core.docutils.PySelection; +import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext; +import org.python.pydev.parser.fastparser.FastParser; +import org.python.pydev.parser.jython.ast.stmtType; +import org.python.pydev.parser.visitors.NodeUtils; +import org.python.pydev.shared_core.callbacks.ICallback; +import org.python.pydev.shared_core.string.FastStringBuffer; + +public class PyTemplatesDefault { + + public static class CallableTemplateVariableResolver extends PyTemplateVariableResolver { + + private ICallback callable; + + public CallableTemplateVariableResolver(String type, String description, + ICallback callable) { + super(type, description); + this.callable = callable; + } + + @Override + public String[] resolveAll(TemplateContext context) { + String ret = this.callable.call((PyDocumentTemplateContext) context); + if (ret == null) { + return new String[0]; + } + return new String[] { ret }; + } + } + + public static CallableTemplateVariableResolver IsoDate() { + return new PyTemplatesDefault.CallableTemplateVariableResolver("isodate", "ISO-8601 Ymd date", (context) -> { + // in Python: time.strftime("%Y-%m-%d") + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + Date date = new Date(); + return formatter.format(date); + }); + } + + public static CallableTemplateVariableResolver IsoDate1() { + return new PyTemplatesDefault.CallableTemplateVariableResolver("isodatestr", "ISO-8601 Ymd HM date", + (context) -> { + // in Python: time.strftime("%Y-%m-%d %H:%M") + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + Date date = new Date(); + return formatter.format(date); + }); + } + + public static CallableTemplateVariableResolver IsoDate2() { + return new PyTemplatesDefault.CallableTemplateVariableResolver("isodatestr2", "ISO-8601 Ymd HM date", + (context) -> { + // in Python: time.strftime("%Y-%m-%d %H:%M:%S") + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date date = new Date(); + return formatter.format(date); + }); + } + + public static CallableTemplateVariableResolver ModuleName() { + return new PyTemplatesDefault.CallableTemplateVariableResolver("module", "Current module", + (context) -> { + return context.getModuleName(); + }); + } + + public static List getCurrentAstPath(PyDocumentTemplateContext context) { + return getCurrentAstPath(context, false); + } + + public static List getCurrentAstPath(PyDocumentTemplateContext context, boolean reverse) { + PySelection selection = context.createPySelection(); + List ret = FastParser.parseToKnowGloballyAccessiblePath(context.getDocument(), + selection.getStartLineIndex()); + if (reverse) { + Collections.reverse(ret); + } + return ret; + } + + public static CallableTemplateVariableResolver QualifiedNameScope() { + return new PyTemplatesDefault.CallableTemplateVariableResolver("current_qualified_scope", + "Current qualified scope", + (context) -> { + List stmts = getCurrentAstPath(context); + FastStringBuffer buf = new FastStringBuffer(); + for (stmtType stmt : stmts) { + if (!buf.isEmpty()) { + buf.append('.'); + } + buf.append(NodeUtils.getRepresentationString(stmt)); + } + return ""; + }); + } + +} diff --git a/plugins/org.python.pydev/tests_completions/org/python/pydev/ast/codecompletion/templates/TemplatesTest.java b/plugins/org.python.pydev/tests_completions/org/python/pydev/ast/codecompletion/templates/TemplatesTest.java new file mode 100644 index 0000000000..1f0aff803f --- /dev/null +++ b/plugins/org.python.pydev/tests_completions/org/python/pydev/ast/codecompletion/templates/TemplatesTest.java @@ -0,0 +1,23 @@ +package org.python.pydev.ast.codecompletion.templates; + +import org.python.pydev.editor.templates.PyTemplatesDefault; +import org.python.pydev.shared_core.string.StringUtils; + +import junit.framework.TestCase; + +public class TemplatesTest extends TestCase { + + public void testIsoDate() throws Exception { + String found = PyTemplatesDefault.IsoDate().resolveAll(null)[0]; + assertEquals(StringUtils.count(found, '-'), 2); + + found = PyTemplatesDefault.IsoDate1().resolveAll(null)[0]; + assertEquals(StringUtils.count(found, '-'), 2); + assertEquals(StringUtils.count(found, ':'), 1); + + found = PyTemplatesDefault.IsoDate2().resolveAll(null)[0]; + assertEquals(StringUtils.count(found, '-'), 2); + assertEquals(StringUtils.count(found, ':'), 2); + } + +}