Search in sources :

Example 1 with Expression

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

the class MacroParser method parseParameters.

private static void parseParameters(MacroCallNode macroCallNode, Lexer lexer, String expression) {
    if (lexer.getTokenType() != MacroTokenType.RPAREN) {
        while (lexer.getTokenType() != null) {
            Expression node = parseMacro(lexer, expression);
            macroCallNode.addParameter(node);
            if (lexer.getTokenType() == MacroTokenType.COMMA) {
                advance(lexer);
            } else {
                break;
            }
        }
    }
}
Also used : Expression(com.intellij.codeInsight.template.Expression)

Example 2 with Expression

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

the class MacroParser method parseVariable.

private static Expression parseVariable(Lexer lexer, String expression) {
    String variableName = getString(lexer, expression);
    advance(lexer);
    if (lexer.getTokenType() == null) {
        if (TemplateImpl.END.equals(variableName)) {
            return new EmptyNode();
        }
        return new VariableNode(variableName, null);
    }
    if (lexer.getTokenType() != MacroTokenType.EQ) {
        return new VariableNode(variableName, null);
    }
    advance(lexer);
    Expression node = parseMacro(lexer, expression);
    return new VariableNode(variableName, node);
}
Also used : Expression(com.intellij.codeInsight.template.Expression)

Example 3 with Expression

use of com.intellij.codeInsight.template.Expression 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 4 with Expression

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

the class CreateJSEventMethod method addParameters.

@Override
protected void addParameters(Template template, JSReferenceExpression refExpr, PsiFile file) {
    Expression expression = new MyExpression("event");
    template.addVariable("$event$", expression, expression, true);
    template.addTextSegment(":");
    String text = myEventTypeGenerator.compute();
    text = ImportUtils.importAndShortenReference(text, refExpr, true, true).first;
    template.addTextSegment(text);
}
Also used : JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) Expression(com.intellij.codeInsight.template.Expression)

Example 5 with Expression

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

the class TestIntegrationUtils method createTestMethodTemplate.

public static Template createTestMethodTemplate(@NotNull MethodKind methodKind, TestFramework descriptor, @NotNull PsiClass targetClass, @Nullable PsiClass sourceClass, @Nullable String name, boolean automatic, Set<String> existingNames) {
    FileTemplateDescriptor templateDesc = methodKind.getFileTemplateDescriptor(descriptor);
    String templateName = templateDesc.getFileName();
    FileTemplate fileTemplate = FileTemplateManager.getInstance(targetClass.getProject()).getCodeTemplate(templateName);
    Template template = TemplateManager.getInstance(targetClass.getProject()).createTemplate("", "");
    String templateText;
    try {
        Properties properties = new Properties();
        if (sourceClass != null && sourceClass.isValid()) {
            properties.setProperty(FileTemplate.ATTRIBUTE_CLASS_NAME, sourceClass.getQualifiedName());
        }
        templateText = fileTemplate.getText(properties);
    } catch (IOException e) {
        LOG.warn(e);
        templateText = fileTemplate.getText();
    }
    if (name == null)
        name = methodKind.getDefaultName();
    if (existingNames != null && !existingNames.add(name)) {
        int idx = 1;
        while (existingNames.contains(name)) {
            final String newName = name + (idx++);
            if (existingNames.add(newName)) {
                name = newName;
                break;
            }
        }
    }
    templateText = StringUtil.replace(templateText, "${BODY}\n", "");
    int from = 0;
    while (true) {
        int index = templateText.indexOf("${NAME}", from);
        if (index == -1)
            break;
        template.addTextSegment(templateText.substring(from, index));
        if (index > 0 && !Character.isWhitespace(templateText.charAt(index - 1))) {
            name = StringUtil.capitalize(name);
        } else {
            name = StringUtil.decapitalize(name);
        }
        if (from == 0) {
            Expression nameExpr = new ConstantNode(name);
            template.addVariable("name", nameExpr, nameExpr, !automatic);
        } else {
            template.addVariableSegment("name");
        }
        from = index + "${NAME}".length();
    }
    template.addTextSegment(templateText.substring(from, templateText.length()));
    template.setToIndent(true);
    template.setToReformat(true);
    template.setToShortenLongNames(true);
    return template;
}
Also used : ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode) FileTemplateDescriptor(com.intellij.ide.fileTemplates.FileTemplateDescriptor) Expression(com.intellij.codeInsight.template.Expression) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IOException(java.io.IOException) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) Template(com.intellij.codeInsight.template.Template)

Aggregations

Expression (com.intellij.codeInsight.template.Expression)7 Template (com.intellij.codeInsight.template.Template)3 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)2 TemplateManager (com.intellij.codeInsight.template.TemplateManager)2 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)2 SuggestVariableNameMacro (com.intellij.codeInsight.template.macro.SuggestVariableNameMacro)2 Editor (com.intellij.openapi.editor.Editor)2 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)2 PsiReference (com.intellij.psi.PsiReference)2 ConstantNode (com.intellij.codeInsight.template.impl.ConstantNode)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateDescriptor (com.intellij.ide.fileTemplates.FileTemplateDescriptor)1 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)1 IOException (java.io.IOException)1