@Override
  public void run(IAction action) {

    try {
      IWorkbenchWindow windoow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
      RefactoringWizard wizard = new RefactoringWizard();

      ITextSelection textSelection = getITextSelection();

      int startLine = textSelection.getStartLine() + 1;
      int endLine = textSelection.getEndLine() + 1;
      InOneMethodVisitor iomv = new InOneMethodVisitor(getICompilationUnit(), startLine, endLine);
      if (iomv.isInOne()) {
        wizard.init(windoow.getWorkbench(), getICompilationUnit(), getITextSelection());
        WizardDialog dialog = new WizardDialog(shell, wizard);
        dialog.open();
      } else {
        throw new Exception("Selected Code Block Illegal!");
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      alert(e);
      e.printStackTrace();
    }
  }
示例#2
0
 public static @Nullable Notifier getTextSelection(
     final @NonNull ITextSelection selection, @Nullable IWorkbenchPart part) {
   if (selection.getStartLine() < 0 || selection.getEndLine() < 0) {
     return null;
   }
   if (!(part instanceof XtextEditor)) {
     return null;
   }
   IXtextDocument document = ((XtextEditor) part).getDocument();
   Notifier selectedObject =
       document.readOnly(
           new IUnitOfWork<Notifier, XtextResource>() {
             public Notifier exec(XtextResource xtextResource) {
               if (xtextResource == null) {
                 return null;
               }
               IParseResult parseResult = xtextResource.getParseResult();
               if (parseResult == null) throw new NullPointerException("parseResult is null");
               ICompositeNode rootNode = parseResult.getRootNode();
               INode lastVisibleNode =
                   NodeModelUtils.findLeafNodeAtOffset(rootNode, selection.getOffset());
               if (lastVisibleNode == null) {
                 return null;
               }
               EObject currentModel = NodeModelUtils.findActualSemanticObjectFor(lastVisibleNode);
               //				if (currentModel instanceof Pivotable) {
               //					return ((Pivotable)currentModel).getPivot();
               //				}
               //				else {
               return currentModel;
               //				}
             }
           });
   return selectedObject;
 }
  @Override
  public void run() {
    ITextSelection ts = (ITextSelection) editor.getSelectionProvider().getSelection();
    line = ts.getEndLine();
    try {
      terminateWithSemicolon();
      boolean changed;
      int count = 0;
      do {
        changed = terminateWithBrace();
        count++;
      } while (changed && count < 5);

      //			change = new DocumentChange("Terminate Statement", doc);
      //			change.setEdit(new MultiTextEdit());
      //			editor.getParseController().parse(doc, new NullProgressMonitor(), null);
      //			terminateWithParen(doc, change);
      //			change.perform(new NullProgressMonitor());

      editor.scheduleParsing();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * Extend the selection that the action will work on. Default implementation, extend to whole
  * lines. Might be overridden.
  *
  * @param document text {@link IDocument}
  * @param selection original selection
  * @return new {@link ITextSelection} extended to the whole lines intersected by selection
  */
 public static ITextSelection extendSelectionToWholeLines(
     final IDocument document, final ITextSelection selection) {
   final int startLine = selection.getStartLine();
   final int endLine = selection.getEndLine();
   int startLineOffset;
   try {
     startLineOffset = document.getLineOffset(startLine);
     final int endTextOffset = document.getLineOffset(endLine) + document.getLineLength(endLine);
     return new TextSelection(document, startLineOffset, endTextOffset - startLineOffset);
   } catch (final BadLocationException e) {
     e.printStackTrace();
   }
   return selection;
 }
示例#5
0
  /**
   * Is the given selection single-line commented?
   *
   * @param selection Selection to check
   * @return <code>true</code> iff all selected lines are commented
   */
  private boolean isSelectionCommented(ISelection selection) {
    if (!(selection instanceof ITextSelection)) return false;

    ITextSelection textSelection = (ITextSelection) selection;
    if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0) return false;

    IDocument document = fViewer.getDocument();

    try {

      IRegion block = getTextBlockFromSelection(textSelection, document);
      ITypedRegion[] regions =
          TextUtilities.computePartitioning(
              document, fDocumentPartitioning, block.getOffset(), block.getLength(), false);

      //			int lineCount= 0;
      int[] lines = new int[regions.length * 2]; // [startline, endline, startline, endline, ...]
      for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
        // start line of region
        lines[j] = getFirstCompleteLineOfRegion(regions[i], document);
        // end line of region
        int length = regions[i].getLength();
        int offset = regions[i].getOffset() + length;
        if (length > 0) offset--;
        lines[j + 1] = (lines[j] == -1 ? -1 : document.getLineOfOffset(offset));
        //				lineCount += lines[j + 1] - lines[j] + 1;
      }

      // Perform the check
      for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
        String[] prefixes = fPrefixesMap.get(regions[i].getType());
        if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0)
          if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document)) return false;
      }

      return true;

    } catch (BadLocationException x) {
      // should not happen
      // FIXME			Activator.getPlugin().log(x);
    }

    return false;
  }
  @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();
    }
  }