View | Details | Raw Unified | Return to bug 44422
Collapse All | Expand All

(-)src/org/eclipse/ui/texteditor/FindNextAction.java (-18 / +17 lines)
Lines 124-135 Link Here
124
	 * @return the find string
124
	 * @return the find string
125
	 */
125
	 */
126
	private String getFindString() {
126
	private String getFindString() {
127
		String string= getSelectionString();
127
		String fullSelection= fTarget.getSelectionText();
128
128
		if (fullSelection == null) // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=192837
129
		if ((string == null || fRegExSearch && string.equals(fSelection)) && !fFindHistory.isEmpty())
129
			fullSelection= ""; //$NON-NLS-1$
130
			string= (String) fFindHistory.get(0);
130
		String firstLine= getFirstLine(fullSelection);
131
131
		if ((firstLine.length() == 0 || fRegExSearch && fullSelection.equals(fSelection)) && !fFindHistory.isEmpty())
132
		return string;
132
			return (String) fFindHistory.get(0);
133
		else if (fRegExSearch && fullSelection.length() > 0)
134
			return FindReplaceDialog.escapeForRegExPattern(fullSelection);
135
		else
136
			return firstLine;
133
	}
137
	}
134
138
135
	/**
139
	/**
Lines 389-412 Link Here
389
	}
393
	}
390
394
391
	/**
395
	/**
392
	 * Returns the actual selection of the find replace target.
396
	 * Returns the first line of the given selection.
393
	 *
397
	 * 
394
	 * @return the actual selection of the find replace target
398
	 * @param selection the selection
399
	 * @return the first line of the selection
395
	 */
400
	 */
396
	private String getSelectionString() {
401
	private String getFirstLine(String selection) {
397
402
		if (selection.length() > 0) {
398
		/*
399
		 * 1GF86V3: ITPUI:WINNT - Internal errors using Find/Replace Dialog
400
		 * Now uses TextUtilities rather than focusing on '\n'
401
		 */
402
		String selection= fTarget.getSelectionText();
403
		if (selection != null && selection.length() > 0) {
404
			int[] info= TextUtilities.indexOf(TextUtilities.DELIMITERS, selection, 0);
403
			int[] info= TextUtilities.indexOf(TextUtilities.DELIMITERS, selection, 0);
405
			if (info[0] > 0)
404
			if (info[0] > 0)
406
				return selection.substring(0, info[0]);
405
				return selection.substring(0, info[0]);
407
			else if (info[0] == -1)
406
			else if (info[0] == -1)
408
				return selection;
407
				return selection;
409
		}
408
		}
410
		return null;
409
		return selection;
411
	}
410
	}
412
}
411
}
(-)src/org/eclipse/ui/texteditor/FindReplaceDialog.java (-10 / +82 lines)
Lines 918-936 Link Here
918
	// ------- init / close ---------------------------------------
918
	// ------- init / close ---------------------------------------
919
919
920
	/**
920
	/**
921
	 * Returns the actual selection of the find replace target.
921
	 * Returns the first line of the given selection.
922
	 * @return the selection of the target
922
	 * 
923
	 * @param selection the selection
924
	 * @return the first line of the selection
923
	 */
925
	 */
924
	private String getSelectionString() {
926
	private String getFirstLine(String selection) {
925
		String selection= fTarget.getSelectionText();
927
		if (selection.length() > 0) {
926
		if (selection != null && selection.length() > 0) {
927
			int[] info= TextUtilities.indexOf(TextUtilities.DELIMITERS, selection, 0);
928
			int[] info= TextUtilities.indexOf(TextUtilities.DELIMITERS, selection, 0);
928
			if (info[0] > 0)
929
			if (info[0] > 0)
929
				return selection.substring(0, info[0]);
930
				return selection.substring(0, info[0]);
930
			else if (info[0] == -1)
931
			else if (info[0] == -1)
931
				return selection;
932
				return selection;
932
		}
933
		}
933
		return null;
934
		return selection;
934
	}
935
	}
935
936
936
	/**
937
	/**
Lines 1008-1018 Link Here
1008
	 */
1009
	 */
1009
	private void initFindStringFromSelection() {
1010
	private void initFindStringFromSelection() {
1010
		if (fTarget != null && okToUse(fFindField)) {
1011
		if (fTarget != null && okToUse(fFindField)) {
1011
			String selection= getSelectionString();
1012
			String fullSelection= fTarget.getSelectionText();
1013
			if (fullSelection == null) // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=192837
1014
				fullSelection= ""; //$NON-NLS-1$
1015
			String firstLine= getFirstLine(fullSelection);
1016
			boolean isRegEx= isRegExSearchAvailableAndChecked();
1012
			fFindField.removeModifyListener(fFindModifyListener);
1017
			fFindField.removeModifyListener(fFindModifyListener);
1013
			if (selection != null) {
1018
			if (firstLine.length() > 0 || (isRegEx && fullSelection.length() > 0)) {
1014
				fFindField.setText(selection);
1019
				String pattern= isRegEx ? escapeForRegExPattern(fullSelection) : firstLine;
1015
				if (!selection.equals(fTarget.getSelectionText())) {
1020
				fFindField.setText(pattern);
1021
				if (!firstLine.equals(fullSelection)) {
1022
					// multiple lines selected
1016
					useSelectedLines(true);
1023
					useSelectedLines(true);
1017
					fGlobalRadioButton.setSelection(false);
1024
					fGlobalRadioButton.setSelection(false);
1018
					fSelectedRangeRadioButton.setSelection(true);
1025
					fSelectedRangeRadioButton.setSelection(true);
Lines 1032-1037 Link Here
1032
	}
1039
	}
1033
1040
1034
	/**
1041
	/**
1042
	 * Escapes special characters in the string, such that the resulting pattern
1043
	 * matches the given string.
1044
	 * 
1045
	 * @param string the string to escape
1046
	 * @return a regex pattern that matches the given string
1047
	 */
1048
	public static String escapeForRegExPattern(String string) {
1049
		//implements https://bugs.eclipse.org/bugs/show_bug.cgi?id=44422
1050
1051
		StringBuffer pattern= new StringBuffer(string.length() + 16);
1052
		int length= string.length();
1053
		if (length > 0 && string.charAt(0) == '^')
1054
			pattern.append('\\');
1055
		for (int i= 0; i < length; i++) {
1056
			char ch= string.charAt(i);
1057
			switch (ch) {
1058
				case '\\':
1059
				case '(':
1060
				case ')':
1061
				case '[':
1062
				case ']':
1063
				case '{':
1064
				case '}':
1065
				case '.':
1066
				case '?':
1067
				case '*':
1068
				case '+':
1069
				case '|':
1070
					pattern.append('\\').append(ch);
1071
					break;
1072
					
1073
				case '\n':
1074
					pattern.append("\\n"); //$NON-NLS-1$
1075
					break;
1076
				case '\r':
1077
					pattern.append("\\r"); //$NON-NLS-1$
1078
					break;
1079
				case '\t':
1080
					pattern.append("\\t"); //$NON-NLS-1$
1081
					break;
1082
				case '\f':
1083
					pattern.append("\\f"); //$NON-NLS-1$
1084
					break;
1085
				case 0x07:
1086
					pattern.append("\\a"); //$NON-NLS-1$
1087
					break;
1088
				case 0x1B:
1089
					pattern.append("\\e"); //$NON-NLS-1$
1090
					break;
1091
1092
				default:
1093
					if (0 <= ch && ch < 0x20) {
1094
						pattern.append("\\x"); //$NON-NLS-1$
1095
						pattern.append(Integer.toHexString(ch).toUpperCase());
1096
					} else {
1097
						pattern.append(ch);
1098
					}
1099
			}
1100
		}
1101
		if (length > 0 && string.charAt(length - 1) == '$')
1102
			pattern.insert(pattern.length() - 1, '\\');
1103
		return pattern.toString();
1104
	}
1105
1106
	/**
1035
	 * Initializes the anchor used as starting point for incremental searching.
1107
	 * Initializes the anchor used as starting point for incremental searching.
1036
	 * @since 2.0
1108
	 * @since 2.0
1037
	 */
1109
	 */

Return to bug 44422