Package org.eclipse.text.edits

Examples of org.eclipse.text.edits.MultiTextEdit


   * @since 3.2
   */
  public TextEdit rewriteAST() throws JavaModelException, IllegalArgumentException {
    ASTNode rootNode= getRootNode();
    if (rootNode == null) {
      return new MultiTextEdit(); // no changes
    }

    ASTNode root= rootNode.getRoot();
    if (!(root instanceof CompilationUnit)) {
      throw new IllegalArgumentException("This API can only be used if the AST is created from a compilation unit or class file"); //$NON-NLS-1$
View Full Code Here


    return internalRewriteAST(content, lineInfo, lineDelim, astRoot.getCommentList(), options, rootNode, (RecoveryScannerData)astRoot.getStatementsRecoveryData());
  }

  private TextEdit internalRewriteAST(char[] content, LineInformation lineInfo, String lineDelim, List commentNodes, Map options, ASTNode rootNode, RecoveryScannerData recoveryScannerData) {
    TextEdit result= new MultiTextEdit();
    //validateASTNotModified(rootNode);

    TargetSourceRangeComputer sourceRangeComputer= getExtendedSourceRangeComputer();
    this.eventStore.prepareMovedNodes(sourceRangeComputer);
View Full Code Here

    if (edit == null) {
        if (initialIndentationLevel > 0) {
            // at least correct the indent
            String indentString = createIndentString(initialIndentationLevel);
        ReplaceEdit[] edits = IndentManipulation.getChangeIndentEdits(unformatted, 0, this.tabWidth, this.indentWidth, indentString);
        edit= new MultiTextEdit();
        edit.addChild(new InsertEdit(0, indentString));
        edit.addChildren(edits);
        } else {
           return unformatted;
        }
View Full Code Here

      newEdit= new InsertEdit(edit.getOffset() - diff,  edit.getText());
    } else if (oldEdit instanceof DeleteEdit) {
      DeleteEdit edit= (DeleteEdit) oldEdit;
      newEdit= new DeleteEdit(edit.getOffset() - diff,  edit.getLength());
    } else if (oldEdit instanceof MultiTextEdit) {
      newEdit= new MultiTextEdit();
    } else {
      return null; // not supported
    }
    TextEdit[] children= oldEdit.getChildren();
    for (int i= 0; i < children.length; i++) {
View Full Code Here

      String lineDelim= this.compilationUnit.findRecommendedLineSeparator();
      IBuffer buffer= this.compilationUnit.getBuffer();

      int currPos= importsStart;
      MultiTextEdit resEdit= new MultiTextEdit();

      if ((this.flags & F_NEEDS_LEADING_DELIM) != 0) {
        // new import container
        resEdit.addChild(new InsertEdit(currPos, lineDelim));
      }

      PackageEntry lastPackage= null;

      Set onDemandConflicts= null;
      if (this.findAmbiguousImports) {
        onDemandConflicts= evaluateStarImportConflicts(monitor);
      }

      int spacesBetweenGroups= getSpacesBetweenImportGroups();

      ArrayList stringsToInsert= new ArrayList();

      int nPackageEntries= this.packageEntries.size();
      for (int i= 0; i < nPackageEntries; i++) {
        PackageEntry pack= (PackageEntry) this.packageEntries.get(i);
        if (this.filterImplicitImports && !pack.isStatic() && isImplicitImport(pack.getName())) {
          pack.filterImplicitImports(this.useContextToFilterImplicitImports);
        }
        int nImports= pack.getNumberOfImports();
        if (nImports == 0) {
          continue;
        }

        if (spacesBetweenGroups > 0 && lastPackage != null) {
          // add a space between two different groups by looking at the two adjacent imports
          if (!lastPackage.isComment() && !pack.isComment() && !pack.isSameGroup(lastPackage)) {
            for (int k= spacesBetweenGroups; k > 0; k--) {
              stringsToInsert.add(lineDelim);
            }
          } else if (lastPackage.isComment() && pack.isSameGroup(lastPackage)) {
            // the last pack may be a dummy for a comment which doesn't belong to any extended range
            stringsToInsert.add(lineDelim);
          }
        }
        lastPackage= pack;

        boolean isStatic= pack.isStatic();
        int threshold= isStatic ? this.staticImportOnDemandThreshold : this.importOnDemandThreshold;

        boolean doStarImport= pack.hasStarImport(threshold, onDemandConflicts);
        boolean allImportsAddedToStar = false;
        if (doStarImport && (pack.find("*") == null)) { //$NON-NLS-1$
          String[] imports = getNewImportStrings(buffer, pack, isStatic, lineDelim);
          for (int j = 0, max = imports.length; j < max; j++) {
            stringsToInsert.add(imports[j]);
          }
          allImportsAddedToStar = true; // may still need to handle onDemandConflicts below
        }

        for (int k= 0; k < nImports; k++) {
          ImportDeclEntry currDecl= pack.getImportAt(k);
          IRegion region= currDecl.getSourceRange();
          boolean isConflict = !currDecl.isComment() && onDemandConflicts != null && onDemandConflicts.contains(currDecl.getSimpleName());
          boolean addRegularToStar = doStarImport && !currDecl.isOnDemand();
         
          if (region == null) { // new entry
            if (!addRegularToStar || isConflict) {
              IRegion rangeBefore = currDecl.getPrecedingCommentRange();
              IRegion rangeAfter = currDecl.getTrailingCommentRange();
              if (rangeBefore != null) {
                stringsToInsert.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength()));
              }
             
              String trailingComment = null;
              if (rangeAfter != null) {
                trailingComment = buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength());
              }
              String str= getNewImportString(currDecl.getElementName(), isStatic, trailingComment, lineDelim);
              stringsToInsert.add(str);
            } else if (addRegularToStar && !allImportsAddedToStar) {
              String simpleName = currDecl.getTypeQualifiedName();
              if (simpleName.indexOf('.') != -1) {
                String str= getNewImportString(currDecl.getElementName(), isStatic, lineDelim);
                if (stringsToInsert.indexOf(str) == -1) {
                  stringsToInsert.add(str);
                }
              }
            }
          } else if (!addRegularToStar || isConflict) {
            int offset= region.getOffset();
            IRegion rangeBefore = currDecl.getPrecedingCommentRange();
            if (rangeBefore != null && currPos > rangeBefore.getOffset()) {
              // moved ahead of the leading comments, bring the currPos back
              currPos = rangeBefore.getOffset();
            }
            if (rangeBefore != null) {
              stringsToInsert.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength()));
            }
            removeAndInsertNew(buffer, currPos, offset, stringsToInsert, resEdit);
            stringsToInsert.clear();
            currPos= offset + region.getLength();
          } else if (addRegularToStar && !allImportsAddedToStar && !currDecl.isComment()) {
            String simpleName = currDecl.getTypeQualifiedName();
            if (simpleName.indexOf('.') != -1) {
              IRegion rangeBefore = currDecl.getPrecedingCommentRange();
              if (rangeBefore != null && currPos > rangeBefore.getOffset()) {
                // moved ahead of the leading comments, bring the currPos back
                currPos = rangeBefore.getOffset();
              }
              if (rangeBefore != null) {
                stringsToInsert.add(buffer.getText(rangeBefore.getOffset(), rangeBefore.getLength()));
              }
              IRegion rangeAfter = currDecl.getTrailingCommentRange();
              String trailingComment = null;
              if (rangeAfter != null) {
                trailingComment = buffer.getText(rangeAfter.getOffset(), rangeAfter.getLength());
              }
              String str= getNewImportString(currDecl.getElementName(), isStatic, trailingComment, lineDelim);
              if (stringsToInsert.indexOf(str) == -1) {
                stringsToInsert.add(str);
              }
            }
          }
        }
      }

      // insert back all existing imports comments since existing imports were not preserved
      if (this.preserveExistingCommentsRanges != null) {
        for (int i = 0, max = this.preserveExistingCommentsRanges.length; (i < max && this.preserveExistingCommentsRanges[i] != null); i++) {
          IRegion region = this.preserveExistingCommentsRanges[i];
          String text = buffer.getText(region.getOffset(), region.getLength());
          // remove preceding whitespaces
          int index = 0;
          int length = text.length();
          loop: while (index < length) {
            if (Character.isWhitespace(text.charAt(index))) {
              index++;
            } else {
              break loop;
            }
          }
          if (index != 0) {
            text = text.substring(index);
          }
          if (!text.endsWith(lineDelim)) {
            text += lineDelim;
          }
          stringsToInsert.add(text);
        }
      }
      int end= importsStart + importsLen;
      removeAndInsertNew(buffer, currPos, end, stringsToInsert, resEdit);

      if (importsLen == 0) {
        if (!this.importsCreated.isEmpty() || !this.staticImportsCreated.isEmpty()) { // new import container
          if ((this.flags & F_NEEDS_TRAILING_DELIM) != 0) {
            resEdit.addChild(new InsertEdit(currPos, lineDelim));
          }
        } else {
          return new MultiTextEdit(); // no changes
        }
      }
      return resEdit;
    } finally {
      monitor.done();
View Full Code Here

        throws CoreException {
      // ImportRewrite impRewrite=
      // StubUtility.createImportRewrite(fCompilationUnit, true);
      // String replaceString= impRewrite.addImport(fTypeProposal);

      MultiTextEdit composedEdit = new MultiTextEdit();
      // composedEdit.addChild(new ReplaceEdit(position.getOffset(),
      // position.getLength(), replaceString));
      // composedEdit.addChild(impRewrite.rewriteImports(null));
      return composedEdit;
    }
View Full Code Here

                if ((content = FileUtil.readFile(drlFile))==null)
                    return null;

                TextFileChange change = new TextFileChange(drlFile.getName(), drlFile);
                MultiTextEdit mte = new MultiTextEdit();
                change.setEdit(mte);

                // rename the field name
                Matcher matcher = fieldPattern.matcher(content);
                while (matcher.find()) {
                  if (isFieldInRightType(content, typeName, matcher.start())) {
                    ReplaceEdit replace = new ReplaceEdit(matcher.start(), currentName.length(), newName);
                    mte.addChild(replace);
                  }
                }

                // search all the variables of the type to replace the getters/setters
                matcher = typePattern.matcher(content);
View Full Code Here

    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=208541
    adaptRegions();
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=234583
    adaptEdits();

    MultiTextEdit edit = null;
    int regionsLength = this.adaptedRegions.length;
    int textRegionStart;
    int textRegionEnd;
    if (regionsLength == 1) {
      IRegion lastRegion = this.adaptedRegions[0];
      textRegionStart = lastRegion.getOffset();
      textRegionEnd = textRegionStart + lastRegion.getLength();
    } else {
      textRegionStart = this.adaptedRegions[0].getOffset();
      IRegion lastRegion = this.adaptedRegions[regionsLength - 1];
      textRegionEnd = lastRegion.getOffset() + lastRegion.getLength();
    }

    int length = textRegionEnd - textRegionStart + 1;
    if (textRegionStart <= 0) {
      if (length <= 0) {
        edit = new MultiTextEdit(0, 0);
      } else {
        edit = new MultiTextEdit(0, textRegionEnd);
      }
    } else {
      edit = new MultiTextEdit(textRegionStart, length - 1);
    }
    for (int i= 0, max = this.editsIndex; i < max; i++) {
      OptimizedReplaceEdit currentEdit = this.edits[i];
      if (currentEdit.offset >= 0 && currentEdit.offset <= this.scannerEndPosition) {
        if (currentEdit.length == 0 || (currentEdit.offset != this.scannerEndPosition && isMeaningfulEdit(currentEdit))) {
          try {
            edit.addChild(new ReplaceEdit(currentEdit.offset, currentEdit.length, currentEdit.replacement));
          }
          catch (MalformedTreeException ex) {
            // log exception in case of error
            CommentFormatterUtil.log(ex);
             throw ex;
View Full Code Here

     * @param replacement the replacement text
     * @return a text edit describing the operation needed to replace <code>selection</code>
     * @throws BadLocationException if computing the edit failed
     */
    TextEdit replace(ISelection selection, String replacement) throws BadLocationException {
      return new MultiTextEdit();
    }
View Full Code Here

    Assert.isNotNull(manager);
    TextFileChange textFileChange = null;
    if (cu != null && cu.getResource() instanceof IFile) {
      textFileChange = new TextFileChange(cu.getElementName(),
          (IFile) cu.getResource());
      MultiTextEdit fileChangeRootEdit = new MultiTextEdit();
      textFileChange.setEdit(fileChangeRootEdit);

      manager.manage(cu, textFileChange);

      IModelElement[] elements = (IModelElement[]) modelElements
          .toArray(new IModelElement[modelElements.size()]);

      for (int cnt = 0, max = elements.length; cnt < max; cnt++) {
        ISourceRange sourceRange = null;
        if (elements[cnt] instanceof IMember) {
          IMember type = (IMember) elements[cnt];
          sourceRange = type.getSourceRange();
        }
        if (sourceRange != null) {
          IStructuredDocument document = determineDocument(cu);
          int suffixLength = getSuffixLength(document,
              sourceRange.getOffset() + sourceRange.getLength(),
              ';');
          int length = sourceRange.getLength() + suffixLength;
          if (sourceRange.getOffset() + length > document.getLength()) {
            length = document.getLength() - sourceRange.getOffset();
          }
          DeleteEdit edit = new DeleteEdit(sourceRange.getOffset(),
              length);

          fileChangeRootEdit.addChild(edit);
          if (cu.isWorkingCopy()) {
            textFileChange.setSaveMode(TextFileChange.LEAVE_DIRTY);
          }

        }
View Full Code Here

TOP

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

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.