Search in sources :

Example 16 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 17 with Template

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

the class HighlightUtils method showRenameTemplate.

public static void showRenameTemplate(PsiElement context, PsiNameIdentifierOwner element, PsiReference... references) {
    context = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(context);
    final Project project = context.getProject();
    final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    final Editor editor = fileEditorManager.getSelectedTextEditor();
    if (editor == null) {
        return;
    }
    final TemplateBuilderImpl builder = new TemplateBuilderImpl(context);
    final Expression macroCallNode = new MacroCallNode(new SuggestVariableNameMacro());
    final PsiElement identifier = element.getNameIdentifier();
    builder.replaceElement(identifier, "PATTERN", macroCallNode, true);
    for (PsiReference reference : references) {
        builder.replaceElement(reference, "PATTERN", "PATTERN", false);
    }
    final Template template = builder.buildInlineTemplate();
    final TextRange textRange = context.getTextRange();
    final int startOffset = textRange.getStartOffset();
    editor.getCaretModel().moveToOffset(startOffset);
    final TemplateManager templateManager = TemplateManager.getInstance(project);
    templateManager.startTemplate(editor, template);
}
Also used : PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) Template(com.intellij.codeInsight.template.Template) Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) Expression(com.intellij.codeInsight.template.Expression) TemplateManager(com.intellij.codeInsight.template.TemplateManager) SuggestVariableNameMacro(com.intellij.codeInsight.template.macro.SuggestVariableNameMacro) MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 18 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 19 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 20 with Template

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

the class OverrideImplementMethodFix method buildFunctionsText.

@Override
protected Template buildFunctionsText(TemplateManager templateManager, DartComponent element) {
    final Template template = templateManager.createTemplate(getClass().getName(), DART_TEMPLATE_GROUP);
    template.setToReformat(true);
    if (CodeStyleSettingsManager.getSettings(element.getProject()).INSERT_OVERRIDE_ANNOTATION) {
        template.addTextSegment("@override\n");
    }
    final boolean isField = element instanceof DartVarAccessDeclaration || element instanceof DartVarDeclarationListPart;
    if (isField && element.isFinal()) {
        template.addTextSegment("final");
        template.addTextSegment(" ");
    }
    final DartReturnType returnType = PsiTreeUtil.getChildOfType(element, DartReturnType.class);
    final DartType dartType = PsiTreeUtil.getChildOfType(element, DartType.class);
    if (returnType != null) {
        template.addTextSegment(DartPresentableUtil.buildTypeText(element, returnType, specializations));
        template.addTextSegment(" ");
    } else if (dartType != null) {
        template.addTextSegment(DartPresentableUtil.buildTypeText(element, dartType, specializations));
        template.addTextSegment(" ");
    }
    if (isField) {
        if (returnType == null && dartType == null) {
            template.addTextSegment("var");
            template.addTextSegment(" ");
        }
        //noinspection ConstantConditions
        template.addTextSegment(element.getName());
        if (element.isFinal()) {
            template.addTextSegment(" ");
            template.addTextSegment("=");
            template.addTextSegment(" ");
            template.addTextSegment("null");
        }
        // trailing space is removed when auto-reformatting, but it helps to enter line break if needed
        template.addTextSegment("; ");
        return template;
    }
    if (element.isOperator()) {
        template.addTextSegment("operator ");
    }
    if (element.isGetter() || element.isSetter()) {
        template.addTextSegment(element.isGetter() ? "get " : "set ");
    }
    //noinspection ConstantConditions
    template.addTextSegment(element.getName());
    if (!element.isGetter()) {
        template.addTextSegment("(");
        template.addTextSegment(DartPresentableUtil.getPresentableParameterList(element, specializations, false, true, true));
        template.addTextSegment(")");
    }
    template.addTextSegment("{\n");
    template.addEndVariable();
    // trailing space is removed when auto-reformatting, but it helps to enter line break if needed
    template.addTextSegment("\n} ");
    return template;
}
Also used : Template(com.intellij.codeInsight.template.Template)

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