This commit is contained in:
Roman Shevchenko
2012-09-21 15:11:54 +02:00
parent ef2cc29b13
commit 7b0a1d6257
4 changed files with 68 additions and 65 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -35,12 +35,7 @@ import java.util.List;
public class IElementType {
private static final Logger LOG = Logger.getInstance("#com.intellij.psi.tree.IElementType");
public static final short FIRST_TOKEN_INDEX = 1;
private static short ourCounter = FIRST_TOKEN_INDEX;
private static final short MAX_INDEXED_TYPES = 15000;
private static final List<IElementType> ourRegistry = new ArrayList<IElementType>(700);
private final short myIndex;
public static final IElementType[] EMPTY_ARRAY = new IElementType[0];
/**
* Default enumeration predicate which matches all token types.
@@ -54,34 +49,16 @@ public class IElementType {
}
};
static int getAllocatedTypesCount() {
return ourCounter;
}
public static final short FIRST_TOKEN_INDEX = 1;
public static final short MAX_INDEXED_TYPES = 15000;
public static final IElementType[] EMPTY_ARRAY = new IElementType[0];
private final String myDebugName;
private static short ourCounter = FIRST_TOKEN_INDEX;
private static final List<IElementType> ourRegistry = new ArrayList<IElementType>(700);
private final short myIndex;
@NotNull private final String myDebugName;
@NotNull private final Language myLanguage;
/**
* Enumerates all registered token types which match the specified predicate.
*
* @param p the predicate which should be matched by the element types.
* @return the array of matching element types.
*/
public static IElementType[] enumerate(Predicate p) {
List<IElementType> matches = new ArrayList<IElementType>();
IElementType[] copy;
synchronized (ourRegistry) {
copy = ourRegistry.toArray(new IElementType[ourRegistry.size()]);
}
for (IElementType value : copy) {
if (p.matches(value)) {
matches.add(value);
}
}
return matches.toArray(new IElementType[matches.size()]);
}
/**
* Creates and registers a new element type for the specified language.
*
@@ -92,7 +69,7 @@ public class IElementType {
this(debugName, language, true);
}
protected IElementType(String debugName, Language language, final boolean register) {
protected IElementType(@NotNull @NonNls String debugName, @Nullable Language language, boolean register) {
myDebugName = debugName;
myLanguage = language == null ? Language.ANY : language;
if (register) {
@@ -132,15 +109,41 @@ public class IElementType {
return myDebugName;
}
/**
* Controls whitespace balancing behavior of PsiBuilder.
* <p>By default, empty composite elements (containing no children) are bounded to the right (previous) neighbour, forming following tree:
* <pre>
* [previous_element]
* [whitespace]
* [empty_element]
* &lt;empty&gt;
* [next_element]
* </pre>
* <p>Left-bound elements are bounded to the left (next) neighbour instead:
* <pre>
* [previous_element]
* [empty_element]
* &lt;empty&gt;
* [whitespace]
* [next_element]
* </pre>
* <p>See com.intellij.lang.impl.PsiBuilderImpl.prepareLightTree() for details.
* @return true if empty elements of this type should be bound to the left.
*/
public boolean isLeftBound() {
return false;
}
/**
* Returns the element type registered at the specified index.
*
* @param idx the index for which the element type should be returned.
* @return the element type at the specified index.
* @throws IndexOutOfBoundsException if the index is out of registered elements' range.
*/
public static IElementType find(short idx) {
synchronized (ourRegistry) {
if (idx == 0) return ourRegistry.get(0); // We've changed FIRST_TOKEN_INDEX from 0 to 1. This is just for old plugins to avoid crashes.
if (idx == 0) return ourRegistry.get(0); // We've changed FIRST_TOKEN_INDEX from 0 to 1. This is just for old plugins to avoid crashes.
return ourRegistry.get(idx - FIRST_TOKEN_INDEX);
}
}
@@ -154,28 +157,29 @@ public class IElementType {
boolean matches(IElementType type);
}
static short getAllocatedTypesCount() {
return ourCounter;
}
/**
* Controls whitespace balancing behavior of PsiBuilder.
* <p>By default, empty composite elements (containing no children) are bounded to the right neighbour, forming following tree:
* <pre>
* [previous_element]
* [whitespace]
* [empty_element]
* &lt;empty&gt;
* [next_element]
* </pre>
* <p>Left-bound elements are bounded to the left neighbour instead:
* <pre>
* [previous_element]
* [empty_element]
* &lt;empty&gt;
* [whitespace]
* [next_element]
* </pre>
* <p>See {@linkplain com.intellij.lang.impl.PsiBuilderImpl#prepareLightTree()} for details.
* @return true if empty elements of this type should be bound to the left.
* Enumerates all registered token types which match the specified predicate.
*
* @param p the predicate which should be matched by the element types.
* @return the array of matching element types.
*/
public boolean isLeftBound() {
return false;
@NotNull
public static IElementType[] enumerate(@NotNull Predicate p) {
IElementType[] copy;
synchronized (ourRegistry) {
copy = ourRegistry.toArray(new IElementType[ourRegistry.size()]);
}
List<IElementType> matches = new ArrayList<IElementType>();
for (IElementType value : copy) {
if (p.matches(value)) {
matches.add(value);
}
}
return matches.toArray(new IElementType[matches.size()]);
}
}
@@ -25,7 +25,7 @@ import java.util.Arrays;
public class TokenSet {
public static final TokenSet EMPTY = new TokenSet();
private final boolean[] mySet = new boolean[IElementType.getAllocatedTypesCount()] ;
private final boolean[] mySet = new boolean[IElementType.getAllocatedTypesCount()];
private volatile IElementType[] myTypes;
/**
@@ -48,7 +48,7 @@ public class TokenSet {
types[count++] = IElementType.find(i);
}
}
myTypes = types;
}
@@ -127,7 +127,7 @@ public class TokenSet {
public TokenSet minus(TokenSet t) {
TokenSet set = new TokenSet();
for (int i = 0; i < mySet.length; i++) {
set.mySet [i] = mySet [i] && (i >= t.mySet.length || !t.mySet[i]);
set.mySet[i] = mySet[i] && (i >= t.mySet.length || !t.mySet[i]);
}
return set;
}
@@ -135,7 +135,7 @@ public class TokenSet {
public static TokenSet not(TokenSet t) {
TokenSet set = new TokenSet();
for (int i = 0; i < t.mySet.length; i++) {
set.mySet [i] = (i >= t.mySet.length || !t.mySet[i]);
set.mySet[i] = !t.mySet[i];
}
return set;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 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.
@@ -156,7 +156,7 @@ public abstract class AbstractTreeNode<T> extends PresentableNodeDescriptor impl
}
@Nullable
public String toTestString(Queryable.PrintInfo printInfo) {
public String toTestString(@Nullable Queryable.PrintInfo printInfo) {
if (getValue() instanceof Queryable) {
String text = Queryable.Util.print((Queryable)getValue(), printInfo, this);
if (text != null) return text;
@@ -98,7 +98,7 @@ public class PlatformTestUtil {
}
@Nullable
protected static String toString(Object node, @Nullable Queryable.PrintInfo printInfo) {
protected static String toString(@Nullable Object node, @Nullable Queryable.PrintInfo printInfo) {
if (node instanceof AbstractTreeNode) {
if (printInfo != null) {
return ((AbstractTreeNode)node).toTestString(printInfo);
@@ -492,10 +492,9 @@ public class PlatformTestUtil {
String logMessage = message;
if (duration > expectedOnMyMachine) {
int percentage = (int)(100.0 * (duration - expectedOnMyMachine) / expectedOnMyMachine);
logMessage += ". (" + percentage + "% longer).";
logMessage += ". (" + percentage + "% longer)";
}
logMessage += " Expected: " + expectedOnMyMachine + "." +
" Actual: " + duration + "." + Timings.getStatistics() ;
logMessage += ". Expected: " + expectedOnMyMachine + ". Actual: " + duration + "." + Timings.getStatistics() ;
if (duration < expectedOnMyMachine) {
int percentage = (int)(100.0 * (expectedOnMyMachine - duration) / expectedOnMyMachine);
logMessage = "(" + percentage + "% faster). " + logMessage;