/**
   * Evaluates a 'java' template in the context of a compilation unit
   *
   * @param template the template to be evaluated
   * @param compilationUnit the compilation unit in which to evaluate the template
   * @param position the position inside the compilation unit for which to evaluate the template
   * @return the evaluated template
   * @throws CoreException in case the template is of an unknown context type
   * @throws BadLocationException in case the position is invalid in the compilation unit
   * @throws TemplateException in case the evaluation fails
   */
  public static String evaluateTemplate(
      Template template, ICompilationUnit compilationUnit, int position)
      throws CoreException, BadLocationException, TemplateException {

    TemplateContextType contextType =
        JavaPlugin.getDefault()
            .getTemplateContextRegistry()
            .getContextType(template.getContextTypeId());
    if (!(contextType instanceof CompilationUnitContextType))
      throw new CoreException(
          new Status(
              IStatus.ERROR,
              JavaUI.ID_PLUGIN,
              IStatus.ERROR,
              JavaTemplateMessages.JavaContext_error_message,
              null));

    IDocument document = new Document();
    if (compilationUnit != null && compilationUnit.exists())
      document.set(compilationUnit.getSource());

    CompilationUnitContext context =
        ((CompilationUnitContextType) contextType)
            .createContext(document, position, 0, compilationUnit);
    context.setForceEvaluation(true);

    TemplateBuffer buffer = context.evaluate(template);
    if (buffer == null) return null;
    return buffer.getString();
  }
  private int getCursorOffset(TemplateBuffer buffer) {
    TemplateVariable[] variables = buffer.getVariables();
    for (int i = 0; i != variables.length; i++) {
      TemplateVariable variable = variables[i];
      if (variable.getType().equals(GlobalTemplateVariables.Cursor.NAME))
        return variable.getOffsets()[0];
    }

    return buffer.getString().length();
  }
 public static TemplateVariable findVariable(
     final TemplateBuffer buffer, final String variableType) {
   final TemplateVariable[] variables = buffer.getVariables();
   for (final TemplateVariable cand : variables) {
     if (variableType.equals(cand.getType())) {
       return cand;
     }
   }
   return null;
 }
  /** @param template */
  protected void drop(Template template, int dropCaretOffset, int length) {
    IDocument document = editor.getTextEditor().getTextViewer().getDocument();
    ContextTypeRegistry registry = XMLUIPlugin.getDefault().getTemplateContextRegistry();
    if (registry != null) {
      TemplateContextType type = registry.getContextType(template.getContextTypeId());

      DocumentTemplateContext templateContext =
          new DocumentTemplateContext(type, document, new Position(dropCaretOffset, length));
      if (templateContext.canEvaluate(template)) {
        try {
          TemplateBuffer templateBuffer = templateContext.evaluate(template);
          String templateString = templateBuffer.getString();
          document.replace(dropCaretOffset, length, templateString);

          StyledText styledText = editor.getTextWidget();
          int position = getCursorOffset(templateBuffer) + dropCaretOffset;
          styledText.setCaretOffset(position);
          styledText.setFocus();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }
  }
  /*
   * @see ICompletionProposal#getAdditionalProposalInfo()
   */
  @Override
  public String getAdditionalProposalInfo() {
    try {
      fContext.setReadOnly(true);
      TemplateBuffer templateBuffer;
      try {
        templateBuffer = fContext.evaluate(fTemplate);
      } catch (TemplateException e) {
        return null;
      }

      IDocument document = new Document(templateBuffer.getString());
      IndentUtil.indentLines(document, new LineRange(0, document.getNumberOfLines()), null, null);
      return document.get();

    } catch (BadLocationException e) {
      handleException(
          JavaPlugin.getActiveWorkbenchShell(),
          new CoreException(
              new Status(
                  IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, "", e))); // $NON-NLS-1$
      return null;
    }
  }
  /**
   * Evalute JDT template code snippet
   *
   * @param effect
   * @param args
   * @return
   */
  private String evaluationJDTTemplate(Effect effect, TemplateBuffer buffer, Object[] args) {
    // Prepare a hash map of snippet variables for use by formulas.
    HashMap<String, String> variableMap = new HashMap<String, String>();
    variableMap.put("cursor", "/*${cursor}*/");

    for (int i = 0; i < args.length; i++) {
      String value = (String) args[i];
      variableMap.put(effect.getParameter(i).getName(), value);
    }
    StringBuilder sb = new StringBuilder(buffer.getString());
    TemplateVariable[] variables = buffer.getVariables();
    List<TemplateVariableElement> varList = new ArrayList<TemplateVariableElement>();
    for (TemplateVariable var : variables) {
      for (int off : var.getOffsets()) {
        String value = variableMap.get(var.getName());
        if (value != null)
          varList.add(new TemplateVariableElement(off, off + var.getLength(), value));
      }
    }

    for (int i = 0; i < varList.size(); i++)
      for (int j = i + 1; j < varList.size(); j++) {
        TemplateVariableElement itemi = varList.get(i);
        TemplateVariableElement itemj = varList.get(j);
        if (itemj.getStart() > itemi.getStart()) {
          TemplateVariableElement temp = itemi;
          varList.set(i, itemj);
          varList.set(j, temp);
        }
      }

    for (TemplateVariableElement item : varList) {
      sb.replace(item.getStart(), item.getLength(), item.getValue());
    }
    return sb.toString();
  }
 public EvaluatedTemplate(final TemplateBuffer buffer, final String lineDelimiter) {
   setContent(buffer.getString());
   final TemplateVariable selectStartVariable =
       findVariable(buffer, SourceEditorContextType.SELECT_START_VARIABLE);
   final TemplateVariable selectEndVariable =
       findVariable(buffer, SourceEditorContextType.SELECT_END_VARIABLE);
   if (selectStartVariable != null && selectStartVariable.getOffsets().length == 1) {
     fSelect =
         new Region(
             selectStartVariable.getOffsets()[0],
             (selectEndVariable != null && selectEndVariable.getOffsets().length == 1)
                 ? Math.max(
                     selectEndVariable.getOffsets()[0] - selectStartVariable.getOffsets()[0], 0)
                 : 0);
   }
   fLineDelimiter = lineDelimiter;
 }
  @Override
  public void resolve(final TemplateBuffer buffer, final TemplateContext context)
      throws MalformedTreeException, BadLocationException {
    Assert.isNotNull(context);
    final TemplateVariable[] variables = buffer.getVariables();

    final IDocument document = new Document(buffer.getString());
    final List<TextEdit> positions = TemplatesUtil.variablesToPositions(variables);
    final List<TextEdit> edits = new ArrayList<>(5);

    // iterate over all variables and try to resolve them
    for (int i = 0; i != variables.length; i++) {
      final TemplateVariable variable = variables[i];

      if (variable.isUnambiguous()) {
        continue;
      }

      // remember old values
      final int[] oldOffsets = variable.getOffsets();
      final int oldLength = variable.getLength();
      final String oldValue = variable.getDefaultValue();

      final String type = variable.getType();
      TemplateVariableResolver resolver = getResolver(type);
      if (resolver == null) {
        resolver = new TemplateVariableResolver();
        resolver.setType(type);
      }

      resolver.resolve(variable, context);

      final String value = variable.getDefaultValue();
      final String[] ln = document.getLegalLineDelimiters();
      final boolean multiLine = (TextUtilities.indexOf(ln, value, 0)[0] != -1);

      if (!oldValue.equals(value)) {
        // update buffer to reflect new value
        for (int k = 0; k != oldOffsets.length; k++) {
          String thisValue = value;
          if (multiLine) {
            final String indent = TemplatesUtil.searchIndentation(document, oldOffsets[k]);
            if (indent.length() > 0) {
              final StringBuilder temp = new StringBuilder(thisValue);
              int offset = 0;
              while (true) {
                final int[] search = TextUtilities.indexOf(ln, temp.toString(), offset);
                if (search[0] == -1) {
                  break;
                }
                offset = search[0] + ln[search[1]].length();
                temp.insert(offset, indent);
                offset += indent.length();
              }
              thisValue = temp.toString();
            }
          }
          edits.add(new ReplaceEdit(oldOffsets[k], oldLength, thisValue));
        }
      }
    }

    final MultiTextEdit edit = new MultiTextEdit(0, document.getLength());
    edit.addChildren(positions.toArray(new TextEdit[positions.size()]));
    edit.addChildren(edits.toArray(new TextEdit[edits.size()]));
    edit.apply(document, TextEdit.UPDATE_REGIONS);

    TemplatesUtil.positionsToVariables(positions, variables);

    buffer.setContent(document.get(), variables);
  }
  /*
   * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
   */
  @Override
  public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {

    IDocument document = viewer.getDocument();
    try {
      fContext.setReadOnly(false);
      int start;
      TemplateBuffer templateBuffer;
      try {
        beginCompoundChange(viewer);

        int oldReplaceOffset = getReplaceOffset();
        try {
          // this may already modify the document (e.g. add imports)
          templateBuffer = fContext.evaluate(fTemplate);
        } catch (TemplateException e1) {
          fSelectedRegion = fRegion;
          return;
        }

        start = getReplaceOffset();
        int shift = start - oldReplaceOffset;
        int end = Math.max(getReplaceEndOffset(), offset + shift);

        // insert template string
        if (end > document.getLength()) end = offset;
        String templateString = templateBuffer.getString();
        document.replace(start, end - start, templateString);
      } finally {
        endCompoundChange(viewer);
      }

      // translate positions
      LinkedModeModel model = new LinkedModeModel();
      TemplateVariable[] variables = templateBuffer.getVariables();

      MultiVariableGuess guess =
          fContext instanceof CompilationUnitContext
              ? ((CompilationUnitContext) fContext).getMultiVariableGuess()
              : null;

      boolean hasPositions = false;
      for (int i = 0; i != variables.length; i++) {
        TemplateVariable variable = variables[i];

        if (variable.isUnambiguous()) continue;

        LinkedPositionGroup group = new LinkedPositionGroup();

        int[] offsets = variable.getOffsets();
        int length = variable.getLength();

        LinkedPosition first;
        if (guess != null && variable instanceof MultiVariable) {
          first =
              new VariablePosition(
                  document, offsets[0] + start, length, guess, (MultiVariable) variable);
          guess.addSlave((VariablePosition) first);
        } else {
          String[] values = variable.getValues();
          ICompletionProposal[] proposals = new ICompletionProposal[values.length];
          for (int j = 0; j < values.length; j++) {
            ensurePositionCategoryInstalled(document, model);
            Position pos = new Position(offsets[0] + start, length);
            document.addPosition(getCategory(), pos);
            proposals[j] = new PositionBasedCompletionProposal(values[j], pos, length);
          }

          if (proposals.length > 1)
            first = new ProposalPosition(document, offsets[0] + start, length, proposals);
          else first = new LinkedPosition(document, offsets[0] + start, length);
        }

        for (int j = 0; j != offsets.length; j++)
          if (j == 0) group.addPosition(first);
          else group.addPosition(new LinkedPosition(document, offsets[j] + start, length));

        model.addGroup(group);
        hasPositions = true;
      }

      if (hasPositions) {
        model.forceInstall();
        JavaEditor editor = getJavaEditor();
        if (editor != null) {
          model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
        }

        LinkedModeUI ui = new EditorLinkedModeUI(model, viewer);
        ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
        ui.enter();

        fSelectedRegion = ui.getSelectedRegion();
      } else {
        fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
      }

    } catch (BadLocationException e) {
      JavaPlugin.log(e);
      openErrorDialog(viewer.getTextWidget().getShell(), e);
      fSelectedRegion = fRegion;
    } catch (BadPositionCategoryException e) {
      JavaPlugin.log(e);
      openErrorDialog(viewer.getTextWidget().getShell(), e);
      fSelectedRegion = fRegion;
    }
  }