1 33 34 package edu.rice.cs.drjava.model.definitions.reducedmodel; 35 36 import java.util.Stack ; 37 38 44 public class ReducedModelBrace extends AbstractReducedModel { 45 46 private ReducedModelControl _parent; 47 48 public ReducedModelBrace(ReducedModelControl parent) { 49 super(); 50 _parent = parent; 51 } 52 53 public void insertChar(char ch) { 54 switch(ch) { 55 case '{': 56 case '}': 57 case '[': 58 case ']': 59 case '(': 60 case ')': 61 _insertBrace(String.valueOf(ch)); 62 break; 63 default: 64 _insertGap(1); 65 break; 66 } 67 } 68 69 70 98 private void _insertBrace(String text) { 99 if (_cursor.atStart() || _cursor.atEnd()) { 100 _cursor.insertNewBrace(text); } 102 else if (_cursor.current().isGap()) { 103 _cursor.insertBraceToGap(text); 104 } 105 106 else { 107 _cursor.insertNewBrace(text); 108 } 109 } 110 111 116 protected void insertGapBetweenMultiCharBrace(int length) { 117 throw new RuntimeException ("ReducedModelBrace does not keep track of multi-character braces."); 118 } 119 120 125 public void move(int count) { _cursor.move(count); } 126 127 131 public void delete( int count ) { 132 if (count == 0) return; 133 _cursor.delete(count); 134 return; 135 } 136 137 141 private boolean _isCurrentBraceMatchable() { 142 String type = _cursor.current().getType(); 143 return (((type.equals("{")) || 144 (type.equals("}")) || 145 (type.equals("(")) || 146 (type.equals(")")) || 147 (type.equals("[")) || 148 (type.equals("]"))) && 149 (_parent.getStateAtCurrent() == FREE)); 150 } 151 152 160 public int previousBrace() { 161 int relDistance; 162 int dist = 0; 163 resetWalkerLocationToCursor(); 165 TokenList.Iterator copyCursor = _cursor._copy(); 166 if (!copyCursor.atStart()) { 167 copyCursor.prev(); 168 } 169 if (copyCursor.atStart()) { 170 copyCursor.dispose(); 171 return -1; 172 } 173 dist += _cursor.getBlockOffset(); 175 relDistance = dist; 176 177 180 while (!copyCursor.atStart()) { 181 if (!copyCursor.current().isGap()) { 182 if (moveWalkerGetState(-relDistance) == FREE) { 183 copyCursor.dispose(); 184 return dist + copyCursor.current().getSize(); 185 } 186 relDistance = 0; 187 } 188 189 dist += copyCursor.current().getSize(); 190 relDistance += copyCursor.current().getSize(); 191 copyCursor.prev(); 192 } 193 copyCursor.dispose(); 194 return -1; 195 } 196 197 198 204 public int nextBrace() { 205 int relDistance = 0; 206 int dist = 0; 207 TokenList.Iterator copyCursor = _cursor._copy(); 208 209 resetWalkerLocationToCursor(); 210 211 if ( copyCursor.atStart()) 212 copyCursor.next(); 213 if (_cursor.getBlockOffset() > 0) { 214 dist = copyCursor.current().getSize() - _cursor.getBlockOffset(); 215 relDistance = dist; 216 copyCursor.next(); 217 } 218 while (!copyCursor.atEnd() ) { 220 if (!copyCursor.current().isGap()) { 221 if (moveWalkerGetState(relDistance) == 222 FREE) { 223 copyCursor.dispose(); 224 return dist; 225 } 226 relDistance = 0; 227 } 228 relDistance += copyCursor.current().getSize(); 229 dist += copyCursor.current().getSize(); 230 copyCursor.next(); 231 } 232 copyCursor.dispose(); 233 return -1; 234 } 235 236 244 public int balanceForward() { 245 Stack <ReducedToken> braceStack = new Stack <ReducedToken>(); 247 TokenList.Iterator iter = _cursor._copy(); 248 resetWalkerLocationToCursor(); 249 int relDistance; 250 int distance = 0; 251 if (iter.atStart() || iter.atFirstItem() || !openBraceImmediatelyLeft()) { 252 iter.dispose(); 254 return -1; 256 } 257 258 iter.prev(); 259 relDistance = -iter.current().getSize(); 260 if (iter.current().isOpenBrace()) { 263 if (moveWalkerGetState(relDistance) == FREE) { 264 braceStack.push(iter.current()); 266 267 iter.next(); 269 moveWalkerGetState(-relDistance); 270 relDistance = 0; 271 } 272 else { 273 iter.dispose(); 275 return -1; 277 } 278 } 279 else { 280 iter.dispose(); 282 return -1; 284 } 285 while (!iter.atEnd() && !braceStack.isEmpty()) { 290 if (!iter.current().isGap()) { 291 if (moveWalkerGetState(relDistance) == FREE) { 292 if (iter.current().isClosedBrace()) { 294 ReducedToken popped = braceStack.pop(); 295 if (!iter.current().isMatch(popped)) { 296 iter.dispose(); 297 return -1; 299 } 300 } 301 else { 303 braceStack.push(iter.current()); 304 } 305 } 306 relDistance = 0; 307 } 308 distance += iter.current().getSize(); 311 relDistance += iter.current().getSize(); 312 iter.next(); 313 } 314 315 if (!braceStack.isEmpty()) { 317 iter.dispose(); 318 return -1; 320 } 321 else { 323 iter.dispose(); 324 return distance; 325 } 326 } 327 328 341 public boolean openBraceImmediatelyLeft() { 342 if (_cursor.atStart() || _cursor.atFirstItem()) { 343 return false; 344 } 345 else { 346 _cursor.prev(); 347 354 boolean isLeft = ((_cursor.getBlockOffset() == 0) && _cursor.current().isOpen() && 355 _isCurrentBraceMatchable()); 356 _cursor.next(); 358 return isLeft; 361 } 362 } 363 364 public boolean closedBraceImmediatelyLeft() { 365 if (_cursor.atStart() || _cursor.atFirstItem()) { 366 return false; 367 } 368 else { 369 _cursor.prev(); 370 377 boolean isLeft = ((_cursor.getBlockOffset() == 0) && _cursor.current().isClosed() && 378 _isCurrentBraceMatchable()); 379 _cursor.next(); 381 return isLeft; 384 } 385 } 386 387 401 public int balanceBackward() { 402 Stack <ReducedToken> braceStack = new Stack <ReducedToken>(); 404 TokenList.Iterator iter = _cursor._copy(); 405 resetWalkerLocationToCursor(); 406 int relDistance; 407 int distance = 0; 408 if (iter.atStart() || iter.atFirstItem() || !closedBraceImmediatelyLeft()) { 409 iter.dispose(); 411 return -1; 413 } 414 415 iter.prev(); 416 relDistance = iter.current().getSize(); 417 if (iter.current().isClosedBrace()) { 420 if (moveWalkerGetState(-relDistance) == FREE) { 421 424 braceStack.push(iter.current()); 425 distance += iter.current().getSize(); 426 iter.prev(); 427 if (!iter.atStart()) { 428 distance += iter.current().getSize(); 429 relDistance = iter.current().getSize(); 430 } 431 } 432 else { 433 iter.dispose(); 434 return -1; 436 } 437 } 438 else { 439 iter.dispose(); 440 return -1; 442 } 443 while (!iter.atStart() && !braceStack.isEmpty()) { 448 if (!iter.current().isGap()) { 449 if (moveWalkerGetState(-relDistance) == 450 FREE) { 451 if (iter.current().isOpenBrace()) { 453 ReducedToken popped = braceStack.pop(); 454 if (!iter.current().isMatch(popped)) { 455 iter.dispose(); 456 return -1; 458 } 459 } 460 else { 462 braceStack.push(iter.current()); 463 } 464 } 465 relDistance = 0; 466 } 467 iter.prev(); 470 if (!iter.atStart() && !braceStack.isEmpty()) { 471 distance += iter.current().getSize(); 472 relDistance += iter.current().getSize(); 473 } 474 } 475 476 if (!braceStack.isEmpty()) { 478 iter.dispose(); 479 return -1; 481 } 482 else { 484 iter.dispose(); 485 return distance; 486 } 487 } 488 489 protected ReducedModelState moveWalkerGetState(int relDistance) { 490 return _parent.moveWalkerGetState(relDistance); 491 } 492 493 protected void resetWalkerLocationToCursor() { 494 _parent.resetLocation(); 495 } 496 497 501 protected void getDistToEnclosingBrace(IndentInfo braceInfo) { 502 Stack <ReducedToken> braceStack = new Stack <ReducedToken>(); 503 TokenList.Iterator iter = _cursor._copy(); 504 resetWalkerLocationToCursor(); 505 int relDistance = braceInfo.distToNewline + 1; 507 int distance = relDistance; 508 509 if (braceInfo.distToNewline == -1) { 510 iter.dispose(); 511 return; 512 } 513 iter.move(-braceInfo.distToNewline - 1); 515 relDistance += iter.getBlockOffset(); 516 distance += iter.getBlockOffset(); 517 518 braceInfo.distToNewline = -1; 521 522 if (iter.atStart() || iter.atFirstItem()) { 523 iter.dispose(); 524 return; 525 } 526 527 iter.prev(); 528 529 while (!iter.atStart()) { 534 535 distance += iter.current().getSize(); 536 relDistance += iter.current().getSize(); 537 538 if (!iter.current().isGap()) { 539 540 if (moveWalkerGetState(-relDistance) == FREE) { 541 if (iter.current().isOpenBrace()) { 543 if (braceStack.isEmpty()) { 544 braceInfo.braceType = iter.current().getType(); 545 braceInfo.distToBrace = distance; 546 iter.dispose(); 547 return; 548 } 549 ReducedToken popped = braceStack.pop(); 550 if (!iter.current().isMatch(popped)) { 551 iter.dispose(); 552 return; 553 } 554 } 555 else { 557 braceStack.push(iter.current()); 558 } 559 } 560 relDistance = 0; 561 } 562 iter.prev(); 565 } 566 567 iter.dispose(); 568 return; 569 } 570 571 572 575 protected void getDistToEnclosingBraceCurrent(IndentInfo braceInfo) { 576 Stack <ReducedToken> braceStack = new Stack <ReducedToken>(); 577 TokenList.Iterator iter = _cursor._copy(); 578 resetWalkerLocationToCursor(); 579 int relDistance = 0; 580 int distance = relDistance; 581 582 583 586 relDistance += iter.getBlockOffset(); 587 distance += iter.getBlockOffset(); 588 589 braceInfo.distToNewlineCurrent = -1; 592 593 if (iter.atStart() || iter.atFirstItem()) { 594 iter.dispose(); 595 return; 596 } 597 598 iter.prev(); 599 600 while (!iter.atStart()) { 605 606 distance += iter.current().getSize(); 607 relDistance += iter.current().getSize(); 608 609 if (!iter.current().isGap()) { 610 611 if (moveWalkerGetState(-relDistance) == FREE) { 612 if (iter.current().isOpenBrace()) { 614 if (braceStack.isEmpty()) { 615 braceInfo.braceTypeCurrent = iter.current().getType(); 616 braceInfo.distToBraceCurrent = distance; 617 iter.dispose(); 618 return; 619 } 620 ReducedToken popped = braceStack.pop(); 621 if (!iter.current().isMatch(popped)) { 622 iter.dispose(); 623 return; 624 } 625 } 626 else { 628 braceStack.push(iter.current()); 629 } 630 } 631 relDistance = 0; 632 } 633 iter.prev(); 636 } 637 638 iter.dispose(); 639 return; 640 } 641 } 642 | Popular Tags |