Exemplo n.º 1
0
 private static void initializeHighlightRange(IEditorPart editorPart) {
   if (editorPart instanceof ITextEditor) {
     IAction toggleAction =
         editorPart
             .getEditorSite()
             .getActionBars()
             .getGlobalActionHandler(
                 ITextEditorActionDefinitionIds.TOGGLE_SHOW_SELECTED_ELEMENT_ONLY);
     boolean enable = toggleAction != null;
     if (enable && editorPart instanceof DartEditor) {
       enable =
           DartToolsPlugin.getDefault()
               .getPreferenceStore()
               .getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
     } else {
       enable = enable && toggleAction.isEnabled() && toggleAction.isChecked();
     }
     if (enable) {
       if (toggleAction instanceof TextEditorAction) {
         // Reset the action
         ((TextEditorAction) toggleAction).setEditor(null);
         // Restore the action
         ((TextEditorAction) toggleAction).setEditor((ITextEditor) editorPart);
       } else {
         // Uncheck
         toggleAction.run();
         // Check
         toggleAction.run();
       }
     }
   }
 }
 /**
  * Implementation of the <code>IUpdate</code> prototype method discovers the operation through the
  * current editor's <code>ITextOperationTarget</code> adapter, and sets the enabled state
  * accordingly.
  */
 public void update() {
   super.update();
   if (!canModifyEditor()) {
     setEnabled(false);
     return;
   }
   ITextEditor editor = getTextEditor();
   if (fOperationTarget == null && editor != null)
     fOperationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
   boolean isEnabled =
       (fOperationTarget != null
           && fOperationTarget.canDoOperation(ITextOperationTarget.PREFIX)
           && fOperationTarget.canDoOperation(ITextOperationTarget.STRIP_PREFIX));
   setEnabled(isEnabled);
 }
  /* (non-Javadoc)
   * @see org.eclipse.ui.texteditor.IUpdate#update()
   */
  @Override
  public void update() {
    super.update();

    if (!isReadOnlyOperation() && !canModifyEditor()) {
      setEnabled(false);
      return;
    }

    ITextEditor editor = getTextEditor();
    if (fOperationTarget == null && editor != null && fOperationCode != -1)
      fOperationTarget = (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);

    boolean isEnabled =
        (fOperationTarget != null && fOperationTarget.canDoOperation(fOperationCode));
    setEnabled(isEnabled);
  }
Exemplo n.º 4
0
  @Override
  public void run() {
    super.run();
    final ISelection sel = getSelection();
    if (sel == null || sel.isEmpty() || !(sel instanceof ITextSelection)) {
      return;
    }
    if (!validateEditorInputState()) {
      return;
    }
    final ITextEditor textEditor = getTextEditor();
    final IDocument document =
        textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    final ITextSelection selection = extendSelectionToWholeLines(document, (ITextSelection) sel);
    final ITextSelection getSelection = getTextSelection(document, selection);
    String text;
    OtpErlangObject r1 = null;
    try {
      text = document.get(getSelection.getOffset(), getSelection.getLength());
      // call erlang, with selection within text
      r1 =
          callErlang(selection.getOffset() - getSelection.getOffset(), selection.getLength(), text);
    } catch (final Exception e) {
      e.printStackTrace();
    }
    final String newText = Util.stringValue(r1);
    if (newText == null) {
      final Status status =
          new Status(
              IStatus.ERROR,
              ErlangCore.PLUGIN_ID,
              ErlangStatus.INTERNAL_ERROR.getValue(),
              "indent returned " + r1 + " instead of a string",
              null);
      ErlLogger.error("INTERNAL ERROR: indent returned " + r1 + " instead of a string");

      ErrorDialog.openError(
          textEditor.getSite().getShell(),
          ActionMessages.IndentAction_error_message,
          String.valueOf(r1),
          status);
      return;
    }
    final int startLine = selection.getStartLine();
    final int endLine = selection.getEndLine();
    final int nLines = endLine - startLine + 1;
    final Runnable runnable =
        new Runnable() {
          @Override
          public void run() {
            final IRewriteTarget target =
                (IRewriteTarget) textEditor.getAdapter(IRewriteTarget.class);
            if (target != null) {
              target.beginCompoundChange();
              if (nLines > 1) {
                target.setRedraw(false);
              }
            }
            try {
              // ErlLogger.debug("'"+newText+"'");
              if (!document.get(selection.getOffset(), selection.getLength()).equals(newText)) {
                document.replace(selection.getOffset(), selection.getLength(), newText);
              }
              selectAndReveal(selection.getOffset(), newText.length());
            } catch (final BadLocationException e) {
              ErlLogger.warn(e);
            }
            if (target != null) {
              target.endCompoundChange();
              if (nLines > 1) {
                target.setRedraw(true);
              }
            }
          }
        };
    if (nLines > 50) {
      final Display display =
          textEditor.getEditorSite().getWorkbenchWindow().getShell().getDisplay();
      BusyIndicator.showWhile(display, runnable);
    } else {
      runnable.run();
    }
  }
 /* (non-Javadoc)
  * @see org.eclipse.ui.texteditor.TextEditorAction#setEditor(org.eclipse.ui.texteditor.ITextEditor)
  */
 @Override
 public void setEditor(ITextEditor editor) {
   super.setEditor(editor);
   fOperationTarget = null;
 }