Skip to main content
summaryrefslogtreecommitdiffstats
blob: f3e472e24a50d48080b2d8faeabb945709affebd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jface.text;

import java.text.CharacterIterator;
import java.util.Locale;

import com.ibm.icu.text.BreakIterator;


/**
 * Standard implementation of
 * {@link org.eclipse.jface.text.ITextDoubleClickStrategy}.
 * <p>
 * Selects words using <code>java.text.BreakIterator</code> for the default
 * locale.</p>
 *
 * @see java.text.BreakIterator
 */
public class DefaultTextDoubleClickStrategy implements ITextDoubleClickStrategy {


	/**
	 * Implements a character iterator that works directly on
	 * instances of <code>IDocument</code>. Used to collaborate with
	 * the break iterator.
	 *
	 * @see IDocument
	 * @since 2.0
	 */
	static class DocumentCharacterIterator implements CharacterIterator {

		/** Document to iterate over. */
		private IDocument fDocument;
		/** Start offset of iteration. */
		private int fOffset= -1;
		/** End offset of iteration. */
		private int fEndOffset= -1;
		/** Current offset of iteration. */
		private int fIndex= -1;

		/** Creates a new document iterator. */
		public DocumentCharacterIterator() {
		}

		/**
		 * Configures this document iterator with the document section to be visited.
		 *
		 * @param document the document to be iterated
		 * @param iteratorRange the range in the document to be iterated
		 */
		public void setDocument(IDocument document, IRegion iteratorRange) {
			fDocument= document;
			fOffset= iteratorRange.getOffset();
			fEndOffset= fOffset + iteratorRange.getLength();
		}

		@Override
		public char first() {
			fIndex= fOffset;
			return current();
		}

		@Override
		public char last() {
			fIndex= fOffset < fEndOffset ? fEndOffset -1 : fEndOffset;
			return current();
		}

		@Override
		public char current() {
			if (fOffset <= fIndex && fIndex < fEndOffset) {
				try {
					return fDocument.getChar(fIndex);
				} catch (BadLocationException x) {
				}
			}
			return DONE;
		}

		@Override
		public char next() {
			++fIndex;
			int end= getEndIndex();
			if (fIndex >= end) {
				fIndex= end;
				return DONE;
			}
			return current();
		}

		@Override
		public char previous() {
			if (fIndex == fOffset)
				return DONE;

			if (fIndex > fOffset)
				-- fIndex;

			return current();
		}

		@Override
		public char setIndex(int index) {
			fIndex= index;
			return current();
		}

		@Override
		public int getBeginIndex() {
			return fOffset;
		}

		@Override
		public int getEndIndex() {
			return fEndOffset;
		}

		@Override
		public int getIndex() {
			return fIndex;
		}

		@Override
		public Object clone() {
			DocumentCharacterIterator i= new DocumentCharacterIterator();
			i.fDocument= fDocument;
			i.fIndex= fIndex;
			i.fOffset= fOffset;
			i.fEndOffset= fEndOffset;
			return i;
		}
	}


	/**
	 * The document character iterator used by this strategy.
	 * @since 2.0
	 */
	private DocumentCharacterIterator fDocIter= new DocumentCharacterIterator();

	/**
	 * The locale specific word break iterator.
	 * @since 3.7
	 */
	private BreakIterator fWordBreakIterator;

	/**
	 * The POSIX word break iterator.
	 * <p>
	 * Used to workaround ICU bug not treating '.' as word boundary, see
	 * http://bugs.icu-project.org/trac/ticket/8371 for details.
	 * </p>
	 *
	 * @since 3.7
	 */
	private BreakIterator fPOSIXWordBreakIterator;


	/**
	 * Creates a new default text double click strategy.
	 */
	public DefaultTextDoubleClickStrategy() {
	}

	@Override
	public void doubleClicked(ITextViewer text) {

		int offset= text.getSelectedRange().x;

		if (offset < 0)
			return;

		final IDocument document= text.getDocument();
		IRegion region= findExtendedDoubleClickSelection(document, offset);
		if (region == null)
			region= findWord(document, offset);
		if (region != null)
			text.setSelectedRange(region.getOffset(), region.getLength());

	}

	/**
	 * Tries to find a suitable double click selection for the given offset.
	 * <p>
	 * <strong>Note:</strong> This method must return <code>null</code> if it simply selects the word at
	 * the given offset.
	 * </p>
	 *
	 * @param document the document
	 * @param offset the offset
	 * @return the selection or <code>null</code> if none to indicate simple word selection
	 * @since 3.5
	 */
	protected IRegion findExtendedDoubleClickSelection(IDocument document, int offset) {
		return null;
	}

	/**
	 * Tries to find the word at the given offset.
	 *
	 * @param document the document
	 * @param offset the offset
	 * @return the word or <code>null</code> if none
	 * @since 3.5
	 */
	protected IRegion findWord(IDocument document, int offset) {
		return findWord(document, offset, getWordBreakIterator());
	}

	/**
	 * Returns the locale specific word break iterator.
	 *
	 * @return the locale specific word break iterator
	 * @since 3.7
	 */
	private BreakIterator getWordBreakIterator() {
		if (fWordBreakIterator == null)
			fWordBreakIterator= BreakIterator.getWordInstance();
		return fWordBreakIterator;
	}

	/**
	 * Returns the POSIX word break iterator.
	 *
	 * <p>
	 * Used to workaround ICU bug not treating '.' as word boundary, see
	 * http://bugs.icu-project.org/trac/ticket/8371 for details.
	 * </p>
	 *
	 * @return the POSIX word break iterator.
	 * @since 3.7
	 */
	private BreakIterator getPOSIXWordBreakIterator() {
		if (fPOSIXWordBreakIterator == null)
			fPOSIXWordBreakIterator= BreakIterator.getWordInstance(new Locale("en", "US", "POSIX")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		return fPOSIXWordBreakIterator;
	}

	/**
	 * Tries to find the word at the given offset.
	 *
	 * @param document the document
	 * @param offset the offset
	 * @param wordBreakIterator the word break iterator
	 * @return the word or <code>null</code> if none
	 * @since 3.7
	 */
	private IRegion findWord(IDocument document, int offset, BreakIterator wordBreakIterator) {
		IRegion line;
		try {
			line= document.getLineInformationOfOffset(offset);
		} catch (BadLocationException e) {
			return null;
		}

		if (offset == line.getOffset() + line.getLength())
			return null;

		fDocIter.setDocument(document, line);

		wordBreakIterator.setText(fDocIter);

		int start= wordBreakIterator.preceding(offset);
		if (start == BreakIterator.DONE)
			start= line.getOffset();

		int end= wordBreakIterator.following(offset);
		if (end == BreakIterator.DONE)
			end= line.getOffset() + line.getLength();

		if (wordBreakIterator.isBoundary(offset)) {
			if (end - offset > offset - start)
				start= offset;
			else
				end= offset;
		}

		if (end == start)
			return null;

		int length= end - start;
		try {
			// Workaround for ICU bug not treating '.' as word boundary, see http://bugs.icu-project.org/trac/ticket/8371 for details.
			if (fPOSIXWordBreakIterator != wordBreakIterator && document.get(start, length).indexOf('.') != -1) {
				IRegion wordRegion= findWord(document, offset, getPOSIXWordBreakIterator());
				if (wordRegion != null) {
					int wordStart= wordRegion.getOffset();
					int wordEnd= wordStart + wordRegion.getLength();
					// Check that no additional breaks besides '.' are introduced
					if ((wordStart == start || wordStart > start && document.getChar(wordStart - 1) == '.') && (wordEnd == end || wordEnd < end && document.getChar(wordEnd) == '.'))
						return wordRegion;
				}
			}
		} catch (BadLocationException e) {
			// Use previously computed word region
		}

		return new Region(start, length);

	}
}

Back to the top