1 11 12 package org.eclipse.jdt.internal.ui.text.comment; 13 14 import java.util.HashSet ; 15 import java.util.Set ; 16 17 import org.eclipse.jface.text.IRegion; 18 import org.eclipse.jface.text.Region; 19 20 import org.eclipse.jdt.internal.ui.text.javadoc.IHtmlTagConstants; 21 import org.eclipse.jdt.internal.ui.text.javadoc.IJavaDocTagConstants; 22 import org.eclipse.jdt.internal.ui.text.javadoc.ILinkTagConstants; 23 24 29 public class MultiCommentLine extends CommentLine implements ICommentAttributes, IHtmlTagConstants, ILinkTagConstants, IJavaDocTagConstants { 30 31 32 public static final String MULTI_COMMENT_CONTENT_PREFIX= " * "; 34 35 public static final String MULTI_COMMENT_END_PREFIX= " */"; 37 38 public static final String MULTI_COMMENT_START_PREFIX= "/* "; 40 41 private String fReferenceIndentation= ""; 43 44 private static final Set fgTagLookup; 45 46 static { 47 fgTagLookup= new HashSet (); 48 for (int i= 0; i < JAVADOC_BREAK_TAGS.length; i++) { 49 fgTagLookup.add(JAVADOC_BREAK_TAGS[i]); 50 } 51 for (int i= 0; i < JAVADOC_SINGLE_BREAK_TAG.length; i++) { 52 fgTagLookup.add(JAVADOC_SINGLE_BREAK_TAG[i]); 53 } 54 for (int i= 0; i < JAVADOC_CODE_TAGS.length; i++) { 55 fgTagLookup.add(JAVADOC_CODE_TAGS[i]); 56 } 57 for (int i= 0; i < JAVADOC_IMMUTABLE_TAGS.length; i++) { 58 fgTagLookup.add(JAVADOC_IMMUTABLE_TAGS[i]); 59 } 60 for (int i= 0; i < JAVADOC_NEWLINE_TAGS.length; i++) { 61 fgTagLookup.add(JAVADOC_NEWLINE_TAGS[i]); 62 } 63 for (int i= 0; i < JAVADOC_SEPARATOR_TAGS.length; i++) { 64 fgTagLookup.add(JAVADOC_SEPARATOR_TAGS[i]); 65 } 66 } 67 68 74 protected MultiCommentLine(final CommentRegion region) { 75 super(region); 76 } 77 78 81 protected void adapt(final CommentLine previous) { 82 83 if (!hasAttribute(COMMENT_ROOT) && !hasAttribute(COMMENT_PARAMETER) && !previous.hasAttribute(COMMENT_BLANKLINE)) 84 fReferenceIndentation= previous.getIndentationReference(); 85 } 86 87 90 protected void append(final CommentRange range) { 91 92 final MultiCommentRegion parent= (MultiCommentRegion)getParent(); 93 94 if (range.hasAttribute(COMMENT_PARAMETER)) 95 setAttribute(COMMENT_PARAMETER); 96 else if (range.hasAttribute(COMMENT_ROOT)) 97 setAttribute(COMMENT_ROOT); 98 else if (range.hasAttribute(COMMENT_BLANKLINE)) 99 setAttribute(COMMENT_BLANKLINE); 100 101 final int ranges= getSize(); 102 if (ranges == 1) { 103 104 if (parent.isIndentRoots()) { 105 106 final CommentRange first= getFirst(); 107 final String common= parent.getText(first.getOffset(), first.getLength()) + CommentRegion.COMMENT_RANGE_DELIMITER; 108 109 if (hasAttribute(COMMENT_ROOT)) 110 fReferenceIndentation= common; 111 else if (hasAttribute(COMMENT_PARAMETER)) { 112 if (parent.isIndentDescriptions()) 113 fReferenceIndentation= common + "\t"; else 115 fReferenceIndentation= common; 116 } 117 } 118 } 119 super.append(range); 120 } 121 122 125 protected String getContentPrefix() { 126 return MULTI_COMMENT_CONTENT_PREFIX; 127 } 128 129 132 protected String getEndingPrefix() { 133 return MULTI_COMMENT_END_PREFIX; 134 } 135 136 141 protected final String getIndentationReference() { 142 return fReferenceIndentation; 143 } 144 145 148 protected String getStartingPrefix() { 149 return MULTI_COMMENT_START_PREFIX; 150 } 151 152 155 protected void scanLine(final int line) { 156 157 final CommentRegion parent= getParent(); 158 final String start= getStartingPrefix().trim(); 159 final String end= getEndingPrefix().trim(); 160 final String content= getContentPrefix().trim(); 161 162 final int lines= parent.getSize(); 163 final CommentRange range= getFirst(); 164 165 int offset= 0; 166 int postfix= 0; 167 168 String text= parent.getText(range.getOffset(), range.getLength()); 169 if (line == 0) { 170 171 offset= text.indexOf(start); 172 if (offset >= 0 && text.substring(0, offset).trim().length() != 0) 173 offset= -1; 174 175 if (offset >= 0) { 176 177 offset += start.length(); 178 range.trimBegin(offset); 179 180 postfix= text.lastIndexOf(end); 181 if (postfix >= 0 && text.substring(postfix + end.length()).trim().length() != 0) 182 postfix= -1; 183 184 if (postfix >= offset) 185 range.setLength(postfix - offset); 187 else { 188 postfix= text.lastIndexOf(content); 189 if (postfix >= 0 && text.substring(postfix + content.length()).trim().length() != 0) 190 postfix= -1; 191 192 if (postfix >= offset) { 193 194 range.setLength(postfix - offset); 195 parent.setBorder(BORDER_UPPER); 196 197 if (postfix > offset) { 198 199 text= parent.getText(range.getOffset(), range.getLength()); 200 final IRegion region= trimLine(text, content); 201 202 range.move(region.getOffset()); 203 range.setLength(region.getLength()); 204 } 205 } 206 } 207 } 208 } else if (line == lines - 1) { 209 210 offset= text.indexOf(content); 211 if (offset >= 0 && text.substring(0, offset).trim().length() != 0) 212 offset= -1; 213 postfix= text.lastIndexOf(end); 214 if (postfix >= 0 && text.substring(postfix + end.length()).trim().length() != 0) 215 postfix= -1; 216 217 if (offset >= 0 && offset == postfix) 218 range.setLength(0); 220 else { 221 if (offset >= 0) 222 range.trimBegin(offset + content.length()); 224 225 if (postfix >= 0) 226 range.trimEnd(-end.length()); 228 229 text= parent.getText(range.getOffset(), range.getLength()); 230 final IRegion region= trimLine(text, content); 231 if (region.getOffset() != 0 || region.getLength() != text.length()) { 232 233 range.move(region.getOffset()); 234 range.setLength(region.getLength()); 235 236 parent.setBorder(BORDER_UPPER); 237 parent.setBorder(BORDER_LOWER); 238 } 239 } 240 } else { 241 242 offset= text.indexOf(content); 243 if (offset >= 0 && text.substring(0, offset).trim().length() != 0) 244 offset= -1; 245 246 if (offset >= 0) { 247 248 offset += content.length(); 249 range.trimBegin(offset); 250 } 251 } 252 } 253 254 257 protected void tokenizeLine(int line) { 258 259 int offset= 0; 260 int index= offset; 261 262 final CommentRegion parent= getParent(); 263 final CommentRange range= getFirst(); 264 final int begin= range.getOffset(); 265 266 final String content= parent.getText(begin, range.getLength()); 267 final int length= content.length(); 268 269 while (offset < length && Character.isWhitespace(content.charAt(offset))) 270 offset++; 271 272 CommentRange result= null; 273 if (offset >= length && !parent.isClearLines() && (line > 0 && line < parent.getSize() - 1)) { 274 275 result= new CommentRange(begin, 0); 276 result.setAttribute(COMMENT_BLANKLINE); 277 result.setAttribute(COMMENT_FIRST_TOKEN); 278 279 parent.append(result); 280 } 281 282 int attribute= COMMENT_FIRST_TOKEN; 283 while (offset < length) { 284 285 while (offset < length && Character.isWhitespace(content.charAt(offset))) 286 offset++; 287 288 index= offset; 289 290 if (index < length) { 291 292 if (content.charAt(index) == HTML_TAG_PREFIX) { 293 294 int tag= ++index; 298 while (index < length && content.charAt(index) != HTML_TAG_POSTFIX && content.charAt(index) != HTML_TAG_PREFIX) 299 index++; 300 301 if (index < length && content.charAt(index) == HTML_TAG_POSTFIX && isValidTag(content.substring(tag, index))) { 302 index++; 303 attribute |= COMMENT_HTML; } else { 305 index= tag; 307 while (index < length && !Character.isWhitespace(content.charAt(index)) && content.charAt(index) != HTML_TAG_PREFIX && !content.startsWith(LINK_TAG_PREFIX, index)) 308 index++; 309 } 310 311 312 } else if (content.startsWith(LINK_TAG_PREFIX, index)) { 313 314 while (index < length && content.charAt(index) != LINK_TAG_POSTFIX) 315 index++; 316 317 if (index < length && content.charAt(index) == LINK_TAG_POSTFIX) 318 index++; 319 320 attribute |= COMMENT_OPEN | COMMENT_CLOSE; 321 322 } else { 323 324 while (index < length && !Character.isWhitespace(content.charAt(index)) && content.charAt(index) != HTML_TAG_PREFIX && !content.startsWith(LINK_TAG_PREFIX, index)) 325 index++; 326 } 327 } 328 329 if (index - offset > 0) { 330 331 result= new CommentRange(begin + offset, index - offset); 332 result.setAttribute(attribute); 333 334 parent.append(result); 335 offset= index; 336 } 337 338 attribute= 0; 339 } 340 } 341 342 352 private boolean isValidTag(String tag) { 353 if (tag.startsWith("/")) tag= tag.substring(1, tag.length()); 356 357 tag= tag.trim(); 359 360 int i= 0; 362 while (i < tag.length() && !Character.isWhitespace(tag.charAt(i))) 363 i++; 364 tag= tag.substring(0, i); 365 366 return isTagName(tag.toLowerCase()); 368 } 369 370 376 private boolean isTagName(String tag) { 377 return fgTagLookup.contains(tag); 378 } 379 380 389 protected final IRegion trimLine(final String line, final String trimmable) { 390 391 final int trim= trimmable.length(); 392 393 int offset= 0; 394 int length= line.length() - trim; 395 396 while (line.startsWith(trimmable, offset)) 397 offset += trim; 398 399 while (line.startsWith(trimmable, length)) 400 length -= trim; 401 402 return new Region(offset, length + trim); 403 } 404 } 405 | Popular Tags |