PSI: Extract JetConstructor class

This commit is contained in:
Alexey Sedunov
2015-06-04 15:54:31 +03:00
parent 64ae0c0036
commit 034af9e7f2
7 changed files with 222 additions and 182 deletions
@@ -655,7 +655,13 @@ public class JetFlowInformationProvider {
}
else if (element instanceof JetParameter) {
PsiElement owner = element.getParent().getParent();
if (owner instanceof JetFunction) {
if (owner instanceof JetPrimaryConstructor) {
if (!((JetParameter) element).hasValOrVar() &&
!((JetPrimaryConstructor) owner).getContainingClassOrObject().isAnnotation()) {
report(Errors.UNUSED_PARAMETER.on((JetParameter) element, variableDescriptor), ctxt);
}
}
else if (owner instanceof JetFunction) {
MainFunctionDetector mainFunctionDetector = new MainFunctionDetector(trace.getBindingContext());
boolean isMain = (owner instanceof JetNamedFunction) && mainFunctionDetector.isMain((JetNamedFunction) owner);
if (owner instanceof JetFunctionLiteral) return;
@@ -672,12 +678,6 @@ public class JetFlowInformationProvider {
}
report(Errors.UNUSED_PARAMETER.on((JetParameter) element, variableDescriptor), ctxt);
}
else if (owner instanceof JetPrimaryConstructor) {
if (!((JetParameter) element).hasValOrVar() &&
!((JetPrimaryConstructor) owner).getContainingClassOrObject().isAnnotation()) {
report(Errors.UNUSED_PARAMETER.on((JetParameter) element, variableDescriptor), ctxt);
}
}
}
}
else if (variableUseState == ONLY_WRITTEN_NEVER_READ && JetPsiUtil.isVariableNotParameterDeclaration(element)) {
@@ -121,6 +121,11 @@ public object PositioningStrategies {
public val DECLARATION_SIGNATURE: PositioningStrategy<JetDeclaration> = object : DeclarationHeader<JetDeclaration>() {
override fun mark(element: JetDeclaration): List<TextRange> {
when (element) {
is JetPrimaryConstructor -> {
val begin = element.getConstructorKeyword() ?: element.getValueParameterList() ?: return markElement(element)
val end = element.getValueParameterList() ?: element.getConstructorKeyword() ?: return markElement(element)
return markRange(begin, end)
}
is JetFunction -> {
val endOfSignatureElement =
element.getTypeReference()
@@ -156,14 +161,6 @@ public object PositioningStrategies {
is JetObjectDeclaration -> {
return DECLARATION_NAME.mark(element)
}
is JetPrimaryConstructor -> {
val begin = element.getConstructorKeyword() ?: element.getValueParameterList() ?: return markElement(element)
val end = element.getValueParameterList() ?: element.getConstructorKeyword()
return markRange(begin, end)
}
is JetSecondaryConstructor -> {
return markRange(element.getConstructorKeyword(), element.getValueParameterList() ?: element.getConstructorKeyword())
}
}
return super.mark(element)
}
@@ -0,0 +1,194 @@
/*
* Copyright 2010-2015 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.kotlin.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
import java.util.Collections;
import java.util.List;
public abstract class JetConstructor<T extends JetConstructor<T>> extends JetDeclarationStub<KotlinPlaceHolderStub<T>> implements JetFunction {
protected JetConstructor(@NotNull ASTNode node) {
super(node);
}
protected JetConstructor(@NotNull KotlinPlaceHolderStub<T> stub, @NotNull IStubElementType nodeType) {
super(stub, nodeType);
}
@NotNull
public abstract JetClassOrObject getClassOrObject();
@Override
public boolean isLocal() {
return false;
}
@Override
@Nullable
public JetParameterList getValueParameterList() {
return getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST);
}
@Override
@NotNull
public List<JetParameter> getValueParameters() {
JetParameterList list = getValueParameterList();
return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
}
@Nullable
@Override
public JetTypeReference getReceiverTypeReference() {
return null;
}
@Nullable
@Override
public JetTypeReference getTypeReference() {
return null;
}
@Nullable
@Override
public JetTypeReference setTypeReference(@Nullable JetTypeReference typeRef) {
return null;
}
@Nullable
@Override
public PsiElement getColon() {
return findChildByType(JetTokens.COLON);
}
@Nullable
@Override
public JetBlockExpression getBodyExpression() {
return null;
}
@Nullable
@Override
public PsiElement getEqualsToken() {
return null;
}
@Override
public boolean hasBlockBody() {
return true;
}
@Override
public boolean hasBody() {
return getBodyExpression() != null;
}
@Override
public boolean hasDeclaredReturnType() {
return false;
}
@Nullable
@Override
public JetTypeParameterList getTypeParameterList() {
return null;
}
@Nullable
@Override
public JetTypeConstraintList getTypeConstraintList() {
return null;
}
@NotNull
@Override
public List<JetTypeConstraint> getTypeConstraints() {
return Collections.emptyList();
}
@NotNull
@Override
public List<JetTypeParameter> getTypeParameters() {
return Collections.emptyList();
}
@Override
@NotNull
public String getName() {
return getClassOrObject().getName();
}
@NotNull
@Override
public Name getNameAsSafeName() {
return Name.identifier(getName());
}
@Nullable
@Override
public FqName getFqName() {
return null;
}
@Nullable
@Override
public Name getNameAsName() {
return getNameAsSafeName();
}
@Nullable
@Override
public PsiElement getNameIdentifier() {
return null;
}
@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
throw new IncorrectOperationException("setName to constructor");
}
@Nullable
public PsiElement getConstructorKeyword() {
return findChildByType(JetTokens.CONSTRUCTOR_KEYWORD);
}
public boolean hasConstructorKeyword() {
if (getStub() != null) return true;
return getConstructorKeyword() != null;
}
@Override
public int getTextOffset() {
PsiElement keyword = getConstructorKeyword();
if (keyword != null) return keyword.getTextOffset();
JetParameterList parameterList = getValueParameterList();
if (parameterList != null) return parameterList.getTextOffset();
return super.getTextOffset();
}
}
@@ -17,19 +17,14 @@
package org.jetbrains.kotlin.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.psi.addRemoveModifier.AddRemoveModifierPackage;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
import java.util.Collections;
import java.util.List;
public class JetPrimaryConstructor extends JetDeclarationStub<KotlinPlaceHolderStub<JetPrimaryConstructor>> {
public class JetPrimaryConstructor extends JetConstructor<JetPrimaryConstructor> {
public JetPrimaryConstructor(@NotNull ASTNode node) {
super(node);
}
@@ -43,15 +38,15 @@ public class JetPrimaryConstructor extends JetDeclarationStub<KotlinPlaceHolderS
return visitor.visitPrimaryConstructor(this, data);
}
@Nullable
public JetParameterList getValueParameterList() {
return getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST);
@NotNull
public JetClassOrObject getContainingClassOrObject() {
return (JetClassOrObject) getParent();
}
@NotNull
public List<JetParameter> getValueParameters() {
JetParameterList list = getValueParameterList();
return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
@Override
public JetClassOrObject getClassOrObject() {
return getContainingClassOrObject();
}
@Override
@@ -70,19 +65,4 @@ public class JetPrimaryConstructor extends JetDeclarationStub<KotlinPlaceHolderS
addBefore(newModifierList, parameterList);
}
}
@NotNull
public JetClassOrObject getContainingClassOrObject() {
return (JetClassOrObject) getParent();
}
public boolean hasConstructorKeyword() {
if (getStub() != null) return true;
return getConstructorKeyword() != null;
}
@Nullable
public PsiElement getConstructorKeyword() {
return findChildByType(JetTokens.CONSTRUCTOR_KEYWORD);
}
}
@@ -18,19 +18,12 @@ package org.jetbrains.kotlin.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
import java.util.Collections;
import java.util.List;
public class JetSecondaryConstructor extends JetDeclarationStub<KotlinPlaceHolderStub<JetSecondaryConstructor>> implements JetFunction {
public class JetSecondaryConstructor extends JetConstructor<JetSecondaryConstructor> {
public JetSecondaryConstructor(@NotNull ASTNode node) {
super(node);
}
@@ -44,46 +37,10 @@ public class JetSecondaryConstructor extends JetDeclarationStub<KotlinPlaceHolde
return visitor.visitSecondaryConstructor(this, data);
}
@Override
public boolean isLocal() {
return false;
}
@Override
@Nullable
public JetParameterList getValueParameterList() {
return getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST);
}
@Override
@NotNull
public List<JetParameter> getValueParameters() {
JetParameterList list = getValueParameterList();
return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
}
@Nullable
@Override
public JetTypeReference getReceiverTypeReference() {
return null;
}
@Nullable
@Override
public JetTypeReference getTypeReference() {
return null;
}
@Nullable
@Override
public JetTypeReference setTypeReference(@Nullable JetTypeReference typeRef) {
return null;
}
@Nullable
@Override
public PsiElement getColon() {
return findChildByType(JetTokens.COLON);
public JetClassOrObject getClassOrObject() {
return (JetClassOrObject) getParent().getParent();
}
@Nullable
@@ -92,100 +49,22 @@ public class JetSecondaryConstructor extends JetDeclarationStub<KotlinPlaceHolde
return findChildByClass(JetBlockExpression.class);
}
@Nullable
@Override
public PsiElement getEqualsToken() {
return null;
}
@Override
public boolean hasBlockBody() {
return true;
}
@Override
public boolean hasBody() {
return getBodyExpression() != null;
}
@Override
public boolean hasDeclaredReturnType() {
return false;
}
@Override
@NotNull
public String getName() {
return getClassOrObject().getName();
}
@Nullable
@Override
public JetTypeParameterList getTypeParameterList() {
return null;
}
@Nullable
@Override
public JetTypeConstraintList getTypeConstraintList() {
return null;
}
@NotNull
@Override
public List<JetTypeConstraint> getTypeConstraints() {
return Collections.emptyList();
}
@NotNull
@Override
public List<JetTypeParameter> getTypeParameters() {
return Collections.emptyList();
}
@NotNull
@Override
public Name getNameAsSafeName() {
return Name.identifier(getName());
}
@Nullable
@Override
public FqName getFqName() {
return null;
}
@Nullable
@Override
public Name getNameAsName() {
return getNameAsSafeName();
}
@Nullable
@Override
public PsiElement getNameIdentifier() {
return null;
}
@Override
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
throw new IncorrectOperationException("setName to constructor");
public PsiElement getConstructorKeyword() {
//noinspection ConstantConditions
return notNullChild(super.getConstructorKeyword());
}
@NotNull
public JetConstructorDelegationCall getDelegationCall() {
return findChildByClass(JetConstructorDelegationCall.class);
return findNotNullChildByClass(JetConstructorDelegationCall.class);
}
public boolean hasImplicitDelegationCall() {
return getDelegationCall().isImplicit();
}
@NotNull
public JetClassOrObject getClassOrObject() {
return (JetClassOrObject) getParent().getParent();
}
@NotNull
public JetConstructorDelegationCall replaceImplicitDelegationCallWithExplicit(boolean isThis) {
JetPsiFactory psiFactory = new JetPsiFactory(getProject());
@@ -201,14 +80,4 @@ public class JetSecondaryConstructor extends JetDeclarationStub<KotlinPlaceHolde
return (JetConstructorDelegationCall) addAfter(psiFactory.createConstructorDelegationCall(delegationName + "()"), colon);
}
@NotNull
public PsiElement getConstructorKeyword() {
return findNotNullChildByType(JetTokens.CONSTRUCTOR_KEYWORD);
}
@Override
public int getTextOffset() {
return getConstructorKeyword().getTextRange().getStartOffset();
}
}
@@ -39,7 +39,7 @@ public class SpecifyTypeExplicitlyIntention : JetSelfTargetingIntention<JetCalla
override fun isApplicableTo(element: JetCallableDeclaration, caretOffset: Int): Boolean {
if (element.getContainingFile() is JetCodeFragment) return false
if (element is JetFunctionLiteral) return false // TODO: should JetFunctionLiteral be JetCallableDeclaration at all?
if (element is JetPrimaryConstructor || element is JetSecondaryConstructor) return false // TODO: base class?
if (element is JetConstructor<*>) return false
if (element.getTypeReference() != null) return false
if (getTypeForDeclaration(element).isError() || hasPublicMemberDiagnostic(element)) return false
@@ -40,7 +40,7 @@ public class ChangeParameterTypeFix extends JetIntentionAction<JetParameter> {
super(element);
this.type = type;
JetNamedDeclaration declaration = PsiTreeUtil.getParentOfType(element, JetNamedDeclaration.class);
isPrimaryConstructorParameter = declaration instanceof JetClass;
isPrimaryConstructorParameter = declaration instanceof JetPrimaryConstructor;
FqName declarationFQName = declaration == null ? null : declaration.getFqName();
containingDeclarationName = declarationFQName == null ? declaration.getName() : declarationFQName.asString();
}