From 182ee7ce16856bfe8c993800565e867a4ad78212 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Thu, 19 Sep 2024 09:39:16 -0300 Subject: [PATCH] Add `condition` to resolve as `True|False` in templates. --- .../ast/surround_with/AssistSurroundWith.java | 16 +++++++++------- .../core/templates/PyAddTemplateResolvers.java | 1 + .../pydev/core/templates/PyTemplatesDefault.java | 8 ++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/plugins/org.python.pydev.ast/src/org/python/pydev/ast/surround_with/AssistSurroundWith.java b/plugins/org.python.pydev.ast/src/org/python/pydev/ast/surround_with/AssistSurroundWith.java index 4374a68d4d..18c9d900eb 100644 --- a/plugins/org.python.pydev.ast/src/org/python/pydev/ast/surround_with/AssistSurroundWith.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/ast/surround_with/AssistSurroundWith.java @@ -13,7 +13,6 @@ import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IRegion; -import org.eclipse.jface.text.templates.DocumentTemplateContext; import org.eclipse.jface.text.templates.Template; import org.eclipse.jface.text.templates.TemplateContext; import org.eclipse.jface.text.templates.TemplateContextType; @@ -24,6 +23,7 @@ import org.python.pydev.core.docutils.PySelection; import org.python.pydev.core.proposals.CompletionProposalFactory; import org.python.pydev.core.templates.PyAddTemplateResolvers; +import org.python.pydev.core.templates.PyDocumentTemplateContext; import org.python.pydev.shared_core.code_completion.ICompletionProposalHandle; import org.python.pydev.shared_core.image.IImageCache; import org.python.pydev.shared_core.image.UIConstants; @@ -35,10 +35,12 @@ */ public class AssistSurroundWith implements IAssistProps { - protected TemplateContext createContext(IRegion region, IDocument document) { + protected TemplateContext createContext(IRegion region, IDocument document, IPythonNature nature) { TemplateContextType contextType = new TemplateContextType(); PyAddTemplateResolvers.addDefaultResolvers(contextType); - return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength()); + return new PyDocumentTemplateContext(contextType, document, region.getOffset(), region.getLength(), "", + DefaultIndentPrefs.get( + nature)); } /** @@ -106,7 +108,7 @@ public List getProps(PySelection ps, IImageCache imag //region IRegion region = ps.getRegion(); - TemplateContext context = createContext(region, ps.getDoc()); + TemplateContext context = createContext(region, ps.getDoc(), nature); //not static because we need the actual code. String[] replace0to3 = new String[] { startIndent, delimiter, surroundedCode, delimiter, startIndent, @@ -148,11 +150,11 @@ private ICompletionProposalHandle createProposal(PySelection ps, IImageCache ima public static final String[] SURROUND_WITH_COMPLETIONS = new String[] { "%stry:%s%s%s%sexcept ${Exception}${cursor}:%s%s%sraise", "try..except Exception", "%stry:%s%s%s%sexcept ${Exception} as e:%s%s%s${raise}${cursor}", "try..except Exception as e", - "%stry:%s%s%s%sfinally:%s%s%s${pass}", "try..finally", "%sif ${True}:%s%s%s%selse:%s%s%s${pass}", + "%stry:%s%s%s%sfinally:%s%s%s${pass}", "try..finally", "%sif ${condition}:%s%s%s%selse:%s%s%s${pass}", "if..else", - "%swhile ${True}:%s%s%s%s%s", "while", "%sfor ${item} in ${collection}:%s%s%s%s%s${cursor}", "for", - "%sif ${True}:%s%s%s%s%s${cursor}", "if", "%swith ${var}:%s%s%s%s%s${cursor}", "with", }; + "%swhile ${condition}:%s%s%s%s%s", "while", "%sfor ${item} in ${collection}:%s%s%s%s%s${cursor}", "for", + "%sif ${condition}:%s%s%s%s%s${cursor}", "if", "%swith ${var}:%s%s%s%s%s${cursor}", "with", }; /** * @see org.python.pydev.core.IAssistProps#isValid(org.python.pydev.core.docutils.PySelection) diff --git a/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyAddTemplateResolvers.java b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyAddTemplateResolvers.java index 1515e0e5cb..baf7d0ff41 100644 --- a/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyAddTemplateResolvers.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyAddTemplateResolvers.java @@ -30,6 +30,7 @@ public static void addDefaultResolvers(TemplateContextType ctx) { ctx.addResolver(PyTemplatesDefault.PreviousClassOrMethod()); ctx.addResolver(PyTemplatesDefault.NextClassOrMethod()); ctx.addResolver(PyTemplatesDefault.Superclass()); + ctx.addResolver(PyTemplatesDefault.Condition()); PyContextTypeVariables.addResolvers(ctx); } diff --git a/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyTemplatesDefault.java b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyTemplatesDefault.java index b41a0c2f42..9389a304d4 100644 --- a/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyTemplatesDefault.java +++ b/plugins/org.python.pydev.ast/src/org/python/pydev/core/templates/PyTemplatesDefault.java @@ -2,6 +2,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; @@ -286,4 +287,11 @@ public static TemplateVariableResolver Superclass() { }); } + public static TemplateVariableResolver Condition() { + return new PyTemplatesDefault.CallableTemplateVariableResolver("condition", "Condition to be matched", + (context) -> { + return Arrays.asList("True", "False"); + }); + } + }