1
2
3
4
5
6
7
8
9
10 package org.syntax.jedit;
11
12 import javax.swing.text.BadLocationException;
13 import javax.swing.text.Document;
14
15 /***
16 * Class with several utility functions used by the text area component.
17 *
18 * @author Slava Pestov
19 * @version $Id$
20 */
21 public class TextUtilities
22 {
23 /***
24 * Returns the offset of the bracket matching the one at the specified offset
25 * of the document, or -1 if the bracket is unmatched (or if the character is
26 * not a bracket).
27 *
28 * @param doc
29 * The document
30 * @param offset
31 * The offset
32 * @exception BadLocationException
33 * If an out-of-bounds access was attempted on the document
34 * text
35 */
36 public static int findMatchingBracket( Document doc, int offset ) throws BadLocationException
37 {
38 if( doc.getLength() == 0 )
39 return -1;
40 char c = doc.getText( offset, 1 ).charAt( 0 );
41 char cprime;
42 boolean direction;
43
44 switch( c )
45 {
46 case '(' :
47 cprime = ')';
48 direction = false;
49 break;
50 case ')' :
51 cprime = '(';
52 direction = true;
53 break;
54 case '[' :
55 cprime = ']';
56 direction = false;
57 break;
58 case ']' :
59 cprime = '[';
60 direction = true;
61 break;
62 case '{' :
63 cprime = '}';
64 direction = false;
65 break;
66 case '}' :
67 cprime = '{';
68 direction = true;
69 break;
70 default :
71 return -1;
72 }
73
74 int count;
75
76
77
78
79
80 if( direction )
81 {
82
83
84 count = 1;
85
86
87 String text = doc.getText( 0, offset );
88
89
90 for( int i = offset - 1; i >= 0; i-- )
91 {
92
93
94
95
96 char x = text.charAt( i );
97 if( x == c )
98 count++ ;
99
100
101
102
103 else if( x == cprime )
104 {
105 if( --count == 0 )
106 return i;
107 }
108 }
109 }
110 else
111 {
112
113
114 count = 1;
115
116
117 offset++ ;
118
119
120 int len = doc.getLength() - offset;
121
122
123 String text = doc.getText( offset, len );
124
125
126 for( int i = 0; i < len; i++ )
127 {
128
129
130
131
132 char x = text.charAt( i );
133
134 if( x == c )
135 count++ ;
136
137
138
139
140 else if( x == cprime )
141 {
142 if( --count == 0 )
143 return i + offset;
144 }
145 }
146 }
147
148
149 return -1;
150 }
151
152 /***
153 * Locates the start of the word at the specified position.
154 *
155 * @param line
156 * The text
157 * @param pos
158 * The position
159 */
160 public static int findWordStart( String line, int pos, String noWordSep )
161 {
162 char ch = line.charAt( pos - 1 );
163
164 if( noWordSep == null )
165 noWordSep = "";
166 boolean selectNoLetter = ( !Character.isLetterOrDigit( ch ) && noWordSep.indexOf( ch ) == -1 );
167
168 int wordStart = 0;
169 for( int i = pos - 1; i >= 0; i-- )
170 {
171 ch = line.charAt( i );
172 if( selectNoLetter ^ ( !Character.isLetterOrDigit( ch ) && noWordSep.indexOf( ch ) == -1 ) )
173 {
174 wordStart = i + 1;
175 break;
176 }
177 }
178
179 return wordStart;
180 }
181
182 /***
183 * Locates the end of the word at the specified position.
184 *
185 * @param line
186 * The text
187 * @param pos
188 * The position
189 */
190 public static int findWordEnd( String line, int pos, String noWordSep )
191 {
192 char ch = line.charAt( pos );
193
194 if( noWordSep == null )
195 noWordSep = "";
196 boolean selectNoLetter = ( !Character.isLetterOrDigit( ch ) && noWordSep.indexOf( ch ) == -1 );
197
198 int wordEnd = line.length();
199 for( int i = pos; i < line.length(); i++ )
200 {
201 ch = line.charAt( i );
202 if( selectNoLetter ^ ( !Character.isLetterOrDigit( ch ) && noWordSep.indexOf( ch ) == -1 ) )
203 {
204 wordEnd = i;
205 break;
206 }
207 }
208 return wordEnd;
209 }
210 }