Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 64665 Details for
Bug 20793
[formatter] The code formatter indent left aligned comments
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
New patch
patch_20793.txt (text/plain), 12.56 KB, created by
Olivier Thomann
on 2007-04-23 20:18:13 EDT
(
hide
)
Description:
New patch
Filename:
MIME Type:
Creator:
Olivier Thomann
Created:
2007-04-23 20:18:13 EDT
Size:
12.56 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.jdt.core >Index: formatter/org/eclipse/jdt/internal/formatter/Scribe.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java,v >retrieving revision 1.105 >diff -u -r1.105 Scribe.java >--- formatter/org/eclipse/jdt/internal/formatter/Scribe.java 15 Mar 2007 18:53:55 -0000 1.105 >+++ formatter/org/eclipse/jdt/internal/formatter/Scribe.java 23 Apr 2007 23:56:23 -0000 >@@ -73,9 +73,12 @@ > public int numberOfIndentations; > private boolean useTabsOnlyForLeadingIndents; > >- /** indent empty lines*/ >- private final boolean indentEmptyLines; >- >+ /** indent empty lines*/ >+ private final boolean indentEmptyLines; >+ >+ private final boolean formatJavadocComment; >+ private final boolean formatBlockComment; >+ > Scribe(CodeFormatterVisitor formatter, long sourceLevel, int offset, int length, CodeSnippetParsingUtil codeSnippetParsingUtil) { > this.scanner = new Scanner(true, true, false/*nls*/, sourceLevel/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); > this.formatter = formatter; >@@ -102,6 +105,8 @@ > this.commentPositions = information.commentPositions; > } > } >+ this.formatBlockComment = formatter.preferences.comment_format_block_comment; >+ this.formatJavadocComment = formatter.preferences.comment_format_javadoc_comment; > reset(); > } > >@@ -401,6 +406,44 @@ > return -(g + 1); > } > >+ private int getCurrentCommentOffset(int start) { >+ int linePtr = -Arrays.binarySearch(this.lineEnds, start); >+ int offset = 0; >+ int beginningOfLine = this.getLineEnd(linePtr - 1); >+ if (beginningOfLine == -1) { >+ beginningOfLine = 0; >+ } >+ int currentStartPosition = start; >+ char[] source = scanner.source; >+ >+ // find the position of the beginning of the line containing the comment >+ while (beginningOfLine > currentStartPosition) { >+ if (linePtr > 0) { >+ beginningOfLine = this.getLineEnd(--linePtr); >+ } else { >+ beginningOfLine = 0; >+ break; >+ } >+ } >+ for (int i = currentStartPosition - 1; i >= beginningOfLine ; i--) { >+ char currentCharacter = source[i]; >+ switch (currentCharacter) { >+ case '\t' : >+ offset += this.tabLength; >+ break; >+ case ' ' : >+ offset++; >+ break; >+ case '\r' : >+ case '\n' : >+ break; >+ default: >+ return offset; >+ } >+ } >+ return offset; >+ } >+ > public String getEmptyLines(int linesNumber) { > if (this.nlsTagCounter > 0) { > return Util.EMPTY_STRING; >@@ -607,7 +650,15 @@ > this.scanner.resetTo(0, this.scannerEndPosition - 1); > this.edits = new OptimizedReplaceEdit[INITIAL_SIZE]; > } >- >+ >+ private boolean isOnFirstColumn(int start) { >+ if (this.lineEnds == null) return start == 0; >+ int index = Arrays.binarySearch(this.lineEnds, start); >+ // we want the line end of the previous line >+ int previousLineEnd = this.getLineEnd(-index - 1); >+ return previousLineEnd != -1 && previousLineEnd == start - 1; >+ } >+ > private boolean isValidEdit(OptimizedReplaceEdit edit) { > final int editLength= edit.length; > final int editReplacementLength= edit.replacement.length(); >@@ -663,7 +714,9 @@ > handleLineTooLong(); > } > this.lastNumberOfNewLines = 0; >- printIndentationIfNecessary(); >+ if (this.indentationLevel != 0) { >+ printIndentationIfNecessary(); >+ } > if (considerSpaceIfAny) { > this.space(); > } >@@ -685,13 +738,26 @@ > boolean isNewLine = false; > int start = currentTokenStartPosition; > int nextCharacterStart = currentTokenStartPosition; >- printIndentationIfNecessary(); >+ int previousStart = currentTokenStartPosition; >+ boolean onFirstColumn = isOnFirstColumn(start); >+ >+ boolean indentComment = false; >+ if (this.indentationLevel != 0) { >+ if (isJavadoc >+ || !this.formatter.preferences.never_indent_block_comments_on_first_column >+ || !onFirstColumn) { >+ indentComment = true; >+ printIndentationIfNecessary(); >+ } >+ } > if (this.pendingSpace) { > this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ > } >- this.needSpace = false; >- this.pendingSpace = false; >- int previousStart = currentTokenStartPosition; >+ this.needSpace = false; >+ this.pendingSpace = false; >+ >+ int currentCommentOffset = onFirstColumn ? 0 : getCurrentCommentOffset(start); >+ boolean formatComment = (isJavadoc && formatJavadocComment) || (!isJavadoc && formatBlockComment); > > while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { > nextCharacterStart = this.scanner.currentPosition; >@@ -708,34 +774,87 @@ > case '\n' : > start = previousStart; > isNewLine = true; >+ nextCharacterStart = this.scanner.currentPosition; > 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++; >- >+ isNewLine = false; >+ > StringBuffer buffer = new StringBuffer(); >- buffer.append(this.lineSeparator); >- printIndentationIfNecessary(buffer); >- buffer.append(' '); >- >+ if (onFirstColumn) { >+ // simply insert indentation if necessary >+ buffer.append(this.lineSeparator); >+ if (indentComment) { >+ printIndentationIfNecessary(buffer); >+ } >+ if (formatComment) { >+ 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; >+ } >+ } >+ if (currentCharacter != '\r' && currentCharacter != '\n') { >+ buffer.append(' '); >+ } >+ } >+ } else { >+ if (ScannerHelper.isWhitespace((char) currentCharacter)) { >+ int previousStartPosition = this.scanner.currentPosition; >+ int count = 0; >+ loop: while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { >+ if (count >= currentCommentOffset) { >+ break loop; >+ } >+ previousStart = nextCharacterStart; >+ previousStartPosition = this.scanner.currentPosition; >+ switch(currentCharacter) { >+ case '\t' : >+ count += this.tabLength; >+ break; >+ default : >+ count ++; >+ } >+ currentCharacter = this.scanner.getNextChar(); >+ nextCharacterStart = this.scanner.currentPosition; >+ } >+ if (currentCharacter == '\r' || currentCharacter == '\n') { >+ nextCharacterStart = previousStartPosition; >+ } >+ } >+ buffer.append(this.lineSeparator); >+ if (indentComment) { >+ printIndentationIfNecessary(buffer); >+ } >+ if (formatComment) { >+ int previousStartTemp = previousStart; >+ int nextCharacterStartTemp = nextCharacterStart; >+ while(currentCharacter != -1 && currentCharacter != '\r' && currentCharacter != '\n' && ScannerHelper.isWhitespace((char) currentCharacter)) { >+ previousStart = nextCharacterStart; >+ currentCharacter = this.scanner.getNextChar(); >+ nextCharacterStart = this.scanner.currentPosition; >+ } >+ if (currentCharacter == '*') { >+ buffer.append(' '); >+ } else { >+ previousStart = previousStartTemp; >+ nextCharacterStart = nextCharacterStartTemp; >+ } >+ this.scanner.currentPosition = nextCharacterStart; >+ } >+ } > addReplaceEdit(start, previousStart - 1, String.valueOf(buffer)); > } else { > this.column += (nextCharacterStart - previousStart); > } >- isNewLine = false; > } > previousStart = nextCharacterStart; > this.scanner.currentPosition = nextCharacterStart; >@@ -805,7 +924,7 @@ > space(); > } > hasWhitespace = false; >- this.printCommentLine(this.scanner.getRawTokenSource()); >+ this.printLineComment(this.scanner.getRawTokenSource()); > currentTokenStartPosition = this.scanner.currentPosition; > hasLineComment = true; > count = 0; >@@ -922,7 +1041,7 @@ > space(); > } > hasWhitespace = false; >- this.printCommentLine(this.scanner.getRawTokenSource()); >+ this.printLineComment(this.scanner.getRawTokenSource()); > currentTokenStartPosition = this.scanner.currentPosition; > hasLineComment = true; > count = 0; >@@ -972,7 +1091,7 @@ > } > } > >- private void printCommentLine(char[] s) { >+ private void printLineComment(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) { >@@ -982,12 +1101,18 @@ > int currentCharacter; > int start = currentTokenStartPosition; > int nextCharacterStart = currentTokenStartPosition; >- printIndentationIfNecessary(); >+ >+ if (this.indentationLevel != 0) { >+ if (!this.formatter.preferences.never_indent_line_comments_on_first_column >+ || !isOnFirstColumn(start)) { >+ printIndentationIfNecessary(); >+ } >+ } > if (this.pendingSpace) { > this.addInsertEdit(currentTokenStartPosition, " "); //$NON-NLS-1$ > } >- this.needSpace = false; >- this.pendingSpace = false; >+ this.needSpace = false; >+ this.pendingSpace = false; > int previousStart = currentTokenStartPosition; > > loop: while (nextCharacterStart <= currentTokenEndPosition && (currentCharacter = this.scanner.getNextChar()) != -1) { >@@ -1190,7 +1315,7 @@ > hasComment = true; > break; > case TerminalTokens.TokenNameCOMMENT_LINE : >- this.printCommentLine(this.scanner.getRawTokenSource()); >+ this.printLineComment(this.scanner.getRawTokenSource()); > currentTokenStartPosition = this.scanner.currentPosition; > break; > case TerminalTokens.TokenNameWHITESPACE : >@@ -1325,7 +1450,7 @@ > currentTokenStartPosition = this.scanner.currentPosition; > break; > case TerminalTokens.TokenNameCOMMENT_LINE : >- this.printCommentLine(this.scanner.getRawTokenSource()); >+ this.printLineComment(this.scanner.getRawTokenSource()); > currentTokenStartPosition = this.scanner.currentPosition; > break; > case TerminalTokens.TokenNameIdentifier : >@@ -1368,7 +1493,7 @@ > currentTokenStartPosition = this.scanner.currentPosition; > break; > case TerminalTokens.TokenNameCOMMENT_LINE : >- this.printCommentLine(this.scanner.getRawTokenSource()); >+ this.printLineComment(this.scanner.getRawTokenSource()); > currentTokenStartPosition = this.scanner.currentPosition; > break; > case TerminalTokens.TokenNameIdentifier : >@@ -1452,7 +1577,7 @@ > if (hasWhitespaces) { > space(); > } >- this.printCommentLine(this.scanner.getRawTokenSource()); >+ this.printLineComment(this.scanner.getRawTokenSource()); > currentTokenStartPosition = this.scanner.currentPosition; > hasLineComment = true; > break; >@@ -1526,7 +1651,7 @@ > if (hasWhitespaces) { > space(); > } >- this.printCommentLine(this.scanner.getRawTokenSource()); >+ this.printLineComment(this.scanner.getRawTokenSource()); > currentTokenStartPosition = this.scanner.currentPosition; > hasLineComment = true; > break;
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 20793
:
61138
| 64665