|
| 1 | +package org.python.pydev.editor.templates; |
| 2 | + |
| 3 | +import java.text.SimpleDateFormat; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.Date; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.eclipse.jface.text.templates.TemplateContext; |
| 9 | +import org.python.pydev.core.docutils.PySelection; |
| 10 | +import org.python.pydev.editor.codecompletion.templates.PyDocumentTemplateContext; |
| 11 | +import org.python.pydev.parser.fastparser.FastParser; |
| 12 | +import org.python.pydev.parser.jython.ast.stmtType; |
| 13 | +import org.python.pydev.parser.visitors.NodeUtils; |
| 14 | +import org.python.pydev.shared_core.callbacks.ICallback; |
| 15 | +import org.python.pydev.shared_core.string.FastStringBuffer; |
| 16 | + |
| 17 | +public class PyTemplatesDefault { |
| 18 | + |
| 19 | + public static class CallableTemplateVariableResolver extends PyTemplateVariableResolver { |
| 20 | + |
| 21 | + private ICallback<String, PyDocumentTemplateContext> callable; |
| 22 | + |
| 23 | + public CallableTemplateVariableResolver(String type, String description, |
| 24 | + ICallback<String, PyDocumentTemplateContext> callable) { |
| 25 | + super(type, description); |
| 26 | + this.callable = callable; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public String[] resolveAll(TemplateContext context) { |
| 31 | + String ret = this.callable.call((PyDocumentTemplateContext) context); |
| 32 | + if (ret == null) { |
| 33 | + return new String[0]; |
| 34 | + } |
| 35 | + return new String[] { ret }; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public static CallableTemplateVariableResolver IsoDate() { |
| 40 | + return new PyTemplatesDefault.CallableTemplateVariableResolver("isodate", "ISO-8601 Ymd date", (context) -> { |
| 41 | + // in Python: time.strftime("%Y-%m-%d") |
| 42 | + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
| 43 | + Date date = new Date(); |
| 44 | + return formatter.format(date); |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + public static CallableTemplateVariableResolver IsoDate1() { |
| 49 | + return new PyTemplatesDefault.CallableTemplateVariableResolver("isodatestr", "ISO-8601 Ymd HM date", |
| 50 | + (context) -> { |
| 51 | + // in Python: time.strftime("%Y-%m-%d %H:%M") |
| 52 | + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| 53 | + Date date = new Date(); |
| 54 | + return formatter.format(date); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + public static CallableTemplateVariableResolver IsoDate2() { |
| 59 | + return new PyTemplatesDefault.CallableTemplateVariableResolver("isodatestr2", "ISO-8601 Ymd HM date", |
| 60 | + (context) -> { |
| 61 | + // in Python: time.strftime("%Y-%m-%d %H:%M:%S") |
| 62 | + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 63 | + Date date = new Date(); |
| 64 | + return formatter.format(date); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + public static CallableTemplateVariableResolver ModuleName() { |
| 69 | + return new PyTemplatesDefault.CallableTemplateVariableResolver("module", "Current module", |
| 70 | + (context) -> { |
| 71 | + return context.getModuleName(); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + public static List<stmtType> getCurrentAstPath(PyDocumentTemplateContext context) { |
| 76 | + return getCurrentAstPath(context, false); |
| 77 | + } |
| 78 | + |
| 79 | + public static List<stmtType> getCurrentAstPath(PyDocumentTemplateContext context, boolean reverse) { |
| 80 | + PySelection selection = context.createPySelection(); |
| 81 | + List<stmtType> ret = FastParser.parseToKnowGloballyAccessiblePath(context.getDocument(), |
| 82 | + selection.getStartLineIndex()); |
| 83 | + if (reverse) { |
| 84 | + Collections.reverse(ret); |
| 85 | + } |
| 86 | + return ret; |
| 87 | + } |
| 88 | + |
| 89 | + public static CallableTemplateVariableResolver QualifiedNameScope() { |
| 90 | + return new PyTemplatesDefault.CallableTemplateVariableResolver("current_qualified_scope", |
| 91 | + "Current qualified scope", |
| 92 | + (context) -> { |
| 93 | + List<stmtType> stmts = getCurrentAstPath(context); |
| 94 | + FastStringBuffer buf = new FastStringBuffer(); |
| 95 | + for (stmtType stmt : stmts) { |
| 96 | + if (!buf.isEmpty()) { |
| 97 | + buf.append('.'); |
| 98 | + } |
| 99 | + buf.append(NodeUtils.getRepresentationString(stmt)); |
| 100 | + } |
| 101 | + return ""; |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments