1 | /*
|
---|
2 | * 08/26/2004
|
---|
3 | *
|
---|
4 | * TokenMakerBase.java - A base class for token makers.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea;
|
---|
10 |
|
---|
11 | import javax.swing.Action;
|
---|
12 | import javax.swing.text.Segment;
|
---|
13 |
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Base class for token makers.
|
---|
17 | *
|
---|
18 | * @author Robert Futrell
|
---|
19 | * @version 1.0
|
---|
20 | */
|
---|
21 | abstract class TokenMakerBase implements TokenMaker {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * The first token in the returned linked list.
|
---|
25 | */
|
---|
26 | protected Token firstToken;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Used in the creation of the linked list.
|
---|
30 | */
|
---|
31 | protected Token currentToken;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Used in the creation of the linked list.
|
---|
35 | */
|
---|
36 | protected Token previousToken;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The factory that gives us our tokens to use.
|
---|
40 | */
|
---|
41 | private TokenFactory tokenFactory;
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Constructor.
|
---|
46 | */
|
---|
47 | public TokenMakerBase() {
|
---|
48 | firstToken = currentToken = previousToken = null;
|
---|
49 | tokenFactory = new DefaultTokenFactory();
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * {@inheritDoc}
|
---|
55 | */
|
---|
56 | public void addNullToken() {
|
---|
57 | if (firstToken==null) {
|
---|
58 | firstToken = tokenFactory.createToken();
|
---|
59 | currentToken = firstToken;
|
---|
60 | }
|
---|
61 | else {
|
---|
62 | currentToken.setNextToken(tokenFactory.createToken());
|
---|
63 | previousToken = currentToken;
|
---|
64 | currentToken = currentToken.getNextToken();
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Adds the token specified to the current linked list of tokens.
|
---|
71 | *
|
---|
72 | * @param segment <code>Segment</code> to get text from.
|
---|
73 | * @param start Start offset in <code>segment</code> of token.
|
---|
74 | * @param end End offset in <code>segment</code> of token.
|
---|
75 | * @param tokenType The token's type.
|
---|
76 | * @param startOffset The offset in the document at which this token
|
---|
77 | * occurs.
|
---|
78 | */
|
---|
79 | public void addToken(Segment segment, int start, int end, int tokenType,
|
---|
80 | int startOffset) {
|
---|
81 | addToken(segment.array, start,end, tokenType, startOffset);
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * {@inheritDoc}
|
---|
87 | */
|
---|
88 | public void addToken(char[] array, int start, int end, int tokenType,
|
---|
89 | int startOffset) {
|
---|
90 | addToken(array, start, end, tokenType, startOffset, false);
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Adds the token specified to the current linked list of tokens.
|
---|
96 | *
|
---|
97 | * @param array The character array.
|
---|
98 | * @param start The starting offset in the array.
|
---|
99 | * @param end The ending offset in the array.
|
---|
100 | * @param tokenType The token's type.
|
---|
101 | * @param startOffset The offset in the document at which this token
|
---|
102 | * occurs.
|
---|
103 | * @param hyperlink Whether this token is a hyperlink.
|
---|
104 | */
|
---|
105 | public void addToken(char[] array, int start, int end, int tokenType,
|
---|
106 | int startOffset, boolean hyperlink) {
|
---|
107 |
|
---|
108 | if (firstToken==null) {
|
---|
109 | firstToken = tokenFactory.createToken(array, start, end,
|
---|
110 | startOffset, tokenType);
|
---|
111 | currentToken = firstToken; // previous token is still null.
|
---|
112 | }
|
---|
113 | else {
|
---|
114 | currentToken.setNextToken(tokenFactory.createToken(array,
|
---|
115 | start,end, startOffset, tokenType));
|
---|
116 | previousToken = currentToken;
|
---|
117 | currentToken = currentToken.getNextToken();
|
---|
118 | }
|
---|
119 |
|
---|
120 | currentToken.setHyperlink(hyperlink);
|
---|
121 |
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Returns whether this programming language uses curly braces
|
---|
127 | * ('<tt>{</tt>' and '<tt>}</tt>') to denote code blocks.<p>
|
---|
128 | *
|
---|
129 | * The default implementation returns <code>false</code>; subclasses can
|
---|
130 | * override this method if necessary.
|
---|
131 | *
|
---|
132 | * @return Whether curly braces denote code blocks.
|
---|
133 | */
|
---|
134 | public boolean getCurlyBracesDenoteCodeBlocks() {
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Returns an action to handle "insert break" key presses (i.e. Enter).
|
---|
141 | * The default implementation returns <code>null</code>. Subclasses
|
---|
142 | * can override.
|
---|
143 | *
|
---|
144 | * @return The default implementation always returns <code>null</code>.
|
---|
145 | */
|
---|
146 | public Action getInsertBreakAction() {
|
---|
147 | return null;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * {@inheritDoc}
|
---|
153 | */
|
---|
154 | public int getLastTokenTypeOnLine(Segment text, int initialTokenType) {
|
---|
155 |
|
---|
156 | // Last parameter doesn't matter if we're not painting.
|
---|
157 | Token t = getTokenList(text, initialTokenType, 0);
|
---|
158 |
|
---|
159 | while (t.getNextToken()!=null)
|
---|
160 | t = t.getNextToken();
|
---|
161 |
|
---|
162 | return t.type;
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * {@inheritDoc}
|
---|
169 | */
|
---|
170 | public String[] getLineCommentStartAndEnd() {
|
---|
171 | return null;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Returns whether tokens of the specified type should have "mark
|
---|
177 | * occurrences" enabled for the current programming language. The default
|
---|
178 | * implementation returns true if <tt>type</tt> is {@link Token#IDENTIFIER}.
|
---|
179 | * Subclasses can override this method to support other token types, such
|
---|
180 | * as {@link Token#VARIABLE}.
|
---|
181 | *
|
---|
182 | * @param type The token type.
|
---|
183 | * @return Whether tokens of this type should have "mark occurrences"
|
---|
184 | * enabled.
|
---|
185 | */
|
---|
186 | public boolean getMarkOccurrencesOfTokenType(int type) {
|
---|
187 | return type==Token.IDENTIFIER;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * The default implementation returns <code>false</code> always. Languages
|
---|
193 | * that wish to better support auto-indentation can override this method.
|
---|
194 | *
|
---|
195 | * @param token The token the previous line ends with.
|
---|
196 | * @return Whether the next line should be indented.
|
---|
197 | */
|
---|
198 | public boolean getShouldIndentNextLineAfter(Token token) {
|
---|
199 | return false;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * The default implementation returns <code>false</code> always.
|
---|
205 | * Subclasses that are highlighting a markup language should override this
|
---|
206 | * method to return <code>true</code>.
|
---|
207 | *
|
---|
208 | * @return <code>false</code> always.
|
---|
209 | */
|
---|
210 | public boolean isMarkupLanguage() {
|
---|
211 | return false;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * {@inheritDoc}
|
---|
217 | */
|
---|
218 | public boolean isWhitespaceVisible() {
|
---|
219 | return tokenFactory instanceof VisibleWhitespaceTokenFactory;
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Deletes the linked list of tokens so we can begin anew. This should
|
---|
225 | * never have to be called by the programmer, as it is automatically
|
---|
226 | * called whenever the user calls
|
---|
227 | * {@link #getLastTokenTypeOnLine(Segment, int)} or
|
---|
228 | * {@link #getTokenList(Segment, int, int)}.
|
---|
229 | */
|
---|
230 | protected void resetTokenList() {
|
---|
231 | firstToken = currentToken = previousToken = null;
|
---|
232 | tokenFactory.resetAllTokens();
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * {@inheritDoc}
|
---|
238 | */
|
---|
239 | public void setWhitespaceVisible(boolean visible) {
|
---|
240 | // FIXME: Initialize with the proper sizes.
|
---|
241 | tokenFactory = visible ? new VisibleWhitespaceTokenFactory() :
|
---|
242 | new DefaultTokenFactory();
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | } |
---|