1 19 20 package org.netbeans.editor.ext; 21 22 import org.netbeans.editor.Syntax; 23 import org.netbeans.editor.TokenID; 24 import org.netbeans.editor.TokenItem; 25 import org.netbeans.editor.ImageTokenID; 26 import org.netbeans.editor.TokenContextPath; 27 28 35 36 public class ExtFormatSupport extends FormatSupport { 37 38 public ExtFormatSupport(FormatWriter formatWriter) { 39 super(formatWriter); 40 } 41 42 50 public int findLineDistance(FormatTokenPosition fromPosition, 51 FormatTokenPosition toPosition) { 52 int lineCounter = 0; 53 54 TokenItem token = fromPosition.getToken(); 55 int offset = fromPosition.getOffset(); 56 TokenItem targetToken = toPosition.getToken(); 57 int targetOffset = toPosition.getOffset(); 58 59 if (token == null && targetToken == null) { 61 return 0; 62 } 63 64 while (token != null) { 65 String text = token.getImage(); 66 int textLen = text.length(); 67 while (offset < textLen) { 68 if (token == targetToken && offset == targetOffset) { 69 return lineCounter; 70 } 71 72 if (text.charAt(offset) == '\n') { 73 lineCounter++; 74 } 75 76 offset++; 77 } 78 79 token = token.getNext(); 80 offset = 0; 81 } 82 83 throw new IllegalStateException ("Tokens don't follow in chain."); } 85 86 89 public boolean isComment(TokenItem token, int offset) { 90 return false; 91 } 92 93 public boolean isComment(FormatTokenPosition pos) { 94 return isComment(pos.getToken(), pos.getOffset()); 95 } 96 97 98 public boolean isImportant(TokenItem token, int offset) { 99 return !isComment(token, offset) && !isWhitespace(token, offset); 100 } 101 102 public boolean isImportant(FormatTokenPosition pos) { 103 return isImportant(pos.getToken(), pos.getOffset()); 104 } 105 106 119 public FormatTokenPosition findImportant(FormatTokenPosition startPosition, 120 FormatTokenPosition limitPosition, boolean stopOnEOL, boolean backward) { 121 if (startPosition.equals(limitPosition)) { 123 return null; 124 } 125 126 if (backward) { 127 TokenItem limitToken; 128 int limitOffset; 129 130 if (limitPosition == null) { 131 limitToken = null; 132 limitOffset = 0; 133 134 } else { limitPosition = getPreviousPosition(limitPosition); 136 if (limitPosition == null) { 137 limitToken = null; 138 limitOffset = 0; 139 140 } else { limitToken = limitPosition.getToken(); 142 limitOffset = limitPosition.getOffset(); 143 } 144 } 145 146 startPosition = getPreviousPosition(startPosition); 147 if (startPosition == null) { 148 return null; 149 } 150 151 TokenItem token = startPosition.getToken(); 152 int offset = startPosition.getOffset(); 153 154 while (true) { 155 String text = token.getImage(); 156 while (offset >= 0) { 157 if (stopOnEOL && text.charAt(offset) == '\n') { 158 return null; 159 } 160 161 if (isImportant(token, offset)) { 162 return getPosition(token, offset); 163 } 164 165 if (token == limitToken && offset == limitOffset) { 166 return null; 167 } 168 169 offset--; 170 } 171 172 token = token.getPrevious(); 173 if (token == null) { 174 return null; 175 } 176 offset = token.getImage().length() - 1; 177 } 178 179 } else { TokenItem limitToken; 181 int limitOffset; 182 183 if (limitPosition == null) { 184 limitToken = null; 185 limitOffset = 0; 186 187 } else { limitToken = limitPosition.getToken(); 189 limitOffset = limitPosition.getOffset(); 190 } 191 192 TokenItem token = startPosition.getToken(); 193 int offset = startPosition.getOffset(); 194 195 if (token == null) 196 return null; 197 198 while (true) { 199 String text = token.getImage(); 200 int textLen = text.length(); 201 while (offset < textLen) { 202 if (token == limitToken && offset == limitOffset) { 203 return null; 204 } 205 206 if (stopOnEOL && text.charAt(offset) == '\n') { 207 return null; 208 } 209 210 if (isImportant(token, offset)) { 211 return getPosition(token, offset); 212 } 213 214 offset++; 215 } 216 217 token = token.getNext(); 218 if (token == null) { 219 return null; 220 } 221 offset = 0; 222 } 223 } 224 } 225 226 229 public FormatTokenPosition findLineFirstImportant(FormatTokenPosition pos) { 230 pos = findLineStart(pos); 231 TokenItem token = pos.getToken(); 232 int offset = pos.getOffset(); 233 234 if (token == null) { return null; 236 } 237 238 while (true) { 239 String text = token.getImage(); 240 int textLen = text.length(); 241 while (offset < textLen) { 242 if (text.charAt(offset) == '\n') { 243 return null; 244 } 245 246 if (isImportant(token, offset)) { 247 return getPosition(token, offset); 248 } 249 250 offset++; 251 } 252 253 if (token.getNext() == null) { 254 return null; 255 } 256 257 token = token.getNext(); 258 offset = 0; 259 } 260 } 261 262 267 public FormatTokenPosition findLineEndNonImportant(FormatTokenPosition pos) { 268 pos = findLineEnd(pos); 269 if (isChainStartPosition(pos)) { return pos; 271 272 } else { 273 pos = getPreviousPosition(pos); 274 } 275 276 277 TokenItem token = pos.getToken(); 278 int offset = pos.getOffset(); 279 280 while (true) { 281 String text = token.getImage(); 282 int textLen = text.length(); 283 while (offset >= 0) { 284 if (offset < textLen 285 && ((text.charAt(offset) == '\n') 286 || isImportant(token, offset)) 287 288 ) { 289 return getNextPosition(token, offset); 290 } 291 292 293 offset--; 294 } 295 296 if (token.getPrevious() == null) { 297 return getPosition(token, 0); 299 } 300 301 token = token.getPrevious(); 302 offset = token.getImage().length() - 1; 303 } 304 } 305 306 309 public TokenItem insertImageToken(TokenItem beforeToken, 310 ImageTokenID tokenID, TokenContextPath tokenContextPath) { 311 return super.insertToken(beforeToken, tokenID, tokenContextPath, 312 tokenID.getImage()); 313 } 314 315 331 public TokenItem findToken(TokenItem startToken, TokenItem limitToken, 332 TokenID tokenID, TokenContextPath tokenContextPath, 333 String tokenImage, boolean backward) { 334 335 if (backward) { if (startToken != null && startToken == limitToken) { return null; 338 } 339 340 startToken = getPreviousToken(startToken); 341 342 if (limitToken != null) { 343 limitToken = limitToken.getPrevious(); 344 } 345 } 346 347 while (startToken != null && startToken != limitToken) { 348 if (tokenEquals(startToken, tokenID, tokenContextPath, tokenImage)) { 349 return startToken; 350 } 351 352 startToken = backward ? startToken.getPrevious() : startToken.getNext(); 353 } 354 355 return null; 356 } 357 358 372 public TokenItem findImportantToken(TokenItem startToken, TokenItem limitToken, 373 boolean backward) { 374 375 if (backward) { if (startToken != null && startToken == limitToken) { return null; 378 } 379 380 startToken = getPreviousToken(startToken); 381 382 if (limitToken != null) { 383 limitToken = limitToken.getPrevious(); 384 } 385 } 386 387 while (startToken != null && startToken != limitToken) { 388 if (isImportant(startToken, 0)) { 389 return startToken; 390 } 391 392 startToken = backward ? startToken.getPrevious() : startToken.getNext(); 393 } 394 395 return null; 396 } 397 398 412 public TokenItem findMatchingToken(TokenItem startToken, TokenItem limitToken, 413 TokenID matchTokenID, String matchTokenImage, boolean backward) { 414 415 int depth = 0; 416 TokenID startTokenID = startToken.getTokenID(); 417 TokenContextPath startTokenContextPath = startToken.getTokenContextPath(); 418 String startText = startToken.getImage(); 419 420 TokenItem token = backward ? startToken.getPrevious() : startToken.getNext(); 422 423 while (token != null && token != limitToken) { 424 if (tokenEquals(token, matchTokenID, startTokenContextPath, 425 matchTokenImage) 426 ) { 427 if (depth-- == 0) { 428 return token; 429 } 430 431 } else if (tokenEquals(token, startTokenID, startTokenContextPath, 432 startText) 433 ) { 434 depth++; 435 } 436 437 token = backward ? token.getPrevious() : token.getNext(); 438 } 439 440 return null; 441 } 442 443 public TokenItem findMatchingToken(TokenItem startToken, TokenItem limitToken, 444 ImageTokenID matchTokenID, boolean backward) { 445 return findMatchingToken(startToken, limitToken, matchTokenID, 446 matchTokenID.getImage(), backward); 447 } 448 449 468 public TokenItem findAnyToken(TokenItem startToken, TokenItem limitToken, 469 TokenID[] tokenIDArray, TokenContextPath tokenContextPath, boolean backward) { 470 471 if (backward) { if (startToken != null && startToken == limitToken) { return null; 474 } 475 476 startToken = getPreviousToken(startToken); 477 478 if (limitToken != null) { 479 limitToken = limitToken.getPrevious(); 480 } 481 } 482 483 while (startToken != null && startToken != limitToken) { 484 for (int i = 0; i < tokenIDArray.length; i++) { 485 if (tokenEquals(startToken, tokenIDArray[i], tokenContextPath)) { 486 return startToken; 487 } 488 } 489 490 startToken = backward ? startToken.getPrevious() : startToken.getNext(); 491 } 492 493 return null; 494 } 495 496 499 public int getIndex(TokenItem token, TokenID[] tokenIDArray) { 500 for (int i = 0; i < tokenIDArray.length; i++) { 501 if (tokenEquals(token, tokenIDArray[i])) { 502 return i; 503 } 504 } 505 return -1; } 507 508 512 public FormatTokenPosition removeLineEndWhitespace(FormatTokenPosition pos) { 513 FormatTokenPosition endWS = findLineEndWhitespace(pos); 514 if (endWS == null || endWS.getToken() == null) { return findLineEnd(pos); 516 517 } else { int removeInd; 519 TokenItem token = endWS.getToken(); 520 int offset = endWS.getOffset(); 521 522 while (true) { 523 String text = token.getImage(); 524 int textLen = text.length(); 525 removeInd = offset; 526 while (offset < textLen) { 527 if (text.charAt(offset) == '\n') { 528 remove(token, removeInd, offset - removeInd); 529 return getPosition(token, removeInd); 530 } 531 532 offset++; 533 } 534 535 TokenItem nextToken = token.getNext(); 536 if (removeInd == 0) { 537 removeToken(token); 538 539 } else { remove(token, removeInd, textLen - removeInd); 541 } 542 543 token = nextToken; 544 if (token == null) { 545 return getPosition(null, 0); 546 } 547 offset = 0; 548 } 549 } 550 } 551 552 555 public char getChar(FormatTokenPosition pos) { 556 return pos.getToken().getImage().charAt(pos.getOffset()); 557 } 558 559 560 public boolean isLineStart(FormatTokenPosition pos) { 561 return isChainStartPosition(pos) || getChar(getPreviousPosition(pos)) == '\n'; 562 } 563 564 public boolean isNewLine(FormatTokenPosition pos) { 565 return (pos.getToken() != null) && getChar(pos) == '\n'; 566 } 567 568 } 569 | Popular Tags |