Search in sources :

Example 1 with InvokeTemplateAction

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

the class IterateOverIterableIntention method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final TemplateImpl template = getTemplate();
    SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection()) {
        final PsiExpression iterableExpression = getIterableExpression(editor, file);
        LOG.assertTrue(iterableExpression != null);
        TextRange textRange = iterableExpression.getTextRange();
        selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
    }
    new InvokeTemplateAction(template, editor, project, new HashSet<>()).perform();
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) InvokeTemplateAction(com.intellij.codeInsight.template.impl.InvokeTemplateAction) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) HashSet(java.util.HashSet)

Example 2 with InvokeTemplateAction

use of com.intellij.codeInsight.template.impl.InvokeTemplateAction 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

InvokeTemplateAction (com.intellij.codeInsight.template.impl.InvokeTemplateAction)2 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)2 CustomLiveTemplate (com.intellij.codeInsight.template.CustomLiveTemplate)1 WrapWithCustomTemplateAction (com.intellij.codeInsight.template.impl.WrapWithCustomTemplateAction)1 Surrounder (com.intellij.lang.surroundWith.Surrounder)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 HashSet (java.util.HashSet)1 Nullable (org.jetbrains.annotations.Nullable)1