コード例 #1
0
  protected void doFormatPreview() {
    if (fPreviewText == null) {
      fPreviewDocument.set(""); // $NON-NLS-1$
      return;
    }
    fPreviewDocument.set(fPreviewText);

    fSourceViewer.setRedraw(false);
    //		final IFormattingContext context = new JavaFormattingContext();
    try {
      //			final IContentFormatter formatter =
      //	fViewerConfiguration.getContentFormatter(fSourceViewer);
      //			if (formatter instanceof IContentFormatterExtension) {
      //				final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
      //				context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, fWorkingValues);
      //				context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT,
      // Boolean.valueOf(true));
      //				extension.format(fPreviewDocument, context);
      //			} else
      //				formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
      final IContentFormatter formatter = new Formatter(fWorkingValues);
      formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
    } catch (Exception e) {
      final IStatus status =
          new Status(
              IStatus.ERROR,
              FormatterPlugin.PLUGIN_ID,
              IStatus.ERROR,
              FormatterMessages.JavaPreview_formatter_exception,
              e);
      FormatterPlugin.log(status);
    } finally {
      //		    context.dispose();
      fSourceViewer.setRedraw(true);
    }
  }
コード例 #2
0
ファイル: SourceViewer.java プロジェクト: neelance/jface4ruby
  /*
   * @see ITextOperationTarget#doOperation(int)
   */
  public void doOperation(int operation) {

    if (getTextWidget() == null || (!redraws() && operation != FORMAT)) return;

    switch (operation) {
      case CONTENTASSIST_PROPOSALS:
        fContentAssistant.showPossibleCompletions();
        return;
      case CONTENTASSIST_CONTEXT_INFORMATION:
        fContentAssistant.showContextInformation();
        return;
      case QUICK_ASSIST:
        // FIXME: must find a way to post to the status line
        /* String msg= */ fQuickAssistAssistant.showPossibleQuickAssists();
        // setStatusLineErrorMessage(msg);
        return;
      case INFORMATION:
        fInformationPresenter.showInformation();
        return;
      case FORMAT:
        {
          final Point selection = rememberSelection();
          final IRewriteTarget target = getRewriteTarget();
          final IDocument document = getDocument();
          IFormattingContext context = null;
          DocumentRewriteSession rewriteSession = null;

          if (document instanceof IDocumentExtension4) {
            IDocumentExtension4 extension = (IDocumentExtension4) document;
            DocumentRewriteSessionType type =
                (selection.y == 0 && document.getLength() > 1000) || selection.y > 1000
                    ? DocumentRewriteSessionType.SEQUENTIAL
                    : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
            rewriteSession = extension.startRewriteSession(type);
          } else {
            setRedraw(false);
            target.beginCompoundChange();
          }

          try {

            final String rememberedContents = document.get();

            try {

              if (fContentFormatter instanceof IContentFormatterExtension) {
                final IContentFormatterExtension extension =
                    (IContentFormatterExtension) fContentFormatter;
                context = createFormattingContext();
                if (selection.y == 0) {
                  context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
                } else {
                  context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
                  context.setProperty(
                      FormattingContextProperties.CONTEXT_REGION,
                      new Region(selection.x, selection.y));
                }
                extension.format(document, context);
              } else {
                IRegion r;
                if (selection.y == 0) {
                  IRegion coverage = getModelCoverage();
                  r = coverage == null ? new Region(0, 0) : coverage;
                } else {
                  r = new Region(selection.x, selection.y);
                }
                fContentFormatter.format(document, r);
              }

              updateSlaveDocuments(document);

            } catch (RuntimeException x) {
              // fire wall for https://bugs.eclipse.org/bugs/show_bug.cgi?id=47472
              // if something went wrong we undo the changes we just did
              // TODO to be removed after 3.0 M8
              document.set(rememberedContents);
              throw x;
            }

          } finally {

            if (document instanceof IDocumentExtension4) {
              IDocumentExtension4 extension = (IDocumentExtension4) document;
              extension.stopRewriteSession(rewriteSession);
            } else {
              target.endCompoundChange();
              setRedraw(true);
            }

            restoreSelection();
            if (context != null) context.dispose();
          }
          return;
        }
      default:
        super.doOperation(operation);
    }
  }