Changeset 25 in default for trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui
- Timestamp:
- 11/02/10 12:29:25 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/de.ugoe.cs.swe.bnftools.ebnf.ui/src/de/ugoe/cs/swe/bnftools/ui/formatter/EbnfFormatterVisitor.java
r24 r25 36 36 private StringBuffer buf; 37 37 private FormatterConfig config; 38 private int bufferPosition = 0;38 private int bufferPositionFormattedTextNoWhitespaces = 0; 39 39 private int bufferPositionOriginalText = 0; 40 40 private int allCommentsPosition = 0; … … 46 46 private int bufferPositionFormattedText; 47 47 private String formattedText; 48 private String bufNoWhitespaces;48 private String formattedTextNoWhitespaces; 49 49 private String originalTextNoWhitespaces; 50 50 … … 105 105 106 106 private void skipWhitespacesOriginalText() { 107 while ( isWhitespace(originalText.charAt(bufferPositionOriginalText))) {107 while (bufferPositionOriginalText < originalText.length() && isWhitespace(originalText.charAt(bufferPositionOriginalText))) { 108 108 bufferPositionOriginalText++; 109 109 } … … 111 111 112 112 private void skipWhitespacesFormattedText(StringBuffer result) { 113 while ( isWhitespace(formattedText.charAt(bufferPositionFormattedText))) {113 while (bufferPositionFormattedText < formattedText.length() && isWhitespace(formattedText.charAt(bufferPositionFormattedText))) { 114 114 result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1)); 115 115 bufferPositionFormattedText++; … … 175 175 176 176 private void weaveComments() { 177 bufferPosition = 0; 177 // if (true) { 178 // StringBuffer result = new StringBuffer(); 179 // result.append(buf.toString()); 180 // buf = result; 181 // return; 182 // } 183 178 184 bufferPositionOriginalText = 0; 185 bufferPositionFormattedTextNoWhitespaces = 0; 179 186 bufferPositionFormattedText = 0; 180 187 181 188 StringBuffer result = new StringBuffer(); 182 bufNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", "");189 formattedTextNoWhitespaces = buf.toString().replaceAll("[ \t\n\r]", ""); 183 190 formattedText = buf.toString(); 184 191 185 while (bufferPosition < bufNoWhitespaces.length()) {192 while (bufferPositionFormattedTextNoWhitespaces < formattedTextNoWhitespaces.length()) { 186 193 skipWhitespacesOriginalText(); 187 194 skipWhitespacesFormattedText(result); 188 189 if (bufNoWhitespaces.charAt(bufferPosition) != originalText.charAt(bufferPositionOriginalText)) { 190 if (isCommentNext(originalText, bufferPositionOriginalText)) { 195 196 if (!(bufferPositionOriginalText < originalText.length())) 197 break; 198 199 char formattedPositionNoWhitespaces = formattedTextNoWhitespaces.charAt(bufferPositionFormattedTextNoWhitespaces); 200 char originalPosition = originalText.charAt(bufferPositionOriginalText); 201 202 if (formattedPositionNoWhitespaces != originalPosition) { 203 if (formattedPositionNoWhitespaces == ';') { // formatted text always outputs the optional semicolon, skip it if necessary 204 bufferPositionFormattedTextNoWhitespaces++; 205 bufferPositionFormattedText++; 206 } else if (isCommentNext(originalText, bufferPositionOriginalText)) { 191 207 LeafNode currentComment = allComments.get(allCommentsPosition); 192 208 if (currentComment.getTotalOffset() == bufferPositionOriginalText) { … … 194 210 int newLinesCount = scanBackNewlinesCount(originalText, bufferPositionOriginalText-1); 195 211 if (newLinesCount > 0) { 212 if (scanBackNewlinesCount(result.toString(), result.toString().length()-1) == 0) { 213 result.append("\n\n"); 214 } 215 196 216 result.append(currentComment.getText()); 197 217 result.append("\n"); … … 216 236 allCommentsPosition++; 217 237 } 238 } else { // disaster handling: return original unformatted text! 239 buf = new StringBuffer(); 240 buf.append(originalText); 241 return; 218 242 } 219 243 } else { 220 244 result.append(formattedText.substring(bufferPositionFormattedText, bufferPositionFormattedText+1)); 221 222 245 bufferPositionOriginalText++; 223 246 bufferPositionFormattedText++; 224 bufferPosition ++;247 bufferPositionFormattedTextNoWhitespaces++; 225 248 } 226 249 }
Note: See TracChangeset
for help on using the changeset viewer.