Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 72953 Details for
Bug 80667
[find/replace] Regex replace patterns should interpret character patterns
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
[patch]
Combined fix for 44422, 80667, and 51530
44422_80667_patch.txt (text/plain), 37.21 KB, created by
Markus Keller
on 2007-07-03 09:59:54 EDT
(
hide
)
Description:
Combined fix for 44422, 80667, and 51530
Filename:
MIME Type:
Creator:
Markus Keller
Created:
2007-07-03 09:59:54 EDT
Size:
37.21 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.platform.doc.user >Index: reference/ref-regex-find-replace.htm >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.platform.doc.user/reference/ref-regex-find-replace.htm,v >retrieving revision 1.1 >diff -u -r1.1 ref-regex-find-replace.htm >--- reference/ref-regex-find-replace.htm 12 Apr 2007 11:27:19 -0000 1.1 >+++ reference/ref-regex-find-replace.htm 3 Jul 2007 13:58:28 -0000 >@@ -5,7 +5,6 @@ > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> > <meta http-equiv="Content-Style-Type" content="text/css"><title>Find/Replace with Regular Expressions</title> > >-<script language="JavaScript" src="PLUGINS_ROOT/org.eclipse.help/livehelp.js"></script> > <link rel="STYLESHEET" href="../book.css" charset="ISO-8859-1" type="text/css"></head><body bgcolor="#ffffff"> > > <h1 class="Head">Find/Replace with Regular Expressions</h1> >@@ -15,11 +14,38 @@ > </p> > > <p> >-The <strong>Replace</strong> field allows the following regular expressions: >-<ul> >- <li>$i to match capturing group i</li> >- <li>- \x to quote character x</li> >-</ul> >+The <strong>Replace</strong> field allows the following constructs: > </p> >+ <table border="1" cellpadding="5" summary="supported constructs in the replace field"> >+ <tbody> >+ <tr> >+ <td>$i</td> >+ <td>inserts >+ <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#cg">capturing group</a> >+ i. >+ </td> >+ </tr> >+ <tr> >+ <td>\i</td> >+ <td>inserts >+ <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#cg">capturing group</a> >+ i (same as $i). >+ </td> >+ </tr> >+ >+ <tr> <td>\<i>x</i></td> <td>quotes character <i>x</i> (unless \<i>x</i> has a special meaning)</td> </tr> >+ <tr> <td>\R</td> <td>inserts the default line delimiter of the document</td> </tr> >+ <tr> <td>\x<i>hh</i></td> <td>inserts the hexadecimal character <i>hh</i></td> </tr> >+ <tr> <td>\u<i>hhhh</i></td> <td>inserts the Unicode character <i>hhhh</i></td> </tr> >+ <tr> <td>\n</td> <td>inserts a newline character (usually, you should use \R instead)</td> </tr> >+ <tr> <td>\r</td> <td>inserts a carriage-return character (usually, you should use \R instead)</td> </tr> >+ <tr> <td>\t</td> <td>inserts a tab character</td> </tr> >+ <tr> <td>\f</td> <td>inserts a form-feed character</td> </tr> >+ <tr> <td>\a</td> <td>inserts an alert (beep) character</td> </tr> >+ <tr> <td>\e</td> <td>inserts an escape character</td> </tr> >+ <tr> <td>\c<i>C</i></td> <td>inserts a the control character for <i>C</i></td> </tr> >+ >+ </tbody> >+ </table> > > </body></html> >\ No newline at end of file >#P org.eclipse.ui.workbench.texteditor >Index: src/org/eclipse/ui/texteditor/RegExMessages.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/RegExMessages.java,v >retrieving revision 1.3 >diff -u -r1.3 RegExMessages.java >--- src/org/eclipse/ui/texteditor/RegExMessages.java 17 May 2006 11:17:27 -0000 1.3 >+++ src/org/eclipse/ui/texteditor/RegExMessages.java 3 Jul 2007 13:58:31 -0000 >@@ -48,6 +48,10 @@ > public static String additionalInfo_bs_n; > public static String displayString_bs_r; > public static String additionalInfo_bs_r; >+ public static String displayString_bs_rnWU; >+ public static String additionalInfo_bs_rnWU; >+ public static String displayString_bs_rn; >+ public static String additionalInfo_bs_rn; > public static String displayString_bs_f; > public static String additionalInfo_bs_f; > public static String displayString_bs_a; >@@ -189,12 +193,14 @@ > //replace > public static String displayString_dollar; > public static String additionalInfo_dollar; >- public static String additionalInfo_replace_bs; >+ public static String displayString_replace_cap; >+ public static String additionalInfo_replace_cap; > public static String displayString_replace_bs; >- public static String displayString_tab; >- public static String additionalInfo_tab; >- public static String displayString_nl; >- public static String additionalInfo_nl; >- public static String displayString_cr; >- public static String additionalInfo_cr; >+ public static String additionalInfo_replace_bs; >+ public static String displayString_replace_bs_n; >+ public static String additionalInfo_replace_bs_n; >+ public static String displayString_replace_bs_r; >+ public static String additionalInfo_replace_bs_r; >+ public static String displayString_replace_bs_R; >+ public static String additionalInfo_replace_bs_R; > } >Index: src/org/eclipse/ui/texteditor/FindNextAction.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindNextAction.java,v >retrieving revision 1.23 >diff -u -r1.23 FindNextAction.java >--- src/org/eclipse/ui/texteditor/FindNextAction.java 20 Jun 2007 14:30:51 -0000 1.23 >+++ src/org/eclipse/ui/texteditor/FindNextAction.java 3 Jul 2007 13:58:30 -0000 >@@ -124,12 +124,14 @@ > * @return the find string > */ > private String getFindString() { >- String string= getSelectionString(); >- >- if ((string == null || fRegExSearch && string.equals(fSelection)) && !fFindHistory.isEmpty()) >- string= (String) fFindHistory.get(0); >- >- return string; >+ String fullSelection= fTarget.getSelectionText(); >+ String firstLine= getFirstLine(fullSelection); >+ if ((firstLine.length() == 0 || fRegExSearch && fullSelection.equals(fSelection)) && !fFindHistory.isEmpty()) >+ return (String) fFindHistory.get(0); >+ else if (fRegExSearch && fullSelection.length() > 0) >+ return FindReplaceDialog.escapeForRegExPattern(fullSelection); >+ else >+ return firstLine; > } > > /** >@@ -385,17 +387,12 @@ > } > > /** >- * Returns the actual selection of the find replace target. >- * >- * @return the actual selection of the find replace target >+ * Returns the first line of the given selection. >+ * >+ * @param selection the selection >+ * @return the first line of the selection > */ >- private String getSelectionString() { >- >- /* >- * 1GF86V3: ITPUI:WINNT - Internal errors using Find/Replace Dialog >- * Now uses TextUtilities rather than focusing on '\n' >- */ >- String selection= fTarget.getSelectionText(); >+ private String getFirstLine(String selection) { > if (selection.length() > 0) { > int[] info= TextUtilities.indexOf(TextUtilities.DELIMITERS, selection, 0); > if (info[0] > 0) >@@ -403,6 +400,6 @@ > else if (info[0] == -1) > return selection; > } >- return null; >+ return selection; > } > } >Index: src/org/eclipse/ui/texteditor/RegExContentProposalProvider.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/RegExContentProposalProvider.java,v >retrieving revision 1.2 >diff -u -r1.2 RegExContentProposalProvider.java >--- src/org/eclipse/ui/texteditor/RegExContentProposalProvider.java 24 Apr 2006 09:38:52 -0000 1.2 >+++ src/org/eclipse/ui/texteditor/RegExContentProposalProvider.java 3 Jul 2007 13:58:31 -0000 >@@ -119,10 +119,12 @@ > addBsProposal("\\t", RegExMessages.displayString_bs_t, RegExMessages.additionalInfo_bs_t); //$NON-NLS-1$ > addBsProposal("\\n", RegExMessages.displayString_bs_n, RegExMessages.additionalInfo_bs_n); //$NON-NLS-1$ > addBsProposal("\\r", RegExMessages.displayString_bs_r, RegExMessages.additionalInfo_bs_r); //$NON-NLS-1$ >+ addBracketProposal("\\r?\\n", 5, RegExMessages.displayString_bs_rnWU, RegExMessages.additionalInfo_bs_rnWU); //$NON-NLS-1$ >+ addBracketProposal("(\\r\\n?|\\n)", 10, RegExMessages.displayString_bs_rn, RegExMessages.additionalInfo_bs_rn); //$NON-NLS-1$ > addBsProposal("\\f", RegExMessages.displayString_bs_f, RegExMessages.additionalInfo_bs_f); //$NON-NLS-1$ > addBsProposal("\\a", RegExMessages.displayString_bs_a, RegExMessages.additionalInfo_bs_a); //$NON-NLS-1$ > addBsProposal("\\e", RegExMessages.displayString_bs_e, RegExMessages.additionalInfo_bs_e); //$NON-NLS-1$ >- addBsProposal("\\c", RegExMessages.displayString_bs_c, RegExMessages.additionalInfo_bs_c); //$NON-NLS-1$ >+ addBracketProposal("\\c", 2, RegExMessages.displayString_bs_c, RegExMessages.additionalInfo_bs_c); //$NON-NLS-1$ > > if (! fIsEscape) > addBracketProposal(".", 1, RegExMessages.displayString_dot, RegExMessages.additionalInfo_dot); //$NON-NLS-1$ >@@ -156,25 +158,6 @@ > addBracketProposal("\\P{}", 3, RegExMessages.displayString_posixNot, RegExMessages.additionalInfo_posixNot); //$NON-NLS-1$ > } > >-// addBsProposal("\\p{Lower}", RegExMessages.displayString_bs_p{Lower}, RegExMessages.additionalInfo_bs_p{Lower}); //$NON-NLS-1$ >-// addBsProposal("\\p{Upper}", RegExMessages.displayString_bs_p{Upper}, RegExMessages.additionalInfo_bs_p{Upper}); //$NON-NLS-1$ >-// addBsProposal("\\p{ASCII}", RegExMessages.displayString_bs_p{ASCII}, RegExMessages.additionalInfo_bs_p{ASCII}); //$NON-NLS-1$ >-// addBsProposal("\\p{Alpha}", RegExMessages.displayString_bs_p{Alpha}, RegExMessages.additionalInfo_bs_p{Alpha}); //$NON-NLS-1$ >-// addBsProposal("\\p{Digit}", RegExMessages.displayString_bs_p{Digit}, RegExMessages.additionalInfo_bs_p{Digit}); //$NON-NLS-1$ >-// addBsProposal("\\p{Alnum}", RegExMessages.displayString_bs_p{Alnum}, RegExMessages.additionalInfo_bs_p{Alnum}); //$NON-NLS-1$ >-// addBsProposal("\\p{Punct}", RegExMessages.displayString_bs_p{Punct}, RegExMessages.additionalInfo_bs_p{Punct}); //$NON-NLS-1$ >-// addBsProposal("\\p{Graph}", RegExMessages.displayString_bs_p{Graph}, RegExMessages.additionalInfo_bs_p{Graph}); //$NON-NLS-1$ >-// addBsProposal("\\p{Print}", RegExMessages.displayString_bs_p{Print}, RegExMessages.additionalInfo_bs_p{Print}); //$NON-NLS-1$ >-// addBsProposal("\\p{Blank}", RegExMessages.displayString_bs_p{Blank}, RegExMessages.additionalInfo_bs_p{Blank}); //$NON-NLS-1$ >-// addBsProposal("\\p{Cntrl}", RegExMessages.displayString_bs_p{Cntrl}, RegExMessages.additionalInfo_bs_p{Cntrl}); //$NON-NLS-1$ >-// addBsProposal("\\p{XDigit}", RegExMessages.displayString_bs_p{XDigit}, RegExMessages.additionalInfo_bs_p{XDigit}); //$NON-NLS-1$ >-// addBsProposal("\\p{Space}", RegExMessages.displayString_bs_p{Space}, RegExMessages.additionalInfo_bs_p{Space}); //$NON-NLS-1$ >-// >-// addBsProposal("\\p{InGreek}", RegExMessages.displayString_bs_p{InGreek}, RegExMessages.additionalInfo_bs_p{InGreek}); //$NON-NLS-1$ >-// addBsProposal("\\p{Lu}", RegExMessages.displayString_bs_p{Lu}, RegExMessages.additionalInfo_bs_p{Lu}); //$NON-NLS-1$ >-// addBsProposal("\\p{Sc}", RegExMessages.displayString_bs_p{Sc}, RegExMessages.additionalInfo_bs_p{Sc}); //$NON-NLS-1$ >-// addBsProposal("\\P{InGreek}", RegExMessages.displayString_bs_P{InGreek}, RegExMessages.additionalInfo_bs_P{InGreek}); //$NON-NLS-1$ >- > //boundary matchers > if (fDocumentOffset == 0) { > addPriorityProposal("^", RegExMessages.displayString_start, RegExMessages.additionalInfo_start); //$NON-NLS-1$ >@@ -203,7 +186,7 @@ > addBracketProposal("(?:)", 3, RegExMessages.displayString_nonCap, RegExMessages.additionalInfo_nonCap); //$NON-NLS-1$ > addBracketProposal("(?>)", 3, RegExMessages.displayString_atomicCap, RegExMessages.additionalInfo_atomicCap); //$NON-NLS-1$ > >- //lookaraound >+ //look around > addBracketProposal("(?=)", 3, RegExMessages.displayString_posLookahead, RegExMessages.additionalInfo_posLookahead); //$NON-NLS-1$ > addBracketProposal("(?!)", 3, RegExMessages.displayString_negLookahead, RegExMessages.additionalInfo_negLookahead); //$NON-NLS-1$ > addBracketProposal("(?<=)", 4, RegExMessages.displayString_posLookbehind, RegExMessages.additionalInfo_posLookbehind); //$NON-NLS-1$ >@@ -249,11 +232,23 @@ > if (fDocumentOffset > 0 && '$' == fExpression.charAt(fDocumentOffset - 1)) { > addProposal("", RegExMessages.displayString_dollar, RegExMessages.additionalInfo_dollar); //$NON-NLS-1$ > } else { >- addProposal("$", RegExMessages.displayString_dollar, RegExMessages.additionalInfo_dollar); //$NON-NLS-1$ >+ if (! fIsEscape) >+ addProposal("$", RegExMessages.displayString_dollar, RegExMessages.additionalInfo_dollar); //$NON-NLS-1$ >+ addBsProposal("\\", RegExMessages.displayString_replace_cap, RegExMessages.additionalInfo_replace_cap); //$NON-NLS-1$ > addBsProposal("\\", RegExMessages.displayString_replace_bs, RegExMessages.additionalInfo_replace_bs); //$NON-NLS-1$ >- addProposal("\t", RegExMessages.displayString_tab, RegExMessages.additionalInfo_tab); //$NON-NLS-1$ >+ addBsProposal("\\R", RegExMessages.displayString_replace_bs_R, RegExMessages.additionalInfo_replace_bs_R); //$NON-NLS-1$ >+ addBracketProposal("\\x", 2, RegExMessages.displayString_bs_x, RegExMessages.additionalInfo_bs_x); //$NON-NLS-1$ >+ addBracketProposal("\\u", 2, RegExMessages.displayString_bs_u, RegExMessages.additionalInfo_bs_u); //$NON-NLS-1$ >+ addBsProposal("\\t", RegExMessages.displayString_bs_t, RegExMessages.additionalInfo_bs_t); //$NON-NLS-1$ >+ addBsProposal("\\n", RegExMessages.displayString_replace_bs_n, RegExMessages.additionalInfo_replace_bs_n); //$NON-NLS-1$ >+ addBsProposal("\\r", RegExMessages.displayString_replace_bs_r, RegExMessages.additionalInfo_replace_bs_r); //$NON-NLS-1$ >+ addBsProposal("\\f", RegExMessages.displayString_bs_f, RegExMessages.additionalInfo_bs_f); //$NON-NLS-1$ >+ addBsProposal("\\a", RegExMessages.displayString_bs_a, RegExMessages.additionalInfo_bs_a); //$NON-NLS-1$ >+ addBsProposal("\\e", RegExMessages.displayString_bs_e, RegExMessages.additionalInfo_bs_e); //$NON-NLS-1$ >+ addBracketProposal("\\c", 2, RegExMessages.displayString_bs_c, RegExMessages.additionalInfo_bs_c); //$NON-NLS-1$ > } >- return (IContentProposal[]) fProposals.toArray(new IContentProposal[fProposals.size()]); >+ fPriorityProposals.addAll(fProposals); >+ return (IContentProposal[]) fPriorityProposals.toArray(new IContentProposal[fPriorityProposals.size()]); > } > > /** >Index: src/org/eclipse/ui/texteditor/FindReplaceDialog.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/FindReplaceDialog.java,v >retrieving revision 1.70 >diff -u -r1.70 FindReplaceDialog.java >--- src/org/eclipse/ui/texteditor/FindReplaceDialog.java 3 Jul 2007 08:44:04 -0000 1.70 >+++ src/org/eclipse/ui/texteditor/FindReplaceDialog.java 3 Jul 2007 13:58:31 -0000 >@@ -919,11 +919,12 @@ > // ------- init / close --------------------------------------- > > /** >- * Returns the actual selection of the find replace target. >- * @return the selection of the target >+ * Returns the first line of the given selection. >+ * >+ * @param selection the selection >+ * @return the first line of the selection > */ >- private String getSelectionString() { >- String selection= fTarget.getSelectionText(); >+ private String getFirstLine(String selection) { > if (selection.length() > 0) { > int[] info= TextUtilities.indexOf(TextUtilities.DELIMITERS, selection, 0); > if (info[0] > 0) >@@ -931,7 +932,7 @@ > else if (info[0] == -1) > return selection; > } >- return null; >+ return selection; > } > > /** >@@ -978,7 +979,7 @@ > private void writeSelection() { > if (fTarget == null) > return; >- >+ > IDialogSettings s= getDialogSettings(); > s.put("selection", fTarget.getSelectionText()); //$NON-NLS-1$ > } >@@ -1006,11 +1007,15 @@ > */ > private void initFindStringFromSelection() { > if (fTarget != null && okToUse(fFindField)) { >- String selection= getSelectionString(); >+ String fullSelection= fTarget.getSelectionText(); >+ String firstLine= getFirstLine(fullSelection); >+ boolean isRegEx= isRegExSearchAvailableAndChecked(); > fFindField.removeModifyListener(fFindModifyListener); >- if (selection != null) { >- fFindField.setText(selection); >- if (!selection.equals(fTarget.getSelectionText())) { >+ if (firstLine.length() > 0 || (isRegEx && fullSelection.length() > 0)) { >+ String pattern= isRegEx ? escapeForRegExPattern(fullSelection) : firstLine; >+ fFindField.setText(pattern); >+ if (!firstLine.equals(fullSelection)) { >+ // multiple lines selected > useSelectedLines(true); > fGlobalRadioButton.setSelection(false); > fSelectedRangeRadioButton.setSelection(true); >@@ -1030,6 +1035,71 @@ > } > > /** >+ * Escapes special characters in the string, such that the resulting pattern >+ * matches the given string. >+ * >+ * @param string the string to escape >+ * @return a regex pattern that matches the given string >+ */ >+ public static String escapeForRegExPattern(String string) { >+ //implements https://bugs.eclipse.org/bugs/show_bug.cgi?id=44422 >+ >+ StringBuffer pattern= new StringBuffer(string.length() + 16); >+ int length= string.length(); >+ if (length > 0 && string.charAt(0) == '^') >+ pattern.append('\\'); >+ for (int i= 0; i < length; i++) { >+ char ch= string.charAt(i); >+ switch (ch) { >+ case '\\': >+ case '(': >+ case ')': >+ case '[': >+ case ']': >+ case '{': >+ case '}': >+ case '.': >+ case '?': >+ case '*': >+ case '+': >+ case '|': >+ pattern.append('\\').append(ch); >+ break; >+ >+ case '\n': >+ pattern.append("\\n"); //$NON-NLS-1$ >+ break; >+ case '\r': >+ pattern.append("\\r"); //$NON-NLS-1$ >+ break; >+ case '\t': >+ pattern.append("\\t"); //$NON-NLS-1$ >+ break; >+ case '\f': >+ pattern.append("\\f"); //$NON-NLS-1$ >+ break; >+ case 0x07: >+ pattern.append("\\a"); //$NON-NLS-1$ >+ break; >+ case 0x1B: >+ pattern.append("\\e"); //$NON-NLS-1$ >+ break; >+ >+ default: >+ if (0 <= ch && ch < 0x20) { >+ pattern.append("\\x"); //$NON-NLS-1$ >+ pattern.append(Integer.toHexString(ch).toUpperCase()); >+ } else { >+ pattern.append(ch); >+ } >+ } >+ } >+ if (length > 0 && string.charAt(length - 1) == '$') >+ pattern.insert(pattern.length() - 1, '\\'); >+ return pattern.toString(); >+ } >+ >+ /** > * Initializes the anchor used as starting point for incremental searching. > * @since 2.0 > */ >Index: src/org/eclipse/ui/texteditor/RegExMessages.properties >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/RegExMessages.properties,v >retrieving revision 1.5 >diff -u -r1.5 RegExMessages.properties >--- src/org/eclipse/ui/texteditor/RegExMessages.properties 12 Mar 2007 10:40:13 -0000 1.5 >+++ src/org/eclipse/ui/texteditor/RegExMessages.properties 3 Jul 2007 13:58:31 -0000 >@@ -22,17 +22,39 @@ > displayString_bs_t= \\t - Tab > additionalInfo_bs_t= Tabulator (\\x09, decimal: 9) > displayString_bs_n= \\n - Newline >-additionalInfo_bs_n= Newline (\\x0A, decimal: 10) >+additionalInfo_bs_n= Newline (\\x0A, decimal: 10)\n\n\ >+Note that \\n only finds newline characters. \ >+This can lead to unexpected results when the actual document uses different line delimiters.\n\n\ >+To find Windows and Unix style line delimiters, use \\r?\\n \n\ >+To find all kinds of line delimiters, use (\\r\\n?|\\n) > displayString_bs_r= \\r - CR >-additionalInfo_bs_r= Carriage Return (\\x0D, decimal: 13) >+additionalInfo_bs_r= Carriage Return (\\x0D, decimal: 13)\n\n\ >+Note that \\r only finds carriage return characters. \ >+This can lead to unexpected results when the actual document uses different line delimiters.\n\n\ >+To find Windows and Unix style line delimiters, use \\r?\\n \n\ >+To find all kinds of line delimiters, use (\\r\\n?|\\n) >+displayString_bs_rnWU= \\r?\\n - Line delimiter (Windows and Unix) >+additionalInfo_bs_rnWU= Line delimiter (Windows and Unix)\n\n\ >+This compound pattern matches Windows- and Unix-style line delimiters, i.e.\n\ >+- Windows (\\r\\n)\n\ >+- Unix (\\n)\n\n\ >+Note that this pattern does not work properly inside []. >+displayString_bs_rn= (\\r\\n?|\\n) - Line delimiter (platform independent) >+additionalInfo_bs_rn= Line delimiter (platform independent)\n\n\ >+This compound pattern matches any form of line delimiters, i.e.\n\ >+- Windows (\\r\\n)\n\ >+- Unix (\\n)\n\ >+- Mac OS 9 (\\r)\n\n\ >+Note that this pattern does not work inside [], and\n\ >+that it introduces a capturing group. > displayString_bs_f= \\f - FF > additionalInfo_bs_f= Form Feed (\\x0C, decimal: 12) > displayString_bs_a= \\a - Beep > additionalInfo_bs_a= Beep, Bell, Alert (\\x07, decimal: 7) > displayString_bs_e= \\e - Esc > additionalInfo_bs_e= Escape (\\x1B, decimal: 27) >-displayString_bs_c= \\c - Control character >-additionalInfo_bs_c= Control character\n\nExample:\n\\cC (Ctrl+C) >+displayString_bs_c= \\cC - Control character >+additionalInfo_bs_c= Control character for C\n\nExample:\n\\cC (Ctrl+C, \\x03, decimal: 3) > > displayString_dot= . - Any character > additionalInfo_dot= The dot matches any character except line terminators.\n\n\ >@@ -214,8 +236,8 @@ > Matches the preceding token if possible. Never backtracks,\n\ > even if this choice renders a full match impossible.\n\nExample:\n\ > The expression "fo?+o\\d" matches the first, but not the second line in text\n\ >-"fo1\n\ >-foo1". >+"foo1\n\ >+fo1". > displayString_starPoss= *+ Possessive match 0 or more times (no backtracking) > additionalInfo_starPoss= Possessive match 0 or more times.\n\n\ > Tries to match the preceding token as many times as possible. Never backtracks,\n\ >@@ -483,14 +505,27 @@ > $0 is the subsequence matched by the entire expression.\n\ > \n\ > Note: in the find expression, \\i stands for the capturing group i. >+displayString_replace_cap= \\i - Match of the capturing group i >+additionalInfo_replace_cap= Match of the capturing group i.\n\n\ >+\\i is the string that has been saved as capturing group i.\n\ >+\\0 is the subsequence matched by the entire expression.\n\ >+\n\ >+Note: \\i is equivalent to $i > displayString_replace_bs= \\ - Quote next character > additionalInfo_replace_bs= Quote next character\n\nExamples:\n\ > "\\$" will be replaced by "$".\n\ >-"\\a" will be replaced by "a".\n\ >+"\\q" will be replaced by "q".\n\ > "\\\\" will be replaced by "\\". >-displayString_tab= Tab - The tabulator character >-additionalInfo_tab= The tabulator character (\\t in the find expression). >-displayString_cr= CR - The carriage return character >-additionalInfo_cr= The carriage return character (\\r or \\x0D in the find expression). >-displayString_nl= Newline - The newline character >-additionalInfo_nl= The newline character (\\n or \\x0A in the find expression). >+displayString_replace_bs_n= \\n - Newline >+additionalInfo_replace_bs_n= Newline (\\x0A, decimal: 10)\n\n\ >+Note that \\n always inserts the newline character,\n\ >+even if the document uses different line delimiters.\n\n\ >+To insert the document line delimiter, use \\R. >+displayString_replace_bs_r= \\r - CR >+additionalInfo_replace_bs_r= Carriage Return (\\x0D, decimal: 13)\n\n\ >+Note that \\r always inserts the carriage return character,\n\ >+even if the document uses different line delimiters.\n\n\ >+To insert the document line delimiter, use \\R. >+displayString_replace_bs_R= \\R - Line delimiter >+additionalInfo_replace_bs_R= Line delimiter\n\n\ >+Inserts the default line delimiter of the document. >#P org.eclipse.text >Index: src/org/eclipse/jface/text/FindReplaceDocumentAdapter.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.text/src/org/eclipse/jface/text/FindReplaceDocumentAdapter.java,v >retrieving revision 1.21 >diff -u -r1.21 FindReplaceDocumentAdapter.java >--- src/org/eclipse/jface/text/FindReplaceDocumentAdapter.java 9 May 2007 11:07:49 -0000 1.21 >+++ src/org/eclipse/jface/text/FindReplaceDocumentAdapter.java 3 Jul 2007 13:58:33 -0000 >@@ -175,6 +175,7 @@ > Pattern pattern= fFindReplaceMatcher.pattern(); > Matcher replaceTextMatcher= pattern.matcher(fFindReplaceMatcher.group()); > try { >+ replaceText= interpretCharacterEscapes(replaceText); > replaceText= replaceTextMatcher.replaceFirst(replaceText); > } catch (IndexOutOfBoundsException ex) { > throw new PatternSyntaxException(ex.getLocalizedMessage(), replaceText, -1); >@@ -228,6 +229,220 @@ > } > > /** >+ * Interprets escaped characters in the given replace pattern. >+ * >+ * @param replaceText the replace pattern >+ * @return a replace pattern with escaped characters substituted by the respective characters >+ */ >+ private String interpretCharacterEscapes(String replaceText) { >+ int length= replaceText.length(); >+ boolean inEscape= false; >+ StringBuffer buf= null; >+ >+ for (int i= 0; i < length; i++) { >+ char ch= replaceText.charAt(i); >+ if (inEscape) { >+ switch (ch) { >+ case 'r': >+ buf= appendToBuffer('\r', buf, replaceText, i - 1); >+ break; >+ case 'n': >+ buf= appendToBuffer('\n', buf, replaceText, i - 1); >+ break; >+ case 't': >+ buf= appendToBuffer('\t', buf, replaceText, i - 1); >+ break; >+ case 'f': >+ buf= appendToBuffer('\f', buf, replaceText, i - 1); >+ break; >+ case 'a': >+ buf= appendToBuffer('\u0007', buf, replaceText, i - 1); >+ break; >+ case 'e': >+ buf= appendToBuffer('\u001B', buf, replaceText, i - 1); >+ break; >+ case 'R': //see http://www.unicode.org/unicode/reports/tr18/#Line_Boundaries >+ buf= appendToBuffer(TextUtilities.getDefaultLineDelimiter(fDocument), buf, replaceText, i - 1); >+ break; >+ /* >+ * \0 for octal is not supported in replace string, since it >+ * would conflict with capturing group \0, etc. >+ */ >+ case '0': >+ buf= appendToBuffer('$', buf, replaceText, i - 1); >+ buf.append(ch); >+ /* >+ * Feature in java.util.regex.Matcher#replaceFirst(String): >+ * $00, $000, etc. are interpreted as $0 and >+ * $01, $001, etc. are interpreted as $1, etc. . >+ * If we support \0 as replacement pattern for capturing group 0, >+ * it would not be possible any more to write a replacement pattern >+ * that appends 0 to a capturing group (like $0\0). >+ * The fix is to consider \00 and $00 as $0\0, and >+ * \01 and $01 as $0\1, etc. >+ */ >+ if (i + 1 < length) { >+ char nextCh= replaceText.charAt(i + 1); >+ if ('0' <= nextCh && nextCh <= '9') { >+ buf.append('\\'); >+ break; >+ } >+ } >+ break; >+ >+ case '1': >+ case '2': >+ case '3': >+ case '4': >+ case '5': >+ case '6': >+ case '7': >+ case '8': >+ case '9': >+ buf= appendToBuffer('$', buf, replaceText, i - 1); >+ buf.append(ch); >+ break; >+ >+ case 'c': >+ if (i + 1 < length) { >+ ch= replaceText.charAt(i + 1); >+ buf= appendToBuffer((char) (ch ^ 64), buf, replaceText, i - 1); >+ i++; >+ } else { >+ String msg= TextMessages.getFormattedString( >+ "FindReplaceDocumentAdapter.illegalControlEscape", //$NON-NLS-1$ >+ "\\c"); //$NON-NLS-1$ >+ throw new PatternSyntaxException(msg, replaceText, i); >+ } >+ break; >+ >+ case 'x': >+ if (i + 2 < length) { >+ int parsedInt; >+ try { >+ parsedInt= Integer.parseInt(replaceText.substring(i + 1, i + 3), 16); >+ if (parsedInt < 0) >+ throw new NumberFormatException(); >+ } catch (NumberFormatException e) { >+ String msg= TextMessages.getFormattedString( >+ "FindReplaceDocumentAdapter.illegalHexEscape", //$NON-NLS-1$ >+ replaceText.substring(i - 1, i + 3)); >+ throw new PatternSyntaxException(msg, replaceText, i); >+ } >+ buf= appendToBuffer((char) parsedInt, buf, replaceText, i - 1); >+ i+= 2; >+ } else { >+ String msg= TextMessages.getFormattedString( >+ "FindReplaceDocumentAdapter.illegalHexEscape", //$NON-NLS-1$ >+ replaceText.substring(i - 1, length)); >+ throw new PatternSyntaxException(msg, replaceText, i); >+ } >+ break; >+ >+ case 'u': >+ if (i + 4 < length) { >+ int parsedInt; >+ try { >+ parsedInt= Integer.parseInt(replaceText.substring(i + 1, i + 5), 16); >+ if (parsedInt < 0) >+ throw new NumberFormatException(); >+ } catch (NumberFormatException e) { >+ String msg= TextMessages.getFormattedString( >+ "FindReplaceDocumentAdapter.illegalUnicodeEscape", //$NON-NLS-1$ >+ replaceText.substring(i - 1, i + 5)); >+ throw new PatternSyntaxException(msg, replaceText, i); >+ } >+ buf= appendToBuffer((char) parsedInt, buf, replaceText, i - 1); >+ i+= 4; >+ } else { >+ String msg= TextMessages.getFormattedString( >+ "FindReplaceDocumentAdapter.illegalUnicodeEscape", //$NON-NLS-1$ >+ replaceText.substring(i - 1, length)); >+ throw new PatternSyntaxException(msg, replaceText, i); >+ } >+ break; >+ >+ default: >+ if (buf != null) >+ buf.append('\\').append(ch); >+ break; >+ } >+ inEscape= false; >+ >+ } else if (ch == '\\') { >+ inEscape= true; >+ >+ } else if (ch == '$') { >+ if (buf != null) { >+ buf.append(ch); >+ } >+ // see explanation above in "Feature in java.util.regex.Matcher#replaceFirst(String)" >+ if (i + 2 < length) { >+ char ch1= replaceText.charAt(i + 1); >+ char ch2= replaceText.charAt(i + 2); >+ if (ch1 == '0' && '0' <= ch2 && ch2 <= '9') { >+ i++; // consume the 0 >+ buf= appendToBuffer("0\\", buf, replaceText, i); //$NON-NLS-1$ >+ } >+ } else >+ break; >+ >+ } else if (buf != null) { >+ buf.append(ch); >+ } >+ } >+ >+ if (buf != null) { >+ if (inEscape) { >+ // '\' as last character is invalid, but we still add it to get an error message >+ buf.append('\\'); >+ } >+ return buf.toString(); >+ } >+ return replaceText; >+ } >+ >+ /** >+ * Creates or reuses a string buffer and appends a string to the buffer. >+ * If <code>buf</code> is <code>null</code>, a new buffer is created >+ * from <code>completeText.substring(0, i)</code>. >+ * Callers should use the result as new buffer. >+ * >+ * @param str string to append >+ * @param buf the existing buffer, or <code>null</code> >+ * @param completeText the complete text >+ * @param i the index into <code>completeText</code> >+ * >+ * @return the buffer >+ */ >+ private StringBuffer appendToBuffer(String str, StringBuffer buf, String completeText, int i) { >+ if (buf == null) >+ buf= new StringBuffer(completeText.substring(0, i)); >+ buf.append(str); >+ return buf; >+ } >+ >+ /** >+ * Creates or reuses a string buffer and appends a character to the buffer. >+ * If <code>buf</code> is <code>null</code>, a new buffer is created >+ * from <code>completeText.substring(0, i)</code>. >+ * Callers should use the result as new buffer. >+ * >+ * @param ch string to append >+ * @param buf the existing buffer, or <code>null</code> >+ * @param completeText the complete text >+ * @param i the index into <code>completeText</code> >+ * >+ * @return the buffer >+ */ >+ private static StringBuffer appendToBuffer(char ch, StringBuffer buf, String completeText, int i) { >+ if (buf == null) >+ buf= new StringBuffer(completeText.substring(0, i)); >+ buf.append(ch); >+ return buf; >+ } >+ >+ /** > * Converts a non-regex string to a pattern > * that can be used with the regex search engine. > * >Index: src/org/eclipse/jface/text/TextMessages.properties >=================================================================== >RCS file: src/org/eclipse/jface/text/TextMessages.properties >diff -N src/org/eclipse/jface/text/TextMessages.properties >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/text/TextMessages.properties 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,14 @@ >+############################################################################### >+# Copyright (c) 2007 IBM Corporation and others. >+# All rights reserved. This program and the accompanying materials >+# are made available under the terms of the Eclipse Public License v1.0 >+# which accompanies this distribution, and is available at >+# http://www.eclipse.org/legal/epl-v10.html >+# >+# Contributors: >+# IBM Corporation - initial API and implementation >+############################################################################### >+ >+FindReplaceDocumentAdapter.illegalControlEscape=Illegal control escape sequence {0} >+FindReplaceDocumentAdapter.illegalHexEscape=Illegal hexadecimal escape sequence {0} >+FindReplaceDocumentAdapter.illegalUnicodeEscape=Illegal Unicode escape sequence {0} >Index: src/org/eclipse/jface/text/TextMessages.java >=================================================================== >RCS file: src/org/eclipse/jface/text/TextMessages.java >diff -N src/org/eclipse/jface/text/TextMessages.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jface/text/TextMessages.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,42 @@ >+/******************************************************************************* >+ * Copyright (c) 2007 IBM Corporation and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * IBM Corporation - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.jface.text; >+ >+import java.util.MissingResourceException; >+import java.util.ResourceBundle; >+ >+import com.ibm.icu.text.MessageFormat; >+ >+public class TextMessages { >+ private static final String BUNDLE_NAME= "org.eclipse.jface.text.TextMessages"; //$NON-NLS-1$ >+ >+ private static final ResourceBundle RESOURCE_BUNDLE= ResourceBundle.getBundle(BUNDLE_NAME); >+ >+ private TextMessages() { >+ } >+ >+ public static String getString(String key) { >+ try { >+ return RESOURCE_BUNDLE.getString(key); >+ } catch (MissingResourceException e) { >+ return '!' + key + '!'; >+ } >+ } >+ >+ public static String getFormattedString(String key, Object arg) { >+ return getFormattedString(key, new Object[] { arg }); >+ } >+ >+ public static String getFormattedString(String key, Object[] args) { >+ return MessageFormat.format(getString(key), args); >+ } >+ >+} >#P org.eclipse.text.tests >Index: src/org/eclipse/text/tests/FindReplaceDocumentAdapterTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.text.tests/src/org/eclipse/text/tests/FindReplaceDocumentAdapterTest.java,v >retrieving revision 1.9 >diff -u -r1.9 FindReplaceDocumentAdapterTest.java >--- src/org/eclipse/text/tests/FindReplaceDocumentAdapterTest.java 29 May 2007 18:23:59 -0000 1.9 >+++ src/org/eclipse/text/tests/FindReplaceDocumentAdapterTest.java 3 Jul 2007 13:58:36 -0000 >@@ -211,6 +211,61 @@ > } > } > >+ public void testRegexReplace() throws Exception { >+ fDocument.set( >+ "UnixWindowsMacInferred\n" + >+ "Chars" >+ ); >+ FindReplaceDocumentAdapter findReplaceDocumentAdapter= new FindReplaceDocumentAdapter(fDocument); >+ regexReplace("Unix", "$0\\n", findReplaceDocumentAdapter); >+ regexReplace("(Windows)", "$1\\r\\n", findReplaceDocumentAdapter); >+ regexReplace("(M)ac", "\\0\\r", findReplaceDocumentAdapter); >+ regexReplace("(Inferred)", "\\1\\R", findReplaceDocumentAdapter); >+ regexReplace("Chars", "\\\\, \\xF6, \\u00F6, \\t, \\n, \\r, \\f, \\a, \\e, \\cF", findReplaceDocumentAdapter); >+ >+ String text= "Unix\nWindows\r\nMac\rInferred\n\n" + >+ "\\, \u00F6, \u00F6, \t, \n, \r, \f, \u0007, \u001B, \u0006"; >+ assertEquals(text, fDocument.get()); >+ } >+ >+ public void testRegexReplace2() throws Exception { >+ FindReplaceDocumentAdapter findReplaceDocumentAdapter= new FindReplaceDocumentAdapter(fDocument); >+ >+ fDocument.set("foo"); >+ regexReplace("foo", "\\00", findReplaceDocumentAdapter); >+ assertEquals("foo0", fDocument.get()); >+ >+ fDocument.set("foo"); >+ regexReplace("foo", "\\010", findReplaceDocumentAdapter); >+ assertEquals("foo10", fDocument.get()); >+ >+ fDocument.set("foo"); >+ regexReplace("foo", "$00", findReplaceDocumentAdapter); >+ assertEquals("foo0", fDocument.get()); >+ >+ fDocument.set("foo"); >+ regexReplace("foo", "$010", findReplaceDocumentAdapter); >+ assertEquals("foo10", fDocument.get()); >+ } >+ >+ public void testRegexReplace3() throws Exception { >+ FindReplaceDocumentAdapter findReplaceDocumentAdapter= new FindReplaceDocumentAdapter(fDocument); >+ >+ fDocument.set("foo"); >+ regexReplace("(f)oo", "\\10", findReplaceDocumentAdapter); >+ assertEquals("f0", fDocument.get()); >+ >+ fDocument.set("foo"); >+ regexReplace("(f)oo", "$10", findReplaceDocumentAdapter); >+ assertEquals("f0", fDocument.get()); >+ } >+ >+ private void regexReplace(String find, String replace, FindReplaceDocumentAdapter findReplaceDocumentAdapter) throws BadLocationException { >+ findReplaceDocumentAdapter.find(0, find, true, true, false, true); //$NON-NLS-1$ >+ IRegion r= findReplaceDocumentAdapter.replace(replace, true); //$NON-NLS-1$ >+ assertNotNull(r); >+ } >+ > public void testIllegalState() { > FindReplaceDocumentAdapter findReplaceDocumentAdapter= new FindReplaceDocumentAdapter(fDocument); > try {
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 80667
:
71358
| 72953