}
return balanced;
}
private void handleTab(IDocument document, DocumentCommand command) throws BadLocationException {
PySelection ps = new PySelection(document, command.offset);
//it is a tab
String lineContentsToCursor = ps.getLineContentsToCursor();
int currSize = lineContentsToCursor.length();
int cursorLine = ps.getCursorLine();
//current line is empty
if (lineContentsToCursor.trim().length() == 0) {
String nextLine = ps.getLine(cursorLine + 1);
String prevLine = ps.getLine(cursorLine - 1);
boolean forceTryOnNext = false;
if (prevLine.trim().length() == 0) {
//previous line is empty, so, if the next line has contents, use it to make the match.
if (nextLine.trim().length() > 0) {
forceTryOnNext = true;
}
}
if (forceTryOnNext || nextLine.trim().startsWith("@") || ps.matchesFunctionLine(nextLine)) {
int firstCharPosition = PySelection.getFirstCharPosition(nextLine);
if (currSize < firstCharPosition) {
String txt = nextLine.substring(currSize, firstCharPosition);
//as it's the same indentation from the next line, we don't have to applyDefaultForTab.
command.text = txt;
return;
}
}
}
if (cursorLine > 0) {
//this is to know which would be expected if it was a new line in the previous line
//(so that we know the 'expected' output
IRegion prevLineInfo = document.getLineInformation(cursorLine - 1);
int prevLineEndOffset = prevLineInfo.getOffset() + prevLineInfo.getLength();
String prevExpectedIndent = autoIndentSameAsPrevious(document, prevLineEndOffset, "\n", false);
String txt = prevExpectedIndent;
Tuple<String, Boolean> prevLineTup = autoIndentNewline(document, 0, txt, prevLineEndOffset);
txt = prevLineTup.o1;
txt = txt.substring(1);//remove the newline
prevExpectedIndent = prevExpectedIndent.substring(1);
if (txt.length() > 0) {
//now, we should not apply that indent if we are already at the 'max' indent in this line
//(or better: we should go to that max if it would pass it)
int sizeExpected = txt.length();
int sizeApplied = currSize + sizeExpected;
if (currSize >= sizeExpected) {
//ok, we already passed what we expected from the indentation, so, let's indent
//to the next 'expected' position...
boolean applied = false;
//handle within parenthesis
if (prevLineTup.o2) {
int len = sizeApplied - sizeExpected;
if (prevExpectedIndent.length() > len) {
command.text = prevExpectedIndent.substring(len);
applied = true;
}
}
if (!applied) {
applyDefaultForTab(command, currSize);
}
} else if (sizeExpected == sizeApplied) {
if (command.length == 0) {
ps.deleteSpacesAfter(command.offset);
}
command.text = txt;
} else if (sizeApplied > sizeExpected) {
ps.deleteSpacesAfter(command.offset);
command.text = txt.substring(0, sizeExpected - currSize);
}
} else {
applyDefaultForTab(command, currSize);
}