Search in sources :

Example 1 with WrapWithCustomTemplateAction

use of com.intellij.codeInsight.template.impl.WrapWithCustomTemplateAction in project intellij-community by JetBrains.

the class SurroundWithHandler method doBuildSurroundActions.

@Nullable
private static List<AnAction> doBuildSurroundActions(Project project, Editor editor, PsiFile file, Map<Surrounder, PsiElement[]> surrounders) {
    final List<AnAction> applicable = new ArrayList<>();
    boolean hasEnabledSurrounders = false;
    Set<Character> usedMnemonicsSet = new HashSet<>();
    int index = 0;
    for (Map.Entry<Surrounder, PsiElement[]> entry : surrounders.entrySet()) {
        Surrounder surrounder = entry.getKey();
        PsiElement[] elements = entry.getValue();
        if (surrounder.isApplicable(elements)) {
            char mnemonic;
            if (index < 9) {
                mnemonic = (char) ('0' + index + 1);
            } else if (index == 9) {
                mnemonic = '0';
            } else {
                mnemonic = (char) ('A' + index - 10);
            }
            index++;
            usedMnemonicsSet.add(Character.toUpperCase(mnemonic));
            applicable.add(new InvokeSurrounderAction(surrounder, project, editor, elements, mnemonic));
            hasEnabledSurrounders = true;
        }
    }
    List<CustomLiveTemplate> customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true);
    List<TemplateImpl> templates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, true);
    if (!templates.isEmpty() || !customTemplates.isEmpty()) {
        applicable.add(new Separator("Live templates"));
    }
    for (TemplateImpl template : templates) {
        applicable.add(new InvokeTemplateAction(template, editor, project, usedMnemonicsSet));
        hasEnabledSurrounders = true;
    }
    for (CustomLiveTemplate customTemplate : customTemplates) {
        applicable.add(new WrapWithCustomTemplateAction(customTemplate, editor, file, usedMnemonicsSet));
        hasEnabledSurrounders = true;
    }
    if (!templates.isEmpty() || !customTemplates.isEmpty()) {
        applicable.add(Separator.getInstance());
        applicable.add(new ConfigureTemplatesAction());
    }
    return hasEnabledSurrounders ? applicable : null;
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) CustomLiveTemplate(com.intellij.codeInsight.template.CustomLiveTemplate) InvokeTemplateAction(com.intellij.codeInsight.template.impl.InvokeTemplateAction) WrapWithCustomTemplateAction(com.intellij.codeInsight.template.impl.WrapWithCustomTemplateAction) Surrounder(com.intellij.lang.surroundWith.Surrounder) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CustomLiveTemplate (com.intellij.codeInsight.template.CustomLiveTemplate)1 InvokeTemplateAction (com.intellij.codeInsight.template.impl.InvokeTemplateAction)1 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)1 WrapWithCustomTemplateAction (com.intellij.codeInsight.template.impl.WrapWithCustomTemplateAction)1 Surrounder (com.intellij.lang.surroundWith.Surrounder)1 PsiElement (com.intellij.psi.PsiElement)1 Nullable (org.jetbrains.annotations.Nullable)1