CloneSet3


Previous CloneSetNext CloneSetBack to Main Report
Clone
Mass
Clones in
CloneSet
Parameter
Count
Clone
Similarity
Syntax Category
[Sequence Length]
541201.000class_body_declarations[13]
Clone AbstractionParameter Bindings
Clone Instance
(Click to see clone)
Line CountSource Line
Source File
1541576
plugins/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java
2541539
plugins/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe2.java
Clone Instance
1
Line Count
541
Source Line
576
Source File
plugins/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java

        public void indent() {
                this.indentationLevel += this.indentationSize;
                this.numberOfIndentations++;
        }

        /**
         * @param compilationUnitSource
         */
        public void initializeScanner(char[] compilationUnitSource) {
                this.scanner.setSource(compilationUnitSource);
                this.scannerEndPosition = compilationUnitSource.length;
                this.scanner.resetTo(0, this.scannerEndPosition);
                this.edits = new OptimizedReplaceEdit[INITIAL_SIZE];
        }

        private boolean isValidEdit(OptimizedReplaceEdit edit) {
                final int editLength = edit.length;
                final int editReplacementLength = edit.replacement.length();
                final int editOffset = edit.offset;
                if (editLength != 0) {
                        if (this.textRegionStart <= editOffset && (editOffset + editLength - 1) <= this.textRegionEnd) {
                                if (editReplacementLength != 0 && editLength == editReplacementLength) {
                                        for (int i = editOffset, max = editOffset + editLength; i < max; i++) {
                                                if (scanner.source[i] != edit.replacement.charAt(i - editOffset)) {
                                                        return true;
                                                }
                                        }
                                        return false;
                                }
                                else   {
                                        return true;
                                }
                        }
                        else   if (editOffset + editLength == this.textRegionStart) {
                                int i = editOffset;
                                for (int max = editOffset + editLength; i < max; i++) {
                                        int replacementStringIndex = i - editOffset;
                                        if (replacementStringIndex >= editReplacementLength || scanner.source[i] != edit.replacement.charAt(replacementStringIndex)) {
                                                break;
                                        }
                                }
                                if (i - editOffset != editReplacementLength && i != editOffset + editLength - 1) {
                                        edit.offset = textRegionStart;
                                        edit.length = 0;
                                        edit.replacement = edit.replacement.substring(i - editOffset);
                                        return true;
                                }
                               }
                }
                else   if (this.textRegionStart <= editOffset && editOffset <= this.textRegionEnd) {
                        return true;
                       }
                       else
                       if (editOffset == this.scannerEndPosition && editOffset == this.textRegionEnd + 1) {
                        return true;
                       }
                return false;
        }

        private void preserveEmptyLines(int count, int insertPosition) {
                if (count > 0) {
                        if (this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
                                int linesToPreserve = Math.min(count, this.formatter.preferences.number_of_empty_lines_to_preserve);
                                this.printEmptyLines(linesToPreserve, insertPosition);
                        }
                        else   {
                                printNewLine(insertPosition);
                        }
                }
        }

        private void print(char[] s, boolean considerSpaceIfAny) {
                if (checkLineWrapping && s.length + column > this.pageWidth) {
                        handleLineTooLong();
                }
                this.lastNumberOfNewLines = 0;
                printIndentationIfNecessary();
                if (considerSpaceIfAny) {
                        this.space();
                }
                if (this.pendingSpace) {
                        this.addInsertEdit(this.scanner.getCurrentTokenStartPosition(), " "); //$NON-NLS-1$
                }
                this.pendingSpace = false;
                this.needSpace = false;
                column += s.length;
                needSpace = true;
        }

        private void printBlockComment(char[] s, boolean isJavadoc) {
                int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition();
                int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1;

                this.scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition - 1);
                int currentCharacter;
                boolean isNewLine = false;
                int start = currentTokenStartPosition;
                int nextCharacterStart = currentTokenStartPosition;
                printIndentationIfNecessary();
                if (this.pendingSpace) {
                        this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$
                }
                this.needSpace = false;
                this.pendingSpace = false;
                int previousStart = currentTokenStartPosition;

                while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) {
                        nextCharacterStart = this.scanner.currentPosition;

                        switch (currentCharacter) {
                                case '\r':
                                        start = previousStart;
                                        isNewLine = true;
                                        if (this.scanner.getNextChar('\n')) {
                                                currentCharacter = '\n';
                                                nextCharacterStart = this.scanner.currentPosition;
                                        }
                                        break;
                                case '\n':
                                        start = previousStart;
                                        isNewLine = true;
                                        break;
                                default:
                                        if (isNewLine) {
                                                if (ScannerHelper.isWhitespace((char) currentCharacter)) {
                                                        int previousStartPosition = this.scanner.currentPosition;
                                                        while (currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) {
                                                                previousStart = nextCharacterStart;
                                                                previousStartPosition = this.scanner.currentPosition;
                                                                currentCharacter = this.scanner.getNextChar();
                                                                nextCharacterStart = this.scanner.currentPosition;
                                                        }
                                                        if (currentCharacter == '\r' || currentCharacter == '\n') {
                                                                nextCharacterStart = previousStartPosition;
                                                        }
                                                }
                                                this.column = 1;
                                                this.line++;

                                                StringBuffer buffer = new StringBuffer();
                                                buffer.append(this.lineSeparator);
                                                printIndentationIfNecessary(buffer);
                                                buffer.append(' ');

                                                addReplaceEdit(start, previousStart - 1, String.valueOf(buffer));
                                        }
                                        else   {
                                                this.column += (nextCharacterStart - previousStart);
                                        }
                                        isNewLine = false;
                              }
                        previousStart = nextCharacterStart;
                        this.scanner.currentPosition = nextCharacterStart;
                }
                this.lastNumberOfNewLines = 0;
                needSpace = false;
                this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1);
                if (isJavadoc) {
                        printNewLine();
                }
        }

        public void printEndOfCompilationUnit() {
                try {
                        // if we have a space between two tokens we ensure it will be dumped in the formatted string
                        int currentTokenStartPosition = this.scanner.currentPosition;
                        boolean hasComment = false;
                        boolean hasLineComment = false;
                        boolean hasWhitespace = false;
                        int count = 0;
                        while (true) {
                                this.currentToken = this.scanner.getNextToken();
                                switch (this.currentToken) {
                                        case TerminalTokens.TokenNameWHITESPACE:
                                                char[] whiteSpaces = this.scanner.getCurrentTokenSource();
                                                count = 0;
                                                for (int i = 0, max = whiteSpaces.length; i < max; i++) {
                                                        switch (whiteSpaces[i]) {
                                                                case '\r':
                                                                        if ((i + 1) < max) {
                                                                                if (whiteSpaces[i + 1] == '\n') {
                                                                                        i++;
                                                                                }
                                                                        }
                                                                        count++;
                                                                        break;
                                                                case '\n':
                                                                        count++;
                                                              }
                                                }
                                                if (count == 0) {
                                                        hasWhitespace = true;
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                }
                                                else   if (hasComment) {
                                                        if (count == 1) {
                                                                this.printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                       else
                                                       if (hasLineComment) {
                                                        this.preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition());
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                       else {
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_LINE:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printCommentLine(this.scanner.getRawTokenSource());
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_BLOCK:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printBlockComment(this.scanner.getRawTokenSource(), false);
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = false;
                                                hasComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_JAVADOC:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printBlockComment(this.scanner.getRawTokenSource(), true);
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = false;
                                                hasComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameSEMICOLON:
                                                char[] currentTokenSource = this.scanner.getRawTokenSource();
                                                this.print(currentTokenSource, this.formatter.preferences.insert_space_before_semicolon);
                                                break;
                                        case TerminalTokens.TokenNameEOF:
                                                if (count >= 1 || this.formatter.preferences.insert_new_line_at_end_of_file_if_missing) {
                                                        this.printNewLine(this.scannerEndPosition);
                                                }
                                                return;
                                        default:
                                                // step back one token
                                                this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
                                                return;
                                      }
                        }
                } catch (InvalidInputException e) {
                        throw new AbortFormatting(e);
                  }
        }

        public void printComment() {
                try {
                        // if we have a space between two tokens we ensure it will be dumped in the formatted string
                        int currentTokenStartPosition = this.scanner.currentPosition;
                        boolean hasComment = false;
                        boolean hasLineComment = false;
                        boolean hasWhitespace = false;
                        int count = 0;
                        while ((this.currentToken = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
                                switch (this.currentToken) {
                                        case TerminalTokens.TokenNameWHITESPACE:
                                                char[] whiteSpaces = this.scanner.getCurrentTokenSource();
                                                count = 0;
                                                for (int i = 0, max = whiteSpaces.length; i < max; i++) {
                                                        switch (whiteSpaces[i]) {
                                                                case '\r':
                                                                        if ((i + 1) < max) {
                                                                                if (whiteSpaces[i + 1] == '\n') {
                                                                                        i++;
                                                                                }
                                                                        }
                                                                        count++;
                                                                        break;
                                                                case '\n':
                                                                        count++;
                                                              }
                                                }
                                                if (count == 0) {
                                                        hasWhitespace = true;
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                }
                                                else   if (hasComment) {
                                                        if (count == 1) {
                                                                this.printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                       else
                                                       if (hasLineComment) {
                                                        this.preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition());
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                       else
                                                       if (count != 0 && this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
                                                        addReplaceEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition(), this.getPreserveEmptyLines(count - 1));
                                                       }
                                                       else {
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_LINE:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printCommentLine(this.scanner.getRawTokenSource());
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_BLOCK:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printBlockComment(this.scanner.getRawTokenSource(), false);
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = false;
                                                hasComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_JAVADOC:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printBlockComment(this.scanner.getRawTokenSource(), true);
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = false;
                                                hasComment = true;
                                                count = 0;
                                                break;
                                        default:
                                                // step back one token
                                                this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
                                                return;
                                      }
                        }
                } catch (InvalidInputException e) {
                        throw new AbortFormatting(e);
                  }
        }

        private void printCommentLine(char[] s) {
                int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition();
                int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1;
                if (CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, currentTokenStartPosition) != -1) {
                        this.nlsTagCounter = 0;
                }
                this.scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition - 1);
                int currentCharacter;
                int start = currentTokenStartPosition;
                int nextCharacterStart = currentTokenStartPosition;
                printIndentationIfNecessary();
                if (this.pendingSpace) {
                        this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$
                }
                this.needSpace = false;
                this.pendingSpace = false;
                int previousStart = currentTokenStartPosition;

                loop: while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) {
                        nextCharacterStart = this.scanner.currentPosition;

                        switch (currentCharacter) {
                                case '\r':
                                        start = previousStart;
                                        break loop;
                                case '\n':
                                        start = previousStart;
                                        break loop;
                              }
                        previousStart = nextCharacterStart;
                      }
                if (start != currentTokenStartPosition) {
                        addReplaceEdit(start, currentTokenEndPosition - 1, lineSeparator);
                }
                line++;
                column = 1;
                needSpace = false;
                this.pendingSpace = false;
                lastNumberOfNewLines = 1;
                // realign to the proper value
                if (this.currentAlignment != null) {
                        if (this.memberAlignment != null) {
                                // select the last alignment
                                if (this.currentAlignment.location.inputOffset > this.memberAlignment.location.inputOffset) {
                                        if (this.currentAlignment.couldBreak() && this.currentAlignment.wasSplit) {
                                                this.currentAlignment.performFragmentEffect();
                                        }
                                }
                                else   {
                                        this.indentationLevel = Math.max(this.indentationLevel, this.memberAlignment.breakIndentationLevel);
                                }
                        }
                        else   if (this.currentAlignment.couldBreak() && this.currentAlignment.wasSplit) {
                                this.currentAlignment.performFragmentEffect();
                               }
                }
                this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1);
        }

        public void printEmptyLines(int linesNumber) {
                this.printEmptyLines(linesNumber, this.scanner.getCurrentTokenEndPosition() + 1);
        }

        private void printEmptyLines(int linesNumber, int insertPosition) {
        final String buffer = getEmptyLines(linesNumber);
        if (EMPTY_STRING == buffer) return;

                addInsertEdit(insertPosition, buffer);
        }

        void printIndentationIfNecessary() {
                StringBuffer buffer = new StringBuffer();
                printIndentationIfNecessary(buffer);
                if (buffer.length() > 0) {
                        addInsertEdit(this.scanner.getCurrentTokenStartPosition(), buffer.toString());
                        this.pendingSpace = false;
                }
        }

        private void printIndentationIfNecessary(StringBuffer buffer) {
                switch (this.tabChar) {
                        case DefaultCodeFormatterOptions.TAB:
                                boolean useTabsForLeadingIndents = this.useTabsOnlyForLeadingIndents;
                                int numberOfLeadingIndents = this.numberOfIndentations;
                                int indentationsAsTab = 0;
                                if (useTabsForLeadingIndents) {
                                        while (this.column <= this.indentationLevel) {
                                                if (indentationsAsTab < numberOfLeadingIndents) {
                                                        buffer.append('\t');
                                                        indentationsAsTab++;
                                                        this.lastNumberOfNewLines = 0;
                                                        int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
                                                        this.column += complement;
                                                        this.needSpace = false;
                                                }
                                                else   {
                                                        buffer.append(' ');
                                                        this.column++;
                                                        this.needSpace = false;
                                                }
                                        }
                                }
                                else   {
                                        while (this.column <= this.indentationLevel) {
                                                buffer.append('\t');
                                                this.lastNumberOfNewLines = 0;
                                                int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
                                                this.column += complement;
                                                this.needSpace = false;
                                        }
                                }
                                break;
                        case DefaultCodeFormatterOptions.SPACE:
                                while (this.column <= this.indentationLevel) {
                                        buffer.append(' ');
                                        this.column++;
                                        this.needSpace = false;
                                }
                                break;
                        case DefaultCodeFormatterOptions.MIXED:
                                useTabsForLeadingIndents = this.useTabsOnlyForLeadingIndents;
                                numberOfLeadingIndents = this.numberOfIndentations;
                                indentationsAsTab = 0;
                                if (useTabsForLeadingIndents) {
                                        final int columnForLeadingIndents = numberOfLeadingIndents * this.indentationSize;
                                        while (this.column <= this.indentationLevel) {
                                                if (this.column <= columnForLeadingIndents) {
                                                        if ((this.column - 1 + this.tabLength) <= this.indentationLevel) {
                                                                buffer.append('\t');
                                                                this.column += this.tabLength;
                                                        }
                                                        else   if ((this.column - 1 + this.indentationSize) <= this.indentationLevel) {
                                                                // print one indentation
                                                                for (int i = 0, max = this.indentationSize; i < max; i++) {
                                                                        buffer.append(' ');
                                                                        this.column++;
                                                                }
                                                               }
                                                               else {
                                                                buffer.append(' ');
                                                                this.column++;
                                                               }
                                                }
                                                else   {
                                                        for (int i = this.column, max = this.indentationLevel; i <= max; i++) {
                                                                buffer.append(' ');
                                                                this.column++;
                                                        }
                                                }
                                                this.needSpace = false;
                                        }
                                }
                                else   {
                                        while (this.column <= this.indentationLevel) {
                                                if ((this.column - 1 + this.tabLength) <= this.indentationLevel) {
                                                        buffer.append('\t');
                                                        this.column += this.tabLength;
                                                }
                                                else   if ((this.column - 1 + this.indentationSize) <= this.indentationLevel) {
                                                        // print one indentation
                                                        for (int i = 0, max = this.indentationSize; i < max; i++) {
                                                                buffer.append(' ');
                                                                this.column++;
                                                        }
                                                       }
                                                       else {
                                                        buffer.append(' ');
                                                        this.column++;
                                                       }
                                                this.needSpace = false;
                                        }
                                }
                                break;
                      }
        }


Clone Instance
2
Line Count
541
Source Line
539
Source File
plugins/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe2.java

        public void indent() {
                this.indentationLevel += this.indentationSize;
                this.numberOfIndentations++;
        }

        /**
         * @param compilationUnitSource
         */
        public void initializeScanner(char[] compilationUnitSource) {
                this.scanner.setSource(compilationUnitSource);
                this.scannerEndPosition = compilationUnitSource.length;
                this.scanner.resetTo(0, this.scannerEndPosition);
                this.edits = new OptimizedReplaceEdit[INITIAL_SIZE];
        }

        private boolean isValidEdit(OptimizedReplaceEdit edit) {
                final int editLength = edit.length;
                final int editReplacementLength = edit.replacement.length();
                final int editOffset = edit.offset;
                if (editLength != 0) {
                        if (this.textRegionStart <= editOffset && (editOffset + editLength - 1) <= this.textRegionEnd) {
                                if (editReplacementLength != 0 && editLength == editReplacementLength) {
                                        for (int i = editOffset, max = editOffset + editLength; i < max; i++) {
                                                if (scanner.source[i] != edit.replacement.charAt(i - editOffset)) {
                                                        return true;
                                                }
                                        }
                                        return false;
                                }
                                else   {
                                        return true;
                                }
                        }
                        else   if (editOffset + editLength == this.textRegionStart) {
                                int i = editOffset;
                                for (int max = editOffset + editLength; i < max; i++) {
                                        int replacementStringIndex = i - editOffset;
                                        if (replacementStringIndex >= editReplacementLength || scanner.source[i] != edit.replacement.charAt(replacementStringIndex)) {
                                                break;
                                        }
                                }
                                if (i - editOffset != editReplacementLength && i != editOffset + editLength - 1) {
                                        edit.offset = textRegionStart;
                                        edit.length = 0;
                                        edit.replacement = edit.replacement.substring(i - editOffset);
                                        return true;
                                }
                               }
                }
                else   if (this.textRegionStart <= editOffset && editOffset <= this.textRegionEnd) {
                        return true;
                       }
                       else
                       if (editOffset == this.scannerEndPosition && editOffset == this.textRegionEnd + 1) {
                        return true;
                       }
                return false;
        }

        private void preserveEmptyLines(int count, int insertPosition) {
                if (count > 0) {
                        if (this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
                                int linesToPreserve = Math.min(count, this.formatter.preferences.number_of_empty_lines_to_preserve);
                                this.printEmptyLines(linesToPreserve, insertPosition);
                        }
                        else   {
                                printNewLine(insertPosition);
                        }
                }
        }

        private void print(char[] s, boolean considerSpaceIfAny) {
                if (checkLineWrapping && s.length + column > this.pageWidth) {
                        handleLineTooLong();
                }
                this.lastNumberOfNewLines = 0;
                printIndentationIfNecessary();
                if (considerSpaceIfAny) {
                        this.space();
                }
                if (this.pendingSpace) {
                        this.addInsertEdit(this.scanner.getCurrentTokenStartPosition(), " "); //$NON-NLS-1$
                }
                this.pendingSpace = false;
                this.needSpace = false;
                column += s.length;
                needSpace = true;
        }

        private void printBlockComment(char[] s, boolean isJavadoc) {
                int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition();
                int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1;

                this.scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition - 1);
                int currentCharacter;
                boolean isNewLine = false;
                int start = currentTokenStartPosition;
                int nextCharacterStart = currentTokenStartPosition;
                printIndentationIfNecessary();
                if (this.pendingSpace) {
                        this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$
                }
                this.needSpace = false;
                this.pendingSpace = false;
                int previousStart = currentTokenStartPosition;

                while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) {
                        nextCharacterStart = this.scanner.currentPosition;

                        switch (currentCharacter) {
                                case '\r':
                                        start = previousStart;
                                        isNewLine = true;
                                        if (this.scanner.getNextChar('\n')) {
                                                currentCharacter = '\n';
                                                nextCharacterStart = this.scanner.currentPosition;
                                        }
                                        break;
                                case '\n':
                                        start = previousStart;
                                        isNewLine = true;
                                        break;
                                default:
                                        if (isNewLine) {
                                                if (ScannerHelper.isWhitespace((char) currentCharacter)) {
                                                        int previousStartPosition = this.scanner.currentPosition;
                                                        while (currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) {
                                                                previousStart = nextCharacterStart;
                                                                previousStartPosition = this.scanner.currentPosition;
                                                                currentCharacter = this.scanner.getNextChar();
                                                                nextCharacterStart = this.scanner.currentPosition;
                                                        }
                                                        if (currentCharacter == '\r' || currentCharacter == '\n') {
                                                                nextCharacterStart = previousStartPosition;
                                                        }
                                                }
                                                this.column = 1;
                                                this.line++;

                                                StringBuffer buffer = new StringBuffer();
                                                buffer.append(this.lineSeparator);
                                                printIndentationIfNecessary(buffer);
                                                buffer.append(' ');

                                                addReplaceEdit(start, previousStart - 1, String.valueOf(buffer));
                                        }
                                        else   {
                                                this.column += (nextCharacterStart - previousStart);
                                        }
                                        isNewLine = false;
                              }
                        previousStart = nextCharacterStart;
                        this.scanner.currentPosition = nextCharacterStart;
                }
                this.lastNumberOfNewLines = 0;
                needSpace = false;
                this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1);
                if (isJavadoc) {
                        printNewLine();
                }
        }

        public void printEndOfCompilationUnit() {
                try {
                        // if we have a space between two tokens we ensure it will be dumped in the formatted string
                        int currentTokenStartPosition = this.scanner.currentPosition;
                        boolean hasComment = false;
                        boolean hasLineComment = false;
                        boolean hasWhitespace = false;
                        int count = 0;
                        while (true) {
                                this.currentToken = this.scanner.getNextToken();
                                switch (this.currentToken) {
                                        case TerminalTokens.TokenNameWHITESPACE:
                                                char[] whiteSpaces = this.scanner.getCurrentTokenSource();
                                                count = 0;
                                                for (int i = 0, max = whiteSpaces.length; i < max; i++) {
                                                        switch (whiteSpaces[i]) {
                                                                case '\r':
                                                                        if ((i + 1) < max) {
                                                                                if (whiteSpaces[i + 1] == '\n') {
                                                                                        i++;
                                                                                }
                                                                        }
                                                                        count++;
                                                                        break;
                                                                case '\n':
                                                                        count++;
                                                              }
                                                }
                                                if (count == 0) {
                                                        hasWhitespace = true;
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                }
                                                else   if (hasComment) {
                                                        if (count == 1) {
                                                                this.printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                       else
                                                       if (hasLineComment) {
                                                        this.preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition());
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                       else {
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_LINE:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printCommentLine(this.scanner.getRawTokenSource());
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_BLOCK:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printBlockComment(this.scanner.getRawTokenSource(), false);
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = false;
                                                hasComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_JAVADOC:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printBlockComment(this.scanner.getRawTokenSource(), true);
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = false;
                                                hasComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameSEMICOLON:
                                                char[] currentTokenSource = this.scanner.getRawTokenSource();
                                                this.print(currentTokenSource, this.formatter.preferences.insert_space_before_semicolon);
                                                break;
                                        case TerminalTokens.TokenNameEOF:
                                                if (count >= 1 || this.formatter.preferences.insert_new_line_at_end_of_file_if_missing) {
                                                        this.printNewLine(this.scannerEndPosition);
                                                }
                                                return;
                                        default:
                                                // step back one token
                                                this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
                                                return;
                                      }
                        }
                } catch (InvalidInputException e) {
                        throw new AbortFormatting(e);
                  }
        }

        public void printComment() {
                try {
                        // if we have a space between two tokens we ensure it will be dumped in the formatted string
                        int currentTokenStartPosition = this.scanner.currentPosition;
                        boolean hasComment = false;
                        boolean hasLineComment = false;
                        boolean hasWhitespace = false;
                        int count = 0;
                        while ((this.currentToken = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
                                switch (this.currentToken) {
                                        case TerminalTokens.TokenNameWHITESPACE:
                                                char[] whiteSpaces = this.scanner.getCurrentTokenSource();
                                                count = 0;
                                                for (int i = 0, max = whiteSpaces.length; i < max; i++) {
                                                        switch (whiteSpaces[i]) {
                                                                case '\r':
                                                                        if ((i + 1) < max) {
                                                                                if (whiteSpaces[i + 1] == '\n') {
                                                                                        i++;
                                                                                }
                                                                        }
                                                                        count++;
                                                                        break;
                                                                case '\n':
                                                                        count++;
                                                              }
                                                }
                                                if (count == 0) {
                                                        hasWhitespace = true;
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                }
                                                else   if (hasComment) {
                                                        if (count == 1) {
                                                                this.printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                       else
                                                       if (hasLineComment) {
                                                        this.preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition());
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                       else
                                                       if (count != 0 && this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
                                                        addReplaceEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition(), this.getPreserveEmptyLines(count - 1));
                                                       }
                                                       else {
                                                        addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                                                       }
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_LINE:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printCommentLine(this.scanner.getRawTokenSource());
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_BLOCK:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printBlockComment(this.scanner.getRawTokenSource(), false);
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = false;
                                                hasComment = true;
                                                count = 0;
                                                break;
                                        case TerminalTokens.TokenNameCOMMENT_JAVADOC:
                                                if (count >= 1) {
                                                        if (count > 1) {
                                                                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
                                                        }
                                                        else   if (count == 1) {
                                                                printNewLine(this.scanner.getCurrentTokenStartPosition());
                                                               }
                                                }
                                                else   if (hasWhitespace) {
                                                        space();
                                                       }
                                                hasWhitespace = false;
                                                this.printBlockComment(this.scanner.getRawTokenSource(), true);
                                                currentTokenStartPosition = this.scanner.currentPosition;
                                                hasLineComment = false;
                                                hasComment = true;
                                                count = 0;
                                                break;
                                        default:
                                                // step back one token
                                                this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
                                                return;
                                      }
                        }
                } catch (InvalidInputException e) {
                        throw new AbortFormatting(e);
                  }
        }

        private void printCommentLine(char[] s) {
                int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition();
                int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1;
                if (CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, currentTokenStartPosition) != -1) {
                        this.nlsTagCounter = 0;
                }
                this.scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition - 1);
                int currentCharacter;
                int start = currentTokenStartPosition;
                int nextCharacterStart = currentTokenStartPosition;
                printIndentationIfNecessary();
                if (this.pendingSpace) {
                        this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$
                }
                this.needSpace = false;
                this.pendingSpace = false;
                int previousStart = currentTokenStartPosition;

                loop: while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) {
                        nextCharacterStart = this.scanner.currentPosition;

                        switch (currentCharacter) {
                                case '\r':
                                        start = previousStart;
                                        break loop;
                                case '\n':
                                        start = previousStart;
                                        break loop;
                              }
                        previousStart = nextCharacterStart;
                      }
                if (start != currentTokenStartPosition) {
                        addReplaceEdit(start, currentTokenEndPosition - 1, lineSeparator);
                }
                line++;
                column = 1;
                needSpace = false;
                this.pendingSpace = false;
                lastNumberOfNewLines = 1;
                // realign to the proper value
                if (this.currentAlignment != null) {
                        if (this.memberAlignment != null) {
                                // select the last alignment
                                if (this.currentAlignment.location.inputOffset > this.memberAlignment.location.inputOffset) {
                                        if (this.currentAlignment.couldBreak() && this.currentAlignment.wasSplit) {
                                                this.currentAlignment.performFragmentEffect();
                                        }
                                }
                                else   {
                                        this.indentationLevel = Math.max(this.indentationLevel, this.memberAlignment.breakIndentationLevel);
                                }
                        }
                        else   if (this.currentAlignment.couldBreak() && this.currentAlignment.wasSplit) {
                                this.currentAlignment.performFragmentEffect();
                               }
                }
                this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1);
        }

        public void printEmptyLines(int linesNumber) {
                this.printEmptyLines(linesNumber, this.scanner.getCurrentTokenEndPosition() + 1);
        }

        private void printEmptyLines(int linesNumber, int insertPosition) {
        final String buffer = getEmptyLines(linesNumber);
        if (EMPTY_STRING == buffer) return;

                addInsertEdit(insertPosition, buffer);
        }

        void printIndentationIfNecessary() {
                StringBuffer buffer = new StringBuffer();
                printIndentationIfNecessary(buffer);
                if (buffer.length() > 0) {
                        addInsertEdit(this.scanner.getCurrentTokenStartPosition(), buffer.toString());
                        this.pendingSpace = false;
                }
        }

        private void printIndentationIfNecessary(StringBuffer buffer) {
                switch (this.tabChar) {
                        case DefaultCodeFormatterOptions.TAB:
                                boolean useTabsForLeadingIndents = this.useTabsOnlyForLeadingIndents;
                                int numberOfLeadingIndents = this.numberOfIndentations;
                                int indentationsAsTab = 0;
                                if (useTabsForLeadingIndents) {
                                        while (this.column <= this.indentationLevel) {
                                                if (indentationsAsTab < numberOfLeadingIndents) {
                                                        buffer.append('\t');
                                                        indentationsAsTab++;
                                                        this.lastNumberOfNewLines = 0;
                                                        int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
                                                        this.column += complement;
                                                        this.needSpace = false;
                                                }
                                                else   {
                                                        buffer.append(' ');
                                                        this.column++;
                                                        this.needSpace = false;
                                                }
                                        }
                                }
                                else   {
                                        while (this.column <= this.indentationLevel) {
                                                buffer.append('\t');
                                                this.lastNumberOfNewLines = 0;
                                                int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
                                                this.column += complement;
                                                this.needSpace = false;
                                        }
                                }
                                break;
                        case DefaultCodeFormatterOptions.SPACE:
                                while (this.column <= this.indentationLevel) {
                                        buffer.append(' ');
                                        this.column++;
                                        this.needSpace = false;
                                }
                                break;
                        case DefaultCodeFormatterOptions.MIXED:
                                useTabsForLeadingIndents = this.useTabsOnlyForLeadingIndents;
                                numberOfLeadingIndents = this.numberOfIndentations;
                                indentationsAsTab = 0;
                                if (useTabsForLeadingIndents) {
                                        final int columnForLeadingIndents = numberOfLeadingIndents * this.indentationSize;
                                        while (this.column <= this.indentationLevel) {
                                                if (this.column <= columnForLeadingIndents) {
                                                        if ((this.column - 1 + this.tabLength) <= this.indentationLevel) {
                                                                buffer.append('\t');
                                                                this.column += this.tabLength;
                                                        }
                                                        else   if ((this.column - 1 + this.indentationSize) <= this.indentationLevel) {
                                                                // print one indentation
                                                                for (int i = 0, max = this.indentationSize; i < max; i++) {
                                                                        buffer.append(' ');
                                                                        this.column++;
                                                                }
                                                               }
                                                               else {
                                                                buffer.append(' ');
                                                                this.column++;
                                                               }
                                                }
                                                else   {
                                                        for (int i = this.column, max = this.indentationLevel; i <= max; i++) {
                                                                buffer.append(' ');
                                                                this.column++;
                                                        }
                                                }
                                                this.needSpace = false;
                                        }
                                }
                                else   {
                                        while (this.column <= this.indentationLevel) {
                                                if ((this.column - 1 + this.tabLength) <= this.indentationLevel) {
                                                        buffer.append('\t');
                                                        this.column += this.tabLength;
                                                }
                                                else   if ((this.column - 1 + this.indentationSize) <= this.indentationLevel) {
                                                        // print one indentation
                                                        for (int i = 0, max = this.indentationSize; i < max; i++) {
                                                                buffer.append(' ');
                                                                this.column++;
                                                        }
                                                       }
                                                       else {
                                                        buffer.append(' ');
                                                        this.column++;
                                                       }
                                                this.needSpace = false;
                                        }
                                }
                                break;
                      }
        }


Clone AbstractionParameter Count: 0Parameter Bindings

public void indent() {
  this.indentationLevel += this.indentationSize;
  this.numberOfIndentations++;
}

/**
         * @param compilationUnitSource
         */
public void initializeScanner(char[] compilationUnitSource) {
  this.scanner.setSource(compilationUnitSource);
  this.scannerEndPosition = compilationUnitSource.length;
  this.scanner.resetTo(0, this.scannerEndPosition);
  this.edits = new OptimizedReplaceEdit[INITIAL_SIZE];
}

private boolean isValidEdit(OptimizedReplaceEdit edit) {
  final int editLength = edit.length;
  final int editReplacementLength = edit.replacement.length();
  final int editOffset = edit.offset;
  if (editLength != 0) {
    if (this.textRegionStart <= editOffset && (editOffset + editLength - 1) <= this.textRegionEnd) {
      if (editReplacementLength != 0 && editLength == editReplacementLength) {
        for (int i = editOffset, max = editOffset + editLength; i < max; i++) {
          if (scanner.source[i] != edit.replacement.charAt(i - editOffset)) {
            return true;
          }
        }
        return false;
      }
      else {
        return true;
      }
    }
    else
      if (editOffset + editLength == this.textRegionStart) {
        int i = editOffset;
        for (int max = editOffset + editLength; i < max; i++) {
          int replacementStringIndex = i - editOffset;
          if (replacementStringIndex >= editReplacementLength || scanner.source[i] != edit.replacement.charAt(replacementStringIndex)) {
            break;
          }
        }
        if (i - editOffset != editReplacementLength && i != editOffset + editLength - 1) {
          edit.offset = textRegionStart;
          edit.length = 0;
          edit.replacement = edit.replacement.substring(i - editOffset);
          return true;
        }
      }
  }
  else
    if (this.textRegionStart <= editOffset && editOffset <= this.textRegionEnd) {
      return true;
    }
    else
      if (editOffset == this.scannerEndPosition && editOffset == this.textRegionEnd + 1) {
        return true;
      }
  return false;
}

private void preserveEmptyLines(int count, int insertPosition) {
  if (count > 0) {
    if (this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
      int linesToPreserve = Math.min(count, this.formatter.preferences.number_of_empty_lines_to_preserve);
      this.printEmptyLines(linesToPreserve, insertPosition);
    }
    else {
      printNewLine(insertPosition);
    }
  }
}

private void print(char[] s, boolean considerSpaceIfAny) {
  if (checkLineWrapping && s.length + column > this.pageWidth) {
    handleLineTooLong();
  }
  this.lastNumberOfNewLines = 0;
  printIndentationIfNecessary();
  if (considerSpaceIfAny) {
    this.space();
  }
  if (this.pendingSpace) {
    this.addInsertEdit(this.scanner.getCurrentTokenStartPosition(), " "); //$NON-NLS-1$
  }
  this.pendingSpace = false;
  this.needSpace = false;
  column += s.length;
  needSpace = true;
}

private void printBlockComment(char[] s, boolean isJavadoc) {
  int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition();
  int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1;
  this.scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition - 1);
  int currentCharacter;
  boolean isNewLine = false;
  int start = currentTokenStartPosition;
  int nextCharacterStart = currentTokenStartPosition;
  printIndentationIfNecessary();
  if (this.pendingSpace) {
    this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$
  }
  this.needSpace = false;
  this.pendingSpace = false;
  int previousStart = currentTokenStartPosition;
  while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) {
    nextCharacterStart = this.scanner.currentPosition;
    switch (currentCharacter) {
      case '\r':
        start = previousStart;
        isNewLine = true;
        if (this.scanner.getNextChar('\n')) {
          currentCharacter = '\n';
          nextCharacterStart = this.scanner.currentPosition;
        }
        break;
      case '\n':
        start = previousStart;
        isNewLine = true;
        break;
      default:
        if (isNewLine) {
          if (ScannerHelper.isWhitespace((char) currentCharacter)) {
            int previousStartPosition = this.scanner.currentPosition;
            while (currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) {
              previousStart = nextCharacterStart;
              previousStartPosition = this.scanner.currentPosition;
              currentCharacter = this.scanner.getNextChar();
              nextCharacterStart = this.scanner.currentPosition;
            }
            if (currentCharacter == '\r' || currentCharacter == '\n') {
              nextCharacterStart = previousStartPosition;
            }
          }
          this.column = 1;
          this.line++;
          StringBuffer buffer = new StringBuffer();
          buffer.append(this.lineSeparator);
          printIndentationIfNecessary(buffer);
          buffer.append(' ');
          addReplaceEdit(start, previousStart - 1, String.valueOf(buffer));
        }
        else {
          this.column += (nextCharacterStart - previousStart);
        }
        isNewLine = false;
    }
    previousStart = nextCharacterStart;
    this.scanner.currentPosition = nextCharacterStart;
  }
  this.lastNumberOfNewLines = 0;
  needSpace = false;
  this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1);
  if (isJavadoc) {
    printNewLine();
  }
}

public void printEndOfCompilationUnit() {
  try {
    // if we have a space between two tokens we ensure it will be dumped in the formatted string
    int currentTokenStartPosition = this.scanner.currentPosition;
    boolean hasComment = false;
    boolean hasLineComment = false;
    boolean hasWhitespace = false;
    int count = 0;
    while (true) {
      this.currentToken = this.scanner.getNextToken();
      switch (this.currentToken) {
        case TerminalTokens.TokenNameWHITESPACE:
          char[] whiteSpaces = this.scanner.getCurrentTokenSource();
          count = 0;
          for (int i = 0, max = whiteSpaces.length; i < max; i++) {
            switch (whiteSpaces[i]) {
              case '\r':
                if ((i + 1) < max) {
                  if (whiteSpaces[i + 1] == '\n') {
                    i++;
                  }
                }
                count++;
                break;
              case '\n':
                count++;
            }
          }
          if (count == 0) {
            hasWhitespace = true;
            addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
          }
          else
            if (hasComment) {
              if (count == 1) {
                this.printNewLine(this.scanner.getCurrentTokenStartPosition());
              }
              else {
                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
              }
              addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
            }
            else
              if (hasLineComment) {
                this.preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition());
                addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
              }
              else {
                addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
              }
          currentTokenStartPosition = this.scanner.currentPosition;
          break;
        case TerminalTokens.TokenNameCOMMENT_LINE:
          if (count >= 1) {
            if (count > 1) {
              preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
            }
            else
              if (count == 1) {
                printNewLine(this.scanner.getCurrentTokenStartPosition());
              }
          }
          else
            if (hasWhitespace) {
              space();
            }
          hasWhitespace = false;
          this.printCommentLine(this.scanner.getRawTokenSource());
          currentTokenStartPosition = this.scanner.currentPosition;
          hasLineComment = true;
          count = 0;
          break;
        case TerminalTokens.TokenNameCOMMENT_BLOCK:
          if (count >= 1) {
            if (count > 1) {
              preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
            }
            else
              if (count == 1) {
                printNewLine(this.scanner.getCurrentTokenStartPosition());
              }
          }
          else
            if (hasWhitespace) {
              space();
            }
          hasWhitespace = false;
          this.printBlockComment(this.scanner.getRawTokenSource(), false);
          currentTokenStartPosition = this.scanner.currentPosition;
          hasLineComment = false;
          hasComment = true;
          count = 0;
          break;
        case TerminalTokens.TokenNameCOMMENT_JAVADOC:
          if (count >= 1) {
            if (count > 1) {
              preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
            }
            else
              if (count == 1) {
                printNewLine(this.scanner.getCurrentTokenStartPosition());
              }
          }
          else
            if (hasWhitespace) {
              space();
            }
          hasWhitespace = false;
          this.printBlockComment(this.scanner.getRawTokenSource(), true);
          currentTokenStartPosition = this.scanner.currentPosition;
          hasLineComment = false;
          hasComment = true;
          count = 0;
          break;
        case TerminalTokens.TokenNameSEMICOLON:
          char[] currentTokenSource = this.scanner.getRawTokenSource();
          this.print(currentTokenSource, this.formatter.preferences.insert_space_before_semicolon);
          break;
        case TerminalTokens.TokenNameEOF:
          if (count >= 1 || this.formatter.preferences.insert_new_line_at_end_of_file_if_missing) {
            this.printNewLine(this.scannerEndPosition);
          }
          return;
        default:
          // step back one token
          this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
          return;
      }
    }
  }
  catch (InvalidInputException e) {
    throw new AbortFormatting(e);
  }
}

public void printComment() {
  try {
    // if we have a space between two tokens we ensure it will be dumped in the formatted string
    int currentTokenStartPosition = this.scanner.currentPosition;
    boolean hasComment = false;
    boolean hasLineComment = false;
    boolean hasWhitespace = false;
    int count = 0;
    while ((this.currentToken = this.scanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
      switch (this.currentToken) {
        case TerminalTokens.TokenNameWHITESPACE:
          char[] whiteSpaces = this.scanner.getCurrentTokenSource();
          count = 0;
          for (int i = 0, max = whiteSpaces.length; i < max; i++) {
            switch (whiteSpaces[i]) {
              case '\r':
                if ((i + 1) < max) {
                  if (whiteSpaces[i + 1] == '\n') {
                    i++;
                  }
                }
                count++;
                break;
              case '\n':
                count++;
            }
          }
          if (count == 0) {
            hasWhitespace = true;
            addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
          }
          else
            if (hasComment) {
              if (count == 1) {
                this.printNewLine(this.scanner.getCurrentTokenStartPosition());
              }
              else {
                preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
              }
              addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
            }
            else
              if (hasLineComment) {
                this.preserveEmptyLines(count, this.scanner.getCurrentTokenStartPosition());
                addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
              }
              else
                if (count != 0 && this.formatter.preferences.number_of_empty_lines_to_preserve != 0) {
                  addReplaceEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition(), this.getPreserveEmptyLines(count - 1));
                }
                else {
                  addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
                }
          currentTokenStartPosition = this.scanner.currentPosition;
          break;
        case TerminalTokens.TokenNameCOMMENT_LINE:
          if (count >= 1) {
            if (count > 1) {
              preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
            }
            else
              if (count == 1) {
                printNewLine(this.scanner.getCurrentTokenStartPosition());
              }
          }
          else
            if (hasWhitespace) {
              space();
            }
          hasWhitespace = false;
          this.printCommentLine(this.scanner.getRawTokenSource());
          currentTokenStartPosition = this.scanner.currentPosition;
          hasLineComment = true;
          count = 0;
          break;
        case TerminalTokens.TokenNameCOMMENT_BLOCK:
          if (count >= 1) {
            if (count > 1) {
              preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
            }
            else
              if (count == 1) {
                printNewLine(this.scanner.getCurrentTokenStartPosition());
              }
          }
          else
            if (hasWhitespace) {
              space();
            }
          hasWhitespace = false;
          this.printBlockComment(this.scanner.getRawTokenSource(), false);
          currentTokenStartPosition = this.scanner.currentPosition;
          hasLineComment = false;
          hasComment = true;
          count = 0;
          break;
        case TerminalTokens.TokenNameCOMMENT_JAVADOC:
          if (count >= 1) {
            if (count > 1) {
              preserveEmptyLines(count - 1, this.scanner.getCurrentTokenStartPosition());
            }
            else
              if (count == 1) {
                printNewLine(this.scanner.getCurrentTokenStartPosition());
              }
          }
          else
            if (hasWhitespace) {
              space();
            }
          hasWhitespace = false;
          this.printBlockComment(this.scanner.getRawTokenSource(), true);
          currentTokenStartPosition = this.scanner.currentPosition;
          hasLineComment = false;
          hasComment = true;
          count = 0;
          break;
        default:
          // step back one token
          this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
          return;
      }
    }
  }
  catch (InvalidInputException e) {
    throw new AbortFormatting(e);
  }
}

private void printCommentLine(char[] s) {
  int currentTokenStartPosition = this.scanner.getCurrentTokenStartPosition();
  int currentTokenEndPosition = this.scanner.getCurrentTokenEndPosition() + 1;
  if (CharOperation.indexOf(Scanner.TAG_PREFIX, this.scanner.source, true, currentTokenStartPosition) != -1) {
    this.nlsTagCounter = 0;
  }
  this.scanner.resetTo(currentTokenStartPosition, currentTokenEndPosition - 1);
  int currentCharacter;
  int start = currentTokenStartPosition;
  int nextCharacterStart = currentTokenStartPosition;
  printIndentationIfNecessary();
  if (this.pendingSpace) {
    this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$
  }
  this.needSpace = false;
  this.pendingSpace = false;
  int previousStart = currentTokenStartPosition;
  loop:
    while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) {
      nextCharacterStart = this.scanner.currentPosition;
      switch (currentCharacter) {
        case '\r':
          start = previousStart;
          break loop;
        case '\n':
          start = previousStart;
          break loop;
      }
      previousStart = nextCharacterStart;
    }
  if (start != currentTokenStartPosition) {
    addReplaceEdit(start, currentTokenEndPosition - 1, lineSeparator);
  }
  line++;
  column = 1;
  needSpace = false;
  this.pendingSpace = false;
  lastNumberOfNewLines = 1;
  // realign to the proper value
  if (this.currentAlignment != null) {
    if (this.memberAlignment != null) {
      // select the last alignment
      if (this.currentAlignment.location.inputOffset > this.memberAlignment.location.inputOffset) {
        if (this.currentAlignment.couldBreak() && this.currentAlignment.wasSplit) {
          this.currentAlignment.performFragmentEffect();
        }
      }
      else {
        this.indentationLevel = Math.max(this.indentationLevel, this.memberAlignment.breakIndentationLevel);
      }
    }
    else
      if (this.currentAlignment.couldBreak() && this.currentAlignment.wasSplit) {
        this.currentAlignment.performFragmentEffect();
      }
  }
  this.scanner.resetTo(currentTokenEndPosition, this.scannerEndPosition - 1);
}

public void printEmptyLines(int linesNumber) {
  this.printEmptyLines(linesNumber, this.scanner.getCurrentTokenEndPosition() + 1);
}

private void printEmptyLines(int linesNumber, int insertPosition) {
  final String buffer = getEmptyLines(linesNumber);
  if (EMPTY_STRING == buffer)
    return;
  addInsertEdit(insertPosition, buffer);
}

void printIndentationIfNecessary() {
  StringBuffer buffer = new StringBuffer();
  printIndentationIfNecessary(buffer);
  if (buffer.length() > 0) {
    addInsertEdit(this.scanner.getCurrentTokenStartPosition(), buffer.toString());
    this.pendingSpace = false;
  }
}

private void printIndentationIfNecessary(StringBuffer buffer) {
  switch (this.tabChar) {
    case DefaultCodeFormatterOptions.TAB:
      boolean useTabsForLeadingIndents = this.useTabsOnlyForLeadingIndents;
      int numberOfLeadingIndents = this.numberOfIndentations;
      int indentationsAsTab = 0;
      if (useTabsForLeadingIndents) {
        while (this.column <= this.indentationLevel) {
          if (indentationsAsTab < numberOfLeadingIndents) {
            buffer.append('\t');
            indentationsAsTab++;
            this.lastNumberOfNewLines = 0;
            int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
            this.column += complement;
            this.needSpace = false;
          }
          else {
            buffer.append(' ');
            this.column++;
            this.needSpace = false;
          }
        }
      }
      else {
        while (this.column <= this.indentationLevel) {
          buffer.append('\t');
          this.lastNumberOfNewLines = 0;
          int complement = this.tabLength - ((this.column - 1) % this.tabLength); // amount of space
          this.column += complement;
          this.needSpace = false;
        }
      }
      break;
    case DefaultCodeFormatterOptions.SPACE:
      while (this.column <= this.indentationLevel) {
        buffer.append(' ');
        this.column++;
        this.needSpace = false;
      }
      break;
    case DefaultCodeFormatterOptions.MIXED:
      useTabsForLeadingIndents = this.useTabsOnlyForLeadingIndents;
      numberOfLeadingIndents = this.numberOfIndentations;
      indentationsAsTab = 0;
      if (useTabsForLeadingIndents) {
        final int columnForLeadingIndents = numberOfLeadingIndents * this.indentationSize;
        while (this.column <= this.indentationLevel) {
          if (this.column <= columnForLeadingIndents) {
            if ((this.column - 1 + this.tabLength) <= this.indentationLevel) {
              buffer.append('\t');
              this.column += this.tabLength;
            }
            else
              if ((this.column - 1 + this.indentationSize) <= this.indentationLevel) {
                // print one indentation
                for (int i = 0, max = this.indentationSize; i < max; i++) {
                  buffer.append(' ');
                  this.column++;
                }
              }
              else {
                buffer.append(' ');
                this.column++;
              }
          }
          else {
            for (int i = this.column, max = this.indentationLevel; i <= max; i++) {
              buffer.append(' ');
              this.column++;
            }
          }
          this.needSpace = false;
        }
      }
      else {
        while (this.column <= this.indentationLevel) {
          if ((this.column - 1 + this.tabLength) <= this.indentationLevel) {
            buffer.append('\t');
            this.column += this.tabLength;
          }
          else
            if ((this.column - 1 + this.indentationSize) <= this.indentationLevel) {
              // print one indentation
              for (int i = 0, max = this.indentationSize; i < max; i++) {
                buffer.append(' ');
                this.column++;
              }
            }
            else {
              buffer.append(' ');
              this.column++;
            }
          this.needSpace = false;
        }
      }
      break;
  }
}
 

CloneAbstraction
Parameter Bindings
Parameter
Index
Clone
Instance
Parameter
Name
Value
None