1 19 20 package org.netbeans.modules.tasklist.docscan; 21 22 import java.beans.*; 23 import java.util.List ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.Collection ; 27 import java.util.regex.*; 28 import java.io.IOException ; 29 30 import org.openide.ErrorManager; 31 import org.openide.text.Line; 32 import org.openide.util.WeakSet; 33 import org.openide.loaders.DataObject; 34 import org.openide.loaders.DataObjectNotFoundException; 35 36 import org.netbeans.modules.tasklist.core.*; 37 import org.netbeans.modules.tasklist.client.Suggestion; 38 import org.netbeans.modules.tasklist.client.SuggestionManager; 39 import org.netbeans.modules.tasklist.client.SuggestionAgent; 40 import org.netbeans.modules.tasklist.providers.DocumentSuggestionProvider; 41 import org.netbeans.modules.tasklist.providers.SuggestionContext; 42 43 52 53 63 public final class SourceTaskProvider extends DocumentSuggestionProvider 64 implements PropertyChangeListener { 65 66 final static String TYPE = "nb-tasklist-scannedtask"; 68 69 private WeakSet agents = new WeakSet(); 70 71 private SuggestionContext env; 73 74 private Settings settings; 75 76 83 public String getType() { 84 return TYPE; 85 } 86 87 public void notifyFinish() { 88 Cache.store(); 89 settings = null; 90 } 91 92 public void notifyPrepare() { 93 settings = Settings.getDefault(); 95 } 96 97 public void notifyRun() { 98 } 100 101 public void notifyStop() { 102 } 104 105 public void propertyChange(PropertyChangeEvent ev) { 106 if (Settings.PROP_SCAN_TAGS.equals(ev.getPropertyName()) 110 || Settings.PROP_SCAN_SKIP.equals(ev.getPropertyName())) { 111 if (env == null) return; 112 } 114 } 115 116 public List scan(final SuggestionContext env) { 119 120 SuggestionManager manager = SuggestionManager.getDefault(); 121 if (!manager.isEnabled(TYPE)) { 122 return null; 123 } 124 125 try { 129 if (DataObject.find(env.getFileObject()).isModified() == false) { 130 List cached = Cache.get(env); 131 if (cached != null) return cached; 132 } 133 } catch (DataObjectNotFoundException e) { 134 } 136 137 boolean skipCode = settings().getSkipComments(); 138 List tasks; 139 140 if (skipCode) { 141 tasks = scanCommentsOnly(env); 142 } else { 143 tasks = scanAll(env); 144 } 145 Cache.put(env, tasks); 146 147 return tasks; 148 } 149 150 151 157 private List scanCommentsOnly(SuggestionContext env) { 158 ArrayList newTasks = new ArrayList (); 159 SourceCodeCommentParser sccp; 160 String suffix = env.getFileObject().getExt(); 161 162 if (suffix.equalsIgnoreCase("java") || suffix.equalsIgnoreCase("c") || suffix.equalsIgnoreCase("cpp")) { sccp = new SourceCodeCommentParser("//", "/*", "*/"); 172 } else if (suffix.equalsIgnoreCase("html") || suffix.equalsIgnoreCase("htm") || suffix.equalsIgnoreCase("xml")) { sccp = new SourceCodeCommentParser("<!--", "-->"); 176 } else if (suffix.equalsIgnoreCase("jsp")) { sccp = new SourceCodeCommentParser("<%--", "--%>"); 178 } else if (suffix.equalsIgnoreCase("sh")) { sccp = new SourceCodeCommentParser("#"); } else { 181 sccp = new SourceCodeCommentParser(); 182 } 183 184 CharSequence text = env.getCharSequence(); 185 sccp.setDocument(env); 186 187 SourceCodeCommentParser.CommentRegion reg = 188 new SourceCodeCommentParser.CommentRegion(); 189 190 TaskTag matchTag = null; 191 192 try { 193 Matcher matcher = settings.getTaskTags().getScanRegexp().matcher(text); 194 int len = text.length(); 195 int lineno = 1; 196 int index = 0; 197 int idx = 0; 198 199 if (!sccp.nextRegion(reg)) { 201 return newTasks; 203 } 204 205 while (index < len && matcher.find(index)) { 206 int begin = matcher.start(); 207 int end = matcher.end(); 208 boolean toosoon = false; 209 boolean goahead; 210 211 do { 212 goahead = true; 213 214 if (begin < reg.start) { 216 toosoon = true; 217 } else if (begin > reg.stop) { 219 goahead = false; 220 if (!sccp.nextRegion(reg)) { 221 return newTasks; 223 } 224 } 225 } while (!goahead); 226 227 if (toosoon) { 228 index = end; 230 continue; 231 } 232 233 matchTag = getTag(text, begin, end); 234 235 char c = 'a'; int nonwhite = begin; 239 while (begin >= index && (c = text.charAt(begin)) != '\n') { if (c != ' ' && c != '\t') { nonwhite = begin; 242 } 243 --begin; 244 } 245 246 begin = nonwhite; 247 248 nonwhite = end; 250 while (end < len) { 251 c = text.charAt(end); 252 if (c == '\n' || c == '\r') { break; 254 } else if (c != ' ' && c != '\t') { nonwhite = end; 256 } 257 ++end; 258 } 259 260 while (idx <= begin) { 262 if (text.charAt(idx) == '\n') { ++lineno; 264 } 265 ++idx; 266 } 267 268 index = end; 269 270 String description = text.subSequence(begin, nonwhite+1).toString(); 271 272 DataObject dataObject = DataObject.find(env.getFileObject()); 273 Line line = TLUtils.getLineByNumber(dataObject, lineno); 274 275 Suggestion task = prepareSuggestion(matchTag, description, line); 276 newTasks.add(task); 277 } 278 } catch (Exception e) { 279 ErrorManager.getDefault().notify(e); 280 } 281 return newTasks; 282 } 283 284 287 private List scanAll(SuggestionContext env) { 288 ArrayList newTasks = new ArrayList (); 289 290 CharSequence text = env.getCharSequence(); 291 292 TaskTag matchTag = null; 293 try { 294 int index = 0; 295 int lineno = 1; 296 int len = text.length(); 297 298 Matcher matcher = settings().getTaskTags().getScanRegexp().matcher(text); 299 while (index < len && matcher.find(index)) { 300 int begin = matcher.start(); 301 int end = matcher.end(); 302 matchTag = getTag(text, begin, end); 303 304 char c = 'a'; int nonwhite = begin; 308 while (begin >= index && (c = text.charAt(begin)) != '\n') { if (c != ' ' && c != '\t') { nonwhite = begin; 311 } 312 --begin; 313 } 314 315 begin = nonwhite; 316 317 nonwhite = end; 319 while (end < len) { 320 c = text.charAt(end); 321 if (c == '\n' || c == '\r') { break; 323 } else if (c != ' ' && c != '\t') { nonwhite = end; 325 } 326 ++end; 327 } 328 329 int idx = index; 331 while (idx <= begin) { 332 if (text.charAt(idx) == '\n') { ++lineno; 334 } 335 ++idx; 336 } 337 338 index = end; 339 340 String description = text.subSequence(begin, nonwhite+1).toString(); 341 342 DataObject dataObject = DataObject.find(env.getFileObject()); 343 Line line = TLUtils.getLineByNumber(dataObject, lineno); 344 345 Suggestion task = prepareSuggestion(matchTag, description, line); 346 newTasks.add(task); 347 348 } 349 } catch (Exception e) { 350 e.printStackTrace(); 351 } 352 return newTasks; 353 } 354 355 356 private Suggestion prepareSuggestion(TaskTag matchTag, String description, Line line) { 357 Suggestion suggestion = null; 358 if (line != null) { 359 SuggestionAgent agent = getAgent(line); 360 if (agent != null) { 361 suggestion = agent.getSuggestion(); 362 agent.setSummary(description); 363 if (matchTag != null) { 364 agent.setPriority(matchTag.getPriority()); 365 } 366 } 367 } 368 369 if (suggestion == null) { 370 SuggestionManager manager = SuggestionManager.getDefault(); 371 SuggestionAgent agent = manager.createSuggestion(null, 372 SourceTaskProvider.TYPE, description, null, this); 373 374 agent.setLine(line); 375 if (matchTag != null) { 376 agent.setPriority(matchTag.getPriority()); 377 } 378 agents.add(agent); 379 suggestion = agent.getSuggestion(); 380 } 381 382 return suggestion; 383 } 384 385 private TaskTag getTag(CharSequence text, int start, int end) { 386 TaskTag tag = settings().getTaskTags().getTag(text, start+1, (end - start)-1); 387 return tag; 388 } 389 390 private Settings settings() { 391 if (settings == null) { 392 settings = Settings.getDefault(); 394 } 395 return settings; 396 } 397 398 private SuggestionAgent getAgent(Line l) { 399 Iterator it = agents.iterator(); 400 while (it.hasNext()) { 401 SuggestionAgent next = (SuggestionAgent) it.next(); 402 if (next == null) continue; 403 if (l.equals(next.getSuggestion().getLine())) return next; 404 } 405 return null; 406 } 407 } 408 409 410 | Popular Tags |