Fix stubs for JetModifierList, store modifier tokens in stub

This commit is contained in:
Pavel V. Talanov
2014-03-31 14:09:17 +04:00
parent b7d029368e
commit 35121643b1
8 changed files with 218 additions and 14 deletions
@@ -0,0 +1,32 @@
/*
* Copyright 2010-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub;
import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
public class JetDeclarationModifierList extends JetModifierList {
public JetDeclarationModifierList(@NotNull ASTNode node) {
super(node);
}
public JetDeclarationModifierList(@NotNull PsiJetModifierListStub stub) {
super(stub, JetStubElementTypes.MODIFIER_LIST);
}
}
@@ -19,12 +19,11 @@ package org.jetbrains.jet.lang.psi;
import com.google.common.collect.Lists;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.stubs.IStubElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetNodeTypes;
import org.jetbrains.jet.lang.psi.stubs.PsiJetPlaceHolderStub;
import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
import org.jetbrains.jet.lexer.JetKeywordToken;
import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub;
import org.jetbrains.jet.lexer.JetModifierKeywordToken;
import org.jetbrains.jet.lexer.JetToken;
@@ -32,13 +31,14 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class JetModifierList extends JetElementImplStub<PsiJetPlaceHolderStub> {
public JetModifierList(@NotNull ASTNode node) {
super(node);
public abstract class JetModifierList extends JetElementImplStub<PsiJetModifierListStub> {
public JetModifierList(@NotNull PsiJetModifierListStub stub, @NotNull IStubElementType nodeType) {
super(stub, nodeType);
}
public JetModifierList(@NotNull PsiJetPlaceHolderStub stub) {
super(stub, JetStubElementTypes.MODIFIER_LIST);
public JetModifierList(@NotNull ASTNode node) {
super(node);
}
@Override
@@ -0,0 +1,32 @@
/*
* Copyright 2010-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub;
import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
public class JetPrimaryConstructorModifierList extends JetModifierList {
public JetPrimaryConstructorModifierList(@NotNull ASTNode node) {
super(node);
}
public JetPrimaryConstructorModifierList(@NotNull PsiJetModifierListStub stub) {
super(stub, JetStubElementTypes.PRIMARY_CONSTRUCTOR_MODIFIER_LIST);
}
}
@@ -0,0 +1,26 @@
/*
* Copyright 2010-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.psi.stubs;
import com.intellij.psi.stubs.StubElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetModifierList;
import org.jetbrains.jet.lexer.JetModifierKeywordToken;
public interface PsiJetModifierListStub extends StubElement<JetModifierList> {
boolean hasModifier(@NotNull JetModifierKeywordToken modifierToken);
}
@@ -16,12 +16,39 @@
package org.jetbrains.jet.lang.psi.stubs.elements;
import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.stubs.StubInputStream;
import com.intellij.psi.stubs.StubOutputStream;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetModifierList;
import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub;
import org.jetbrains.jet.lang.psi.stubs.impl.PsiJetModifierListStubImpl;
public class JetModifierListElementType extends JetPlaceHolderStubElementType<JetModifierList> {
public JetModifierListElementType(@NotNull @NonNls String debugName) {
super(debugName, JetModifierList.class);
import java.io.IOException;
import static org.jetbrains.jet.lang.psi.stubs.impl.PsiJetModifierListStubImpl.computeMaskFromPsi;
public class JetModifierListElementType<T extends JetModifierList> extends JetStubElementType<PsiJetModifierListStub, T> {
public JetModifierListElementType(@NotNull @NonNls String debugName, @NotNull Class<T> psiClass) {
super(debugName, psiClass, PsiJetModifierListStub.class);
}
@Override
public PsiJetModifierListStub createStub(@NotNull T psi, StubElement parentStub) {
return new PsiJetModifierListStubImpl(parentStub, computeMaskFromPsi(psi), this);
}
@Override
public void serialize(@NotNull PsiJetModifierListStub stub, @NotNull StubOutputStream dataStream) throws IOException {
int mask = ((PsiJetModifierListStubImpl) stub).getMask();
dataStream.writeVarInt(mask);
}
@NotNull
@Override
public PsiJetModifierListStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
int mask = dataStream.readVarInt();
return new PsiJetModifierListStubImpl(parentStub, mask, this);
}
}
@@ -27,7 +27,7 @@ public interface JetStubElementTypes {
JetClassElementType ENUM_ENTRY = new JetClassElementType("ENUM_ENTRY");
JetObjectElementType OBJECT_DECLARATION = new JetObjectElementType("OBJECT_DECLARATION");
JetPlaceHolderStubElementType<JetClassObject> CLASS_OBJECT =
new JetPlaceHolderStubElementType<JetClassObject>("CLASS_OBJECT", JetClassObject.class);
new JetPlaceHolderStubElementType<JetClassObject>("CLASS_OBJECT", JetClassObject.class);
JetParameterElementType VALUE_PARAMETER = new JetParameterElementType("VALUE_PARAMETER");
JetPlaceHolderStubElementType<JetParameterList> VALUE_PARAMETER_LIST =
@@ -49,8 +49,11 @@ public interface JetStubElementTypes {
JetImportDirectiveElementType IMPORT_DIRECTIVE = new JetImportDirectiveElementType("IMPORT_DIRECTIVE");
JetModifierListElementType MODIFIER_LIST = new JetModifierListElementType("MODIFIER_LIST");
JetModifierListElementType PRIMARY_CONSTRUCTOR_MODIFIER_LIST = new JetModifierListElementType("PRIMARY_CONSTRUCTOR_MODIFIER_LIST");
JetModifierListElementType<JetDeclarationModifierList> MODIFIER_LIST =
new JetModifierListElementType<JetDeclarationModifierList>("MODIFIER_LIST", JetDeclarationModifierList.class);
JetModifierListElementType<JetPrimaryConstructorModifierList> PRIMARY_CONSTRUCTOR_MODIFIER_LIST =
new JetModifierListElementType<JetPrimaryConstructorModifierList>("PRIMARY_CONSTRUCTOR_MODIFIER_LIST", JetPrimaryConstructorModifierList.class);
JetPlaceHolderStubElementType<JetTypeConstraintList> TYPE_CONSTRAINT_LIST =
new JetPlaceHolderStubElementType<JetTypeConstraintList>("TYPE_CONSTRAINT_LIST", JetTypeConstraintList.class);
@@ -0,0 +1,79 @@
/*
* Copyright 2010-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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.jet.lang.psi.stubs.impl;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetModifierList;
import org.jetbrains.jet.lang.psi.stubs.PsiJetModifierListStub;
import org.jetbrains.jet.lang.psi.stubs.elements.JetModifierListElementType;
import org.jetbrains.jet.lexer.JetModifierKeywordToken;
import static org.jetbrains.jet.lexer.JetTokens.MODIFIER_KEYWORDS_ARRAY;
public class PsiJetModifierListStubImpl extends StubBase<JetModifierList> implements PsiJetModifierListStub {
static {
assert MODIFIER_KEYWORDS_ARRAY.length <= 32 : "Current implementation depends on the ability to represent modifier list as bit mask";
}
public static int computeMaskFromPsi(@NotNull JetModifierList modifierList) {
int mask = 0;
JetModifierKeywordToken[] orderedKeywords = MODIFIER_KEYWORDS_ARRAY;
for (int i = 0; i < orderedKeywords.length; i++) {
JetModifierKeywordToken modifierKeywordToken = orderedKeywords[i];
if (modifierList.hasModifier(modifierKeywordToken)) {
mask |= 1 << i;
}
}
return mask;
}
private final int mask;
public PsiJetModifierListStubImpl(StubElement parent, int mask, @NotNull JetModifierListElementType<?> elementType) {
super(parent, elementType);
this.mask = mask;
}
@Override
public boolean hasModifier(@NotNull JetModifierKeywordToken modifierToken) {
int index = ArrayUtil.indexOf(MODIFIER_KEYWORDS_ARRAY, modifierToken);
assert index >= 0 : "All JetModifierKeywordTokens should present in MODIFIER_KEYWORDS_ARRAY";
return (mask & (1 << index)) != 0;
}
public int getMask() {
return mask;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString());
sb.append("[");
for (JetModifierKeywordToken modifierKeyword : MODIFIER_KEYWORDS_ARRAY) {
if (hasModifier(modifierKeyword)) {
sb.append(modifierKeyword.getValue()).append(" ");
}
}
sb.append("]");
return sb.toString();
}
}
@@ -169,6 +169,11 @@ public interface JetTokens {
CATCH_KEYWORD, FINALLY_KEYWORD, OUT_KEYWORD, FINAL_KEYWORD, VARARG_KEYWORD, REIFIED_KEYWORD
);
/*
This array is used in stub serialization:
1. Do not change order.
2. If you add an entry or change order, increase stub version.
*/
JetModifierKeywordToken[] MODIFIER_KEYWORDS_ARRAY =
new JetModifierKeywordToken[] {
ABSTRACT_KEYWORD, ENUM_KEYWORD, OPEN_KEYWORD, INNER_KEYWORD, ANNOTATION_KEYWORD, OVERRIDE_KEYWORD, PRIVATE_KEYWORD,