### Eclipse Workspace Patch 1.0 #P org.eclipse.ui.workbench.texteditor 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.22 diff -u -r1.22 FindNextAction.java --- src/org/eclipse/ui/texteditor/FindNextAction.java 10 Jul 2006 09:56:52 -0000 1.22 +++ src/org/eclipse/ui/texteditor/FindNextAction.java 15 Jun 2007 14:41:46 -0000 @@ -124,12 +124,16 @@ * @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(); + if (fullSelection == null) // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=192837 + fullSelection= ""; //$NON-NLS-1$ + 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; } /** @@ -389,24 +393,19 @@ } /** - * 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(); - if (selection != null && selection.length() > 0) { + private String getFirstLine(String selection) { + if (selection.length() > 0) { int[] info= TextUtilities.indexOf(TextUtilities.DELIMITERS, selection, 0); if (info[0] > 0) return selection.substring(0, info[0]); else if (info[0] == -1) return selection; } - return null; + return selection; } } 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.66 diff -u -r1.66 FindReplaceDialog.java --- src/org/eclipse/ui/texteditor/FindReplaceDialog.java 4 Jun 2007 16:19:56 -0000 1.66 +++ src/org/eclipse/ui/texteditor/FindReplaceDialog.java 15 Jun 2007 14:41:46 -0000 @@ -918,19 +918,20 @@ // ------- 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(); - if (selection != null && selection.length() > 0) { + private String getFirstLine(String selection) { + if (selection.length() > 0) { int[] info= TextUtilities.indexOf(TextUtilities.DELIMITERS, selection, 0); if (info[0] > 0) return selection.substring(0, info[0]); else if (info[0] == -1) return selection; } - return null; + return selection; } /** @@ -1008,11 +1009,17 @@ */ private void initFindStringFromSelection() { if (fTarget != null && okToUse(fFindField)) { - String selection= getSelectionString(); + String fullSelection= fTarget.getSelectionText(); + if (fullSelection == null) // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=192837 + fullSelection= ""; //$NON-NLS-1$ + 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); @@ -1032,6 +1039,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 */