Skip to content

Commit

Permalink
Moved more template-related classes to org.python.pydev.ast.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Apr 20, 2024
1 parent 1904e1e commit e78310a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
package org.python.pydev.editor.templates;
package org.python.pydev.core.templates;

import org.eclipse.jface.text.templates.GlobalTemplateVariables;
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.python.pydev.core.IGrammarVersionProvider;
import org.python.pydev.core.templates.PyDocumentTemplateContext;
import org.python.pydev.shared_core.callbacks.ICallback;

public class PyAddTemplateResolvers {

public static void addDefaultResolvers(TemplateContextType ctx) {
ctx.addResolver(new GlobalTemplateVariables.Cursor());
ctx.addResolver(new GlobalTemplateVariables.WordSelection());
ctx.addResolver(new GlobalTemplateVariables.LineSelection());
ctx.addResolver(new GlobalTemplateVariables.Dollar());
ctx.addResolver(new GlobalTemplateVariables.Date());
ctx.addResolver(new GlobalTemplateVariables.Year());
ctx.addResolver(new GlobalTemplateVariables.Time());
ctx.addResolver(new GlobalTemplateVariables.User());
ctx.addResolver(PyTemplatesDefault.IsoDate());
ctx.addResolver(PyTemplatesDefault.IsoDate1());
ctx.addResolver(PyTemplatesDefault.IsoDate2());
ctx.addResolver(PyTemplatesDefault.ModuleName());
ctx.addResolver(PyTemplatesDefault.QualifiedNameScope());
ctx.addResolver(PyTemplatesDefault.CurrentClass());
ctx.addResolver(PyTemplatesDefault.SelfOrCls());
ctx.addResolver(PyTemplatesDefault.PydevdFileLocation());
ctx.addResolver(PyTemplatesDefault.PydevdDirLocation());
ctx.addResolver(PyTemplatesDefault.CurrentMethod());
ctx.addResolver(PyTemplatesDefault.PreviousClassOrMethod());
ctx.addResolver(PyTemplatesDefault.NextClassOrMethod());
ctx.addResolver(PyTemplatesDefault.Superclass());

PyContextTypeVariables.addResolvers(ctx);
}
}

class CallableTemplateVariableResolver extends PyTemplateVariableResolver {

private ICallback<String, PyDocumentTemplateContext> callable;
Expand All @@ -25,7 +55,7 @@ protected String[] resolveAll(TemplateContext context) {
}
};

public class PyContextTypeVariables {
class PyContextTypeVariables {

private static boolean isGrammar3(PyDocumentTemplateContext context) {
if (context == null) {
Expand All @@ -34,7 +64,7 @@ private static boolean isGrammar3(PyDocumentTemplateContext context) {
return context.getGrammarVersion() >= IGrammarVersionProvider.GRAMMAR_PYTHON_VERSION_3_5;
}

public static void addResolvers(PyContextType pyContextType) {
public static void addResolvers(TemplateContextType pyContextType) {

pyContextType.addResolver(new CallableTemplateVariableResolver("file", "Full path for file", (context) -> {
return context.getEditorFile().toString().replace("\\", "/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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.templates;
package org.python.pydev.core.templates;

import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateVariableResolver;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.python.pydev.editor.templates;
package org.python.pydev.core.templates;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -14,7 +14,6 @@
import org.python.pydev.core.CorePlugin;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.log.Log;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

import java.util.HashMap;

import org.eclipse.jface.text.templates.GlobalTemplateVariables;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.python.pydev.core.templates.PyAddTemplateResolvers;
import org.python.pydev.jython.JythonPlugin;
import org.python.pydev.shared_core.jython.IPythonInterpreter;

Expand Down Expand Up @@ -44,37 +44,10 @@ public PyContextType() {
//fRegistry.addContextType(PyContextType.PY_MODULES_CONTEXT_TYPE);

interpreter = JythonPlugin.newPythonInterpreter();
addGlobalResolvers();

}

private void addGlobalResolvers() {
addResolver(new GlobalTemplateVariables.Cursor());
addResolver(new GlobalTemplateVariables.WordSelection());
addResolver(new GlobalTemplateVariables.LineSelection());
addResolver(new GlobalTemplateVariables.Dollar());
addResolver(new GlobalTemplateVariables.Date());
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());
addResolver(PyTemplatesDefault.CurrentClass());
addResolver(PyTemplatesDefault.SelfOrCls());
addResolver(PyTemplatesDefault.PydevdFileLocation());
addResolver(PyTemplatesDefault.PydevdDirLocation());
addResolver(PyTemplatesDefault.CurrentMethod());
addResolver(PyTemplatesDefault.PreviousClassOrMethod());
addResolver(PyTemplatesDefault.NextClassOrMethod());
addResolver(PyTemplatesDefault.Superclass());

PyContextTypeVariables.addResolvers(this);
PyAddTemplateResolvers.addDefaultResolvers(this);
HashMap<String, Object> locals = new HashMap<String, Object>();
locals.put("py_context_type", this);

//execute all the files that start with 'pytemplate' that are located beneath
//the org.python.pydev.jython/jysrc directory and some user specified dir (if any).
JythonPlugin.execAll(locals, "pytemplate", interpreter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.python.pydev.ast.codecompletion.templates;

import org.python.pydev.editor.templates.PyTemplatesDefault;
import org.python.pydev.core.templates.PyTemplatesDefault;
import org.python.pydev.shared_core.string.StringUtils;

import junit.framework.TestCase;
Expand Down

0 comments on commit e78310a

Please sign in to comment.