Package org.eclipse.text.edits

Examples of org.eclipse.text.edits.MultiTextEdit


      throw new IllegalArgumentException();
    }

    ASTNode rootNode= getRootNode();
    if (rootNode == null) {
      return new MultiTextEdit(); // no changes
    }

    char[] content= document.get().toCharArray();
    LineInformation lineInfo= LineInformation.create(document);
    String lineDelim= TextUtilities.getDefaultLineDelimiter(document);
View Full Code Here


   * @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

      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

   * @param options the given options
   * @throws IllegalArgumentException if the rewrite fails
   * @return Returns the edit describing the text changes.
   */
  public TextEdit rewriteAST(IDocument document, Map options) {
    TextEdit result = new MultiTextEdit();

    final CompilationUnit rootNode = getRootNode();
    if (rootNode != null) {
      TargetSourceRangeComputer xsrComputer = new TargetSourceRangeComputer() {
        /**
 
View Full Code Here

    try {
      monitor.beginTask(Messages.bind(Messages.importRewrite_processDescription), 2);
      if (!hasRecordedChanges()) {
        this.createdImports= CharOperation.NO_STRINGS;
        this.createdStaticImports= CharOperation.NO_STRINGS;
        return new MultiTextEdit();
      }

      CompilationUnit usedAstRoot= this.astRoot;
      if (usedAstRoot == null) {
        ASTParser parser= ASTParser.newParser(AST.JLS8);
View Full Code Here

          // return new MultiTextEdit();
          // // throw new IllegalArgumentException("illegal format context");
          // // System.err.println("illegal format context");
          // }
        } else {
          return new MultiTextEdit();
        }
      }
    }
    return null;
  }
View Full Code Here

        edits.add(new ReplaceEdit(offsets[k], variable.getInitialLength(), value));

        }

      IDocument document= new Document(buffer.getString());
        MultiTextEdit edit= new MultiTextEdit(0, document.getLength());
        edit.addChildren((TextEdit[]) positions.toArray(new TextEdit[positions.size()]));
        edit.addChildren((TextEdit[]) edits.toArray(new TextEdit[edits.size()]));
        edit.apply(document, TextEdit.UPDATE_REGIONS);

    positionsToVariables(positions, variables);

        buffer.setContent(document.get(), variables);
    }
View Full Code Here

      }

      try {
        for (Iterator it= map.keySet().iterator(); it.hasNext();) {
          IDocument d= (IDocument) it.next();
          TextEdit edit= new MultiTextEdit(0, d.getLength());
          edit.addChildren((TextEdit[]) ((List) map.get(d)).toArray(new TextEdit[0]));
          map.put(d, edit);
        }

        return map;
      } catch (MalformedTreeException x) {
View Full Code Here

        } else {
            // Not insisting on a source file makes testing easier.
            change = PyDocumentChange.create(name, info.getDocument());

        }
        multiEdit = new MultiTextEdit();
        change.setEdit(this.multiEdit);
        processEdit();
        return change;
    }
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.