comparison dwtx/jface/text/FindReplaceDocumentAdapter.d @ 140:26688fec6d23

Following dsss compile errors
author Frank Benoit <benoit@tionex.de>
date Sun, 24 Aug 2008 03:23:46 +0200
parents 6dcb0baaa031
children 000f9136b8f7
comparison
equal deleted inserted replaced
139:93a6ec48fd28 140:26688fec6d23
215 215
216 /** 216 /**
217 * The match offset from the last findReplace call. 217 * The match offset from the last findReplace call.
218 */ 218 */
219 private int fFindReplaceMatchOffset; 219 private int fFindReplaceMatchOffset;
220 220
221 /** 221 /**
222 * Retain case mode 222 * Retain case mode
223 */ 223 */
224 private int fRetainCaseMode; 224 private int fRetainCaseMode;
225 225
348 } 348 }
349 349
350 int offset= fFindReplaceMatcher.start(); 350 int offset= fFindReplaceMatcher.start();
351 int length= fFindReplaceMatcher.group().length(); 351 int length= fFindReplaceMatcher.group().length();
352 352
353 if (fDocument instanceof IRepairableDocumentExtension 353 if (cast(IRepairableDocumentExtension)fDocument
354 && (cast(IRepairableDocumentExtension)fDocument).isLineInformationRepairNeeded(offset, length, replaceText)) { 354 && (cast(IRepairableDocumentExtension)fDocument).isLineInformationRepairNeeded(offset, length, replaceText)) {
355 String message= TextMessages.getString("FindReplaceDocumentAdapter.incompatibleLineDelimiter"); //$NON-NLS-1$ 355 String message= TextMessages.getString("FindReplaceDocumentAdapter.incompatibleLineDelimiter"); //$NON-NLS-1$
356 throw new PatternSyntaxException(message, replaceText, offset); 356 throw new PatternSyntaxException(message, replaceText, offset);
357 } 357 }
358 358
401 return null; 401 return null;
402 } 402 }
403 403
404 /** 404 /**
405 * Substitutes \R in a regex find pattern with (?>\r\n?|\n) 405 * Substitutes \R in a regex find pattern with (?>\r\n?|\n)
406 * 406 *
407 * @param findString the original find pattern 407 * @param findString the original find pattern
408 * @return the transformed find pattern 408 * @return the transformed find pattern
409 * @throws PatternSyntaxException if \R is added at an illegal position (e.g. in a character set) 409 * @throws PatternSyntaxException if \R is added at an illegal position (e.g. in a character set)
410 * @since 3.4 410 * @since 3.4
411 */ 411 */
412 private String substituteLinebreak(String findString) { 412 private String substituteLinebreak(String findString) {
413 int length= findString.length(); 413 int length= findString.length();
414 StringBuffer buf= new StringBuffer(length); 414 StringBuffer buf= new StringBuffer(length);
415 415
416 int inCharGroup= 0; 416 int inCharGroup= 0;
417 int inBraces= 0; 417 int inBraces= 0;
418 bool inQuote= false; 418 bool inQuote= false;
419 for (int i= 0; i < length; i++) { 419 for (int i= 0; i < length; i++) {
420 char ch= findString.charAt(i); 420 char ch= findString.charAt(i);
422 case '[': 422 case '[':
423 buf.append(ch); 423 buf.append(ch);
424 if (! inQuote) 424 if (! inQuote)
425 inCharGroup++; 425 inCharGroup++;
426 break; 426 break;
427 427
428 case ']': 428 case ']':
429 buf.append(ch); 429 buf.append(ch);
430 if (! inQuote) 430 if (! inQuote)
431 inCharGroup--; 431 inCharGroup--;
432 break; 432 break;
433 433
434 case '{': 434 case '{':
435 buf.append(ch); 435 buf.append(ch);
436 if (! inQuote && inCharGroup is 0) 436 if (! inQuote && inCharGroup is 0)
437 inBraces++; 437 inBraces++;
438 break; 438 break;
439 439
440 case '}': 440 case '}':
441 buf.append(ch); 441 buf.append(ch);
442 if (! inQuote && inCharGroup is 0) 442 if (! inQuote && inCharGroup is 0)
443 inBraces--; 443 inBraces--;
444 break; 444 break;
445 445
446 case '\\': 446 case '\\':
447 if (i + 1 < length) { 447 if (i + 1 < length) {
448 char ch1= findString.charAt(i + 1); 448 char ch1= findString.charAt(i + 1);
449 if (inQuote) { 449 if (inQuote) {
450 if (ch1 is 'E') 450 if (ch1 is 'E')
451 inQuote= false; 451 inQuote= false;
452 buf.append(ch).append(ch1); 452 buf.append(ch).append(ch1);
453 i++; 453 i++;
454 454
455 } else if (ch1 is 'R') { 455 } else if (ch1 is 'R') {
456 if (inCharGroup > 0 || inBraces > 0) { 456 if (inCharGroup > 0 || inBraces > 0) {
457 String msg= TextMessages.getString("FindReplaceDocumentAdapter.illegalLinebreak"); //$NON-NLS-1$ 457 String msg= TextMessages.getString("FindReplaceDocumentAdapter.illegalLinebreak"); //$NON-NLS-1$
458 throw new PatternSyntaxException(msg, findString, i); 458 throw new PatternSyntaxException(msg, findString, i);
459 } 459 }
460 buf.append("(?>\\r\\n?|\\n)"); //$NON-NLS-1$ 460 buf.append("(?>\\r\\n?|\\n)"); //$NON-NLS-1$
461 i++; 461 i++;
462 462
463 } else { 463 } else {
464 if (ch1 is 'Q') { 464 if (ch1 is 'Q') {
465 inQuote= true; 465 inQuote= true;
466 } 466 }
467 buf.append(ch).append(ch1); 467 buf.append(ch).append(ch1);
469 } 469 }
470 } else { 470 } else {
471 buf.append(ch); 471 buf.append(ch);
472 } 472 }
473 break; 473 break;
474 474
475 default: 475 default:
476 buf.append(ch); 476 buf.append(ch);
477 break; 477 break;
478 } 478 }
479 479
480 } 480 }
481 return buf.toString(); 481 return buf.toString();
482 } 482 }
483 483
484 /** 484 /**
485 * Interprets current Retain Case mode (all upper-case,all lower-case,capitalized or mixed) 485 * Interprets current Retain Case mode (all upper-case,all lower-case,capitalized or mixed)
486 * and appends the character <code>ch</code> to <code>buf</code> after processing. 486 * and appends the character <code>ch</code> to <code>buf</code> after processing.
487 * 487 *
488 * @param buf the output buffer 488 * @param buf the output buffer
489 * @param ch the character to process 489 * @param ch the character to process
490 * @since 3.4 490 * @since 3.4
491 */ 491 */
492 private void interpretRetainCase(StringBuffer buf, char ch) { 492 private void interpretRetainCase(StringBuffer buf, char ch) {
501 buf.append(ch); 501 buf.append(ch);
502 } 502 }
503 503
504 /** 504 /**
505 * Interprets escaped characters in the given replace pattern. 505 * Interprets escaped characters in the given replace pattern.
506 * 506 *
507 * @param replaceText the replace pattern 507 * @param replaceText the replace pattern
508 * @param foundText the found pattern to be replaced 508 * @param foundText the found pattern to be replaced
509 * @return a replace pattern with escaped characters substituted by the respective characters 509 * @return a replace pattern with escaped characters substituted by the respective characters
510 * @since 3.4 510 * @since 3.4
511 */ 511 */
512 private String interpretReplaceEscapes(String replaceText, String foundText) { 512 private String interpretReplaceEscapes(String replaceText, String foundText) {
513 int length= replaceText.length(); 513 int length= replaceText.length();
514 bool inEscape= false; 514 bool inEscape= false;
515 StringBuffer buf= new StringBuffer(length); 515 StringBuffer buf= new StringBuffer(length);
516 516
517 /* every string we did not check looks mixed at first 517 /* every string we did not check looks mixed at first
518 * so initialize retain case mode with RC_MIXED 518 * so initialize retain case mode with RC_MIXED
519 */ 519 */
520 fRetainCaseMode= RC_MIXED; 520 fRetainCaseMode= RC_MIXED;
521 521
522 for (int i= 0; i < length; i++) { 522 for (int i= 0; i < length; i++) {
523 final char ch= replaceText.charAt(i); 523 final char ch= replaceText.charAt(i);
524 if (inEscape) { 524 if (inEscape) {
525 i= interpretReplaceEscape(ch, i, buf, replaceText, foundText); 525 i= interpretReplaceEscape(ch, i, buf, replaceText, foundText);
526 inEscape= false; 526 inEscape= false;
527 527
528 } else if (ch is '\\') { 528 } else if (ch is '\\') {
529 inEscape= true; 529 inEscape= true;
530 530
531 } else if (ch is '$') { 531 } else if (ch is '$') {
532 buf.append(ch); 532 buf.append(ch);
533 533
534 /* 534 /*
535 * Feature in java.util.regex.Matcher#replaceFirst(String): 535 * Feature in java.util.regex.Matcher#replaceFirst(String):
551 } 551 }
552 } else { 552 } else {
553 interpretRetainCase(buf, ch); 553 interpretRetainCase(buf, ch);
554 } 554 }
555 } 555 }
556 556
557 if (inEscape) { 557 if (inEscape) {
558 // '\' as last character is invalid, but we still add it to get an error message 558 // '\' as last character is invalid, but we still add it to get an error message
559 buf.append('\\'); 559 buf.append('\\');
560 } 560 }
561 return buf.toString(); 561 return buf.toString();
562 } 562 }
563 563
564 /** 564 /**
565 * Interprets the escaped character <code>ch</code> at offset <code>i</code> 565 * Interprets the escaped character <code>ch</code> at offset <code>i</code>
566 * of the <code>replaceText</code> and appends the interpretation to <code>buf</code>. 566 * of the <code>replaceText</code> and appends the interpretation to <code>buf</code>.
567 * 567 *
568 * @param ch the escaped character 568 * @param ch the escaped character
569 * @param i the offset 569 * @param i the offset
570 * @param buf the output buffer 570 * @param buf the output buffer
571 * @param replaceText the original replace pattern 571 * @param replaceText the original replace pattern
572 * @param foundText the found pattern to be replaced 572 * @param foundText the found pattern to be replaced
612 if ('0' <= ch1 && ch1 <= '9') { 612 if ('0' <= ch1 && ch1 <= '9') {
613 buf.append('\\'); 613 buf.append('\\');
614 } 614 }
615 } 615 }
616 break; 616 break;
617 617
618 case '1': 618 case '1':
619 case '2': 619 case '2':
620 case '3': 620 case '3':
621 case '4': 621 case '4':
622 case '5': 622 case '5':
635 } else { 635 } else {
636 String msg= TextMessages.getFormattedString("FindReplaceDocumentAdapter.illegalControlEscape", "\\c"); //$NON-NLS-1$ //$NON-NLS-2$ 636 String msg= TextMessages.getFormattedString("FindReplaceDocumentAdapter.illegalControlEscape", "\\c"); //$NON-NLS-1$ //$NON-NLS-2$
637 throw new PatternSyntaxException(msg, replaceText, i); 637 throw new PatternSyntaxException(msg, replaceText, i);
638 } 638 }
639 break; 639 break;
640 640
641 case 'x': 641 case 'x':
642 if (i + 2 < length) { 642 if (i + 2 < length) {
643 int parsedInt; 643 int parsedInt;
644 try { 644 try {
645 parsedInt= Integer.parseInt(replaceText.substring(i + 1, i + 3), 16); 645 parsedInt= Integer.parseInt(replaceText.substring(i + 1, i + 3), 16);
654 } else { 654 } else {
655 String msg= TextMessages.getFormattedString("FindReplaceDocumentAdapter.illegalHexEscape", replaceText.substring(i - 1, length)); //$NON-NLS-1$ 655 String msg= TextMessages.getFormattedString("FindReplaceDocumentAdapter.illegalHexEscape", replaceText.substring(i - 1, length)); //$NON-NLS-1$
656 throw new PatternSyntaxException(msg, replaceText, i); 656 throw new PatternSyntaxException(msg, replaceText, i);
657 } 657 }
658 break; 658 break;
659 659
660 case 'u': 660 case 'u':
661 if (i + 4 < length) { 661 if (i + 4 < length) {
662 int parsedInt; 662 int parsedInt;
663 try { 663 try {
664 parsedInt= Integer.parseInt(replaceText.substring(i + 1, i + 5), 16); 664 parsedInt= Integer.parseInt(replaceText.substring(i + 1, i + 5), 16);
673 } else { 673 } else {
674 String msg= TextMessages.getFormattedString("FindReplaceDocumentAdapter.illegalUnicodeEscape", replaceText.substring(i - 1, length)); //$NON-NLS-1$ 674 String msg= TextMessages.getFormattedString("FindReplaceDocumentAdapter.illegalUnicodeEscape", replaceText.substring(i - 1, length)); //$NON-NLS-1$
675 throw new PatternSyntaxException(msg, replaceText, i); 675 throw new PatternSyntaxException(msg, replaceText, i);
676 } 676 }
677 break; 677 break;
678 678
679 case 'C': 679 case 'C':
680 if(foundText.toUpperCase().equals(foundText)) // is whole match upper-case? 680 if(foundText.toUpperCase().equals(foundText)) // is whole match upper-case?
681 fRetainCaseMode= RC_UPPER; 681 fRetainCaseMode= RC_UPPER;
682 else if (foundText.toLowerCase().equals(foundText)) // is whole match lower-case? 682 else if (foundText.toLowerCase().equals(foundText)) // is whole match lower-case?
683 fRetainCaseMode= RC_LOWER; 683 fRetainCaseMode= RC_LOWER;
701 * 701 *
702 * @param string the non-regex pattern 702 * @param string the non-regex pattern
703 * @return the string converted to a regex pattern 703 * @return the string converted to a regex pattern
704 */ 704 */
705 private String asRegPattern(String string) { 705 private String asRegPattern(String string) {
706 StringBuffer out= new StringBuffer(string.length()); 706 StringBuffer out_= new StringBuffer(string.length());
707 bool quoting= false; 707 bool quoting= false;
708 708
709 for (int i= 0, length= string.length(); i < length; i++) { 709 for (int i= 0, length= string.length(); i < length; i++) {
710 char ch= string.charAt(i); 710 char ch= string.charAt(i);
711 if (ch is '\\') { 711 if (ch is '\\') {
712 if (quoting) { 712 if (quoting) {
713 out.append("\\E"); //$NON-NLS-1$ 713 out_.append("\\E"); //$NON-NLS-1$
714 quoting= false; 714 quoting= false;
715 } 715 }
716 out.append("\\\\"); //$NON-NLS-1$ 716 out_.append("\\\\"); //$NON-NLS-1$
717 continue; 717 continue;
718 } 718 }
719 if (!quoting) { 719 if (!quoting) {
720 out.append("\\Q"); //$NON-NLS-1$ 720 out_.append("\\Q"); //$NON-NLS-1$
721 quoting= true; 721 quoting= true;
722 } 722 }
723 out.append(ch); 723 out_.append(ch);
724 } 724 }
725 if (quoting) 725 if (quoting)
726 out.append("\\E"); //$NON-NLS-1$ 726 out_.append("\\E"); //$NON-NLS-1$
727 727
728 return out.toString(); 728 return out_.toString();
729 } 729 }
730 730
731 /** 731 /**
732 * Substitutes the previous match with the given text. 732 * Substitutes the previous match with the given text.
733 * Sends a <code>DocumentEvent</code> to all registered <code>IDocumentListener</code>. 733 * Sends a <code>DocumentEvent</code> to all registered <code>IDocumentListener</code>.