/**
   * Creates a style range for the text viewer.
   *
   * @param viewer the text viewer
   * @return the new style range for the text viewer or <code>null</code>
   */
  private StyleRange createStyleRange(ITextViewer viewer) {
    StyledText text = viewer.getTextWidget();
    if (text == null || text.isDisposed()) {
      return null;
    }

    int widgetCaret = text.getCaretOffset();

    int modelCaret = 0;
    if (viewer instanceof ITextViewerExtension5) {
      ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
      modelCaret = extension.widgetOffset2ModelOffset(widgetCaret);
    } else {
      IRegion visibleRegion = viewer.getVisibleRegion();
      modelCaret = widgetCaret + visibleRegion.getOffset();
    }

    if (modelCaret >= getReplacementOffset() + getReplacementLength()) {
      return null;
    }

    int length = getReplacementOffset() + getReplacementLength() - modelCaret;

    Color foreground = getForegroundColor();
    Color background = getBackgroundColor();

    return new StyleRange(modelCaret, length, foreground, background);
  }
Пример #2
0
 /**
  * Convert a widget offset to the corresponding document offset.
  *
  * @param widgetOffset
  * @return document offset
  */
 private int getDocumentOffset(int widgetOffset) {
   if (fViewer instanceof ITextViewerExtension5) {
     ITextViewerExtension5 extension = (ITextViewerExtension5) fViewer;
     return extension.widgetOffset2ModelOffset(widgetOffset);
   }
   IRegion visible = fViewer.getVisibleRegion();
   if (widgetOffset > visible.getLength()) {
     return -1;
   }
   return widgetOffset + visible.getOffset();
 }
 /**
  * Convert a document offset to the corresponding widget offset.
  *
  * @param viewer the text viewer
  * @param documentOffset the document offset
  * @return widget offset
  */
 private int getWidgetOffset(ITextViewer viewer, int documentOffset) {
   if (viewer instanceof ITextViewerExtension5) {
     ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
     return extension.modelOffset2WidgetOffset(documentOffset);
   }
   IRegion visible = viewer.getVisibleRegion();
   int widgetOffset = documentOffset - visible.getOffset();
   if (widgetOffset > visible.getLength()) {
     return -1;
   }
   return widgetOffset;
 }
Пример #4
0
    // modified version from TextViewer
    private int computeOffsetAtLocation(ITextViewer textViewer, int x, int y) {

      StyledText styledText = textViewer.getTextWidget();
      IDocument document = textViewer.getDocument();

      if (document == null) return -1;

      try {
        int widgetOffset = styledText.getOffsetAtLocation(new Point(x, y));
        Point p = styledText.getLocationAtOffset(widgetOffset);
        if (p.x > x) widgetOffset--;

        if (textViewer instanceof ITextViewerExtension5) {
          ITextViewerExtension5 extension = (ITextViewerExtension5) textViewer;
          return extension.widgetOffset2ModelOffset(widgetOffset);
        } else {
          IRegion visibleRegion = textViewer.getVisibleRegion();
          return widgetOffset + visibleRegion.getOffset();
        }
      } catch (IllegalArgumentException e) {
        return -1;
      }
    }
  public void gotoMatchingBracket() {
    ITextViewer sourceViewer = langEditor.getSourceViewer_();

    IDocument document = sourceViewer.getDocument();
    if (document == null) return;

    IRegion selection = EditorUtils.getSignedSelection(sourceViewer);
    if (fPreviousSelections == null) initializePreviousSelectionList();

    IRegion region =
        getBracketMatcher().match(document, selection.getOffset(), selection.getLength());
    if (region == null) {
      region =
          getBracketMatcher()
              .findEnclosingPeerCharacters(document, selection.getOffset(), selection.getLength());
      initializePreviousSelectionList();
      fPreviousSelections.add(selection);
    } else {
      if (fPreviousSelections.size() == 2) {
        if (!selection.equals(fPreviousSelections.get(1))) {
          initializePreviousSelectionList();
        }
      } else if (fPreviousSelections.size() == 3) {
        if (selection.equals(fPreviousSelections.get(2))
            && !selection.equals(fPreviousSelections.get(0))) {
          IRegion originalSelection = fPreviousSelections.get(0);
          sourceViewer.setSelectedRange(
              originalSelection.getOffset(), originalSelection.getLength());
          sourceViewer.revealRange(originalSelection.getOffset(), originalSelection.getLength());
          initializePreviousSelectionList();
          return;
        }
        initializePreviousSelectionList();
      }
    }

    if (region == null) {
      langEditor.setStatusLineErrorMessage(
          LangEditorMessages.GotoMatchingBracket_error_noMatchingBracket);
      sourceViewer.getTextWidget().getDisplay().beep();
      return;
    }

    int offset = region.getOffset();
    int length = region.getLength();

    if (length < 1) return;

    int anchor = getBracketMatcher().getAnchor();
    // http://dev.eclipse.org/bugs/show_bug.cgi?id=34195
    int targetOffset = (ICharacterPairMatcher.RIGHT == anchor) ? offset + 1 : offset + length - 1;

    boolean visible = false;
    if (sourceViewer instanceof ITextViewerExtension5) {
      ITextViewerExtension5 extension = (ITextViewerExtension5) sourceViewer;
      visible = (extension.modelOffset2WidgetOffset(targetOffset) > -1);
    } else {
      IRegion visibleRegion = sourceViewer.getVisibleRegion();
      // http://dev.eclipse.org/bugs/show_bug.cgi?id=34195
      visible =
          (targetOffset >= visibleRegion.getOffset()
              && targetOffset <= visibleRegion.getOffset() + visibleRegion.getLength());
    }

    if (!visible) {
      langEditor.setStatusLineErrorMessage(
          LangEditorMessages.GotoMatchingBracket_error_bracketOutsideSelectedElement);
      sourceViewer.getTextWidget().getDisplay().beep();
      return;
    }

    int adjustment =
        getBracketMatcher()
            .getOffsetAdjustment(
                document, selection.getOffset() + selection.getLength(), selection.getLength());
    targetOffset += adjustment;
    int direction = (selection.getLength() == 0) ? 0 : ((selection.getLength() > 0) ? 1 : -1);
    if (fPreviousSelections.size() == 1 && direction < 0) {
      targetOffset++;
    }

    if (fPreviousSelections.size() > 0) {
      fPreviousSelections.add(new Region(targetOffset, direction));
    }
    sourceViewer.setSelectedRange(targetOffset, direction);
    sourceViewer.revealRange(targetOffset, direction);
  }