Package org.eclipse.text.edits

Examples of org.eclipse.text.edits.UndoEdit


  if (buffer instanceof IBuffer.ITextEditCapability) {
    return ((IBuffer.ITextEditCapability) buffer).applyTextEdit(edit, monitor);
  } else if (buffer != null) {
    IDocument document = buffer instanceof IDocument ? (IDocument) buffer : new DocumentAdapter(buffer);
    try {
      UndoEdit undoEdit= edit.apply(document);
      return undoEdit;
    } catch (MalformedTreeException e) {
      throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION);
    } catch (BadLocationException e) {
      throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION);


      String javaText = getJavaText();
      for (int i = 0; i < deltas.length; i++)
        System.out.println("pos[" + deltas[i].preOffset + ":" + deltas[i].preLength + "]" + javaText.substring(deltas[i].preOffset, deltas[i].preOffset + deltas[i].preLength)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      System.out.println("==============================================="); //$NON-NLS-1$
    }
    UndoEdit undo = null;
    // apply the edit to the java document
    try {
      undo = javaEdit.apply(getJavaDocument());
    }
    catch (MalformedTreeException e) {
      Logger.logException(e);
    }
    catch (BadLocationException e) {
      Logger.logException(e);
    }
    // now at this point Java positions are unreliable since they were
    // updated after applying java edit.

    String newJavaText = getJavaDocument().get();
    if (DEBUG)
      System.out.println("java post format text:\n" + newJavaText); //$NON-NLS-1$

    // record post edit data
    for (int i = 0; i < javaPositions.length; i++)
      deltas[i].setPostEditData(javaPositions[i].offset, javaPositions[i].length, javaPositions[i].isDeleted);

    // create appropriate text edits for deltas
    Position jspPos = null;
    String replaceText = ""; //$NON-NLS-1$
    for (int i = 0; i < deltas.length; i++) {
      jspPos = jspPositions[i];
      // can be null if it's an indirect mapping position
      // or if something was added into java that was not originally in
      // JSP (like a new import...)

      if (jspPos != null) {
        if (deltas[i].isDeleted) {
          jspEdits.add(new DeleteEdit(jspPos.offset, jspPos.length));
        }
        else {
          replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);

          // get rid of pre and post white space or fine tuned
          // adjustment later.
          // fix text here...
          replaceText = fixJspReplaceText(replaceText, jspPos);
          if (!(replaceText.length() == 0 && jspPos.length == 0))//Unwanted TextEdit can lead to MalformedTreeException.See: Bug 321977
              jspEdits.add(new ReplaceEdit(jspPos.offset, jspPos.length, replaceText));
        }
        if (DEBUG)
          debugReplace(deltas, jspPos, replaceText, i);
      }
      else {
        // the new Java text has no corresponding JSP position
        // possible new import?
        if (isImport(javaPositions[i].getOffset()) && replaceText.lastIndexOf("import ") != -1) { //$NON-NLS-1$
          replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
          String importText = replaceText.substring(replaceText.lastIndexOf("import "), replaceText.indexOf(";")); //$NON-NLS-1$ //$NON-NLS-2$
          // evenutally need to check if it's XML-JSP
          importText = "<%@page import=\"" + importText + "\" %>\n"; //$NON-NLS-1$ //$NON-NLS-2$
          jspEdits.add(new InsertEdit(0, importText));
        }
      }
    }
    TextEdit allJspEdits = createMultiTextEdit((TextEdit[]) jspEdits.toArray(new TextEdit[jspEdits.size()]));

    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=105632
    // undo the java edit
    // (so the underlying Java document still represents what's in the
    // editor)
    if (undo != null) {
      try {
        undo.apply(getJavaDocument());
      }
      catch (MalformedTreeException e) {
        Logger.logException(e);
      }
      catch (BadLocationException e) {

      String javaText = getJavaText();
      for (int i = 0; i < deltas.length; i++)
        System.out.println("pos[" + deltas[i].preOffset + ":" + deltas[i].preLength + "]" + javaText.substring(deltas[i].preOffset, deltas[i].preOffset + deltas[i].preLength)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      System.out.println("==============================================="); //$NON-NLS-1$
    }
    UndoEdit undo = null;
    // apply the edit to the java document
    try {
      undo = javaEdit.apply(getJavaDocument());
    }
    catch (MalformedTreeException e) {
      Logger.logException(e);
    }
    catch (BadLocationException e) {
      Logger.logException(e);
    }
    // now at this point Java positions are unreliable since they were
    // updated after applying java edit.

    String newJavaText = getJavaDocument().get();
    if (DEBUG)
      System.out.println("java post format text:\n" + newJavaText); //$NON-NLS-1$

    // record post edit data
    for (int i = 0; i < javaPositions.length; i++)
      deltas[i].setPostEditData(javaPositions[i].offset, javaPositions[i].length, javaPositions[i].isDeleted);

    // create appropriate text edits for deltas
    Position jspPos = null;
    String replaceText = ""; //$NON-NLS-1$
    for (int i = 0; i < deltas.length; i++) {
      jspPos = jspPositions[i];
      // can be null if it's an indirect mapping position
      // or if something was added into java that was not originally in
      // JSP (like a new import...)

      if (jspPos != null) {
        if (deltas[i].isDeleted) {
          jspEdits.add(new DeleteEdit(jspPos.offset, jspPos.length));
        }
        else {
          replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);

          // get rid of pre and post white space or fine tuned
          // adjustment later.
          // fix text here...
          replaceText = fixJspReplaceText(replaceText, jspPos.offset);

          jspEdits.add(new ReplaceEdit(jspPos.offset, jspPos.length, replaceText));
        }
        if (DEBUG)
          debugReplace(deltas, jspPos, replaceText, i);
      }
      else {
        // the new Java text has no corresponding JSP position
        // possible new import?
        if (isImport(javaPositions[i].getOffset()) && replaceText.lastIndexOf("import ") != -1) { //$NON-NLS-1$
          replaceText = newJavaText.substring(deltas[i].postOffset, deltas[i].postOffset + deltas[i].postLength);
          String importText = replaceText.substring(replaceText.lastIndexOf("import "), replaceText.indexOf(";")); //$NON-NLS-1$ //$NON-NLS-2$
          // evenutally need to check if it's XML-JSP
          importText = "<%@page import=\"" + importText + "\" %>\n"; //$NON-NLS-1$ //$NON-NLS-2$
          jspEdits.add(new InsertEdit(0, importText));
        }
      }
    }
    TextEdit allJspEdits = createMultiTextEdit((TextEdit[]) jspEdits.toArray(new TextEdit[jspEdits.size()]));

    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=105632
    // undo the java edit
    // (so the underlying Java document still represents what's in the
    // editor)
    if (undo != null) {
      try {
        undo.apply(getJavaDocument());
      }
      catch (MalformedTreeException e) {
        Logger.logException(e);
      }
      catch (BadLocationException e) {

  if (buffer instanceof IBuffer.ITextEditCapability) {
    return ((IBuffer.ITextEditCapability) buffer).applyTextEdit(edit, monitor);
  } else if (buffer != null) {
    IDocument document = buffer instanceof IDocument ? (IDocument) buffer : new DocumentAdapter(buffer);
    try {
      UndoEdit undoEdit= edit.apply(document);
      return undoEdit;
    } catch (MalformedTreeException e) {
      throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION);
    } catch (BadLocationException e) {
      throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION);

  if (buffer instanceof IBuffer.ITextEditCapability) {
    return ((IBuffer.ITextEditCapability) buffer).applyTextEdit(edit, monitor);
  } else if (buffer != null) {
    IDocument document = buffer instanceof IDocument ? (IDocument) buffer : new DocumentAdapter(buffer);
    try {
      UndoEdit undoEdit= edit.apply(document);
      return undoEdit;
    } catch (MalformedTreeException e) {
      throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION);
    } catch (BadLocationException e) {
      throw new JavaModelException(e, IJavaModelStatusConstants.BAD_TEXT_EDIT_LOCATION);

   * DeleteText
   */
  protected UndoEdit  DeleteText(IDocument doc2Alter, int offset, int length)
  {
    DeleteEdit charRemoval = new DeleteEdit(offset, length);
    UndoEdit removal = null;
    try {
      removal = charRemoval.apply(doc2Alter);
    } catch (BadLocationException e) {
      // Doing nowt right now. The Java editor doesn't, so should we?
    }

    return removal;
  }
 
  protected UndoEdit  InsertText(IDocument doc2Alter, int offset, String newText)
  {
    UndoEdit insertion = null;
    InsertEdit newEdit = new InsertEdit(offset, newText);
    try {
      newEdit.apply(doc2Alter);
    } catch(BadLocationException e) { // And context for the worst variable names goes to me!
      // Still doing nowt...

    m_cu = cu;
  }

  public void apply(IDocument document) {
    TextEdit edits = m_rewriter.rewriteAST(document, null);
    UndoEdit undo = null;
    try {
        undo = edits.apply(document);
    } catch(MalformedTreeException e) {
        e.printStackTrace();
    } catch(BadLocationException e) {

TOP

Related Classes of org.eclipse.text.edits.UndoEdit

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.