Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 23739f5c8eb2d9c0f13fc59b6bf508d6704efa89 (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 QNX Software Systems 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:
 *     QNX Software Systems - Initial API and implementation
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.text;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;


/**
 * This is a helper class for the text editor to be able to determine, given a
 * particular offset in a document, various candidates segments for things like
 * context help, proposals and hovering.
 */
public class CWordFinder {
	
	private static final char CBRACE_L = '{';
	private static final char CBRACE_R = '}';
	private static final char PAREN_R  = ')';
	
	/**
	 * This method determines for a given offset into a given document what the
	 * region is which defines the current word. A word is defined as a contiguous
	 * sequence of C-identifier characters. So assuming that | indicates the current
	 * cursor position:
	 * <pre>
	 *   |afunction(int a, int b) --> word = afunction
	 *   afunc|tion(int a, int b) --> word = afunction
	 *   afunction|(int a, int b) --> word = afunction
	 *   afunction(|int a, int b) --> word = int
	 *   afunction(int a,| int b) --> word = length 0
	 *   afunction(|)             --> word = length 0
	 * </pre>
	 * 
	 * @param document
	 *            The document to be examined
	 * @param offset
	 *            The offset into the document where a word should be
	 *            identified.
	 * @return The region defining the current word, which may be a region of
	 *         length 0 if the offset is not in a word, or null if there is an
	 *         error accessing the document data.
	 */
	public static IRegion findWord(IDocument document, int offset) {
		int start = -2;
		int end = -1;

		try {
			int pos = offset;
			char c;

			while (--pos >= 0) {
				c = document.getChar(pos);
				if (!Character.isJavaIdentifierPart(c)) {
					break;
				}
			}

			start = pos;

			pos = offset;
			int length = document.getLength();

			while (pos < length) {
				c = document.getChar(pos);
				if (!Character.isJavaIdentifierPart(c))
					break;
				++pos;
			}

			end = pos;
		} catch (BadLocationException x) {
		}

		if (start >= -1 && end > -1) {
			if (start == offset && end == offset)
				return new Region(offset, 0);
			else if (start == offset)
				return new Region(start, end - start);
			else
				return new Region(start + 1, end - start - 1);
		}

		return null;
	}

	/**
	 * This method will determine the region for the name of the function within
	 * which the current offset is contained.
	 * 
	 * @param document
	 *            The document to be examined
	 * @param offset
	 *            The offset into the document where a word should be
	 *            identified.
	 * @return The region defining the current word, which may be a region of
	 *         length 0 if the offset is not in a function, or null if there is
	 *         an error accessing the document data.
	 */
	public static IRegion findFunction(IDocument document, int offset) {
		int leftbracket = -1;
		int leftbracketcount = 0;
		int rightbracket = -1;
		int rightbracketcount = 0;
		int functionstart = -1;
		int functionend = -1;

		try {
			int length = document.getLength();
			int pos;
			char c;

			//Find most relevant right bracket from our position
			pos = offset;
			rightbracketcount = leftbracketcount = 0;
			while (pos < length) {
				c = document.getChar(pos);

				if (c == ')') {
					rightbracketcount++;
					if (rightbracketcount >= leftbracketcount) {
						rightbracket = pos;
						break;
					}
				}

				if (c == '(') {
					leftbracketcount++;
				}

				if (c == ';') {
					break;
				}

				pos++;
			}

			if (rightbracket == -1) {
				return new Region(offset, 0);
			}

			// Now backtrack our way from the right bracket to the left
			pos = rightbracket;
			rightbracketcount = leftbracketcount = 0;
			while (pos >= 0) {
				c = document.getChar(pos);

				if (c == ')') {
					rightbracketcount++;
				}

				if (c == '(') {
					leftbracketcount++;
					if (leftbracketcount >= rightbracketcount) {
						leftbracket = pos;
						break;
					}
				}

				if (c == ';') {
					break;
				}

				pos--;
			}

			if (leftbracket == -1) {
				return new Region(offset, 0);
			}

			// Now work our way to the function name
			pos = leftbracket - 1;
			while (pos >= 0) {
				c = document.getChar(pos);
				if (functionend == -1 && c == ' ') {
					pos--;
					continue;
				}

				if (!Character.isJavaIdentifierPart(c)) {
					break;
				}

				functionstart = pos;
				if (functionend == -1) {
					functionend = pos;
				}

				pos--;
			}
		} catch (BadLocationException x) {
			/* Ignore */
		}

		if (functionstart > -1 && functionend > -1) {
			return new Region(functionstart, functionend - functionstart + 1);
		}

		return null;
	}

	/**
	 * This method will determine whether current offset is contained 
	 * in any function's body or it's outside it. 
	 * 
	 * @param document
	 *            The document to be examined
	 * @param offset
	 *            The offset into the document
	 * @return 
	 *      <code>true</code> if there is no function body around offset 
	 *      <code>false</code> otherwise 
	 */
	public static boolean isGlobal(IDocument document, int offset) {
		try {
			int pos = offset;
			int bracketcount = 0;
			char c;

			// Find left curled brace from our position
			while (pos > 0) {
				c = document.getChar(pos--);

				if (c == CBRACE_R) {
					bracketcount++; // take into account nested blocks
				} else if (c == CBRACE_L) {
					if (bracketcount-- == 0) {
						do {
							c = document.getChar(pos--);
							if (c == PAREN_R)
								return false;
						} while (Character.isWhitespace(c));
						// Container block seems to be not a function or statement body
						pos++;             // step back one symbol
						bracketcount = 0;  // let's search for upper block
					}
				}
			}
		} catch (BadLocationException x) {
			// Ignore
		}
		return true;  // return true in case of unknown result or exception 
	}
	
	/**
	 * Searches for line feed symbols in string.
	 * First met '\r' or '\n' is treated as LF symbol
	 * 
	 * @param s
	 * 			string to search in.
	 * @return  number of LFs met.
	 */
	public static int countLFs (String s) {
		int counter = 0;
		char lf = 0;
		char c;
		for (int i= 0; i < s.length(); i++) {
			c = s.charAt(i);
			if (lf == 0) {
				if (c == '\n' || c == '\r') {
					lf = c;
					counter++;
				}
			} else if (lf == c) {
				counter++;
			}
		}
		return counter;
	}

	/**
	 * Checks whether the string contains any C-block delimiters ( { } ) 
	 * 
	 * @param s 
	 * 			text to check
	 * @return  true if curled brace found.
	 */
	public static boolean hasCBraces (String s) {
		if (s.indexOf(CBRACE_L) > -1 || s.indexOf(CBRACE_R) > -1) 
			return true;
		return false;
	}
}

Back to the top