Search in sources :

Example 1 with ConstantNode

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

the class TemplateBuilderImpl method replaceRange.

@Override
public void replaceRange(TextRange rangeWithinElement, String replacementText) {
    final RangeMarker key = myDocument.createRangeMarker(rangeWithinElement.shiftRight(myContainerElement.getStartOffset()));
    ConstantNode value = new ConstantNode(replacementText);
    replaceElement(key, value);
}
Also used : ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode) RangeMarker(com.intellij.openapi.editor.RangeMarker)

Example 2 with ConstantNode

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

the class JavaMethodCallElement method createArgTemplate.

@NotNull
private static Template createArgTemplate(PsiMethod method, int caretOffset, PsiExpressionList argList, TextRange argRange) {
    Template template = TemplateManager.getInstance(method.getProject()).createTemplate("", "");
    PsiParameter[] parameters = method.getParameterList().getParameters();
    for (int i = 0; i < parameters.length; i++) {
        if (i > 0) {
            template.addTextSegment(", ");
        }
        String name = StringUtil.notNullize(parameters[i].getName());
        Expression expression = Registry.is("java.completion.argument.live.template.completion") ? new AutoPopupCompletion() : new ConstantNode(name);
        template.addVariable(name, expression, new ConstantNode(name), true);
    }
    boolean finishInsideParens = method.isVarArgs();
    if (finishInsideParens) {
        template.addEndVariable();
    }
    template.addTextSegment(argList.getText().substring(caretOffset - argRange.getStartOffset(), argList.getTextLength()));
    if (!finishInsideParens) {
        template.addEndVariable();
    }
    return template;
}
Also used : ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ConstantNode

use of com.intellij.codeInsight.template.impl.ConstantNode in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoIntroduceFunctionFix method setupFunctionParameters.

private static void setupFunctionParameters(@NotNull Template template, @NotNull List<GoExpression> args, PsiFile file) {
    Map<String, GoImportSpec> importMap = ((GoFile) file).getImportedPackagesMap();
    template.addTextSegment("(");
    for (int i = 0; i < args.size(); i++) {
        GoExpression e = args.get(i);
        template.addVariable(GoRefactoringUtil.createParameterNameSuggestedExpression(e), true);
        template.addTextSegment(" ");
        String type = convertType(file, e.getGoType(null), importMap);
        template.addVariable(new ConstantNode(type), true);
        if (i != args.size() - 1)
            template.addTextSegment(", ");
    }
    template.addTextSegment(")");
}
Also used : ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode)

Example 4 with ConstantNode

use of com.intellij.codeInsight.template.impl.ConstantNode in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoIntroduceFunctionFix method setupFunctionResult.

private static void setupFunctionResult(@NotNull Template template, @Nullable GoType type) {
    if (type instanceof GoTypeList) {
        template.addTextSegment(" (");
        List<GoType> list = ((GoTypeList) type).getTypeList();
        for (int i = 0; i < list.size(); i++) {
            template.addVariable(new ConstantNode(list.get(i).getText()), true);
            if (i < list.size() - 1)
                template.addTextSegment(", ");
        }
        template.addTextSegment(")");
        return;
    }
    if (type != null) {
        template.addTextSegment(" ");
        template.addVariable(new ConstantNode(type.getText()), true);
    }
}
Also used : ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode)

Example 5 with ConstantNode

use of com.intellij.codeInsight.template.impl.ConstantNode in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoCreateWrapperTypeQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (editor == null) {
        LOG.error("Cannot run quick fix without editor: " + getClass().getSimpleName(), AttachmentFactory.createAttachment(file.getVirtualFile()));
        return;
    }
    if (!(startElement instanceof GoType))
        return;
    GoType type = (GoType) startElement;
    PsiElement anchor = PsiTreeUtil.findPrevParent(file, type);
    String name = "TypeName";
    GoTypeDeclaration decl = (GoTypeDeclaration) file.addBefore(GoElementFactory.createTypeDeclaration(project, name, type), anchor);
    if (decl == null)
        return;
    decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
    if (decl == null)
        return;
    GoTypeSpec spec = ContainerUtil.getFirstItem(decl.getTypeSpecList());
    if (spec == null)
        return;
    TemplateBuilderImpl builder = new TemplateBuilderImpl(file);
    builder.replaceElement(type, OTHER_NAME, INPUT_NAME, false);
    builder.replaceElement(spec.getIdentifier(), INPUT_NAME, new ConstantNode(name), true);
    editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset());
    Template template = builder.buildInlineTemplate();
    editor.getCaretModel().moveToOffset(file.getTextRange().getStartOffset());
    TemplateManager.getInstance(project).startTemplate(editor, template);
}
Also used : GoTypeDeclaration(com.goide.psi.GoTypeDeclaration) TemplateBuilderImpl(com.intellij.codeInsight.template.TemplateBuilderImpl) ConstantNode(com.intellij.codeInsight.template.impl.ConstantNode) GoTypeSpec(com.goide.psi.GoTypeSpec) GoType(com.goide.psi.GoType) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement) Template(com.intellij.codeInsight.template.Template)

Aggregations

ConstantNode (com.intellij.codeInsight.template.impl.ConstantNode)7 Template (com.intellij.codeInsight.template.Template)2 RangeMarker (com.intellij.openapi.editor.RangeMarker)2 GoType (com.goide.psi.GoType)1 GoTypeDeclaration (com.goide.psi.GoTypeDeclaration)1 GoTypeSpec (com.goide.psi.GoTypeSpec)1 Expression (com.intellij.codeInsight.template.Expression)1 TemplateBuilderImpl (com.intellij.codeInsight.template.TemplateBuilderImpl)1 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateDescriptor (com.intellij.ide.fileTemplates.FileTemplateDescriptor)1 PsiElement (com.intellij.psi.PsiElement)1 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1