getElement() and getRangeInElement() are expected to return non-nullable values

This commit is contained in:
Nikolay Krasko
2012-02-24 19:29:21 +04:00
parent 7f97f9710e
commit 782ea7b1c3
8 changed files with 87 additions and 66 deletions
@@ -207,8 +207,12 @@ public class JetParsing extends AbstractJetParsing {
advance(); // DOT advance(); // DOT
reference = mark(); reference = mark();
expect(IDENTIFIER, "Qualified name must be a '.'-separated identifier list", TokenSet.create(AS_KEYWORD, DOT, SEMICOLON)); if (expect(IDENTIFIER, "Qualified name must be a '.'-separated identifier list", TokenSet.create(AS_KEYWORD, DOT, SEMICOLON))) {
reference.done(REFERENCE_EXPRESSION); reference.done(REFERENCE_EXPRESSION);
}
else {
reference.drop();
}
PsiBuilder.Marker precede = qualifiedName.precede(); PsiBuilder.Marker precede = qualifiedName.precede();
qualifiedName.done(DOT_QUALIFIED_EXPRESSION); qualifiedName.done(DOT_QUALIFIED_EXPRESSION);
@@ -817,7 +821,7 @@ public class JetParsing extends AbstractJetParsing {
errorAndAdvance("Expecting 'val' or 'var'"); errorAndAdvance("Expecting 'val' or 'var'");
} }
boolean typeParametersDeclared = at(LT) ? parseTypeParameterList(TokenSet.create(IDENTIFIER, EQ, COLON, SEMICOLON)) : false; boolean typeParametersDeclared = at(LT) && parseTypeParameterList(TokenSet.create(IDENTIFIER, EQ, COLON, SEMICOLON));
TokenSet propertyNameFollow = TokenSet.create(COLON, EQ, LBRACE, SEMICOLON, VAL_KEYWORD, VAR_KEYWORD, FUN_KEYWORD, CLASS_KEYWORD); TokenSet propertyNameFollow = TokenSet.create(COLON, EQ, LBRACE, SEMICOLON, VAL_KEYWORD, VAR_KEYWORD, FUN_KEYWORD, CLASS_KEYWORD);
@@ -1236,8 +1240,11 @@ public class JetParsing extends AbstractJetParsing {
} }
PsiBuilder.Marker reference = mark(); PsiBuilder.Marker reference = mark();
expect(IDENTIFIER, "Expecting type parameter name", TokenSet.orSet(TokenSet.create(COLON, COMMA), TYPE_REF_FIRST)); if (expect(IDENTIFIER, "Expecting type parameter name", TokenSet.orSet(TokenSet.create(COLON, COMMA), TYPE_REF_FIRST))) {
reference.done(REFERENCE_EXPRESSION); reference.done(REFERENCE_EXPRESSION);
} else {
reference.drop();
}
expect(COLON, "Expecting ':' before the upper bound", TYPE_REF_FIRST); expect(COLON, "Expecting ':' before the upper bound", TYPE_REF_FIRST);
@@ -1394,8 +1401,13 @@ public class JetParsing extends AbstractJetParsing {
PsiBuilder.Marker reference = mark(); PsiBuilder.Marker reference = mark();
while (true) { while (true) {
expect(IDENTIFIER, "Expecting type name", TokenSet.orSet(JetExpressionParsing.EXPRESSION_FIRST, JetExpressionParsing.EXPRESSION_FOLLOW)); if (expect(IDENTIFIER, "Expecting type name", TokenSet.orSet(JetExpressionParsing.EXPRESSION_FIRST, JetExpressionParsing.EXPRESSION_FOLLOW))) {
reference.done(REFERENCE_EXPRESSION); reference.done(REFERENCE_EXPRESSION);
}
else {
reference.drop();
break;
}
parseTypeArgumentList(-1); parseTypeArgumentList(-1);
if (!at(DOT)) { if (!at(DOT)) {
@@ -81,27 +81,27 @@ public class JetSimpleNameExpression extends JetReferenceExpression {
@Nullable @IfNotParsed @Nullable @IfNotParsed
public String getReferencedName() { public String getReferencedName() {
PsiElement referencedNameElement = getReferencedNameElement(); String text = getReferencedNameElement().getNode().getText();
if (referencedNameElement == null) {
return null;
}
String text = referencedNameElement.getNode().getText();
return text != null ? JetPsiUtil.unquoteIdentifierOrFieldReference(text) : null; return text != null ? JetPsiUtil.unquoteIdentifierOrFieldReference(text) : null;
} }
@Nullable @IfNotParsed @NotNull
public PsiElement getReferencedNameElement() { public PsiElement getReferencedNameElement() {
PsiElement element = findChildByType(REFERENCE_TOKENS); PsiElement element = findChildByType(REFERENCE_TOKENS);
if (element == null) { if (element == null) {
element = findChildByType(JetExpressionParsing.ALL_OPERATIONS); element = findChildByType(JetExpressionParsing.ALL_OPERATIONS);
} }
return element;
if (element != null) {
return element;
}
return this;
} }
@Nullable @IfNotParsed @Nullable @IfNotParsed
public IElementType getReferencedNameElementType() { public IElementType getReferencedNameElementType() {
PsiElement element = getReferencedNameElement(); return getReferencedNameElement().getNode().getElementType();
return element == null ? null : element.getNode().getElementType();
} }
@NotNull @NotNull
@@ -194,27 +194,31 @@ public class ImportsResolver {
} }
JetExpression selectorExpression = importedReference.getSelectorExpression(); JetExpression selectorExpression = importedReference.getSelectorExpression();
assert selectorExpression instanceof JetSimpleNameExpression;
JetSimpleNameExpression selector = (JetSimpleNameExpression) selectorExpression; if (selectorExpression instanceof JetSimpleNameExpression) {
JetSimpleNameExpression lastReference = getLastReference(receiverExpression); JetSimpleNameExpression selector = (JetSimpleNameExpression) selectorExpression;
if (lastReference == null || !canImportMembersFrom(declarationDescriptors, lastReference)) { JetSimpleNameExpression lastReference = getLastReference(receiverExpression);
return Collections.emptyList(); if (lastReference == null || !canImportMembersFrom(declarationDescriptors, lastReference)) {
} return Collections.emptyList();
Collection<? extends DeclarationDescriptor> result;
for (DeclarationDescriptor declarationDescriptor : declarationDescriptors) {
if (declarationDescriptor instanceof NamespaceDescriptor) {
result = lookupDescriptorsForSimpleNameReference(selector, ((NamespaceDescriptor) declarationDescriptor).getMemberScope(), true);
if (!result.isEmpty()) return result;
} }
if (declarationDescriptor instanceof ClassDescriptor) {
result = lookupObjectMembers((ClassDescriptor) declarationDescriptor, selector); Collection<? extends DeclarationDescriptor> result;
if (!result.isEmpty()) return result; for (DeclarationDescriptor declarationDescriptor : declarationDescriptors) {
} if (declarationDescriptor instanceof NamespaceDescriptor) {
if (declarationDescriptor instanceof VariableDescriptor) { result = lookupDescriptorsForSimpleNameReference(selector, ((NamespaceDescriptor) declarationDescriptor).getMemberScope(), true);
result = lookupVariableMembers((VariableDescriptor) declarationDescriptor, selector); if (!result.isEmpty()) return result;
if (!result.isEmpty()) return result; }
if (declarationDescriptor instanceof ClassDescriptor) {
result = lookupObjectMembers((ClassDescriptor) declarationDescriptor, selector);
if (!result.isEmpty()) return result;
}
if (declarationDescriptor instanceof VariableDescriptor) {
result = lookupVariableMembers((VariableDescriptor) declarationDescriptor, selector);
if (!result.isEmpty()) return result;
}
} }
} }
return Collections.emptyList(); return Collections.emptyList();
} }
+15 -21
View File
@@ -92,9 +92,8 @@ JetFile: Imports_ERR.jet
PsiElement(IDENTIFIER)('foo') PsiElement(IDENTIFIER)('foo')
PsiElement(DOT)('.') PsiElement(DOT)('.')
PsiWhiteSpace(' ') PsiWhiteSpace(' ')
REFERENCE_EXPRESSION PsiErrorElement:Expecting type name
PsiErrorElement:Expecting type name PsiElement(as)('as')
PsiElement(as)('as')
PsiWhiteSpace(' ') PsiWhiteSpace(' ')
ANNOTATION_ENTRY ANNOTATION_ENTRY
CONSTRUCTOR_CALLEE CONSTRUCTOR_CALLEE
@@ -122,9 +121,8 @@ JetFile: Imports_ERR.jet
REFERENCE_EXPRESSION REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('bar') PsiElement(IDENTIFIER)('bar')
PsiElement(DOT)('.') PsiElement(DOT)('.')
REFERENCE_EXPRESSION PsiErrorElement:Expecting type name
PsiErrorElement:Expecting type name PsiElement(MUL)('*')
PsiElement(MUL)('*')
PsiWhiteSpace(' ') PsiWhiteSpace(' ')
PsiErrorElement:Expecting namespace or top level declaration PsiErrorElement:Expecting namespace or top level declaration
PsiElement(as)('as') PsiElement(as)('as')
@@ -156,9 +154,8 @@ JetFile: Imports_ERR.jet
REFERENCE_EXPRESSION REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('bar') PsiElement(IDENTIFIER)('bar')
PsiElement(DOT)('.') PsiElement(DOT)('.')
REFERENCE_EXPRESSION PsiErrorElement:Expecting type name
PsiErrorElement:Expecting type name PsiElement(MUL)('*')
PsiElement(MUL)('*')
PsiWhiteSpace(' ') PsiWhiteSpace(' ')
PsiErrorElement:Expecting namespace or top level declaration PsiErrorElement:Expecting namespace or top level declaration
PsiElement(as)('as') PsiElement(as)('as')
@@ -182,10 +179,9 @@ JetFile: Imports_ERR.jet
REFERENCE_EXPRESSION REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('foo') PsiElement(IDENTIFIER)('foo')
PsiElement(DOT)('.') PsiElement(DOT)('.')
PsiWhiteSpace(' ') PsiErrorElement:Qualified name must be a '.'-separated identifier list
REFERENCE_EXPRESSION <empty list>
PsiErrorElement:Qualified name must be a '.'-separated identifier list PsiWhiteSpace(' ')
<empty list>
PsiElement(SEMICOLON)(';') PsiElement(SEMICOLON)(';')
PsiWhiteSpace('\n') PsiWhiteSpace('\n')
IMPORT_DIRECTIVE IMPORT_DIRECTIVE
@@ -199,10 +195,9 @@ JetFile: Imports_ERR.jet
REFERENCE_EXPRESSION REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('bar') PsiElement(IDENTIFIER)('bar')
PsiElement(DOT)('.') PsiElement(DOT)('.')
PsiWhiteSpace(' ') PsiErrorElement:Qualified name must be a '.'-separated identifier list
REFERENCE_EXPRESSION <empty list>
PsiErrorElement:Qualified name must be a '.'-separated identifier list PsiWhiteSpace(' ')
<empty list>
PsiElement(SEMICOLON)(';') PsiElement(SEMICOLON)(';')
PsiWhiteSpace('\n') PsiWhiteSpace('\n')
IMPORT_DIRECTIVE IMPORT_DIRECTIVE
@@ -212,10 +207,9 @@ JetFile: Imports_ERR.jet
REFERENCE_EXPRESSION REFERENCE_EXPRESSION
PsiElement(IDENTIFIER)('foo') PsiElement(IDENTIFIER)('foo')
PsiElement(DOT)('.') PsiElement(DOT)('.')
PsiWhiteSpace(' ') PsiErrorElement:Qualified name must be a '.'-separated identifier list
REFERENCE_EXPRESSION <empty list>
PsiErrorElement:Qualified name must be a '.'-separated identifier list PsiWhiteSpace(' ')
<empty list>
PsiElement(as)('as') PsiElement(as)('as')
PsiWhiteSpace(' ') PsiWhiteSpace(' ')
PsiElement(IDENTIFIER)('bar') PsiElement(IDENTIFIER)('bar')
@@ -19,6 +19,7 @@ package org.jetbrains.jet.plugin.references;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*; import com.intellij.psi.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.psi.JetArrayAccessExpression; import org.jetbrains.jet.lang.psi.JetArrayAccessExpression;
import org.jetbrains.jet.lang.psi.JetContainerNode; import org.jetbrains.jet.lang.psi.JetContainerNode;
@@ -44,7 +45,7 @@ class JetArrayAccessReference extends JetPsiReference implements MultiRangeRefer
return indicesNode == null ? PsiReference.EMPTY_ARRAY : new PsiReference[] { new JetArrayAccessReference(expression) }; return indicesNode == null ? PsiReference.EMPTY_ARRAY : new PsiReference[] { new JetArrayAccessReference(expression) };
} }
public JetArrayAccessReference(JetArrayAccessExpression expression) { public JetArrayAccessReference(@NotNull JetArrayAccessExpression expression) {
super(expression); super(expression);
this.expression = expression; this.expression = expression;
} }
@@ -17,7 +17,6 @@
package org.jetbrains.jet.plugin.references; package org.jetbrains.jet.plugin.references;
import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.compiler.TipsManager; import org.jetbrains.jet.compiler.TipsManager;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
@@ -33,7 +32,7 @@ public class JetPackageReference extends JetPsiReference {
private JetNamespaceHeader packageExpression; private JetNamespaceHeader packageExpression;
public JetPackageReference(JetNamespaceHeader expression) { public JetPackageReference(@NotNull JetNamespaceHeader expression) {
super(expression); super(expression);
packageExpression = expression; packageExpression = expression;
} }
@@ -48,11 +47,10 @@ public class JetPackageReference extends JetPsiReference {
bindingContext, TipsManager.getReferenceVariants(packageExpression, bindingContext)); bindingContext, TipsManager.getReferenceVariants(packageExpression, bindingContext));
} }
@NotNull
@Override @Override
public TextRange getRangeInElement() { public TextRange getRangeInElement() {
PsiElement element = getElement(); return new TextRange(0, getElement().getTextLength());
if (element == null) return null;
return new TextRange(0, element.getTextLength());
} }
@NotNull @NotNull
@@ -22,6 +22,7 @@ import com.intellij.psi.PsiPolyVariantReference;
import com.intellij.psi.ResolveResult; import com.intellij.psi.ResolveResult;
import com.intellij.util.IncorrectOperationException; import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetReferenceExpression; import org.jetbrains.jet.lang.psi.JetReferenceExpression;
@@ -36,12 +37,15 @@ import static org.jetbrains.jet.lang.resolve.BindingContext.AMBIGUOUS_REFERENCE_
import static org.jetbrains.jet.lang.resolve.BindingContext.DESCRIPTOR_TO_DECLARATION; import static org.jetbrains.jet.lang.resolve.BindingContext.DESCRIPTOR_TO_DECLARATION;
public abstract class JetPsiReference implements PsiPolyVariantReference { public abstract class JetPsiReference implements PsiPolyVariantReference {
@NotNull
protected final JetReferenceExpression myExpression; protected final JetReferenceExpression myExpression;
protected JetPsiReference(JetReferenceExpression expression) { protected JetPsiReference(@NotNull JetReferenceExpression expression) {
this.myExpression = expression; this.myExpression = expression;
} }
@NotNull
@Override @Override
public PsiElement getElement() { public PsiElement getElement() {
return myExpression; return myExpression;
@@ -69,6 +73,7 @@ public abstract class JetPsiReference implements PsiPolyVariantReference {
throw new IncorrectOperationException(); throw new IncorrectOperationException();
} }
@NotNull
@Override @Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException { public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
throw new IncorrectOperationException(); throw new IncorrectOperationException();
@@ -90,6 +95,7 @@ public abstract class JetPsiReference implements PsiPolyVariantReference {
return false; return false;
} }
@Nullable
protected PsiElement doResolve() { protected PsiElement doResolve() {
JetFile file = (JetFile) getElement().getContainingFile(); JetFile file = (JetFile) getElement().getContainingFile();
BindingContext bindingContext = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file); BindingContext bindingContext = WholeProjectAnalyzerFacade.analyzeProjectWithCacheOnAFile(file);
@@ -33,23 +33,29 @@ import org.jetbrains.jet.plugin.completion.DescriptorLookupConverter;
*/ */
public class JetSimpleNameReference extends JetPsiReference { public class JetSimpleNameReference extends JetPsiReference {
@NotNull
private final JetSimpleNameExpression myExpression; private final JetSimpleNameExpression myExpression;
public JetSimpleNameReference(JetSimpleNameExpression jetSimpleNameExpression) { public JetSimpleNameReference(@NotNull JetSimpleNameExpression jetSimpleNameExpression) {
super(jetSimpleNameExpression); super(jetSimpleNameExpression);
myExpression = jetSimpleNameExpression; myExpression = jetSimpleNameExpression;
} }
@NotNull
@Override @Override
public PsiElement getElement() { public PsiElement getElement() {
return myExpression.getReferencedNameElement(); return myExpression.getReferencedNameElement();
} }
@NotNull
public JetSimpleNameExpression getExpression() {
return myExpression;
}
@NotNull
@Override @Override
public TextRange getRangeInElement() { public TextRange getRangeInElement() {
PsiElement element = getElement(); return new TextRange(0, getElement().getTextLength());
if (element == null) return null;
return new TextRange(0, element.getTextLength());
} }
@NotNull @NotNull