1 11 12 package org.eclipse.jdt.internal.ui.text.comment; 13 14 import java.util.LinkedList ; 15 16 21 public abstract class CommentLine implements IBorderAttributes { 22 23 24 protected static final String NON_FORMAT_START_PREFIX= "/*-"; 26 27 private int fAttributes= 0; 28 29 30 private final CommentRegion fParent; 31 32 33 private final LinkedList fRanges= new LinkedList (); 34 35 41 protected CommentLine(final CommentRegion parent) { 42 fParent= parent; 43 } 44 45 51 protected abstract void adapt(final CommentLine previous); 52 53 59 protected void append(final CommentRange range) { 60 fRanges.add(range); 61 } 62 63 76 protected CommentRange formatLine(final CommentLine predecessor, final CommentRange last, final String indentation, final int line) { 77 78 int offset= 0; 79 int length= 0; 80 81 CommentRange next= last; 82 CommentRange previous= null; 83 84 final int stop= fRanges.size() - 1; 85 final int end= fParent.getSize() - 1; 86 87 for (int index= stop; index >= 0; index--) { 88 89 previous= next; 90 next= (CommentRange)fRanges.get(index); 91 92 if (fParent.canFormat(previous, next)) { 93 94 offset= next.getOffset() + next.getLength(); 95 length= previous.getOffset() - offset; 96 97 if (index == stop && line != end) 98 fParent.logEdit(fParent.getDelimiter(predecessor, this, previous, next, indentation), offset, length); 99 else 100 fParent.logEdit(fParent.getDelimiter(previous, next), offset, length); 101 } 102 } 103 return next; 104 } 105 106 119 protected void formatLowerBorder(final CommentRange range, final String indentation, final int length) { 120 121 final int offset= range.getOffset() + range.getLength(); 122 123 final StringBuffer buffer= new StringBuffer (length); 124 final String end= getEndingPrefix(); 125 final String delimiter= fParent.getDelimiter(); 126 127 if (fParent.isSingleLine() && fParent.getSize() == 1) 128 buffer.append(end); 129 else { 130 131 final String filler= getContentPrefix().trim(); 132 133 buffer.append(delimiter); 134 buffer.append(indentation); 135 136 if (fParent.hasBorder(BORDER_LOWER)) { 137 138 buffer.append(' '); 139 for (int character= 0; character < length; character++) 140 buffer.append(filler); 141 142 buffer.append(end.trim()); 143 144 } else 145 buffer.append(end); 146 } 147 fParent.logEdit(buffer.toString(), offset, fParent.getLength() - offset); 148 } 149 150 162 protected void formatUpperBorder(final CommentRange range, final String indentation, final int length) { 163 164 final StringBuffer buffer= new StringBuffer (length); 165 final String start= getStartingPrefix(); 166 final String content= getContentPrefix(); 167 168 if (fParent.isSingleLine() && fParent.getSize() == 1) 169 buffer.append(start); 170 else { 171 172 final String trimmed= start.trim(); 173 final String filler= content.trim(); 174 175 buffer.append(trimmed); 176 177 if (fParent.hasBorder(BORDER_UPPER)) { 178 179 for (int character= 0; character < length - trimmed.length() + start.length(); character++) 180 buffer.append(filler); 181 } 182 183 buffer.append(fParent.getDelimiter()); 184 buffer.append(indentation); 185 buffer.append(content); 186 } 187 fParent.logEdit(buffer.toString(), 0, range.getOffset()); 188 } 189 190 195 protected abstract String getContentPrefix(); 196 197 202 protected abstract String getEndingPrefix(); 203 204 209 protected final CommentRange getFirst() { 210 return (CommentRange)fRanges.getFirst(); 211 } 212 213 218 protected String getIndentationReference() { 219 return ""; } 221 222 227 protected final CommentRange getLast() { 228 return (CommentRange)fRanges.getLast(); 229 } 230 231 236 protected final CommentRegion getParent() { 237 return fParent; 238 } 239 240 245 protected final int getSize() { 246 return fRanges.size(); 247 } 248 249 254 protected abstract String getStartingPrefix(); 255 256 264 protected final boolean hasAttribute(final int attribute) { 265 return (fAttributes & attribute) == attribute; 266 } 267 268 274 protected abstract void scanLine(final int line); 275 276 282 protected final void setAttribute(final int attribute) { 283 fAttributes |= attribute; 284 } 285 286 292 protected void tokenizeLine(final int line) { 293 294 int offset= 0; 295 int index= offset; 296 297 final CommentRange range= (CommentRange)fRanges.get(0); 298 final int begin= range.getOffset(); 299 300 final String content= fParent.getText(begin, range.getLength()); 301 final int length= content.length(); 302 303 while (offset < length) { 304 305 while (offset < length && Character.isWhitespace(content.charAt(offset))) 306 offset++; 307 308 index= offset; 309 310 while (index < length && !Character.isWhitespace(content.charAt(index))) 311 index++; 312 313 if (index - offset > 0) { 314 fParent.append(new CommentRange(begin + offset, index - offset)); 315 316 offset= index; 317 } 318 } 319 } 320 } 321 | Popular Tags |