1 11 12 package org.eclipse.jdt.internal.formatter.comment; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.jdt.core.compiler.CharOperation; 19 import org.eclipse.jface.text.Position; 20 21 26 public class CommentRange extends Position implements ICommentAttributes, IHtmlTagDelimiters { 27 28 29 private int fAttributes= 0; 30 31 37 public CommentRange(final int position, final int count) { 38 super(position, count); 39 } 40 41 48 protected final boolean hasAttribute(final int attribute) { 49 return (fAttributes & attribute) == attribute; 50 } 51 52 60 protected final boolean isClosingTag(final char[] token, final char[] tag) { 61 62 boolean result= (CharOperation.indexOf(HTML_CLOSE_PREFIX, token, false) == 0) 63 && token[token.length - 1] == HTML_TAG_POSTFIX; 64 if (result) { 65 66 setAttribute(COMMENT_CLOSE); 67 result= CharOperation.equals(tag, token, HTML_CLOSE_PREFIX.length, token.length - 1, false); 68 } 69 return result; 70 } 71 72 80 protected final boolean isOpeningTag(final char[] token, final char[] tag) { 81 82 boolean result= token.length > 0 83 && token[0] == HTML_TAG_PREFIX 84 && (CharOperation.indexOf(HTML_CLOSE_PREFIX, token, false) != 0) 85 && token[token.length - 1] == HTML_TAG_POSTFIX; 86 if (result) { 87 88 setAttribute(COMMENT_OPEN); 89 result= CharOperation.indexOf(tag, token, false) == 1; 90 } 91 return result; 92 } 93 94 105 protected final void markHtmlTag(final char[][] tags, final char[] token, final int attribute, final boolean open, final boolean close) { 106 if (token[0] == HTML_TAG_PREFIX && token[token.length - 1] == HTML_TAG_POSTFIX) { 107 108 char[] tag= null; 109 boolean isOpen= false; 110 boolean isClose= false; 111 112 for (int index= 0; index < tags.length; index++) { 113 114 tag= tags[index]; 115 116 isOpen= isOpeningTag(token, tag); 117 isClose= isClosingTag(token, tag); 118 119 if ((open && isOpen) || (close && isClose)) { 120 121 setAttribute(attribute); 122 break; 123 } 124 } 125 } 126 } 127 128 136 protected final void markPrefixTag(final char[][] tags, final char prefix, final char[] token, final int attribute) { 137 138 if (token[0] == prefix) { 139 140 char[] tag= null; 141 for (int index= 0; index < tags.length; index++) { 142 143 tag= tags[index]; 144 if (CharOperation.equals(token, tag)) { 145 146 setAttribute(attribute); 147 break; 148 } 149 } 150 } 151 } 152 153 165 protected final int markTagRange(final char[] token, final char[] tag, int level, final int key, final boolean html) { 166 167 if (isOpeningTag(token, tag)) { 168 if (level++ > 0) 169 setAttribute(key); 170 } else if (isClosingTag(token, tag)) { 171 if (--level > 0) 172 setAttribute(key); 173 } else if (level > 0) { 174 if (html || !hasAttribute(COMMENT_HTML)) 175 setAttribute(key); 176 } 177 return level; 178 } 179 180 185 public final void move(final int delta) { 186 offset += delta; 187 } 188 189 194 protected final void setAttribute(final int attribute) { 195 fAttributes |= attribute; 196 } 197 198 203 public final void trimBegin(final int delta) { 204 offset += delta; 205 length -= delta; 206 } 207 208 213 public final void trimEnd(final int delta) { 214 length += delta; 215 } 216 217 221 public String toString() { 222 List attributes= new ArrayList (); 223 if (hasAttribute(COMMENT_BLANKLINE)) 224 attributes.add("COMMENT_BLANKLINE"); if (hasAttribute(COMMENT_BREAK)) 226 attributes.add("COMMENT_BREAK"); if (hasAttribute(COMMENT_CLOSE)) 228 attributes.add("COMMENT_CLOSE"); if (hasAttribute(COMMENT_CODE)) 230 attributes.add("COMMENT_CODE"); if (hasAttribute(COMMENT_HTML)) 232 attributes.add("COMMENT_HTML"); if (hasAttribute(COMMENT_IMMUTABLE)) 234 attributes.add("COMMENT_IMMUTABLE"); if (hasAttribute(COMMENT_NEWLINE)) 236 attributes.add("COMMENT_NEWLINE"); if (hasAttribute(COMMENT_OPEN)) 238 attributes.add("COMMENT_OPEN"); if (hasAttribute(COMMENT_PARAGRAPH)) 240 attributes.add("COMMENT_PARAGRAPH"); if (hasAttribute(COMMENT_PARAMETER)) 242 attributes.add("COMMENT_PARAMETER"); if (hasAttribute(COMMENT_ROOT)) 244 attributes.add("COMMENT_ROOT"); if (hasAttribute(COMMENT_SEPARATOR)) 246 attributes.add("COMMENT_SEPARATOR"); if (hasAttribute(COMMENT_FIRST_TOKEN)) 248 attributes.add("COMMENT_FIRST_TOKEN"); if (hasAttribute(COMMENT_STARTS_WITH_RANGE_DELIMITER)) 250 attributes.add("COMMENT_STARTS_WITH_RANGE_DELIMITER"); 252 StringBuffer buf= new StringBuffer ("CommentRange [" + offset + "+" + length + "] {"); for (Iterator it= attributes.iterator(); it.hasNext();) { 254 String string= (String ) it.next(); 255 buf.append(string); 256 if (it.hasNext()) 257 buf.append(", "); } 259 260 return buf.toString() + "}"; } 262 } 263 | Popular Tags |