1 11 12 package org.eclipse.jdt.internal.formatter.comment; 13 14 import java.util.Iterator ; 15 import java.util.ListIterator ; 16 17 import org.eclipse.jdt.internal.formatter.CodeFormatterVisitor; 18 import org.eclipse.jface.text.BadLocationException; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.Position; 21 22 27 public class MultiCommentRegion extends CommentRegion implements IJavaDocTagConstants { 28 29 30 private final boolean fIndentDescriptions; 31 32 33 private final boolean fIndentRoots; 34 35 36 private final boolean fParameterNewLine; 37 38 39 private boolean fSeparateRoots; 40 41 48 public MultiCommentRegion(final IDocument document, final Position position, final CodeFormatterVisitor formatter) { 49 super(document, position, formatter); 50 51 fIndentRoots= this.preferences.comment_indent_root_tags; 52 fIndentDescriptions= this.preferences.comment_indent_parameter_description; 53 fSeparateRoots= this.preferences.comment_insert_empty_line_before_root_tags; 54 fParameterNewLine= this.preferences.comment_insert_new_line_for_parameter; 55 fClear = this.preferences.comment_clear_blank_lines_in_block_comment; 56 } 57 58 61 protected boolean canAppend(final CommentLine line, final CommentRange previous, final CommentRange next, final int index, int count) { 62 63 final boolean blank= next.hasAttribute(COMMENT_BLANKLINE); 64 65 if (next.getLength() <= 2 && !blank && isNonAlphaNumeric(next)) 67 return true; 68 69 if (fParameterNewLine && line.hasAttribute(COMMENT_PARAMETER) && line.getSize() > 1) 70 return false; 71 72 if (previous != null) { 73 74 if (index != 0 && (blank || previous.hasAttribute(COMMENT_BLANKLINE) || next.hasAttribute(COMMENT_PARAMETER) || next.hasAttribute(COMMENT_ROOT) || next.hasAttribute(COMMENT_SEPARATOR) || next.hasAttribute(COMMENT_NEWLINE) || previous.hasAttribute(COMMENT_BREAK) || previous.hasAttribute(COMMENT_SEPARATOR))) 75 return false; 76 77 if (previous.hasAttribute(COMMENT_ROOT)) 78 return true; 79 80 if (next.hasAttribute(COMMENT_IMMUTABLE) && previous.hasAttribute(COMMENT_IMMUTABLE)) 81 return true; 82 } 83 84 if (!next.hasAttribute(COMMENT_STARTS_WITH_RANGE_DELIMITER)) { 86 return true; 87 } 88 89 if (fIndentRoots && !line.hasAttribute(COMMENT_ROOT) && !line.hasAttribute(COMMENT_PARAMETER)) 90 count -= stringToLength(line.getIndentationReference()); 91 92 if (next.hasAttribute(COMMENT_IMMUTABLE) && (previous == null || !previous.hasAttribute(COMMENT_IMMUTABLE))) { 94 Iterator iter= getRanges().iterator(); 96 CommentRange current= null; 97 while (iter.hasNext() && current != next) 98 current= (CommentRange) iter.next(); 99 100 if (current != null && iter.hasNext()) { 101 try { 102 int lineNumber= getDocument().getLineOfOffset(getOffset() + current.getOffset()); 103 CommentRange last= current; 104 while (iter.hasNext()) { 105 current= (CommentRange) iter.next(); 106 if (current.hasAttribute(COMMENT_IMMUTABLE) && getDocument().getLineOfOffset(getOffset() + current.getOffset()) == lineNumber) 107 last= current; 108 else 109 break; 110 } 111 count -= last.getOffset() + last.getLength() - (next.getOffset() + next.getLength()); 112 } catch (BadLocationException e) { 113 } 115 } 116 } 117 118 return super.canAppend(line, previous, next, index, count); 119 } 120 121 124 protected String getDelimiter(CommentLine predecessor, CommentLine successor, CommentRange previous, CommentRange next, String indentation) { 125 126 final String delimiter= super.getDelimiter(predecessor, successor, previous, next, indentation); 127 128 if (previous != null) { 129 130 if (previous.hasAttribute(COMMENT_IMMUTABLE | COMMENT_SEPARATOR) && !next.hasAttribute(COMMENT_CODE) && !successor.hasAttribute(COMMENT_BLANKLINE)) 132 return delimiter + delimiter; 133 134 else if (next.hasAttribute(COMMENT_IMMUTABLE | COMMENT_SEPARATOR) && !successor.hasAttribute(COMMENT_BLANKLINE) && !predecessor.hasAttribute(COMMENT_BLANKLINE)) 136 return delimiter + delimiter; 137 138 else if (fSeparateRoots && previous.hasAttribute(COMMENT_PARAGRAPH) && !successor.hasAttribute(COMMENT_BLANKLINE) && !predecessor.hasAttribute(COMMENT_BLANKLINE)) 140 return delimiter + delimiter; 141 142 else if (fIndentRoots && !predecessor.hasAttribute(COMMENT_ROOT) && !predecessor.hasAttribute(COMMENT_PARAMETER) && !predecessor.hasAttribute(COMMENT_BLANKLINE)) 143 return delimiter + stringToIndent(predecessor.getIndentationReference()); 144 } 145 return delimiter; 146 } 147 148 151 protected String getDelimiter(final CommentRange previous, final CommentRange next) { 152 if (previous != null && !previous.hasAttribute(COMMENT_STARTS_WITH_RANGE_DELIMITER)) { 154 return ""; } else { 156 return super.getDelimiter(previous, next); 157 } 158 } 159 160 166 protected final boolean isIndentDescriptions() { 167 return fIndentDescriptions; 168 } 169 170 176 protected final boolean isIndentRoots() { 177 return fIndentRoots; 178 } 179 180 183 protected void markHtmlRanges() { 184 } 186 187 193 protected void markHtmlTag(final CommentRange range, final char[] token) { 194 } 196 197 203 protected void markJavadocTag(final CommentRange range, final char[] token) { 204 range.markPrefixTag(COMMENT_ROOT_TAGS, COMMENT_TAG_PREFIX, token, COMMENT_ROOT); 205 } 206 207 210 protected void markRegion() { 211 212 int count= 0; 213 boolean paragraph= false; 214 215 char[] token= null; 216 CommentRange range= null; 217 218 for (final ListIterator iterator= getRanges().listIterator(); iterator.hasNext();) { 219 220 range= (CommentRange)iterator.next(); 221 count= range.getLength(); 222 223 if (count > 0) { 224 225 token= getText(range.getOffset(), count).toLowerCase().toCharArray(); 226 227 markJavadocTag(range, token); 228 if (!paragraph && (range.hasAttribute(COMMENT_ROOT) || range.hasAttribute(COMMENT_PARAMETER))) { 229 range.setAttribute(COMMENT_PARAGRAPH); 230 paragraph= true; 231 } 232 markHtmlTag(range, token); 233 } 234 } 235 markHtmlRanges(); 236 } 237 238 242 protected CommentLine createLine() { 243 return new MultiCommentLine(this); 244 } 245 } 246 | Popular Tags |