1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import org.netbeans.jmi.javamodel.Annotation; 22 import org.netbeans.jmi.javamodel.Element; 23 import org.netbeans.jmi.javamodel.EnumConstant; 24 import org.netbeans.jmi.javamodel.Expression; 25 import org.netbeans.jmi.javamodel.Feature; 26 import org.netbeans.jmi.javamodel.Field; 27 import org.netbeans.jmi.javamodel.FieldGroup; 28 import org.netbeans.jmi.javamodel.ForStatement; 29 import org.netbeans.jmi.javamodel.JavaClass; 30 import org.netbeans.jmi.javamodel.LocalVarDeclaration; 31 import org.netbeans.jmi.javamodel.Resource; 32 import org.netbeans.jmi.javamodel.Statement; 33 import org.netbeans.jmi.javamodel.StatementBlock; 34 import org.netbeans.lib.java.parser.ASTree; 35 import org.netbeans.lib.java.parser.ParserTokens; 36 import org.netbeans.lib.java.parser.Token; 37 import org.netbeans.modules.javacore.parser.MDRParser; 38 39 44 public final class IndentUtil { 45 46 54 static String leftTrim(String s) { 55 int len = s.length(); 56 char[] val = s.toCharArray(); 57 int pos = 0; 58 while ((pos < len) && (val[pos] <= ' ')) 59 pos++; 60 return pos == 0 ? s : s.substring(pos, len); 61 } 62 63 72 static String getOriginalIndentation(String s, int[] origPos) { 73 int len = s.length(); 74 char[] val = s.toCharArray(); 75 int pos = 0; 76 StringBuffer result = new StringBuffer (); 77 78 while ((pos < len) && (val[pos] <= ' ')) 79 pos++; 80 81 while (pos > 0 && (val[--pos] == ' ' || val[pos] == '\t')) 82 if (val[pos] == ' ' || val[pos] == '\t') 83 result.append(val[pos]); 84 origPos[0] = pos; 85 return result.reverse().toString(); 86 } 87 88 101 static String indentExistingElement(MetadataElement element, String src) { 102 StringBuffer result = new StringBuffer (80); 103 if (element instanceof EnumConstant || 104 ((element instanceof Field && element.refImmediateComposite() instanceof FieldGroup))) { 105 return src; 107 } else if (element instanceof Statement || element instanceof Feature || element instanceof FieldGroup) { 108 int[] oP = new int[1]; 109 String origInd = getOriginalIndentation(src, oP); 110 String [] statementLines = src.split("\\n"); for (int i = 0; i < statementLines.length; i++) { 112 String line = statementLines[i]; 113 if (leftTrim(line).length() == 0) { 114 result.append('\n'); 115 continue; 116 } 117 if (line.endsWith("\r")) { line = line.substring(0, line.length()-1); 119 } 120 result.append(element.getIndentation()); 121 if (line.indexOf(origInd) > -1) { 122 result.append(line.substring(origInd.length())); 123 } else { 124 result.append(line); 125 } 126 if (i < (statementLines.length-1)) 127 result.append('\n'); 128 } 129 return result.toString(); 130 } 131 return src; 133 } 134 135 static String indentNewElement(MetadataElement e) { 136 StringBuffer sb = new StringBuffer (1024); 137 String sourceText = e.getRawText(); 138 MetadataElement parent = (MetadataElement) e.refImmediateComposite(); 139 if (e instanceof StatementBlock) { 140 sb.append(e.getElementPrefix(MetadataElement.BLOCK_OPEN_CURLY)); 141 sb.append(sourceText); 142 } else if (e instanceof Annotation || 143 e instanceof EnumConstant) 144 { 145 sb.append(sourceText); 146 } else if (e instanceof Statement || 147 e instanceof Feature || 148 e instanceof FieldGroup) 149 { 150 if ((e instanceof LocalVarDeclaration && parent instanceof ForStatement) || 155 (e instanceof Field && parent instanceof FieldGroup)) 156 { 157 sb.append(sourceText); 158 } else { 159 sb.append('\n'); 160 sb.append(e.getIndentation()); 161 sb.append(sourceText); 162 } 163 } else { 164 sb.append(sourceText); 165 } 166 return sb.toString(); 167 } 168 169 187 static int getElementStart(Element el) { 188 MetadataElement e = (MetadataElement) el; 189 int result = e.getStartOffset(e.getParser(), e.getASTree(), true); 191 if (e instanceof Resource) { 192 result = 0; 194 195 } else if (e instanceof Expression) { 196 result = e.getStartOffset(e.getParser(), e.getASTree(), false); 198 199 } else if (e instanceof Annotation) { 200 result = e.getParser().getToken(e.getASTree().getFirstToken()).getStartOffset(); 203 } 204 return result; 205 } 206 207 225 static int getElementEnd(Element el) { 226 MetadataElement e = (MetadataElement) el; 227 int result = e.getEndOffset(e.getParser(), e.getASTree()); 229 if (e instanceof Resource) { 230 result = e.getParser().getSourceText().length(); 232 } 233 return result; 234 } 235 236 245 static final void printTokenWithAllAround(int token, int origToken, MetadataElement element, StringBuffer buf) { 246 assert !element.isNew() : "Cannot call it for the new element!"; 247 MDRParser parser = element.getParser(); 248 Token t = parser.getToken(token-1); 249 int begin = t.getEndOffset(); 250 t = parser.getToken(token+1); 251 int end = t.getStartOffset(); 252 String s = parser.getSourceText().substring(begin, end); 253 StringBuffer tempInd = new StringBuffer (); 255 if (!getOriginalIndentation(origToken, element, tempInd)) { 256 buf.append(s); 257 } else { 258 buf.append(reformat(s, element.getIndentation(), tempInd.toString())); 259 } 260 } 261 262 269 static final void printTailGarbage(MetadataElement element, StringBuffer buf) { 270 assert !element.isNew() : "Cannot call it for new elements!"; ASTree tree = element.getASTree(); 272 MDRParser parser = element.getParser(); 273 buf.append(getGarbage(tree.getLastToken()+1, parser, true)); 274 } 275 276 283 static final void printHeadGarbage(MetadataElement element, StringBuffer buf) { 284 assert !element.isNew() : "Cannot call it for new elements!"; ASTree tree = element.getASTree(); 286 MDRParser parser = element.getParser(); 287 buf.append(getGarbage(tree.getFirstToken(), parser, false)); 288 } 289 290 static String getGarbage(int tokenId, MDRParser p, boolean tail) { 291 StringBuffer buf = new StringBuffer (15); 292 int startOffset = tokenId == 0? 0 : p.getToken(tokenId-1).getEndOffset(), 293 endOffset = p.getToken(tokenId).getStartOffset(); 294 Token[] pad = p.getToken(tokenId).getPadding(); 295 if (pad.length > 0) { 296 for (int i = 0; i < pad.length; i++) { 297 if (pad[i].getType() == ParserTokens.EOL) { 298 if (tail) 299 endOffset = pad[i].getStartOffset(); 300 else 301 startOffset = pad[i].getStartOffset(); 302 break; 303 } 304 } 305 } 306 return p.getSourceText().substring(startOffset, endOffset); 307 } 308 309 static String printTokenWithPads(int tokenId, MDRParser p) { 310 String startPads = getGarbage(tokenId, p, false); 311 String endPads = getGarbage(tokenId+1, p, true); 312 Token token = p.getToken(tokenId); 313 return startPads + p.getText(token) + endPads; 314 } 315 316 static final String reformatTokenWithPads(MetadataElement element, int tokenId) { 317 MDRParser p = element.getParser(); 318 String startPads = getGarbage(tokenId, p, false); 319 String endPads = getGarbage(tokenId+1, p, true); 320 return indentExistingElement(element, startPads, tokenId) + p.getText(p.getToken(tokenId)) + endPads; 321 } 322 323 static final void reformatHeadGarbage(MetadataElement element, int tokenId, StringBuffer buf) { 324 MDRParser p = element.getParser(); 325 String startPads = getGarbage(tokenId, p, false); 326 buf.append(indentExistingElement(element, startPads, tokenId)); 327 } 328 329 static void reformatHeadGarbage(MetadataElement element, StringBuffer buf) { 330 StringBuffer sb = new StringBuffer (80); 331 printHeadGarbage(element, sb); 332 buf.append(indentExistingElement2(element, sb.toString())); 333 } 334 335 static String indentExistingElement2(MetadataElement element, String src) { 337 StringBuffer result = new StringBuffer (80); 338 if (element instanceof EnumConstant || 339 ((element instanceof Field && element.refImmediateComposite() instanceof FieldGroup))) { 340 return src; 342 } else if (element instanceof Statement || element instanceof Feature || element instanceof FieldGroup) { 343 int[] oP = new int[1]; 344 String origInd = getOriginalIndentation(src, oP); 345 String [] statementLines = src.split("\\n"); for (int i = 0; i < statementLines.length; i++) { 347 String line = statementLines[i]; 348 if (line.endsWith("\r")) { line = line.substring(0, line.length()-1); 350 } 351 if (line.indexOf(origInd) > -1) { 352 result.append(element.getIndentation()); 353 result.append(line.substring(origInd.length())); 354 } else { 355 result.append(line); 356 } 357 if (i < statementLines.length-1) 358 result.append('\n'); 359 } 360 return result.toString(); 361 } 362 return src; 364 } 365 366 381 private static boolean getOriginalIndentation(int tokenId, MetadataElement element, StringBuffer buf) { 382 assert !element.isNew() : "Cannot call it for the new element!"; 383 MDRParser p = element.getParser(); 384 Token[] pad = p.getToken(tokenId).getPadding(); 385 int startOffset = -1; 386 if (pad.length > 0) { 387 int endOffset = p.getToken(tokenId).getStartOffset(); 388 for (int i = 0; i < pad.length; i++) { 389 if (pad[i].getType() == ParserTokens.EOL) { 390 startOffset = pad[i].getEndOffset(); 391 } 392 } 393 if (startOffset != -1) { 394 buf.append(p.getSourceText().substring(startOffset, endOffset)); 395 return true; 396 } 397 } 398 return false; 399 } 400 401 414 static String indentExistingElement(MetadataElement element, String src, int tokenId) { 415 assert !element.isNew() : "Cannot call it for the new element!"; 416 if (element instanceof EnumConstant || 417 ((element instanceof Field && element.refImmediateComposite() instanceof FieldGroup))) { 418 return src; 420 } else if (element instanceof Statement || element instanceof Feature || element instanceof FieldGroup) { 421 StringBuffer tempInd = new StringBuffer (); 423 if (!getOriginalIndentation(tokenId, element, tempInd)) { 424 return src; 425 } 426 return reformat(src, element.getIndentation(), tempInd.toString()); 427 } 428 return src; 430 } 431 432 private static String reformat(final String src, final String newInd, final String origInd) { 433 StringBuffer result = new StringBuffer (80); 434 String [] statementLines = src.split("\\n"); for (int i = 0; i < statementLines.length; i++) { 439 String line = statementLines[i]; 440 if (line.endsWith("\r")) { line = line.substring(0, line.length()-1); 443 } 444 if (line.indexOf(origInd) == 0) { 447 result.append(newInd); 448 result.append(line.substring(origInd.length())); 449 } else { 450 result.append(line); 451 } 452 if (i < statementLines.length-1) 455 result.append('\n'); 456 } 457 return result.toString(); 458 } 459 460 } 461 | Popular Tags |