Search in sources :

Example 11 with Template

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

the class PatternCompiler method doCompile.

private static List<PsiElement> doCompile(Project project, MatchOptions options, CompiledPattern result, PrefixProvider prefixProvider, CompileContext context) {
    result.clearHandlers();
    context.init(result, options, project, options.getScope() instanceof GlobalSearchScope);
    final StringBuilder buf = new StringBuilder();
    Template template = TemplateManager.getInstance(project).createTemplate("", "", options.getSearchPattern());
    int segmentsCount = template.getSegmentsCount();
    String text = template.getTemplateText();
    buf.setLength(0);
    int prevOffset = 0;
    for (int i = 0; i < segmentsCount; ++i) {
        final int offset = template.getSegmentOffset(i);
        final String name = template.getSegmentName(i);
        final String prefix = prefixProvider.getPrefix(i);
        if (prefix == null) {
            throw new MalformedPatternException();
        }
        buf.append(text.substring(prevOffset, offset));
        buf.append(prefix);
        buf.append(name);
        MatchVariableConstraint constraint = options.getVariableConstraint(name);
        if (constraint == null) {
            // we do not edited the constraints
            constraint = new MatchVariableConstraint();
            constraint.setName(name);
            options.addVariableConstraint(constraint);
        }
        SubstitutionHandler handler = result.createSubstitutionHandler(name, prefix + name, constraint.isPartOfSearchResults(), constraint.getMinCount(), constraint.getMaxCount(), constraint.isGreedy());
        if (constraint.isWithinHierarchy()) {
            handler.setSubtype(true);
        }
        if (constraint.isStrictlyWithinHierarchy()) {
            handler.setStrictSubtype(true);
        }
        MatchPredicate predicate;
        if (!StringUtil.isEmptyOrSpaces(constraint.getRegExp())) {
            predicate = new RegExpPredicate(constraint.getRegExp(), options.isCaseSensitiveMatch(), name, constraint.isWholeWordsOnly(), constraint.isPartOfSearchResults());
            if (constraint.isInvertRegExp()) {
                predicate = new NotPredicate(predicate);
            }
            addPredicate(handler, predicate);
        }
        if (constraint.isReference()) {
            predicate = new ReferencePredicate(constraint.getNameOfReferenceVar());
            if (constraint.isInvertReference()) {
                predicate = new NotPredicate(predicate);
            }
            addPredicate(handler, predicate);
        }
        addExtensionPredicates(options, constraint, handler);
        addScriptConstraint(project, name, constraint, handler);
        if (!StringUtil.isEmptyOrSpaces(constraint.getContainsConstraint())) {
            predicate = new ContainsPredicate(name, constraint.getContainsConstraint());
            if (constraint.isInvertContainsConstraint()) {
                predicate = new NotPredicate(predicate);
            }
            addPredicate(handler, predicate);
        }
        if (!StringUtil.isEmptyOrSpaces(constraint.getWithinConstraint())) {
            assert false;
        }
        prevOffset = offset;
    }
    MatchVariableConstraint constraint = options.getVariableConstraint(Configuration.CONTEXT_VAR_NAME);
    if (constraint != null) {
        SubstitutionHandler handler = result.createSubstitutionHandler(Configuration.CONTEXT_VAR_NAME, Configuration.CONTEXT_VAR_NAME, constraint.isPartOfSearchResults(), constraint.getMinCount(), constraint.getMaxCount(), constraint.isGreedy());
        if (!StringUtil.isEmptyOrSpaces(constraint.getWithinConstraint())) {
            MatchPredicate predicate = new WithinPredicate(Configuration.CONTEXT_VAR_NAME, constraint.getWithinConstraint(), options.getFileType(), project);
            if (constraint.isInvertWithinConstraint()) {
                predicate = new NotPredicate(predicate);
            }
            addPredicate(handler, predicate);
        }
        addExtensionPredicates(options, constraint, handler);
        addScriptConstraint(project, Configuration.CONTEXT_VAR_NAME, constraint, handler);
    }
    buf.append(text.substring(prevOffset, text.length()));
    PsiElement[] matchStatements;
    try {
        matchStatements = MatcherImplUtil.createTreeFromText(buf.toString(), PatternTreeContext.Block, options.getFileType(), options.getDialect(), options.getPatternContext(), project, false);
        if (matchStatements.length == 0)
            throw new MalformedPatternException();
    } catch (IncorrectOperationException e) {
        throw new MalformedPatternException(e.getMessage());
    }
    NodeFilter filter = LexicalNodesFilter.getInstance();
    GlobalCompilingVisitor compilingVisitor = new GlobalCompilingVisitor();
    compilingVisitor.compile(matchStatements, context);
    ArrayList<PsiElement> elements = new ArrayList<>();
    for (PsiElement matchStatement : matchStatements) {
        if (!filter.accepts(matchStatement)) {
            elements.add(matchStatement);
        }
    }
    new DeleteNodesAction(compilingVisitor.getLexicalNodes()).run();
    return elements;
}
Also used : TIntArrayList(gnu.trove.TIntArrayList) MatchPredicate(com.intellij.structuralsearch.impl.matcher.handlers.MatchPredicate) Template(com.intellij.codeInsight.template.Template) SubstitutionHandler(com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NodeFilter(com.intellij.dupLocator.util.NodeFilter)

Example 12 with Template

use of com.intellij.codeInsight.template.Template in project intellij-plugins by JetBrains.

the class FlexLiveTemplatesTest method doTest.

protected void doTest(final String templateName, final String extension, final Consumer<Integer> segmentHandler) throws Exception {
    final Template template = TemplateSettings.getInstance().getTemplate(templateName, "ActionScript");
    doTest(template, segmentHandler, getBasePath() + getTestName(false) + "." + extension);
}
Also used : Template(com.intellij.codeInsight.template.Template)

Example 13 with Template

use of com.intellij.codeInsight.template.Template in project intellij-plugins by JetBrains.

the class FlexLiveTemplatesTest method doTest.

protected void doTest(final String templateName, final String extension, final String group) throws Exception {
    final Template template = TemplateSettings.getInstance().getTemplate(templateName, group);
    //noinspection unchecked
    doTest(template, Consumer.EMPTY_CONSUMER, getBasePath() + getTestName(false) + "." + extension);
}
Also used : Template(com.intellij.codeInsight.template.Template)

Example 14 with Template

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

the class StringBasedPostfixTemplate method expandForChooseExpression.

@Override
public final void expandForChooseExpression(@NotNull PsiElement expr, @NotNull Editor editor) {
    Project project = expr.getProject();
    Document document = editor.getDocument();
    PsiElement elementForRemoving = getElementToRemove(expr);
    document.deleteString(elementForRemoving.getTextRange().getStartOffset(), elementForRemoving.getTextRange().getEndOffset());
    TemplateManager manager = TemplateManager.getInstance(project);
    String templateString = getTemplateString(expr);
    if (templateString == null) {
        PostfixTemplatesUtils.showErrorHint(expr.getProject(), editor);
        return;
    }
    Template template = createTemplate(manager, templateString);
    if (shouldAddExpressionToContext()) {
        template.addVariable("expr", new TextExpression(expr.getText()), false);
    }
    setVariables(template, expr);
    manager.startTemplate(editor, template);
}
Also used : Project(com.intellij.openapi.project.Project) TemplateManager(com.intellij.codeInsight.template.TemplateManager) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) TextExpression(com.intellij.codeInsight.template.impl.TextExpression) Template(com.intellij.codeInsight.template.Template)

Example 15 with Template

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

the class LookupItemUtil method objectToLookupItem.

/**
   * @deprecated
   * @see LookupElementBuilder
  */
@NotNull
public static LookupElement objectToLookupItem(Object object) {
    if (object instanceof LookupElement)
        return (LookupElement) object;
    if (object instanceof PsiClass) {
        return JavaClassNameCompletionContributor.createClassLookupItem((PsiClass) object, true);
    }
    if (object instanceof PsiMethod) {
        return new JavaMethodCallElement((PsiMethod) object);
    }
    if (object instanceof PsiVariable) {
        return new VariableLookupItem((PsiVariable) object);
    }
    if (object instanceof PsiExpression) {
        return new ExpressionLookupItem((PsiExpression) object);
    }
    if (object instanceof PsiType) {
        return PsiTypeLookupItem.createLookupItem((PsiType) object, null);
    }
    if (object instanceof PsiPackage) {
        return new PackageLookupItem((PsiPackage) object);
    }
    String s = null;
    LookupItem item = new LookupItem(object, "");
    if (object instanceof PsiElement) {
        s = PsiUtilCore.getName((PsiElement) object);
    }
    TailType tailType = TailType.NONE;
    if (object instanceof PsiMetaData) {
        s = ((PsiMetaData) object).getName();
    } else if (object instanceof String) {
        s = (String) object;
    } else if (object instanceof Template) {
        s = ((Template) object).getKey();
    } else if (object instanceof PresentableLookupValue) {
        s = ((PresentableLookupValue) object).getPresentation();
    }
    if (object instanceof LookupValueWithUIHint && ((LookupValueWithUIHint) object).isBold()) {
        item.setBold();
    }
    if (s == null) {
        LOG.error("Null string for object: " + object + " of class " + (object != null ? object.getClass() : null));
    }
    item.setLookupString(s);
    item.setTailType(tailType);
    return item;
}
Also used : JavaMethodCallElement(com.intellij.codeInsight.completion.JavaMethodCallElement) TailType(com.intellij.codeInsight.TailType) Template(com.intellij.codeInsight.template.Template) PsiMetaData(com.intellij.psi.meta.PsiMetaData) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Template (com.intellij.codeInsight.template.Template)54 Editor (com.intellij.openapi.editor.Editor)20 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)19 Project (com.intellij.openapi.project.Project)17 TemplateManager (com.intellij.codeInsight.template.TemplateManager)15 TextRange (com.intellij.openapi.util.TextRange)15 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)12 PsiElement (com.intellij.psi.PsiElement)11 RangeMarker (com.intellij.openapi.editor.RangeMarker)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 NotNull (org.jetbrains.annotations.NotNull)5 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)4 TextExpression (com.intellij.codeInsight.template.impl.TextExpression)4 Document (com.intellij.openapi.editor.Document)4 DartComponent (com.jetbrains.lang.dart.psi.DartComponent)4 Expression (com.intellij.codeInsight.template.Expression)3 SuggestVariableNameMacro (com.intellij.codeInsight.template.macro.SuggestVariableNameMacro)3 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)3 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)3 XmlTag (com.intellij.psi.xml.XmlTag)3