コード例 #1
0
  /**
   * Implementation of the <code>IAction</code> prototype. Checks if the selected lines are all
   * commented or not and uncomments/comments them respectively.
   */
  @Override
  public void run() {
    if (fOperationTarget == null || fDocumentPartitioning == null || fPrefixesMap == null) return;

    final int operationCode;
    if (isSelectionCommented(fViewer.getSelectionProvider().getSelection()))
      operationCode = ITextOperationTarget.STRIP_PREFIX;
    else operationCode = ITextOperationTarget.PREFIX;

    Shell shell = fViewer.getTextWidget().getShell();
    if (!fOperationTarget.canDoOperation(operationCode)) {
      if (shell != null)
        MessageDialog.openError(
            shell,
            "An error occured",
            "ToggleComment_error_message=An error occurred while toggling comments."); //$NON-NLS-1$//$NON-NLS-2$
      return;
    }

    Display display = null;
    if (shell != null && !shell.isDisposed()) display = shell.getDisplay();

    BusyIndicator.showWhile(
        display,
        new Runnable() {
          public void run() {
            fOperationTarget.doOperation(operationCode);
          }
        });
  }
コード例 #2
0
  /** 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()]);
  }