Java 类org.eclipse.jface.text.templates.DocumentTemplateContext 实例源码

项目:che    文件:JavaFormatter.java   
private boolean isReplacedAreaEmpty(TemplateContext context) {
  // don't trim the buffer if the replacement area is empty
  // case: surrounding empty lines with block
  if (context instanceof DocumentTemplateContext) {
    DocumentTemplateContext dtc = (DocumentTemplateContext) context;
    if (dtc.getStart() == dtc.getCompletionOffset())
      try {
        IDocument document = dtc.getDocument();
        int lineOffset = document.getLineInformationOfOffset(dtc.getStart()).getOffset();
        // only if we are at the beginning of the line
        if (lineOffset != dtc.getStart()) return false;

        // Does the selection only contain whitespace characters?
        if (document.get(dtc.getStart(), dtc.getEnd() - dtc.getStart()).trim().length() == 0)
          return true;
      } catch (BadLocationException x) {
        // ignore - this may happen when the document was modified after the initial invocation,
        // and the
        // context does not track the changes properly - don't trim in that case
        return true;
      }
  }
  return false;
}
项目:brainfuck    文件:ExpressionEvaluator.java   
@Override
int calculateValue(TemplateContext context) {
    String parseValue = null;
    if (this.isVariable) {
        parseValue = context.getVariable(this.value);
        if (parseValue == null) {
            String contextInfo = "";
            if (context instanceof DocumentTemplateContext) {
                contextInfo = ": '" + ((DocumentTemplateContext) context).getKey() + "'";
            }
            throw new VariableEvaluationException("Variable " + this.value + " is undefined in context"+ contextInfo);
        }
        parseValue = this.sign + parseValue;
    }
    else {
        parseValue = this.value;
    }
    try {
        return Integer.parseInt(parseValue);
    }
    catch (NumberFormatException ex) {
        throw new VariableEvaluationException(ex.getMessage(), ex);
    }
}
项目:gama    文件:GamlEditor.java   
/**
 * @see msi.gama.lang.gaml.ui.editor.IGamlEditor#applyTemplate(org.eclipse.jface.text.templates.Template)
 */

public void applyTemplateAtTheEnd(final Template t) {

    try {
        final IDocument doc = getDocument();
        int offset = doc.getLineOffset(doc.getNumberOfLines() - 1);
        doc.replace(offset, 0, "\n\n");
        offset += 2;
        final int length = 0;
        final Position pos = new Position(offset, length);
        final XtextTemplateContextType ct = new XtextTemplateContextType();
        final DocumentTemplateContext dtc = new DocumentTemplateContext(ct, doc, pos);
        final IRegion r = new Region(offset, length);
        final TemplateProposal tp = new TemplateProposal(t, dtc, r, null);
        tp.apply(getInternalSourceViewer(), (char) 0, 0, offset);
    } catch (final BadLocationException e) {
        e.printStackTrace();
    }
}
项目:Eclipse-Postfix-Code-Completion    文件:JavaFormatter.java   
private boolean isReplacedAreaEmpty(TemplateContext context) {
    // don't trim the buffer if the replacement area is empty
    // case: surrounding empty lines with block
    if (context instanceof DocumentTemplateContext) {
        DocumentTemplateContext dtc= (DocumentTemplateContext) context;
        if (dtc.getStart() == dtc.getCompletionOffset())
            try {
                IDocument document= dtc.getDocument();
                int lineOffset= document.getLineInformationOfOffset(dtc.getStart()).getOffset();
                //only if we are at the beginning of the line
                if (lineOffset != dtc.getStart())
                    return false;

                //Does the selection only contain whitespace characters?
                if (document.get(dtc.getStart(), dtc.getEnd() - dtc.getStart()).trim().length() == 0)
                    return true;
            } catch (BadLocationException x) {
                // ignore - this may happen when the document was modified after the initial invocation, and the
                // context does not track the changes properly - don't trim in that case
                return true;
            }
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:JavaFormatter.java   
private boolean isReplacedAreaEmpty(TemplateContext context) {
    // don't trim the buffer if the replacement area is empty
    // case: surrounding empty lines with block
    if (context instanceof DocumentTemplateContext) {
        DocumentTemplateContext dtc= (DocumentTemplateContext) context;
        if (dtc.getStart() == dtc.getCompletionOffset())
            try {
                IDocument document= dtc.getDocument();
                int lineOffset= document.getLineInformationOfOffset(dtc.getStart()).getOffset();
                //only if we are at the beginning of the line
                if (lineOffset != dtc.getStart())
                    return false;

                //Does the selection only contain whitespace characters?
                if (document.get(dtc.getStart(), dtc.getEnd() - dtc.getStart()).trim().length() == 0)
                    return true;
            } catch (BadLocationException x) {
                // ignore - this may happen when the document was modified after the initial invocation, and the
                // context does not track the changes properly - don't trim in that case
                return true;
            }
    }
    return false;
}
项目:glassmaker    文件:NewCardTemplatesWizardPage.java   
/**
 * Returns template string to insert.
 * 
 * @return String to insert or null if none is to be inserted
 */
String getTemplateString() {
    String templateString = null;

    Template template = getSelectedTemplate();
    if (template != null) {
        TemplateContextType contextType = GlassmakerUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CardContextType.CONTEXT_TYPE);
        IDocument document = new Document();
        TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0);
        try {
            TemplateBuffer buffer = context.evaluate(template);
            templateString = buffer.getString();
        }
        catch (Exception e) {
            GlassmakerUIPlugin.logError("Could not create template for new html", e); 
        }
    }

    return templateString;
}
项目:eclipse-silverstripedt    文件:NewSilverStripeTemplatesWizardPage.java   
/**
 * Returns template string to insert.
 * 
 * @return String to insert or null if none is to be inserted
 */
public String getTemplateString() {
    String templateString = null;

    Template template = getSelectedTemplate();
    if (template != null) {
        TemplateContextType contextType=SilverStripePDTPlugin.getDefault().getTemplateContextRegistry().getContextType(this._languageProvider.getTemplateContext());
        IDocument document = new Document();
        TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0);
        try {
            TemplateBuffer buffer = context.evaluate(template);
            templateString = buffer.getString();
        }
        catch (Exception e) {
            Logger.log(Logger.WARNING_DEBUG, "Could not create template for new html", e); //$NON-NLS-1$
        }
    }

    return templateString;
}
项目:goclipse    文件:JavaFormatter.java   
protected boolean isReplacedAreaEmpty(TemplateContext context) {
    // don't trim the buffer if the replacement area is empty
    // case: surrounding empty lines with block
    if (context instanceof DocumentTemplateContext) {
        DocumentTemplateContext dtc= (DocumentTemplateContext) context;
        if (dtc.getStart() == dtc.getCompletionOffset())
            try {
                IDocument document= dtc.getDocument();
                int lineOffset= document.getLineInformationOfOffset(dtc.getStart()).getOffset();
                //only if we are at the beginning of the line
                if (lineOffset != dtc.getStart())
                    return false;

                //Does the selection only contain whitespace characters?
                if (document.get(dtc.getStart(), dtc.getEnd() - dtc.getStart()).trim().length() == 0)
                    return true;
            } catch (BadLocationException x) {
                // ignore - this may happen when the document was modified after the initial invocation, and the
                // context does not track the changes properly - don't trim in that case
                return true;
            }
    }
    return false;
}
项目:ftc    文件:TweakedTemplateProposal.java   
/**
 * Returns the offset of the range in the document that will be replaced by
 * applying this template.
 *
 * @return the offset of the range in the document that will be replaced by
 *         applying this template
 * @since 3.1
 */
protected final int getReplaceOffset() {
    int start;
    if (fContext instanceof DocumentTemplateContext) {
        DocumentTemplateContext docContext = (DocumentTemplateContext) fContext;
        start = docContext.getStart();
    } else {
        start = fRegion.getOffset();
    }
    return start;
}
项目:ftc    文件:TweakedTemplateProposal.java   
/**
 * Returns the end offset of the range in the document that will be replaced
 * by applying this template.
 *
 * @return the end offset of the range in the document that will be replaced
 *         by applying this template
 * @since 3.1
 */
protected final int getReplaceEndOffset() {
    int end;
    if (fContext instanceof DocumentTemplateContext) {
        DocumentTemplateContext docContext = (DocumentTemplateContext) fContext;
        end = docContext.getEnd();
    } else {
        end = fRegion.getOffset() + fRegion.getLength();
    }
    return end;
}
项目:che    文件:TemplateProposal.java   
/**
 * Computes the relevance to match the relevance values generated by the core content assistant.
 *
 * @return a sensible relevance value.
 */
private int computeRelevance() {
  // see org.eclipse.jdt.internal.codeassist.RelevanceConstants
  final int R_DEFAULT = 0;
  final int R_INTERESTING = 5;
  final int R_CASE = 10;
  final int R_NON_RESTRICTED = 3;
  final int R_EXACT_NAME = 4;
  final int R_INLINE_TAG = 31;

  int base = R_DEFAULT + R_INTERESTING + R_NON_RESTRICTED;

  try {
    if (fContext instanceof DocumentTemplateContext) {
      DocumentTemplateContext templateContext = (DocumentTemplateContext) fContext;
      IDocument document = templateContext.getDocument();

      String content = document.get(fRegion.getOffset(), fRegion.getLength());
      if (content.length() > 0 && fTemplate.getName().startsWith(content)) base += R_CASE;
      if (fTemplate.getName().equalsIgnoreCase(content)) base += R_EXACT_NAME;
      if (fContext instanceof JavaDocContext) base += R_INLINE_TAG;
    }
  } catch (BadLocationException e) {
    // ignore - not a case sensitive match then
  }

  // see CompletionProposalCollector.computeRelevance
  // just under keywords, but better than packages
  final int TEMPLATE_RELEVANCE = 1;
  return base * 16 + TEMPLATE_RELEVANCE;
}
项目:che    文件:TemplateProposal.java   
/**
 * Returns the offset of the range in the document that will be replaced by applying this
 * template.
 *
 * @return the offset of the range in the document that will be replaced by applying this template
 */
protected final int getReplaceOffset() {
  int start;
  if (fContext instanceof DocumentTemplateContext) {
    DocumentTemplateContext docContext = (DocumentTemplateContext) fContext;
    start = docContext.getStart();
  } else {
    start = fRegion.getOffset();
  }
  return start;
}
项目:che    文件:TemplateProposal.java   
/**
 * Returns the end offset of the range in the document that will be replaced by applying this
 * template.
 *
 * @return the end offset of the range in the document that will be replaced by applying this
 *     template
 */
protected final int getReplaceEndOffset() {
  int end;
  if (fContext instanceof DocumentTemplateContext) {
    DocumentTemplateContext docContext = (DocumentTemplateContext) fContext;
    end = docContext.getEnd();
  } else {
    end = fRegion.getOffset() + fRegion.getLength();
  }
  return end;
}
项目:che    文件:TemplateProposal.java   
/**
 * Returns <code>true</code> if the proposal has a selection, e.g. will wrap some code.
 *
 * @return <code>true</code> if the proposals completion length is non zero
 * @since 3.2
 */
private boolean isSelectionTemplate() {
  if (fContext instanceof DocumentTemplateContext) {
    DocumentTemplateContext ctx = (DocumentTemplateContext) fContext;
    if (ctx.getCompletionLength() > 0) return true;
  }
  return false;
}
项目:KaiZen-OpenAPI-Editor    文件:JsonContentAssistProcessor.java   
@Override
protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region,
        int relevance) {
       if (context instanceof DocumentTemplateContext) {
           context = new SwaggerTemplateContext((DocumentTemplateContext) context);
       }
    return new StyledTemplateProposal(template, context, region, getImage(template), getTemplateLabel(template),
            relevance);
}
项目:brainfuck    文件:BfKeyResolver.java   
@Override
protected String resolve(TemplateContext context) {
    if (context instanceof DocumentTemplateContext) {
        return ((DocumentTemplateContext) context).getKey();
    }
    return null;
}
项目:gama    文件:GamlEditor.java   
public void applyTemplate(final Template t) {
    // TODO Create a specific context type (with GAML specific variables ??)
    final XtextTemplateContextType ct = new XtextTemplateContextType();
    final IDocument doc = getDocument();
    final ITextSelection selection = (ITextSelection) getSelectionProvider().getSelection();
    final int offset = selection.getOffset();
    final int length = selection.getLength();
    final Position pos = new Position(offset, length);
    final DocumentTemplateContext dtc = new DocumentTemplateContext(ct, doc, pos);
    final IRegion r = new Region(offset, length);
    final TemplateProposal tp = new TemplateProposal(t, dtc, r, null);
    tp.apply(getInternalSourceViewer(), (char) 0, 0, offset);
}
项目:Eclipse-Postfix-Code-Completion    文件:JavaTemplatesPage.java   
@Override
protected boolean isValidTemplate(IDocument document, Template template, int offset, int length) {
    String[] contextIds= getContextTypeIds(document, offset);
    for (int i= 0; i < contextIds.length; i++) {
        if (contextIds[i].equals(template.getContextTypeId())) {
            DocumentTemplateContext context= getContext(document, template, offset, length);
            return context.canEvaluate(template) || isTemplateAllowed(context, template);
        }
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion    文件:JavaTemplatesPage.java   
/**
 * Check whether the template is allowed eventhough the context can't evaluate it. This is
 * needed because the Dropping of a template is more lenient than ctl-space invoked code assist.
 * 
 * @param context the template context
 * @param template the template
 * @return true if the template is allowed
 */
private boolean isTemplateAllowed(DocumentTemplateContext context, Template template) {
    int offset;
    try {
        if (template.getContextTypeId().equals(JavaDocContextType.ID)) {
            return (offset= context.getCompletionOffset()) > 0 && Character.isWhitespace(context.getDocument().getChar(offset - 1));
        } else {
            return ((offset= context.getCompletionOffset()) > 0 && !isTemplateNamePart(context.getDocument().getChar(offset - 1)));
        }
    } catch (BadLocationException e) {
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion    文件:TemplateProposal.java   
/**
 * Computes the relevance to match the relevance values generated by the
 * core content assistant.
 *
 * @return a sensible relevance value.
 */
private int computeRelevance() {
    // see org.eclipse.jdt.internal.codeassist.RelevanceConstants
    final int R_DEFAULT= 0;
    final int R_INTERESTING= 5;
    final int R_CASE= 10;
    final int R_NON_RESTRICTED= 3;
    final int R_EXACT_NAME = 4;
    final int R_INLINE_TAG = 31;

    int base= R_DEFAULT + R_INTERESTING + R_NON_RESTRICTED;

    try {
        if (fContext instanceof DocumentTemplateContext) {
            DocumentTemplateContext templateContext= (DocumentTemplateContext) fContext;
            IDocument document= templateContext.getDocument();

            String content= document.get(fRegion.getOffset(), fRegion.getLength());
            if (content.length() > 0 && fTemplate.getName().startsWith(content))
                base += R_CASE;
            if (fTemplate.getName().equalsIgnoreCase(content))
                base += R_EXACT_NAME;
            if (fContext instanceof JavaDocContext)
                base += R_INLINE_TAG;
        }
    } catch (BadLocationException e) {
        // ignore - not a case sensitive match then
    }

    // see CompletionProposalCollector.computeRelevance
    // just under keywords, but better than packages
    final int TEMPLATE_RELEVANCE= 1;
    return base * 16 + TEMPLATE_RELEVANCE;
}
项目:Eclipse-Postfix-Code-Completion    文件:TemplateProposal.java   
/**
 * Returns the offset of the range in the document that will be replaced by
 * applying this template.
 *
 * @return the offset of the range in the document that will be replaced by
 *         applying this template
 */
protected int getReplaceOffset() {
    int start;
    if (fContext instanceof DocumentTemplateContext) {
        DocumentTemplateContext docContext = (DocumentTemplateContext)fContext;
        start= docContext.getStart();
    } else {
        start= fRegion.getOffset();
    }
    return start;
}
项目:Eclipse-Postfix-Code-Completion    文件:TemplateProposal.java   
/**
 * Returns the end offset of the range in the document that will be replaced
 * by applying this template.
 *
 * @return the end offset of the range in the document that will be replaced
 *         by applying this template
 */
protected final int getReplaceEndOffset() {
    int end;
    if (fContext instanceof DocumentTemplateContext) {
        DocumentTemplateContext docContext = (DocumentTemplateContext)fContext;
        end= docContext.getEnd();
    } else {
        end= fRegion.getOffset() + fRegion.getLength();
    }
    return end;
}
项目:Eclipse-Postfix-Code-Completion    文件:TemplateProposal.java   
/**
 * Returns <code>true</code> if the proposal has a selection, e.g. will wrap some code.
 *
 * @return <code>true</code> if the proposals completion length is non zero
 * @since 3.2
 */
private boolean isSelectionTemplate() {
    if (fContext instanceof DocumentTemplateContext) {
        DocumentTemplateContext ctx= (DocumentTemplateContext) fContext;
        if (ctx.getCompletionLength() > 0)
            return true;
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:JavaTemplatesPage.java   
@Override
protected boolean isValidTemplate(IDocument document, Template template, int offset, int length) {
    String[] contextIds= getContextTypeIds(document, offset);
    for (int i= 0; i < contextIds.length; i++) {
        if (contextIds[i].equals(template.getContextTypeId())) {
            DocumentTemplateContext context= getContext(document, template, offset, length);
            return context.canEvaluate(template) || isTemplateAllowed(context, template);
        }
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:JavaTemplatesPage.java   
/**
 * Check whether the template is allowed eventhough the context can't evaluate it. This is
 * needed because the Dropping of a template is more lenient than ctl-space invoked code assist.
 * 
 * @param context the template context
 * @param template the template
 * @return true if the template is allowed
 */
private boolean isTemplateAllowed(DocumentTemplateContext context, Template template) {
    int offset;
    try {
        if (template.getContextTypeId().equals(JavaDocContextType.ID)) {
            return (offset= context.getCompletionOffset()) > 0 && Character.isWhitespace(context.getDocument().getChar(offset - 1));
        } else {
            return ((offset= context.getCompletionOffset()) > 0 && !isTemplateNamePart(context.getDocument().getChar(offset - 1)));
        }
    } catch (BadLocationException e) {
    }
    return false;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:TemplateProposal.java   
/**
 * Computes the relevance to match the relevance values generated by the
 * core content assistant.
 *
 * @return a sensible relevance value.
 */
private int computeRelevance() {
    // see org.eclipse.jdt.internal.codeassist.RelevanceConstants
    final int R_DEFAULT= 0;
    final int R_INTERESTING= 5;
    final int R_CASE= 10;
    final int R_NON_RESTRICTED= 3;
    final int R_EXACT_NAME = 4;
    final int R_INLINE_TAG = 31;

    int base= R_DEFAULT + R_INTERESTING + R_NON_RESTRICTED;

    try {
        if (fContext instanceof DocumentTemplateContext) {
            DocumentTemplateContext templateContext= (DocumentTemplateContext) fContext;
            IDocument document= templateContext.getDocument();

            String content= document.get(fRegion.getOffset(), fRegion.getLength());
            if (fTemplate.getName().startsWith(content))
                base += R_CASE;
            if (fTemplate.getName().equalsIgnoreCase(content))
                base += R_EXACT_NAME;
            if (fContext instanceof JavaDocContext)
                base += R_INLINE_TAG;
        }
    } catch (BadLocationException e) {
        // ignore - not a case sensitive match then
    }

    // see CompletionProposalCollector.computeRelevance
    // just under keywords, but better than packages
    final int TEMPLATE_RELEVANCE= 1;
    return base * 16 + TEMPLATE_RELEVANCE;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:TemplateProposal.java   
/**
 * Returns the offset of the range in the document that will be replaced by
 * applying this template.
 *
 * @return the offset of the range in the document that will be replaced by
 *         applying this template
 */
protected int getReplaceOffset() {
    int start;
    if (fContext instanceof DocumentTemplateContext) {
        DocumentTemplateContext docContext = (DocumentTemplateContext)fContext;
        start= docContext.getStart();
    } else {
        start= fRegion.getOffset();
    }
    return start;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:TemplateProposal.java   
/**
 * Returns the end offset of the range in the document that will be replaced
 * by applying this template.
 *
 * @return the end offset of the range in the document that will be replaced
 *         by applying this template
 */
protected final int getReplaceEndOffset() {
    int end;
    if (fContext instanceof DocumentTemplateContext) {
        DocumentTemplateContext docContext = (DocumentTemplateContext)fContext;
        end= docContext.getEnd();
    } else {
        end= fRegion.getOffset() + fRegion.getLength();
    }
    return end;
}
项目:Eclipse-Postfix-Code-Completion-Juno38    文件:TemplateProposal.java   
/**
 * Returns <code>true</code> if the proposal has a selection, e.g. will wrap some code.
 *
 * @return <code>true</code> if the proposals completion length is non zero
 * @since 3.2
 */
private boolean isSelectionTemplate() {
    if (fContext instanceof DocumentTemplateContext) {
        DocumentTemplateContext ctx= (DocumentTemplateContext) fContext;
        if (ctx.getCompletionLength() > 0)
            return true;
    }
    return false;
}
项目:goclipse    文件:LangTemplateProposal.java   
/**
 * Returns <code>true</code> if the proposal has a selection, e.g. will wrap some code.
 *
 * @return <code>true</code> if the proposals completion length is non zero
 * @since 3.2
 */
private boolean isSelectionTemplate() {
    if (getContext() instanceof DocumentTemplateContext) {
        DocumentTemplateContext ctx= (DocumentTemplateContext) getContext();
        if (ctx.getCompletionLength() > 0)
            return true;
    }
    return false;
}
项目:ftc    文件:FtcDocumentTemplateContext.java   
public FtcDocumentTemplateContext(DocumentTemplateContext context, Template template) {
    this(context.getContextType(), context.getDocument(), context.getStart(), context.getCompletionLength(), template);
}
项目:ftc    文件:FtcTemplateProposal.java   
private static DocumentTemplateContext getDc(TemplateContext context)
{
    Check.isTrue(context instanceof DocumentTemplateContext);
    return (DocumentTemplateContext)context;
}
项目:KaiZen-OpenAPI-Editor    文件:SwaggerTemplateContext.java   
public SwaggerTemplateContext(DocumentTemplateContext context) {
    super(context.getContextType(), context.getDocument(), context.getCompletionOffset(), context
            .getCompletionLength());
}
项目:byteman-editor    文件:BytemanRuleKeywordAssistProcessor.java   
protected ICompletionProposal createRuleTemplate(IDocument document, int offset) {
    TemplateContext context = new DocumentTemplateContext(new TemplateContextType(), document, offset, 0);
    TemplateProposal proposal = new TemplateProposal(TEMPLATE_RULE, context, new Region(offset, 0), BytemanEditorPluginImages.IMG_OBJS_TEMPLATE);
    return proposal;
}
项目:ftc    文件:TweakedTemplateCompletionProcessor.java   
/**
 * Creates a concrete template context for the given region in the document. This involves finding out which
 * context type is valid at the given location, and then creating a context of this type. The default implementation
 * returns a <code>DocumentTemplateContext</code> for the context type at the given location.
 *
 * @param viewer the viewer for which the context is created
 * @param region the region into <code>document</code> for which the context is created
 * @return a template context that can handle template insertion at the given location, or <code>null</code>
 */
protected TemplateContext createContext(ITextViewer viewer, IRegion region) {
    TemplateContextType contextType= getContextType(viewer, region);
    if (contextType != null) {
        IDocument document= viewer.getDocument();
        return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength());
    }
    return null;
}
项目:Pydev    文件:AbstractTemplateCodeCompletion.java   
/**
 * Creates a concrete template context for the given region in the document. This involves finding out which
 * context type is valid at the given location, and then creating a context of this type. The default implementation
 * returns a <code>DocumentTemplateContext</code> for the context type at the given location.
 *
 * @param viewer the viewer for which the context is created
 * @param region the region into <code>document</code> for which the context is created
 * @return a template context that can handle template insertion at the given location, or <code>null</code>
 */
protected TemplateContext createContext(ITextViewer viewer, IRegion region, IDocument document) {
    TemplateContextType contextType = getContextType(viewer, region);
    if (contextType != null) {
        return new DocumentTemplateContext(contextType, document, region.getOffset(), region.getLength());
    }
    return null;
}