1 33 34 package edu.rice.cs.drjava.model.definitions.reducedmodel; 35 36 42 43 public class ReducedModelComment extends AbstractReducedModel { 44 45 46 TokenList.Iterator _walker; 47 48 49 public ReducedModelComment() { 50 super(); 51 _walker = _cursor._copy(); 52 } 53 54 public void insertChar(char ch) { 55 switch(ch) { 56 case '*': insertSpecial("*"); break; 57 case '/': insertSpecial("/"); break; 58 case '\n': insertNewline(); break; 59 case '\\': insertSpecial("\\"); break; 60 case '\'': insertQuote("'"); break; 61 case '\"': insertQuote("\""); break; 62 default: 63 _insertGap(1); break; 64 } 65 } 66 67 93 private void insertSpecial(String special) { 94 if (_tokens.isEmpty()) { 96 _cursor.insertNewBrace(special); return; 98 } 99 if (_cursor.atStart()) _cursor.next(); 101 102 if (_cursor.atEnd()) _checkPreviousInsertSpecial(special); 104 105 else if (_cursor.getBlockOffset() > 0 && _cursor.current().isMultipleCharBrace()) { 107 _cursor._splitCurrentIfCommentBlock(true,true); 108 _cursor.next(); _cursor.insertNewBrace(special); move(-2); 112 _updateBasedOnCurrentState(); 113 move(2); 114 } 115 else if (_cursor.getBlockOffset() > 0 && _cursor.current().isGap()) { 117 _cursor.insertBraceToGap(special); 118 _cursor.prev(); 119 _cursor.prev(); 120 _updateBasedOnCurrentState(); 121 _cursor.next(); 123 _cursor.next(); 124 } 126 else if ((_cursor.getBlockOffset() == 0) && _cursor.current().isMultipleCharBrace()) { 128 _cursor._splitCurrentIfCommentBlock(false,special.equals("\\")); 133 135 _checkPreviousInsertSpecial(special); 136 } 137 else _checkPreviousInsertSpecial(special); 138 } 139 140 145 private void _checkPreviousInsertSpecial(String special) { 146 if (special.equals("\\")) { 147 _checkPreviousInsertBackSlash(); 148 } 149 else { 150 _checkPreviousInsertCommentChar(special); 151 } 152 } 153 154 157 158 private void _checkPreviousInsertBackSlash() { 159 if (!_cursor.atStart() && !_cursor.atFirstItem()) { 160 if (_cursor.prevItem().getType().equals("\\")) { 161 _cursor.prevItem().setType("\\\\"); 162 _updateBasedOnCurrentState(); 163 return; 164 } 165 } 166 _cursor.insertNewBrace("\\"); _cursor.prev(); 169 _updateBasedOnCurrentState(); 170 if (_cursor.current().getSize() == 2) _cursor.setBlockOffset(1); 171 else _cursor.next(); 172 } 173 174 178 private void _checkPreviousInsertCommentChar(String special) { 179 if (!_cursor.atStart() && !_cursor.atFirstItem()) { 180 if ((_cursor.prevItem().getType().equals("/")) && (_cursor.prevItem().getState() == FREE)) { 181 _cursor.prevItem().setType("/" + special); 182 _updateBasedOnCurrentState(); 183 return; 184 } 185 else if (_cursor.prevItem().getType().equals("*") && 187 getStateAtCurrent() == INSIDE_BLOCK_COMMENT && 188 special.equals("/")) { 189 _cursor.prevItem().setType("*" + special); 190 _cursor.prevItem().setState(FREE); 191 _updateBasedOnCurrentState(); 192 return; 193 } 194 } 195 _cursor.insertNewBrace(special); _cursor.prev(); 198 _updateBasedOnCurrentState(); 199 if (_cursor.current().getSize() == 2) _cursor.setBlockOffset(1); 200 else _cursor.next(); 201 } 202 203 221 public void insertNewline() { 222 if (_cursor.atStart()) { 223 _insertNewEndOfLine(); 224 } 225 else if (_cursor.atEnd()) { 226 _insertNewEndOfLine(); 227 } 228 else if ((_cursor.getBlockOffset() > 0) && _cursor.current().isMultipleCharBrace()) { 229 _cursor._splitCurrentIfCommentBlock(true, true); 230 _cursor.next(); 231 _cursor.insert(Brace.MakeBrace("\n", getStateAtCurrent())); 232 _cursor.prev(); 233 _updateBasedOnCurrentState(); 234 _cursor.next(); 235 _cursor.next(); 236 _cursor.setBlockOffset(0); 237 } 238 else if ((_cursor.getBlockOffset() > 0) && _cursor.current().isGap()) { 239 _cursor.insertBraceToGap("\n"); 240 _cursor.prev(); 241 _cursor.prev(); 242 _updateBasedOnCurrentState(); 243 _cursor.next(); 245 _cursor.next(); 246 } 247 else { 248 _insertNewEndOfLine(); 249 } 250 return; 251 } 252 253 private void _insertNewEndOfLine() { 254 _cursor.insertNewBrace("\n"); 255 _cursor.prev(); 256 _updateBasedOnCurrentState(); 257 _cursor.next(); 258 _cursor.setBlockOffset(0); 259 } 260 261 288 public void insertQuote(String quote) { 289 if (_cursor.atStart()) { 290 _insertNewQuote(quote); 291 } 292 else if (_cursor.atEnd()) { 293 _insertNewQuote(quote); 294 } 295 else if ((_cursor.getBlockOffset() > 0) && _cursor.current().isMultipleCharBrace()) { 297 _cursor._splitCurrentIfCommentBlock(true,true); 298 _cursor.next(); 299 _cursor.insert(Brace.MakeBrace(quote, getStateAtCurrent())); 300 _cursor.prev(); 301 _updateBasedOnCurrentState(); 302 if (!_cursor.current().isMultipleCharBrace()) 303 _cursor.next(); 304 _cursor.next(); 305 _cursor.setBlockOffset(0); 306 } 307 else if ((_cursor.getBlockOffset() > 0) && _cursor.current().isGap()) { 309 _cursor.insertBraceToGap(quote); 310 _cursor.prev(); 311 _cursor.prev(); 312 _updateBasedOnCurrentState(); 313 _cursor.next(); 315 _cursor.next(); 316 317 } 318 else _insertNewQuote(quote); 319 return; 320 } 321 322 327 private void _insertNewQuote(String quote) { 328 String insert = _getQuoteType(quote); 329 _cursor.insertNewBrace(insert); 330 _cursor.prev(); 331 _updateBasedOnCurrentState(); 332 _cursor.next(); 333 _cursor.setBlockOffset(0); 334 } 335 336 344 private String _getQuoteType(String quote) { 345 if (_cursor.atStart() || _cursor.atFirstItem()) return quote; 346 else if (_cursor.prevItem().getType().equals("\\")) { 347 _cursor.prev(); 348 _cursor.remove(); 349 return "\\" + quote; 350 } 351 else return quote; 352 } 353 354 360 protected void insertGapBetweenMultiCharBrace(int length) { 361 if (_cursor.getBlockOffset() > 1) 362 throw new IllegalArgumentException ("OFFSET TOO BIG: " + _cursor.getBlockOffset()); 363 364 _cursor._splitCurrentIfCommentBlock(true, true); 365 _cursor.next(); 366 _insertNewGap(length); _cursor.prev(); 372 _cursor.prev(); 373 _updateBasedOnCurrentState(); 374 _cursor.next(); 376 _cursor.next(); 377 return; 378 } 379 380 387 388 private void _updateBasedOnCurrentState() { 389 TokenList.Iterator copyCursor = _cursor._copy(); 390 copyCursor.updateBasedOnCurrentState(); 391 copyCursor.dispose(); 392 } 393 394 398 public void move(int count) { _cursor.move(count); } 399 400 405 public void delete(int count) { 406 if (count == 0) return; 407 408 _cursor.delete(count); 409 410 416 int absOff = this.absOffset(); 418 int movement; 419 if (absOff < 2) movement = absOff; 420 else movement = 2; 421 _cursor.move(-movement); 422 _updateBasedOnCurrentState(); 424 _cursor.move(movement); 426 return; 427 } 428 429 430 435 436 439 protected ReducedModelState moveWalkerGetState(int relLocation) { 440 _walker.move(relLocation); 441 return _walker.getStateAtCurrent(); 442 } 443 444 445 protected void resetWalkerLocationToCursor() { 446 _walker.dispose(); 447 _walker = _cursor._copy(); 448 } 449 450 451 void getDistToPreviousNewline(IndentInfo braceInfo) { 452 braceInfo.distToPrevNewline = _getDistToPreviousNewline(_cursor._copy()); 453 braceInfo.distToNewline = braceInfo.distToPrevNewline; 454 return; 455 } 456 457 458 private int _getDistToPreviousNewline(TokenList.Iterator copyCursor) { 459 int walkcount = copyCursor.getBlockOffset(); 460 if (!copyCursor.atStart()) copyCursor.prev(); 461 while ((!copyCursor.atStart()) && 462 (!(copyCursor.current().getType().equals("\n")))) 463 { 464 walkcount += copyCursor.current().getSize(); 466 copyCursor.prev(); 467 } 468 469 if (copyCursor.atStart()) return -1; 470 return walkcount; 471 } 472 473 void getDistToIndentNewline(IndentInfo braceInfo) { 474 TokenList.Iterator copyCursor = _cursor._copy(); 475 476 if (braceInfo.distToBrace == -1 || copyCursor.atStart()) 477 return; 479 copyCursor.move(-braceInfo.distToBrace); 480 int walkcount = _getDistToPreviousNewline(copyCursor); 481 482 if (walkcount == -1) { 483 braceInfo.distToNewline = -1; 484 } 485 else { 486 braceInfo.distToNewline = walkcount + braceInfo.distToBrace; 487 } 488 return; 489 } 490 491 495 void getDistToCurrentBraceNewline(IndentInfo braceInfo) { 496 TokenList.Iterator copyCursor = _cursor._copy(); 497 498 if (braceInfo.distToBraceCurrent == -1 || copyCursor.atStart()) { return; 500 } 501 502 copyCursor.move(-braceInfo.distToBraceCurrent); 503 int walkcount = _getDistToPreviousNewline(copyCursor); 504 505 if (walkcount == -1) { 506 braceInfo.distToNewlineCurrent = -1; 507 } 508 else { 509 braceInfo.distToNewlineCurrent = walkcount + braceInfo.distToBraceCurrent; 510 } 511 return; 512 } 513 514 518 public int getDistToPreviousNewline(int relLoc) { 519 TokenList.Iterator copyCursor = _cursor._copy(); 520 copyCursor.move(-relLoc); 521 int dist = _getDistToPreviousNewline(copyCursor); 522 copyCursor.dispose(); 523 if (dist == -1) { 524 return -1; 525 } 526 return dist + relLoc; 527 } 528 529 530 public int getDistToNextNewline() { 531 TokenList.Iterator copyCursor = _cursor._copy(); 532 if (copyCursor.atStart()) { 533 copyCursor.next(); 534 } 535 if (copyCursor.atEnd() || copyCursor.current().getType().equals("\n")) { 536 return 0; 537 } 538 int walkcount = copyCursor.current().getSize() - _cursor.getBlockOffset(); 539 copyCursor.next(); 540 541 while ((!copyCursor.atEnd()) && 542 (!(copyCursor.current().getType().equals("\n")))) 543 { 544 walkcount += copyCursor.current().getSize(); 546 copyCursor.next(); 547 } 548 return walkcount; 549 } 550 } 551 | Popular Tags |