-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting to move templates from pytemplate_defaults.py to java code.
- Loading branch information
Showing
4 changed files
with
138 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
plugins/org.python.pydev/src/org/python/pydev/editor/templates/PyTemplatesDefault.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, PyDocumentTemplateContext> callable; | ||
|
||
public CallableTemplateVariableResolver(String type, String description, | ||
ICallback<String, PyDocumentTemplateContext> 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<stmtType> getCurrentAstPath(PyDocumentTemplateContext context) { | ||
return getCurrentAstPath(context, false); | ||
} | ||
|
||
public static List<stmtType> getCurrentAstPath(PyDocumentTemplateContext context, boolean reverse) { | ||
PySelection selection = context.createPySelection(); | ||
List<stmtType> 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<stmtType> stmts = getCurrentAstPath(context); | ||
FastStringBuffer buf = new FastStringBuffer(); | ||
for (stmtType stmt : stmts) { | ||
if (!buf.isEmpty()) { | ||
buf.append('.'); | ||
} | ||
buf.append(NodeUtils.getRepresentationString(stmt)); | ||
} | ||
return ""; | ||
}); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
....pydev/tests_completions/org/python/pydev/ast/codecompletion/templates/TemplatesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |