Cleanup (readability; formatting)

This commit is contained in:
Roman Shevchenko
2014-07-25 16:25:01 +02:00
parent 11848993ec
commit 52e7e0abb8
5 changed files with 62 additions and 160 deletions
@@ -1152,8 +1152,8 @@ public class PsiBuilderImpl extends UserDataHolderBase implements PsiBuilder {
}
private void balanceWhiteSpaces() {
RelativeTokenTypesView wsTokens = null;
RelativeTokenTextView tokenTextGetter = null;
RelativeTokenTypesView wsTokens = new RelativeTokenTypesView();
RelativeTokenTextView tokenTextGetter = new RelativeTokenTextView();
for (int i = 1, size = myProduction.size() - 1; i < size; i++) {
final ProductionMarker item = myProduction.get(i);
@@ -1162,54 +1162,50 @@ public class PsiBuilderImpl extends UserDataHolderBase implements PsiBuilder {
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 = item.myLexemeIndex;
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);
boolean atEnd = wsStartIndex == 0 || wsEndIndex == myLexemeCount;
item.myLexemeIndex = wsStartIndex + item.myEdgeTokenBinder.getEdgePosition(wsTokens, atEnd, tokenTextGetter);
}
}
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]);
}
}
@@ -39,8 +39,7 @@ import java.util.List;
* User: cdr
*/
public class DiffLog implements DiffTreeChangeBuilder<ASTNode,ASTNode> {
public DiffLog() {
}
public DiffLog() { }
private abstract static class LogEntry {
protected LogEntry() {
@@ -58,7 +57,6 @@ public class DiffLog implements DiffTreeChangeBuilder<ASTNode,ASTNode> {
entry.doActualPsiChange(file, astDiffBuilder);
}
file.subtreeChanged();
return astDiffBuilder.getEvent();
}
@@ -83,7 +81,6 @@ public class DiffLog implements DiffTreeChangeBuilder<ASTNode,ASTNode> {
@Override
public void nodeDeleted(@NotNull ASTNode oldParent, @NotNull ASTNode oldNode) {
myEntries.add(new DeleteEntry(oldParent, oldNode));
}
@Override
@@ -126,14 +123,12 @@ public class DiffLog implements DiffTreeChangeBuilder<ASTNode,ASTNode> {
astDiffBuilder.nodeReplaced(oldNode, newNode);
/////////////////
((TreeElement)newNode).clearCaches();
if (!(newNode instanceof FileElement)) {
((CompositeElement)newNode.getTreeParent()).subtreeChanged();
}
DebugUtil.checkTreeStructure(parent);
}
}
@@ -168,7 +163,6 @@ public class DiffLog implements DiffTreeChangeBuilder<ASTNode,ASTNode> {
((CompositeElement)parent).subtreeChanged();
DebugUtil.checkTreeStructure(parent);
}
}
@@ -222,7 +216,6 @@ public class DiffLog implements DiffTreeChangeBuilder<ASTNode,ASTNode> {
DebugUtil.checkTreeStructure(myOldParent);
}
}
private static PsiElement getPsi(ASTNode node, PsiFile file) {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,16 +18,14 @@ package com.intellij.lang;
import com.intellij.lang.impl.PsiBuilderImpl;
import com.intellij.lexer.Lexer;
import com.intellij.lexer.LexerBase;
import com.intellij.openapi.fileTypes.PlainTextParserDefinition;
import com.intellij.openapi.project.Project;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.TokenType;
import com.intellij.psi.impl.DebugUtil;
import com.intellij.psi.impl.source.tree.ASTStructure;
import com.intellij.psi.tree.*;
import com.intellij.testFramework.LightPlatformTestCase;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.testFramework.LightPlatformLangTestCase;
import com.intellij.testFramework.PlatformTestUtil;
import com.intellij.util.ThreeState;
import com.intellij.util.diff.DiffTree;
import com.intellij.util.diff.DiffTreeChangeBuilder;
@@ -36,12 +34,9 @@ import com.intellij.util.diff.ShallowNodeComparator;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.List;
public class PsiBuilderQuickTest extends LightPlatformTestCase {
public class PsiBuilderQuickTest extends LightPlatformLangTestCase {
private static final IFileElementType ROOT = new IFileElementType("ROOT", Language.ANY);
private static final IElementType LETTER = new IElementType("LETTER", Language.ANY);
@@ -57,11 +52,6 @@ public class PsiBuilderQuickTest extends LightPlatformTestCase {
private static final TokenSet WHITESPACE_SET = TokenSet.create(TokenType.WHITE_SPACE);
private static final TokenSet COMMENT_SET = TokenSet.create(COMMENT);
@SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors")
public PsiBuilderQuickTest() {
PlatformTestCase.initPlatformLangPrefix();
}
public void testPlain() {
doTest("a<<b",
new Parser() {
@@ -478,25 +468,14 @@ public class PsiBuilderQuickTest extends LightPlatformTestCase {
" PsiElement(OTHER)('}')\n");
}
@SuppressWarnings("ConstantConditions")
private static PsiBuilderImpl createBuilder(CharSequence text) {
ParserDefinition parserDefinition = new ParserDefinition() {
ParserDefinition parserDefinition = new PlainTextParserDefinition() {
@NotNull
@Override
public Lexer createLexer(Project project) {
return new MyTestLexer();
}
@Override
public PsiParser createParser(Project project) {
return null;
}
@Override
public IFileElementType getFileNodeType() {
return null;
}
@NotNull
@Override
public TokenSet getWhitespaceTokens() {
@@ -508,28 +487,6 @@ public class PsiBuilderQuickTest extends LightPlatformTestCase {
public TokenSet getCommentTokens() {
return COMMENT_SET;
}
@NotNull
@Override
public TokenSet getStringLiteralElements() {
return null;
}
@NotNull
@Override
public PsiElement createElement(ASTNode node) {
return null;
}
@Override
public PsiFile createFile(FileViewProvider viewProvider) {
return null;
}
@Override
public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
return null;
}
};
return new PsiBuilderImpl(getProject(), null, parserDefinition, parserDefinition.createLexer(getProject()), null, text, null, null);
}
@@ -594,75 +551,21 @@ public class PsiBuilderQuickTest extends LightPlatformTestCase {
}
private static void doFailTest(@NonNls final String text, final Parser parser, @NonNls final String expected) {
final PrintStream std = System.err;
//noinspection IOResourceOpenedButNotSafelyClosed
System.setErr(new PrintStream(new NullStream()));
try {
try {
ParserDefinition parserDefinition = new ParserDefinition() {
@NotNull
@Override
public Lexer createLexer(Project project) {
return null;
}
@Override
public PsiParser createParser(Project project) {
return null;
}
@Override
public IFileElementType getFileNodeType() {
return null;
}
@NotNull
@Override
public TokenSet getWhitespaceTokens() {
return TokenSet.EMPTY;
}
@NotNull
@Override
public TokenSet getCommentTokens() {
return TokenSet.EMPTY;
}
@NotNull
@Override
public TokenSet getStringLiteralElements() {
return null;
}
@NotNull
@Override
public PsiElement createElement(ASTNode node) {
return null;
}
@Override
public PsiFile createFile(FileViewProvider viewProvider) {
return null;
}
@Override
public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) {
return null;
}
};
final PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(parserDefinition, new MyTestLexer(),text);
builder.setDebugMode(true);
parser.parse(builder);
builder.getLightTree();
fail("should fail");
PlatformTestUtil.withStdErrSuppressed(new Runnable() {
@Override
public void run() {
try {
PsiBuilder builder = PsiBuilderFactory.getInstance().createBuilder(new PlainTextParserDefinition(), new MyTestLexer(), text);
builder.setDebugMode(true);
parser.parse(builder);
builder.getLightTree();
fail("should fail");
}
catch (AssertionError e) {
assertEquals(expected, e.getMessage());
}
}
catch (AssertionError e) {
assertEquals(expected, e.getMessage());
}
}
finally {
System.setErr(std);
}
});
}
private static class MyTestLexer extends LexerBase {
@@ -719,11 +622,6 @@ public class PsiBuilderQuickTest extends LightPlatformTestCase {
}
}
private static class NullStream extends OutputStream {
@Override
public void write(final int b) throws IOException { }
}
private static class MyChameleon1Type extends MyLazyElementType {
private final IElementType myCHAMELEON_2;
@@ -63,9 +63,7 @@ import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.InvocationEvent;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.*;
import java.lang.ref.SoftReference;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
@@ -826,4 +824,21 @@ public class PlatformTestUtil {
ReflectionUtil.resetField(Charset.class, Charset.class, "defaultCharset");
System.setProperty("file.encoding", encoding);
}
public static void withStdErrSuppressed(@NotNull Runnable r) {
PrintStream std = System.err;
System.setErr(new PrintStream(NULL));
try {
r.run();
}
finally {
System.setErr(std);
}
}
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
private static final OutputStream NULL = new OutputStream() {
@Override
public void write(int b) throws IOException { }
};
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2014 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util.diff;
import com.intellij.openapi.util.Ref;
@@ -39,7 +38,6 @@ public class DiffTree<OT, NT> {
final FlyweightCapableTreeStructure<NT> newTree,
final ShallowNodeComparator<OT, NT> comparator,
final DiffTreeChangeBuilder<OT, NT> consumer) {
myOldTree = oldTree;
myNewTree = newTree;
myComparator = comparator;
@@ -53,7 +51,7 @@ public class DiffTree<OT, NT> {
new DiffTree<OT, NT>(oldTree, newTree, comparator, consumer).build(oldTree.getRoot(), newTree.getRoot(), 0);
}
private static enum CompareResult {
private enum CompareResult {
EQUAL, // 100% equal
DRILL_DOWN_NEEDED, // element types are equal, but elements are composite
TYPE_ONLY, // only element types are equal
@@ -146,6 +144,7 @@ public class DiffTree<OT, NT> {
newIndex++;
continue;
}
CompareResult c12 = looksEqual(comparator, oldChild1, newChild2);
if (c12 == CompareResult.EQUAL || c12 == CompareResult.DRILL_DOWN_NEEDED || c12 == CompareResult.TYPE_ONLY) {
myConsumer.nodeInserted(oldNode, newChild1, newIndex);
@@ -170,6 +169,7 @@ public class DiffTree<OT, NT> {
oldIndex++;
continue;
}
myConsumer.nodeReplaced(oldChild1, newChild1);
oldIndex++;
newIndex++;