Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.modules.javadoc.highlighting; |
44 |
|
45 |
import java.util.ArrayList; |
46 |
import java.util.ConcurrentModificationException; |
47 |
import java.util.List; |
48 |
import java.util.NoSuchElementException; |
49 |
import java.util.logging.Level; |
50 |
import java.util.logging.Logger; |
51 |
import javax.swing.text.AttributeSet; |
52 |
import javax.swing.text.BadLocationException; |
53 |
import javax.swing.text.Document; |
54 |
import javax.swing.text.Element; |
55 |
import org.netbeans.api.editor.mimelookup.MimeLookup; |
56 |
import org.netbeans.api.editor.mimelookup.MimePath; |
57 |
import org.netbeans.api.editor.settings.FontColorSettings; |
58 |
import org.netbeans.api.java.lexer.JavadocTokenId; |
59 |
import org.netbeans.api.lexer.TokenChange; |
60 |
import org.netbeans.api.lexer.TokenHierarchy; |
61 |
import org.netbeans.api.lexer.TokenHierarchyEvent; |
62 |
import org.netbeans.api.lexer.TokenHierarchyListener; |
63 |
import org.netbeans.api.lexer.TokenId; |
64 |
import org.netbeans.api.lexer.TokenSequence; |
65 |
import org.netbeans.spi.editor.highlighting.HighlightsSequence; |
66 |
import org.netbeans.spi.editor.highlighting.support.AbstractHighlightsContainer; |
67 |
import org.openide.util.WeakListeners; |
68 |
|
69 |
/** |
70 |
* @author Jan Becicka |
71 |
*/ |
72 |
public class Highlighting extends AbstractHighlightsContainer implements TokenHierarchyListener { |
73 |
|
74 |
private static final Logger LOG = Logger.getLogger(Highlighting.class.getName()); |
75 |
|
76 |
public static final String LAYER_ID = "org.netbeans.modules.javadoc.highlighting"; //NOI18N |
77 |
|
78 |
private static AttributeSet fontColor = MimeLookup.getLookup(MimePath.get("text/x-java")).lookup(FontColorSettings.class).getTokenFontColors("javadoc-first-sentence"); |
79 |
|
80 |
|
81 |
private Document document; |
82 |
private TokenHierarchy<? extends Document> hierarchy = null; |
83 |
private long version = 0; |
84 |
|
85 |
/** Creates a new instance of Highlighting */ |
86 |
public Highlighting(Document doc) { |
87 |
this.document = doc; |
88 |
} |
89 |
|
90 |
public HighlightsSequence getHighlights(int startOffset, int endOffset) { |
91 |
synchronized (this) { |
92 |
if (hierarchy == null) { |
93 |
hierarchy = TokenHierarchy.get(document); |
94 |
if (hierarchy != null) { |
95 |
hierarchy.addTokenHierarchyListener(WeakListeners.create(TokenHierarchyListener.class, this, hierarchy)); |
96 |
} |
97 |
} |
98 |
|
99 |
if (hierarchy != null) { |
100 |
return new HSImpl(version, hierarchy, startOffset, endOffset); |
101 |
} else { |
102 |
return HighlightsSequence.EMPTY; |
103 |
} |
104 |
} |
105 |
} |
106 |
|
107 |
// ---------------------------------------------------------------------- |
108 |
// TokenHierarchyListener implementation |
109 |
// ---------------------------------------------------------------------- |
110 |
|
111 |
public void tokenHierarchyChanged(TokenHierarchyEvent evt) { |
112 |
TokenChange<?> tc = evt.tokenChange(); |
113 |
int affectedArea [] = null; |
114 |
|
115 |
TokenSequence<? extends TokenId> seq = tc.currentTokenSequence(); |
116 |
if (seq.language().equals(JavadocTokenId.language())) { |
117 |
// Change inside javadoc |
118 |
int [] firstSentence = findFirstSentence(seq); |
119 |
if (firstSentence != null) { |
120 |
if (tc.offset() <= firstSentence[1]) { |
121 |
// Change before the end of the first sentence |
122 |
affectedArea = firstSentence; |
123 |
} |
124 |
} else { |
125 |
// XXX: need the embedding token (i.e. JavaTokenId.JAVADOC_COMMENT*) |
126 |
// and fire a change in its whole area |
127 |
affectedArea = new int [] { tc.offset(), Integer.MAX_VALUE }; |
128 |
} |
129 |
} else { |
130 |
// The change may or may not involve javadoc, so reset everyting. |
131 |
// It would be more efficient to traverse the changed area and |
132 |
// find out whether it really involves javadoc or not. |
133 |
affectedArea = new int [] { tc.offset(), Integer.MAX_VALUE }; |
134 |
} |
135 |
|
136 |
if (affectedArea != null) { |
137 |
synchronized (this) { |
138 |
version++; |
139 |
} |
140 |
|
141 |
fireHighlightsChange(affectedArea[0], affectedArea[1]); |
142 |
} |
143 |
} |
144 |
|
145 |
// ---------------------------------------------------------------------- |
146 |
// Private implementation |
147 |
// ---------------------------------------------------------------------- |
148 |
|
149 |
private int [] findFirstSentence(TokenSequence<? extends TokenId> seq) { |
150 |
seq.moveStart(); |
151 |
if (seq.moveNext()) { |
152 |
int start = seq.offset(); |
153 |
do { |
154 |
if (seq.token().id() == JavadocTokenId.DOT) { |
155 |
return new int [] { start, seq.offset() }; |
156 |
} |
157 |
} while (seq.moveNext()); |
158 |
} |
159 |
|
160 |
return null; |
161 |
} |
162 |
|
163 |
private final class HSImpl implements HighlightsSequence { |
164 |
|
165 |
private long version; |
166 |
private TokenHierarchy<? extends Document> scanner; |
167 |
private List<TokenSequence<? extends TokenId>> sequences; |
168 |
private int startOffset; |
169 |
private int endOffset; |
170 |
|
171 |
private List<Integer> lines = null; |
172 |
private int linesIdx = -1; |
173 |
|
174 |
public HSImpl(long version, TokenHierarchy<? extends Document> scanner, int startOffset, int endOffset) { |
175 |
this.version = version; |
176 |
this.scanner = scanner; |
177 |
this.startOffset = startOffset; |
178 |
this.endOffset = endOffset; |
179 |
this.sequences = null; |
180 |
} |
181 |
|
182 |
public boolean moveNext() { |
183 |
synchronized (Highlighting.this) { |
184 |
checkVersion(); |
185 |
|
186 |
if (sequences == null) { |
187 |
// initialize |
188 |
TokenSequence<?> seq = scanner.tokenSequence().subSequence(startOffset, endOffset); |
189 |
sequences = new ArrayList<TokenSequence<? extends TokenId>>(); |
190 |
sequences.add(seq); |
191 |
} |
192 |
|
193 |
if (lines != null) { |
194 |
if (linesIdx + 2 < lines.size()) { |
195 |
linesIdx += 2; |
196 |
return true; |
197 |
} |
198 |
|
199 |
lines = null; |
200 |
linesIdx = -1; |
201 |
} |
202 |
|
203 |
while (!sequences.isEmpty()) { |
204 |
TokenSequence<? extends TokenId> seq = sequences.get(sequences.size() - 1); |
205 |
|
206 |
if (seq.language().equals(JavadocTokenId.language())) { |
207 |
int [] firstSentence = findFirstSentence(seq); |
208 |
sequences.remove(sequences.size() - 1); |
209 |
|
210 |
if (firstSentence != null) { |
211 |
lines = splitByLines(firstSentence[0], firstSentence[1]); |
212 |
if (lines != null) { |
213 |
linesIdx = 0; |
214 |
return true; |
215 |
} |
216 |
} |
217 |
} else { |
218 |
boolean hasNextToken; |
219 |
|
220 |
while (true == (hasNextToken = seq.moveNext())) { |
221 |
TokenSequence<?> embeddedSeq = seq.embedded(); |
222 |
if (embeddedSeq != null) { |
223 |
sequences.add(sequences.size(), embeddedSeq); |
224 |
break; |
225 |
} |
226 |
} |
227 |
|
228 |
if (!hasNextToken) { |
229 |
sequences.remove(sequences.size() - 1); |
230 |
} |
231 |
} |
232 |
} |
233 |
|
234 |
return false; |
235 |
} |
236 |
} |
237 |
|
238 |
public int getStartOffset() { |
239 |
synchronized (Highlighting.this) { |
240 |
checkVersion(); |
241 |
|
242 |
if (sequences == null) { |
243 |
throw new NoSuchElementException("Call moveNext() first."); //NOI18N |
244 |
} |
245 |
|
246 |
if (lines != null) { |
247 |
return lines.get(linesIdx); |
248 |
} else { |
249 |
throw new NoSuchElementException(); |
250 |
} |
251 |
} |
252 |
} |
253 |
|
254 |
public int getEndOffset() { |
255 |
synchronized (Highlighting.this) { |
256 |
checkVersion(); |
257 |
|
258 |
if (sequences == null) { |
259 |
throw new NoSuchElementException("Call moveNext() first."); //NOI18N |
260 |
} |
261 |
|
262 |
if (lines != null) { |
263 |
return lines.get(linesIdx + 1); |
264 |
} else { |
265 |
throw new NoSuchElementException(); |
266 |
} |
267 |
} |
268 |
} |
269 |
|
270 |
public AttributeSet getAttributes() { |
271 |
synchronized (Highlighting.this) { |
272 |
checkVersion(); |
273 |
|
274 |
if (sequences == null) { |
275 |
throw new NoSuchElementException("Call moveNext() first."); //NOI18N |
276 |
} |
277 |
|
278 |
if (lines != null) { |
279 |
return fontColor; |
280 |
} else { |
281 |
throw new NoSuchElementException(); |
282 |
} |
283 |
} |
284 |
} |
285 |
|
286 |
private void checkVersion() { |
287 |
if (this.version != Highlighting.this.version) { |
288 |
throw new ConcurrentModificationException(); |
289 |
} |
290 |
} |
291 |
|
292 |
private List<Integer> splitByLines(int sentenceStart, int sentenceEnd) { |
293 |
ArrayList<Integer> lines = new ArrayList<Integer>(); |
294 |
int offset = sentenceStart; |
295 |
|
296 |
try { |
297 |
while (offset < sentenceEnd) { |
298 |
Element lineElement = document.getDefaultRootElement().getElement( |
299 |
document.getDefaultRootElement().getElementIndex(offset)); |
300 |
|
301 |
int rowStart = offset == sentenceStart ? offset : lineElement.getStartOffset(); |
302 |
int rowEnd = lineElement.getEndOffset(); |
303 |
|
304 |
String line = document.getText(rowStart, rowEnd - rowStart); |
305 |
int idx = 0; |
306 |
while (idx < line.length() && |
307 |
(line.charAt(idx) == ' ' || |
308 |
line.charAt(idx) == '\t' || |
309 |
line.charAt(idx) == '*')) |
310 |
{ |
311 |
idx++; |
312 |
} |
313 |
|
314 |
if (rowStart + idx < rowEnd) { |
315 |
lines.add(rowStart + idx); |
316 |
lines.add(Math.min(rowEnd, sentenceEnd)); |
317 |
} |
318 |
|
319 |
offset = rowEnd + 1; |
320 |
} |
321 |
} catch (BadLocationException e) { |
322 |
LOG.log(Level.WARNING, "Can't determine javadoc first sentence", e); |
323 |
} |
324 |
|
325 |
return lines.isEmpty() ? null : lines; |
326 |
} |
327 |
} // End of HSImpl class |
328 |
} |