private boolean isSelectionBasedMatch(Template template, TemplateContext context) {
   String pattern = template.getPattern();
   Set<String> vars = new HashSet<>();
   Matcher matcher = VARIABLE_PATTERN.matcher(pattern);
   while (matcher.find()) {
     String variableName = matcher.group(1);
     if (vars.add(variableName)) {
       String variable = context.getVariable(variableName);
       if (variable != null && variable.length() > 0) {
         return true;
       }
     }
   }
   return false;
 }
  /*
   * @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;
    }
  }
 @Override
 protected String resolve(final TemplateContext context) {
   return context.getVariable(getType());
 }
  /** Override to improve matching accuracy */
  @Override
  public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {

    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();

    // adjust offset to end of normalized selection
    if (selection.getOffset() == offset) {
      offset = selection.getOffset() + selection.getLength();
    }

    String prefix = extractPrefix(viewer, offset);
    Region region = new Region(offset - prefix.length(), prefix.length());
    TemplateContext context = createContext(viewer, region);
    if (context == null) {
      return new ICompletionProposal[0];
    }
    Region selectionRegion = new Region(selection.getOffset(), selection.getLength());
    TemplateContext selectionContext = createContext(viewer, selectionRegion);

    int lineOffset = 0;
    try {
      IRegion lineInformationOfOffset = viewer.getDocument().getLineInformationOfOffset(offset);
      lineOffset = offset - lineInformationOfOffset.getOffset();
    } catch (BadLocationException e1) {
      // ignore
    }

    String selectionText = selection.getText();
    context.setVariable("selection", selectionText); // $NON-NLS-1$
    selectionContext.setVariable("selection", selectionText); // $NON-NLS-1$
    context.setVariable("text", selectionText); // $NON-NLS-1$
    selectionContext.setVariable("text", selectionText); // $NON-NLS-1$

    Template[] templates = getTemplates(context.getContextType().getId());

    List<ICompletionProposal> matches = new ArrayList<>(templates.length);
    for (Template template : templates) {
      try {
        context.getContextType().validate(template.getPattern());
      } catch (TemplateException e) {
        continue;
      }
      if (!template.matches(prefix, context.getContextType().getId())) {
        continue;
      }
      boolean selectionBasedMatch = isSelectionBasedMatch(template, context);
      if (template.getName().startsWith(prefix) || selectionBasedMatch) {

        int relevance = getRelevance(template, lineOffset, prefix);
        if (selectionBasedMatch) {
          matches.add(
              createProposal(template, selectionContext, (IRegion) selectionRegion, relevance));
        } else {
          matches.add(createProposal(template, context, (IRegion) region, relevance));
        }
      }
    }

    Collections.sort(matches, proposalComparator);

    return matches.toArray(new ICompletionProposal[matches.size()]);
  }
  /*
   * @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;
    }
  }