Package org.eclipse.jdt.core

Examples of org.eclipse.jdt.core.IBuffer


  protected void executeOperation() throws JavaModelException {
    try {
      beginTask(Messages.operation_sortelements, getMainAmountOfWork());
      CompilationUnit copy = (CompilationUnit) this.elementsToProcess[0];
      ICompilationUnit unit = copy.getPrimary();
      IBuffer buffer = copy.getBuffer();
      if (buffer  == null) {
        return;
      }
      char[] bufferContents = buffer.getCharacters();
      String result = processElement(unit, bufferContents);
      if (!CharOperation.equals(result.toCharArray(), bufferContents)) {
        copy.getBuffer().setContents(result);
      }
      worked(1);
View Full Code Here


    try {
      int importsStart=  this.replaceRange.getOffset();
      int importsLen= this.replaceRange.getLength();

      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))) {
View Full Code Here

        if (isIncluded && (!isPrimary || !workingCopy.isConsistent())) {
          deltaBuilder = new JavaElementDeltaBuilder(primary);
        }

        // save the cu
        IBuffer primaryBuffer = primary.getBuffer();
        if (!isPrimary) {
          if (primaryBuffer == null) return;
          char[] primaryContents = primaryBuffer.getCharacters();
          boolean hasSaved = false;
          try {
            IBuffer workingCopyBuffer = workingCopy.getBuffer();
            if (workingCopyBuffer == null) return;
            primaryBuffer.setContents(workingCopyBuffer.getCharacters());
            primaryBuffer.save(this.progressMonitor, this.force);
            primary.makeConsistent(this);
            hasSaved = true;
          } finally {
            if (!hasSaved){
View Full Code Here

    worked(1);
    IFile compilationUnitFile = folder.getFile(new Path(this.name));
    if (compilationUnitFile.exists()) {
      // update the contents of the existing unit if fForce is true
      if (this.force) {
        IBuffer buffer = unit.getBuffer();
        if (buffer == null) return;
        buffer.setContents(this.source);
        unit.save(new NullProgressMonitor(), false);
        this.resultElements = new IJavaElement[] {unit};
        if (!Util.isExcluded(unit)
            && unit.getParent().exists()) {
          for (int i = 0; i < this.resultElements.length; i++) {
View Full Code Here

    return this.classFile.getBuffer();
}

public char[] getContents() {
  try {
    IBuffer buffer = getBuffer();
    if (buffer == null) return CharOperation.NO_CHAR;
    char[] characters = buffer.getCharacters();
    if (characters == null) return CharOperation.NO_CHAR;
    return characters;
  } catch (JavaModelException e) {
    return CharOperation.NO_CHAR;
  }
View Full Code Here

* @see Openable#openBuffer(IProgressMonitor, Object)
*/
protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException {

  // create buffer
  IBuffer buffer = BufferManager.createBuffer(this);

  // set the buffer source
  IBuffer classFileBuffer = this.classFile.getBuffer();
  if (classFileBuffer != null) {
    buffer.setContents(classFileBuffer.getCharacters());
  } else {
    // Disassemble
    IClassFileReader reader = ToolFactory.createDefaultClassFileReader(this.classFile, IClassFileReader.ALL);
    Disassembler disassembler = new Disassembler();
    String contents = disassembler.disassemble(reader, Util.getLineSeparator("", getJavaProject()), ClassFileBytesDisassembler.WORKING_COPY); //$NON-NLS-1$
View Full Code Here

   * @return The IDocument.
   */
  public static IDocument getDocument(ICompilationUnit src)
    throws Exception
  {
    IBuffer buffer = src.getBuffer();
    if(buffer instanceof IDocument){
      return (IDocument)buffer;
    }
    return new DocumentAdapter(buffer);
  }
View Full Code Here

   * @param length The length of the region to format.
   */
  public static void format(ICompilationUnit src, int kind, int offset, int length)
    throws Exception
  {
    IBuffer buffer = src.getBuffer();
    String contents = buffer.getContents();
    String delimiter = StubUtility.getLineDelimiterUsed(src);
    DefaultCodeFormatter formatter =
      new DefaultCodeFormatter(src.getJavaProject().getOptions(true));

    // when the eclipse indent settings differ from vim (tabs vs spaces) then
    // the inserted method's indent may be a bit off. this is a workaround to
    // force reformatting of the code from the start of the line to the start of
    // the next set of code following the new method. Doesn't quite fix indent
    // formatting of methods in nested classes.
    while (offset > 0 &&
        !IndentManipulation.isLineDelimiterChar(buffer.getChar(offset - 1)))
    {
      offset--;
      length++;
    }
    while ((offset + length) < contents.length() &&
        IndentManipulation.isLineDelimiterChar(buffer.getChar(offset + length)))
    {
      length++;
    }

    TextEdit edits = formatter.format(kind, contents, offset, length, 0, delimiter);
View Full Code Here

          return overridden;
        }
        IOpenable openable = member.getOpenable();
        if (openable == null)
          return InheritDocVisitor.CONTINUE;
        IBuffer buf= openable.getBuffer();
        if (buf == null) {
          // no source attachment found. This method maybe the one. Stop.
          return InheritDocVisitor.STOP_BRANCH;
        }

        ISourceRange javadocRange= member.getJavadocRange();
        if (javadocRange == null)
          return InheritDocVisitor.CONTINUE;  // this method doesn't have javadoc, continue to look.
        String rawJavadoc= buf.getText(javadocRange.getOffset(), javadocRange.getLength());
        if (rawJavadoc != null) {
          return overridden;
        }
        return InheritDocVisitor.CONTINUE;
      }
View Full Code Here

*
* <p>NOTE: this triggers an external removal of this buffer
* by closing the buffer.
*/
protected boolean close(LRUCacheEntry entry) {
  IBuffer buffer= (IBuffer) entry.value;

  // prevent buffer that have unsaved changes or working copy buffer to be removed
  // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=39311
  if (!((Openable)buffer.getOwner()).canBufferBeRemovedFromCache(buffer)) {
    return false;
  } else {
    ArrayList buffers = (ArrayList) this.buffersToClose.get();
    if (buffers == null) {
      buffers = new ArrayList();
View Full Code Here

TOP

Related Classes of org.eclipse.jdt.core.IBuffer

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.