diff options
Diffstat (limited to 'platform/core-impl/src/com/intellij/lang/impl/PsiBuilderImpl.java')
-rw-r--r-- | platform/core-impl/src/com/intellij/lang/impl/PsiBuilderImpl.java | 58 |
1 files changed, 30 insertions, 28 deletions
diff --git a/platform/core-impl/src/com/intellij/lang/impl/PsiBuilderImpl.java b/platform/core-impl/src/com/intellij/lang/impl/PsiBuilderImpl.java index 3338241e705a..b68fa936af23 100644 --- a/platform/core-impl/src/com/intellij/lang/impl/PsiBuilderImpl.java +++ b/platform/core-impl/src/com/intellij/lang/impl/PsiBuilderImpl.java @@ -1152,64 +1152,66 @@ public class PsiBuilderImpl extends UserDataHolderBase implements PsiBuilder { } private void balanceWhiteSpaces() { - RelativeTokenTypesView wsTokens = null; - RelativeTokenTextView tokenTextGetter = null; + RelativeTokenTypesView wsTokens = new RelativeTokenTypesView(); + RelativeTokenTextView tokenTextGetter = new RelativeTokenTextView(); + int lastIndex = 0; for (int i = 1, size = myProduction.size() - 1; i < size; i++) { - final ProductionMarker item = myProduction.get(i); - + ProductionMarker item = myProduction.get(i); if (item instanceof StartMarker && ((StartMarker)item).myDoneMarker == null) { LOG.error(UNBALANCED_MESSAGE); } - final int prevProductionLexIndex = myProduction.get(i - 1).myLexemeIndex; - int idx = item.myLexemeIndex; - while (idx > prevProductionLexIndex && whitespaceOrComment(myLexTypes[idx - 1])) idx--; - final int wsStartIndex = idx; - + int prevProductionLexIndex = myProduction.get(i - 1).myLexemeIndex; + int wsStartIndex = Math.max(item.myLexemeIndex, lastIndex); + while (wsStartIndex > prevProductionLexIndex && whitespaceOrComment(myLexTypes[wsStartIndex - 1])) wsStartIndex--; int wsEndIndex = item.myLexemeIndex; while (wsEndIndex < myLexemeCount && whitespaceOrComment(myLexTypes[wsEndIndex])) wsEndIndex++; - if (wsTokens == null) wsTokens = new RelativeTokenTypesView(); - wsTokens.configure(wsStartIndex, wsEndIndex); - final boolean atEnd = wsStartIndex == 0 || wsEndIndex == myLexemeCount; - if (tokenTextGetter == null) tokenTextGetter = new RelativeTokenTextView(); - tokenTextGetter.configure(wsStartIndex); + if (wsStartIndex != wsEndIndex) { + wsTokens.configure(wsStartIndex, wsEndIndex); + tokenTextGetter.configure(wsStartIndex); + boolean atEnd = wsStartIndex == 0 || wsEndIndex == myLexemeCount; + item.myLexemeIndex = wsStartIndex + item.myEdgeTokenBinder.getEdgePosition(wsTokens, atEnd, tokenTextGetter); + } + else if (item.myLexemeIndex < wsStartIndex) { + item.myLexemeIndex = wsStartIndex; + } - item.myLexemeIndex = wsStartIndex + item.myEdgeTokenBinder.getEdgePosition(wsTokens, atEnd, tokenTextGetter); + lastIndex = item.myLexemeIndex; } } private final class RelativeTokenTypesView extends AbstractList<IElementType> { - private int start; - private int size; + private int myStart; + private int mySize; - private void configure(int _start, int _end) { - size = _end - _start; - start = _start; + private void configure(int start, int end) { + myStart = start; + mySize = end - start; } @Override - public IElementType get(final int index) { - return myLexTypes[start + index]; + public IElementType get(int index) { + return myLexTypes[myStart + index]; } @Override public int size() { - return size; + return mySize; } } private final class RelativeTokenTextView implements WhitespacesAndCommentsBinder.TokenTextGetter { - private int start; + private int myStart; - private void configure(int _start) { - start = _start; + private void configure(int start) { + myStart = start; } @Override - public CharSequence get(final int i) { - return myText.subSequence(myLexStarts[start + i], myLexStarts[start + i + 1]); + public CharSequence get(int i) { + return myText.subSequence(myLexStarts[myStart + i], myLexStarts[myStart + i + 1]); } } |