Search in sources :

Example 11 with TemplateImpl

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

the class IterateOverIterableIntention method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    final TemplateImpl template = getTemplate();
    if (template != null) {
        int offset = editor.getCaretModel().getOffset();
        int startOffset = offset;
        if (editor.getSelectionModel().hasSelection()) {
            final int selStart = editor.getSelectionModel().getSelectionStart();
            final int selEnd = editor.getSelectionModel().getSelectionEnd();
            startOffset = (offset == selStart) ? selEnd : selStart;
        }
        PsiElement element = file.findElementAt(startOffset);
        while (element instanceof PsiWhiteSpace) {
            element = element.getPrevSibling();
        }
        PsiStatement psiStatement = PsiTreeUtil.getParentOfType(element, PsiStatement.class, false);
        if (psiStatement != null) {
            startOffset = psiStatement.getTextRange().getStartOffset();
        }
        if (!template.isDeactivated() && (TemplateManagerImpl.isApplicable(file, offset, template) || (TemplateManagerImpl.isApplicable(file, startOffset, template)))) {
            return getIterableExpression(editor, file) != null;
        }
    }
    return false;
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl)

Example 12 with TemplateImpl

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

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

the class CustomTemplateCallback method getMatchingTemplates.

@NotNull
private static List<TemplateImpl> getMatchingTemplates(@NotNull String templateKey) {
    TemplateSettings settings = TemplateSettings.getInstance();
    List<TemplateImpl> candidates = new ArrayList<>();
    for (TemplateImpl template : settings.getTemplates(templateKey)) {
        if (!template.isDeactivated()) {
            candidates.add(template);
        }
    }
    return candidates;
}
Also used : TemplateSettings(com.intellij.codeInsight.template.impl.TemplateSettings) TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with TemplateImpl

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

Example 15 with TemplateImpl

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

the class CreateFromTemplateActionBase method startLiveTemplate.

public static void startLiveTemplate(@NotNull PsiFile file, @NotNull Map<String, String> defaultValues) {
    Editor editor = EditorHelper.openInEditor(file);
    if (editor == null)
        return;
    TemplateImpl template = new TemplateImpl("", file.getText(), "");
    template.setInline(true);
    int count = template.getSegmentsCount();
    if (count == 0)
        return;
    Set<String> variables = new HashSet<>();
    for (int i = 0; i < count; i++) {
        variables.add(template.getSegmentName(i));
    }
    variables.removeAll(TemplateImpl.INTERNAL_VARS_SET);
    for (String variable : variables) {
        String defaultValue = defaultValues.getOrDefault(variable, variable);
        template.addVariable(variable, null, '"' + defaultValue + '"', true);
    }
    Project project = file.getProject();
    WriteCommandAction.runWriteCommandAction(project, () -> editor.getDocument().setText(template.getTemplateText()));
    // ensures caret at the start of the template
    editor.getCaretModel().moveToOffset(0);
    TemplateManager.getInstance(project).startTemplate(editor, template);
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) Project(com.intellij.openapi.project.Project) Editor(com.intellij.openapi.editor.Editor)

Aggregations

TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)22 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (org.jetbrains.annotations.Nullable)6 TemplateToken (com.intellij.codeInsight.template.emmet.tokens.TemplateToken)4 XmlZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator)3 Editor (com.intellij.openapi.editor.Editor)3 ZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator)2 InvokeTemplateAction (com.intellij.codeInsight.template.impl.InvokeTemplateAction)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 Matcher (java.util.regex.Matcher)2 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)1 ExpressionLookupItem (com.intellij.codeInsight.lookup.ExpressionLookupItem)1 CustomLiveTemplate (com.intellij.codeInsight.template.CustomLiveTemplate)1 LiveTemplateBuilder (com.intellij.codeInsight.template.LiveTemplateBuilder)1 SmartCompletionContextType (com.intellij.codeInsight.template.SmartCompletionContextType)1 SingleLineEmmetFilter (com.intellij.codeInsight.template.emmet.filters.SingleLineEmmetFilter)1 ZenCodingFilter (com.intellij.codeInsight.template.emmet.filters.ZenCodingFilter)1 TemplateSettings (com.intellij.codeInsight.template.impl.TemplateSettings)1