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;
}
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;
}
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());
});
}
}
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);
}
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);
}
Aggregations