Search in sources :

Example 21 with Template

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

the class CreateConstructorFix method buildFunctionsText.

protected Template buildFunctionsText(TemplateManager templateManager, Set<DartComponent> elementsToProcess) {
    final Template template = templateManager.createTemplate(getClass().getName(), DART_TEMPLATE_GROUP);
    template.setToReformat(true);
    //noinspection ConstantConditions
    template.addTextSegment(myDartClass.getName());
    template.addTextSegment("(");
    for (Iterator<DartComponent> iterator = elementsToProcess.iterator(); iterator.hasNext(); ) {
        DartComponent component = iterator.next();
        template.addTextSegment("this.");
        //noinspection ConstantConditions
        template.addTextSegment(component.getName());
        if (iterator.hasNext()) {
            template.addTextSegment(",");
        }
    }
    template.addTextSegment(");");
    template.addEndVariable();
    // trailing space is removed when auto-reformatting, but it helps to enter line break if needed
    template.addTextSegment(" ");
    return template;
}
Also used : DartComponent(com.jetbrains.lang.dart.psi.DartComponent) Template(com.intellij.codeInsight.template.Template)

Example 22 with Template

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

the class CreateEqualsAndHashcodeFix method buildFunctionsText.

protected Template buildFunctionsText(TemplateManager templateManager, @NotNull Set<DartComponent> elementsToProcess) {
    final Template template = templateManager.createTemplate(getClass().getName(), DART_TEMPLATE_GROUP);
    template.setToReformat(true);
    final boolean doInsertOverrideAnnotation = CodeStyleSettingsManager.getSettings(myDartClass.getProject()).INSERT_OVERRIDE_ANNOTATION;
    if (doInsertOverrideAnnotation) {
        template.addTextSegment("@override\n");
    }
    template.addTextSegment("bool operator==(Object other) =>\nidentical(this, other) ||\n");
    if (mySuperclassOverridesEqualEqualAndHashCode) {
        template.addTextSegment("super == other &&\n");
    }
    template.addTextSegment("other is " + myDartClass.getName() + " &&\n");
    template.addTextSegment("runtimeType == other.runtimeType");
    for (DartComponent component : elementsToProcess) {
        template.addTextSegment(" &&\n");
        template.addTextSegment(component.getName() + " == other." + component.getName());
    }
    template.addTextSegment(";\n");
    if (doInsertOverrideAnnotation) {
        template.addTextSegment("@override\n");
    }
    template.addTextSegment("int get hashCode => ");
    boolean firstItem = true;
    if (mySuperclassOverridesEqualEqualAndHashCode) {
        template.addTextSegment("\nsuper.hashCode");
        firstItem = false;
    }
    for (DartComponent component : elementsToProcess) {
        if (!firstItem) {
            template.addTextSegment(" ^\n");
        }
        template.addTextSegment(component.getName() + ".hashCode");
        firstItem = false;
    }
    if (!mySuperclassOverridesEqualEqualAndHashCode && elementsToProcess.isEmpty()) {
        template.addTextSegment("0");
    }
    template.addTextSegment(";\n");
    template.addEndVariable();
    // trailing space is removed when auto-reformatting, but it helps to enter line break if needed
    template.addTextSegment(" ");
    return template;
}
Also used : DartComponent(com.jetbrains.lang.dart.psi.DartComponent) Template(com.intellij.codeInsight.template.Template)

Example 23 with Template

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

the class BracesInsertHandler method handleInsert.

@Override
public void handleInsert(@NotNull InsertionContext context, LookupElement item) {
    Editor editor = context.getEditor();
    CharSequence documentText = context.getDocument().getImmutableCharSequence();
    int offset = skipWhiteSpaces(editor.getCaretModel().getOffset(), documentText);
    if (documentText.charAt(offset) != '{') {
        Project project = context.getProject();
        Template template = TemplateManager.getInstance(project).createTemplate("braces", "go", myOneLine ? "{$END$}" : " {\n$END$\n}");
        template.setToReformat(true);
        TemplateManager.getInstance(project).startTemplate(editor, template);
    } else {
        editor.getCaretModel().moveToOffset(offset);
        ApplicationManager.getApplication().runWriteAction(() -> {
            EditorActionHandler enterAction = EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_START_NEW_LINE);
            enterAction.execute(editor, editor.getCaretModel().getCurrentCaret(), ((EditorEx) editor).getDataContext());
        });
    }
}
Also used : Project(com.intellij.openapi.project.Project) EditorActionHandler(com.intellij.openapi.editor.actionSystem.EditorActionHandler) Editor(com.intellij.openapi.editor.Editor) Template(com.intellij.codeInsight.template.Template)

Example 24 with Template

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

the class GoIntroduceFunctionFix 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 GoCallExpr))
        return;
    GoCallExpr call = (GoCallExpr) startElement;
    List<GoExpression> args = call.getArgumentList().getExpressionList();
    GoType resultType = ContainerUtil.getFirstItem(GoTypeUtil.getExpectedTypes(call));
    PsiElement anchor = PsiTreeUtil.findPrevParent(file, call);
    Template template = TemplateManager.getInstance(project).createTemplate("", "");
    template.addTextSegment("\nfunc " + myName);
    setupFunctionParameters(template, args, file);
    setupFunctionResult(template, resultType);
    template.addTextSegment(" {\n\t");
    template.addEndVariable();
    template.addTextSegment("\n}");
    int offset = anchor.getTextRange().getEndOffset();
    editor.getCaretModel().moveToOffset(offset);
    startTemplate(editor, template, project);
}
Also used : LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement) Template(com.intellij.codeInsight.template.Template)

Example 25 with Template

use of com.intellij.codeInsight.template.Template 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

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