コード例 #1
0
 /**
  * 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;
 }
コード例 #2
0
  /**
   * 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;
  }
コード例 #3
0
 /**
  * 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;
 }
コード例 #4
0
 /**
  * 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;
 }
コード例 #5
0
  /** @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);
        }
      }
    }
  }