Example #1
0
  void loadTemplates(InputStream inputStream, final String templateName) {
    final SAXBuilder parser = new SAXBuilder();
    try {

      TemplateSettings templateSettings = TemplateSettings.getInstance();
      Document doc = parser.build(inputStream);
      Element root = doc.getRootElement();
      for (Object element : root.getChildren()) {
        if (element instanceof Element) {
          final Template template = readExternal((Element) element, templateName);
          final String key = template.getKey();
          if (key != null) {
            TemplateImpl existingTemplate = templateSettings.getTemplate(key, TemplateGroupName);
            if (existingTemplate == null) {
              templateSettings.addTemplate(template);
            } else if (TemplateGroupName.equals(existingTemplate.getGroupName())) {
              // Update only add if template is in the AribaWeb group
              templateSettings.removeTemplate(existingTemplate);
              templateSettings.addTemplate(template);
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void resetFrom(TemplateImpl another) {
    removeAllParsed();
    toParseSegments = another.toParseSegments;

    myKey = another.getKey();
    myString = another.myString;
    myTemplateText = another.myTemplateText;
    myGroupName = another.myGroupName;
    myId = another.myId;
    myDescription = another.myDescription;
    myShortcutChar = another.myShortcutChar;
    isToReformat = another.isToReformat;
    isToShortenLongNames = another.isToShortenLongNames;
    myIsInline = another.myIsInline;
    myTemplateContext = another.myTemplateContext.createCopy();
    isDeactivated = another.isDeactivated;
    for (Property property : Property.values()) {
      boolean value = another.getValue(property);
      if (value != Template.getDefaultValue(property)) {
        setValue(property, value);
      }
    }
    for (Variable variable : another.myVariables) {
      addVariable(
          variable.getName(),
          variable.getExpressionString(),
          variable.getDefaultValueString(),
          variable.isAlwaysStopAt());
    }
  }
 @Override
 protected void addExprVariable(@NotNull PsiElement expr, Template template) {
   final FindViewByIdMacro toStringIfNeedMacro = new FindViewByIdMacro();
   MacroCallNode macroCallNode = new MacroCallNode(toStringIfNeedMacro);
   macroCallNode.addParameter(new ConstantNode(expr.getText()));
   template.addVariable("expr", macroCallNode, false);
 }
 protected void buildTemplate(final Template template, final JSClass jsClass) {
   if (JSInheritanceUtil.findMember(
           "tearDown",
           jsClass,
           JSInheritanceUtil.SearchedMemberType.Methods,
           JSFunction.FunctionKind.SIMPLE,
           true)
       != null) {
     template.addTextSegment(
         "[After]\npublic override function tearDown():void{\nsuper.tearDown();");
     template.addEndVariable();
     template.addTextSegment("\n}");
   } else {
     template.addTextSegment("[After]\npublic function tearDown():void{\n");
     template.addEndVariable();
     template.addTextSegment("\n}");
   }
 }
  private static Template generateTemplate(
      Project project, String exprText, final PsiType[] suggestedTypes) {
    final TemplateManager templateManager = TemplateManager.getInstance(project);
    final Template template = templateManager.createTemplate("", "");
    template.setToReformat(true);

    Set<LookupElement> itemSet = new LinkedHashSet<>();
    for (PsiType type : suggestedTypes) {
      itemSet.add(PsiTypeLookupItem.createLookupItem(type, null));
    }
    final LookupElement[] lookupItems = itemSet.toArray(new LookupElement[itemSet.size()]);

    final Result result =
        suggestedTypes.length > 0 ? new PsiTypeResult(suggestedTypes[0], project) : null;

    Expression expr =
        new Expression() {
          @Override
          public LookupElement[] calculateLookupItems(ExpressionContext context) {
            return lookupItems.length > 1 ? lookupItems : null;
          }

          @Override
          public Result calculateResult(ExpressionContext context) {
            return result;
          }

          @Override
          public Result calculateQuickResult(ExpressionContext context) {
            return null;
          }
        };
    template.addTextSegment("((");
    template.addVariable(TYPE_TEMPLATE_VARIABLE, expr, expr, true);
    template.addTextSegment(")" + exprText + ")");
    template.addEndVariable();

    return template;
  }
  private static void startTemplate(
      GrTypeParameterList oldTypeParameterList,
      final Project project,
      final GrTypeDefinition psiClass,
      final GrTypeDefinition targetClass,
      boolean includeClassName) {
    PsiElementFactory jfactory = JavaPsiFacade.getElementFactory(project);
    final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
    GrCodeReferenceElement ref = elementFactory.createCodeReferenceElementFromClass(psiClass);
    try {
      if (psiClass.isInterface()) {
        GrImplementsClause clause = targetClass.getImplementsClause();
        if (clause == null) {
          clause =
              (GrImplementsClause)
                  targetClass.addAfter(
                      elementFactory.createImplementsClause(),
                      targetClass.getNameIdentifierGroovy());
        }
        ref = (GrCodeReferenceElement) clause.add(ref);
      } else {
        GrExtendsClause clause = targetClass.getExtendsClause();
        if (clause == null) {
          clause =
              (GrExtendsClause)
                  targetClass.addAfter(
                      elementFactory.createExtendsClause(), targetClass.getNameIdentifierGroovy());
        }
        ref = (GrCodeReferenceElement) clause.add(ref);
      }
      if (psiClass.hasTypeParameters() || includeClassName) {
        final Editor editor =
            CodeInsightUtil.positionCursor(
                project, targetClass.getContainingFile(), targetClass.getLBrace());
        final TemplateBuilderImpl templateBuilder =
            editor == null || ApplicationManager.getApplication().isUnitTestMode()
                ? null
                : (TemplateBuilderImpl)
                    TemplateBuilderFactory.getInstance().createTemplateBuilder(targetClass);

        if (includeClassName && templateBuilder != null) {
          templateBuilder.replaceElement(targetClass.getNameIdentifier(), targetClass.getName());
        }

        if (oldTypeParameterList != null) {
          for (PsiTypeParameter parameter : oldTypeParameterList.getTypeParameters()) {
            final PsiElement param =
                ref.getTypeArgumentList()
                    .add(elementFactory.createTypeElement(jfactory.createType(parameter)));
            if (templateBuilder != null) {
              templateBuilder.replaceElement(param, param.getText());
            }
          }
        }

        final GrTypeParameterList typeParameterList = targetClass.getTypeParameterList();
        assert typeParameterList != null;
        typeParameterList.replace(oldTypeParameterList);

        if (templateBuilder != null) {
          templateBuilder.setEndVariableBefore(ref);
          final Template template = templateBuilder.buildTemplate();
          template.addEndVariable();

          final PsiFile containingFile = targetClass.getContainingFile();

          PsiDocumentManager.getInstance(project)
              .doPostponedOperationsAndUnblockDocument(editor.getDocument());

          final TextRange textRange = targetClass.getTextRange();
          final int startClassOffset = textRange.getStartOffset();
          editor.getDocument().deleteString(textRange.getStartOffset(), textRange.getEndOffset());
          CreateFromUsageBaseFix.startTemplate(
              editor,
              template,
              project,
              new TemplateEditingAdapter() {
                @Override
                public void templateFinished(Template template, boolean brokenOff) {
                  chooseAndImplement(
                      psiClass,
                      project,
                      PsiTreeUtil.getParentOfType(
                          containingFile.findElementAt(startClassOffset), GrTypeDefinition.class),
                      editor);
                }
              },
              getTitle(psiClass));
        }
      }
    } catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }
 @Override
 protected void setVariables(@NotNull Template template, @NotNull PsiElement element) {
   MacroCallNode node = new MacroCallNode(new VariableOfTypeMacro());
   node.addParameter(new ConstantNode(CONTEXT.toString()));
   template.addVariable("context", node, new ConstantNode(""), false);
 }