1 11 12 package org.eclipse.jdt.internal.ui.text.comment; 13 14 import java.util.Iterator ; 15 import java.util.ListIterator ; 16 import java.util.Map ; 17 18 import org.eclipse.jface.preference.IPreferenceStore; 19 20 import org.eclipse.jface.text.BadLocationException; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.TypedPosition; 23 24 import org.eclipse.jdt.ui.PreferenceConstants; 25 26 import org.eclipse.jdt.internal.ui.text.javadoc.ICommentTagConstants; 27 28 33 public class MultiCommentRegion extends CommentRegion implements ICommentTagConstants { 34 35 36 private final boolean fIndentDescriptions; 37 38 39 private final boolean fIndentRoots; 40 41 42 private final boolean fParameterNewLine; 43 44 45 private boolean fSeparateRoots; 46 47 61 protected MultiCommentRegion(final IDocument document, final TypedPosition position, final String delimiter, final Map preferences, final ITextMeasurement textMeasurement) { 62 super(document, position, delimiter, preferences, textMeasurement); 63 64 fIndentRoots= IPreferenceStore.TRUE.equals(preferences.get(PreferenceConstants.FORMATTER_COMMENT_INDENTROOTTAGS)); 65 fIndentDescriptions= IPreferenceStore.TRUE.equals(preferences.get(PreferenceConstants.FORMATTER_COMMENT_INDENTPARAMETERDESCRIPTION)); 66 fSeparateRoots= IPreferenceStore.TRUE.equals(preferences.get(PreferenceConstants.FORMATTER_COMMENT_SEPARATEROOTTAGS)); 67 fParameterNewLine= IPreferenceStore.TRUE.equals(preferences.get(PreferenceConstants.FORMATTER_COMMENT_NEWLINEFORPARAMETER)); 68 } 69 70 73 protected boolean canAppend(final CommentLine line, final CommentRange previous, final CommentRange next, final int index, int count) { 74 75 final boolean blank= next.hasAttribute(COMMENT_BLANKLINE); 76 77 if (next.getLength() <= 2 && !blank && isNonAlphaNumeric(next)) 79 return true; 80 81 if (fParameterNewLine && line.hasAttribute(COMMENT_PARAMETER) && line.getSize() > 1) 82 return false; 83 84 if (previous != null) { 85 86 if (previous.hasAttribute(COMMENT_ROOT)) 87 return true; 88 89 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))) 90 return false; 91 92 if (next.hasAttribute(COMMENT_IMMUTABLE) && previous.hasAttribute(COMMENT_IMMUTABLE)) 93 return true; 94 } 95 96 if (fIndentRoots && !line.hasAttribute(COMMENT_ROOT) && !line.hasAttribute(COMMENT_PARAMETER)) 97 count -= stringToLength(line.getIndentationReference()); 98 99 if (next.hasAttribute(COMMENT_IMMUTABLE) && (previous == null || !previous.hasAttribute(COMMENT_IMMUTABLE))) { 101 Iterator iter= getRanges().iterator(); 103 CommentRange current= null; 104 while (iter.hasNext() && current != next) 105 current= (CommentRange) iter.next(); 106 107 if (current != null && iter.hasNext()) { 108 try { 109 int lineNumber= getDocument().getLineOfOffset(getOffset() + current.getOffset()); 110 CommentRange last= current; 111 while (iter.hasNext()) { 112 current= (CommentRange) iter.next(); 113 if (current.hasAttribute(COMMENT_IMMUTABLE) && getDocument().getLineOfOffset(getOffset() + current.getOffset()) == lineNumber) 114 last= current; 115 else 116 break; 117 } 118 count -= last.getOffset() + last.getLength() - (next.getOffset() + next.getLength()); 119 } catch (BadLocationException e) { 120 } 122 } 123 } 124 125 return super.canAppend(line, previous, next, index, count); 126 } 127 128 131 protected String getDelimiter(CommentLine predecessor, CommentLine successor, CommentRange previous, CommentRange next, String indentation) { 132 133 final String delimiter= super.getDelimiter(predecessor, successor, previous, next, indentation); 134 135 if (previous != null) { 136 137 if (previous.hasAttribute(COMMENT_IMMUTABLE | COMMENT_SEPARATOR) && !next.hasAttribute(COMMENT_CODE) && !successor.hasAttribute(COMMENT_BLANKLINE)) 139 return delimiter + delimiter; 140 141 148 else if (next.hasAttribute(COMMENT_IMMUTABLE | COMMENT_SEPARATOR) && !successor.hasAttribute(COMMENT_BLANKLINE) && !predecessor.hasAttribute(COMMENT_BLANKLINE)) 150 return delimiter + delimiter; 151 152 else if (fSeparateRoots && previous.hasAttribute(COMMENT_PARAGRAPH) && !successor.hasAttribute(COMMENT_BLANKLINE) && !predecessor.hasAttribute(COMMENT_BLANKLINE)) 154 return delimiter + delimiter; 155 156 else if (fIndentRoots && !predecessor.hasAttribute(COMMENT_ROOT) && !predecessor.hasAttribute(COMMENT_PARAMETER) && !predecessor.hasAttribute(COMMENT_BLANKLINE)) 157 return delimiter + stringToIndent(predecessor.getIndentationReference(), false); 158 } 159 return delimiter; 160 } 161 162 165 protected String getDelimiter(final CommentRange previous, final CommentRange next) { 166 167 if (previous != null) { 168 169 if (previous.hasAttribute(COMMENT_HTML) && next.hasAttribute(COMMENT_HTML)) 170 return ""; 172 else if (next.hasAttribute(COMMENT_OPEN) || previous.hasAttribute(COMMENT_HTML | COMMENT_CLOSE)) 173 return ""; 175 else if (!next.hasAttribute(COMMENT_CODE) && previous.hasAttribute(COMMENT_CODE)) 176 return ""; 178 else if (next.hasAttribute(COMMENT_CLOSE) && previous.getLength() <= 2 && !isAlphaNumeric(previous)) 179 return ""; 181 else if (previous.hasAttribute(COMMENT_OPEN) && next.getLength() <= 2 && !isAlphaNumeric(next)) 182 return ""; } 184 return super.getDelimiter(previous, next); 185 } 186 187 193 protected final boolean isIndentDescriptions() { 194 return fIndentDescriptions; 195 } 196 197 203 protected final boolean isIndentRoots() { 204 return fIndentRoots; 205 } 206 207 210 protected void markHtmlRanges() { 211 } 213 214 222 protected void markHtmlTag(final CommentRange range, final String token) { 223 } 225 226 234 protected void markJavadocTag(final CommentRange range, final String token) { 235 range.markPrefixTag(COMMENT_ROOT_TAGS, COMMENT_TAG_PREFIX, token, COMMENT_ROOT); 236 } 237 238 241 protected void markRegion() { 242 243 int count= 0; 244 boolean paragraph= false; 245 246 String token= null; 247 CommentRange range= null; 248 249 for (final ListIterator iterator= getRanges().listIterator(); iterator.hasNext();) { 250 251 range= (CommentRange)iterator.next(); 252 count= range.getLength(); 253 254 if (count > 0) { 255 256 token= getText(range.getOffset(), count).toLowerCase(); 257 258 markJavadocTag(range, token); 259 if (!paragraph && (range.hasAttribute(COMMENT_ROOT) || range.hasAttribute(COMMENT_PARAMETER))) { 260 range.setAttribute(COMMENT_PARAGRAPH); 261 paragraph= true; 262 } 263 markHtmlTag(range, token); 264 } 265 } 266 markHtmlRanges(); 267 } 268 } 269 | Popular Tags |