Skip to main content
summaryrefslogtreecommitdiffstats
blob: 010cd6f6c6b515073788d9196b42e155a9b0a667 (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
package org.eclipse.cdt.internal.ui.text;

/*
 * (c) Copyright QNX Software Systems Ltd. 2002.
 * All Rights Reserved.
 */

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 
{
	/**
	 * 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 the set of non "C" identifiers.  So assuming that !
	 * indicated the current cursor postion:
	 *  !afunction(int a, int b) --> word = length 0
	 *  afunc!tion(int a, int b) --> word = afunction
	 *  afunction!(int a, int b) --> word = afunction
	 *  afunction(!int a, int b) --> word = length 0
	 *  afunction(int a,! int b) --> word = length 0
	 *  afunction(!)			 --> word = length 0
	 * @param document The document to be examined
	 * @param offset The offset into the document where a word should 
	 * be idendified.
	 * @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 docment data.
	 */
	public static IRegion findWord( IDocument document, int offset ) 
	{
		int start = -1;
		int end = -1;
				
		try 
		{	
			int pos = offset;
			char c;
			
			while( pos >= 0 ) 
			{
				c = document.getChar( pos );
				if ( !Character.isJavaIdentifierPart( c ) )
					break;
				--pos;
			}
			
			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 idendified.
	 * @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 docment 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 rightbracket 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;
	}
	
}


Back to the top