Search in sources :

Example 6 with TemplateImpl

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

the class EmmetParser method parseTemplate.

@Nullable
protected ZenCodingNode parseTemplate() {
    ZenCodingToken token = getToken();
    if (!(token instanceof IdentifierToken)) {
        return null;
    }
    String templateKey = ((IdentifierToken) token).getText();
    advance();
    TemplateImpl template = myCallback.findApplicableTemplate(templateKey);
    if (template == null && !ZenCodingUtil.isXML11ValidQName(templateKey)) {
        return null;
    }
    final TemplateToken templateToken = new TemplateToken(templateKey);
    if (!setTemplate(templateToken, template)) {
        return null;
    }
    return new TemplateNode(templateToken);
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with TemplateImpl

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

the class LiveTemplateBuilder method buildTemplate.

@NotNull
public TemplateImpl buildTemplate() {
    List<Variable> variables = getListWithLimit(myVariables);
    if (!findVarOccurence(TemplateImpl.END)) {
        if (myLastEndVarName == null) {
            for (Variable variable : variables) {
                if (isEndVariable(variable.getName())) {
                    myLastEndVarName = variable.getName();
                    break;
                }
            }
        }
        if (myLastEndVarName != null) {
            int endOffset = -1;
            if (myAddEndVariableAtTheEndOfTemplate) {
                endOffset = myText.length();
            } else {
                Iterator<VarOccurence> it = myVariableOccurrences.iterator();
                while (it.hasNext()) {
                    VarOccurence occurence = it.next();
                    if (occurence.myName.equals(myLastEndVarName)) {
                        endOffset = occurence.myOffset;
                        break;
                    }
                }
                if (endOffset >= 0) {
                    for (Iterator<Variable> it1 = variables.iterator(); it1.hasNext(); ) {
                        Variable variable = it1.next();
                        if (myLastEndVarName.equals(variable.getName()) && variable.isAlwaysStopAt()) {
                            it.remove();
                            it1.remove();
                        }
                    }
                }
            }
            if (endOffset >= 0) {
                myVariableOccurrences.add(new VarOccurence(TemplateImpl.END, endOffset));
            }
        }
    }
    TemplateImpl template = new TemplateImpl("", "");
    for (Variable variable : variables) {
        template.addVariable(variable.getName(), variable.getExpressionString(), variable.getDefaultValueString(), variable.isAlwaysStopAt());
    }
    List<VarOccurence> variableOccurrences = getListWithLimit(myVariableOccurrences);
    Collections.sort(variableOccurrences, (o1, o2) -> {
        if (o1.myOffset < o2.myOffset) {
            return -1;
        }
        if (o1.myOffset > o2.myOffset) {
            return 1;
        }
        return 0;
    });
    int last = 0;
    for (VarOccurence occurence : variableOccurrences) {
        template.addTextSegment(myText.substring(last, occurence.myOffset));
        template.addVariableSegment(occurence.myName);
        last = occurence.myOffset;
    }
    template.addTextSegment(myText.substring(last));
    template.setToReformat(myIsToReformat);
    return template;
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) Variable(com.intellij.codeInsight.template.impl.Variable) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with TemplateImpl

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

the class JavaMethodCallElement method setupNonFilledArgumentRemoving.

private static void setupNonFilledArgumentRemoving(final Editor editor, final TemplateState templateState) {
    AtomicInteger maxEditedVariable = new AtomicInteger(-1);
    editor.getDocument().addDocumentListener(new DocumentAdapter() {

        @Override
        public void documentChanged(DocumentEvent e) {
            maxEditedVariable.set(Math.max(maxEditedVariable.get(), templateState.getCurrentVariableNumber()));
        }
    }, templateState);
    templateState.addTemplateStateListener(new TemplateEditingAdapter() {

        @Override
        public void currentVariableChanged(TemplateState templateState, Template template, int oldIndex, int newIndex) {
            maxEditedVariable.set(Math.max(maxEditedVariable.get(), oldIndex));
        }

        @Override
        public void beforeTemplateFinished(TemplateState state, Template template, boolean brokenOff) {
            if (brokenOff) {
                removeUntouchedArguments((TemplateImpl) template);
            }
        }

        private void removeUntouchedArguments(TemplateImpl template) {
            int firstUnchangedVar = maxEditedVariable.get() + 1;
            if (firstUnchangedVar >= template.getVariableCount())
                return;
            TextRange startRange = templateState.getVariableRange(template.getVariableNameAt(firstUnchangedVar));
            TextRange endRange = templateState.getVariableRange(template.getVariableNameAt(template.getVariableCount() - 1));
            if (startRange == null || endRange == null)
                return;
            WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> editor.getDocument().deleteString(startRange.getStartOffset(), endRange.getEndOffset()));
        }
    });
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DocumentAdapter(com.intellij.openapi.editor.event.DocumentAdapter) TextRange(com.intellij.openapi.util.TextRange) DocumentEvent(com.intellij.openapi.editor.event.DocumentEvent) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 9 with TemplateImpl

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

the class BasicExpressionCompletionContributor method fillCompletionVariants.

public static void fillCompletionVariants(JavaSmartCompletionParameters parameters, final Consumer<LookupElement> result, PrefixMatcher matcher) {
    final PsiElement element = parameters.getPosition();
    if (JavaKeywordCompletion.isAfterTypeDot(element)) {
        addKeyword(result, element, PsiKeyword.CLASS);
        addKeyword(result, element, PsiKeyword.THIS);
    }
    if (!JavaKeywordCompletion.AFTER_DOT.accepts(element)) {
        if (parameters.getParameters().getInvocationCount() <= 1) {
            new CollectionsUtilityMethodsProvider(parameters.getPosition(), parameters.getExpectedType(), parameters.getDefaultType(), result).addCompletions(StringUtil.isNotEmpty(matcher.getPrefix()));
        }
        ClassLiteralGetter.addCompletions(parameters, result, matcher);
        final PsiElement position = parameters.getPosition();
        final PsiType expectedType = parameters.getExpectedType();
        for (final TemplateImpl template : TemplateSettings.getInstance().getTemplates()) {
            if (!template.isDeactivated() && template.getTemplateContext().isEnabled(new SmartCompletionContextType())) {
                result.consume(new SmartCompletionTemplateItem(template, position));
            }
        }
        addKeyword(result, position, PsiKeyword.TRUE);
        addKeyword(result, position, PsiKeyword.FALSE);
        final PsiElement parent = position.getParent();
        if (parent != null && !(parent.getParent() instanceof PsiSwitchLabelStatement)) {
            for (final PsiExpression expression : ThisGetter.getThisExpressionVariants(position)) {
                result.consume(new ExpressionLookupItem(expression));
            }
        }
        processDataflowExpressionTypes(position, expectedType, matcher, result);
    }
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) SmartCompletionContextType(com.intellij.codeInsight.template.SmartCompletionContextType) ExpressionLookupItem(com.intellij.codeInsight.lookup.ExpressionLookupItem)

Example 10 with TemplateImpl

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

the class ListTemplateActionTest method addTemplate.

private void addTemplate(String key, String text, String description, String group) {
    TemplateManager manager = TemplateManager.getInstance(getProject());
    TemplateImpl template = (TemplateImpl) manager.createTemplate(key, group, text);
    template.setDescription(description);
    TemplateContextType contextType = ContainerUtil.findInstance(TemplateContextType.EP_NAME.getExtensions(), JavaCodeContextType.class);
    template.getTemplateContext().setEnabled(contextType, true);
    CodeInsightTestUtil.addTemplate(template, myFixture.getTestRootDisposable());
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl)

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