generate indent token if first line of file is indented (PY-4941)

This commit is contained in:
Dmitry Jemerov
2011-11-21 19:19:11 +01:00
parent ccb7e2fb63
commit 2bb62bb56b
2 changed files with 12 additions and 5 deletions
@@ -174,6 +174,9 @@ public class PythonIndentingProcessor extends MergingLexerAdapter {
adjustBraceLevel();
myLineHasSignificantTokens = false;
checkSignificantTokens();
if (isBaseAt(PyTokenTypes.SPACE)) {
processIndent(0, PyTokenTypes.SPACE);
}
}
private void adjustBraceLevel() {
@@ -254,7 +257,7 @@ public class PythonIndentingProcessor extends MergingLexerAdapter {
}
myLineHasSignificantTokens = false;
advanceBase();
processIndent(startPos);
processIndent(startPos, PyTokenTypes.LINE_BREAK);
}
else {
processInsignificantLineBreak(startPos, false);
@@ -281,7 +284,7 @@ public class PythonIndentingProcessor extends MergingLexerAdapter {
myTokenQueue.add(new PendingToken(PyTokenTypes.LINE_BREAK, startPos, end));
}
protected void processIndent(int whiteSpaceStart) {
protected void processIndent(int whiteSpaceStart, IElementType whitespaceTokenType) {
int lastIndent = myIndentStack.peek();
int indent = getNextLineIndent();
myLastNewLineIndent = indent;
@@ -292,7 +295,7 @@ public class PythonIndentingProcessor extends MergingLexerAdapter {
int whiteSpaceEnd = (getBaseTokenType() == null) ? super.getBufferEnd() : getBaseTokenStart();
if (indent > lastIndent) {
myIndentStack.push(indent);
myTokenQueue.add(new PendingToken(PyTokenTypes.LINE_BREAK, whiteSpaceStart, whiteSpaceEnd));
myTokenQueue.add(new PendingToken(whitespaceTokenType, whiteSpaceStart, whiteSpaceEnd));
int insertIndex = skipPrecedingCommentsWithIndent(indent, myTokenQueue.size() - 1);
int indentOffset = insertIndex == myTokenQueue.size() ? whiteSpaceEnd : myTokenQueue.get(insertIndex).getStart();
myTokenQueue.add(insertIndex, new PendingToken(PyTokenTypes.INDENT, indentOffset, indentOffset));
@@ -315,10 +318,10 @@ public class PythonIndentingProcessor extends MergingLexerAdapter {
}
myTokenQueue.add(insertIndex, new PendingToken(PyTokenTypes.DEDENT, dedentOffset, dedentOffset));
}
myTokenQueue.add(new PendingToken(PyTokenTypes.LINE_BREAK, whiteSpaceStart, whiteSpaceEnd));
myTokenQueue.add(new PendingToken(whitespaceTokenType, whiteSpaceStart, whiteSpaceEnd));
}
else {
myTokenQueue.add(new PendingToken(PyTokenTypes.LINE_BREAK, whiteSpaceStart, whiteSpaceEnd));
myTokenQueue.add(new PendingToken(whitespaceTokenType, whiteSpaceStart, whiteSpaceEnd));
}
}
@@ -196,6 +196,10 @@ public class PythonLexerTest extends PyLexerTestCase {
"Py:INDENT", "Py:PASS_KEYWORD", "Py:STATEMENT_BREAK", "Py:LINE_BREAK",
"Py:END_OF_LINE_COMMENT", "Py:DEDENT", "Py:LINE_BREAK");
}
public void testIndentAtStartOfFile() { // PY-4941
doTest(" a", "Py:SPACE", "Py:INDENT", "Py:IDENTIFIER");
}
private static void doTest(String text, String... expectedTokens) {
doLexerTest(text, new PythonIndentingLexer(), expectedTokens);