Skip to content

Commit

Permalink
Add condition to resolve as True|False in templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Sep 19, 2024
1 parent 8ff9916 commit 182ee7c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -106,7 +108,7 @@ public List<ICompletionProposalHandle> 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,
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
});
}

}

0 comments on commit 182ee7c

Please sign in to comment.