354
359
currentPos = offs;
362
// line starting from 0
363
public int getLineStartOffset(int linenum) throws BadLocationException {
364
Element map = this.getDefaultRootElement();
366
throw new BadLocationException("Negative line", -1);
367
} else if (linenum >= map.getElementCount()) {
368
throw new BadLocationException("No such line", this.getLength()+1);
370
Element lineElem = map.getElement(linenum);
371
return lineElem.getStartOffset();
375
public int getLineLength(int linenum) throws BadLocationException {
376
Element map = this.getDefaultRootElement();
378
throw new BadLocationException("Negative line", -1);
379
} else if (linenum >= map.getElementCount()) {
380
throw new BadLocationException("No such line", this.getLength()+1);
382
Element lineElem = map.getElement(linenum);
383
return lineElem.getEndOffset() - lineElem.getStartOffset();
388
* Change the indentation of a line. Any existing leading whitespace is replaced by
389
* the appropriate number of tab characters and padded with blank characters if
391
* @param linenum the line number (0-based)
392
* @param columns the number of columns by which to increase the indentation (if
393
* columns is greater than 0) or decrease the indentation (if columns is
395
* @throws BadLocationException if the specified line does not exist
397
public void changeIndentation(int linenum, int columns) throws BadLocationException {
398
if (columns == 0) return;
399
int lineStart = getLineStartOffset(linenum);
400
int lineLength = getLineLength(linenum);
401
String line = this.getText(lineStart, lineLength);
403
// determine current indentation and number of whitespace characters
406
int tabWidth = UserPreferences.getInstance().getTabWidth();
407
for (wsChars = 0; wsChars < line.length(); wsChars++) {
408
char c = line.charAt(wsChars);
411
} else if (c == '\t') {
412
indentation += tabWidth;
418
int newIndentation = indentation + columns;
419
if (newIndentation <= 0) {
420
this.remove(lineStart, wsChars);
424
// build whitespace string for new indentation
425
StringBuilder newWs = new StringBuilder(newIndentation / tabWidth + tabWidth - 1);
427
for (; ind + tabWidth <= newIndentation; ind += tabWidth) {
430
for (; ind < newIndentation; ind++) {
433
this.replace(lineStart, wsChars, newWs.toString(), null);
358
436
public Vector getKeywords(){
359
437
return this.keywords;