diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/JetFlowInformationProvider.java b/compiler/frontend/src/org/jetbrains/kotlin/cfg/JetFlowInformationProvider.java index de57fe9e566..bc60175afb6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/JetFlowInformationProvider.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/JetFlowInformationProvider.java @@ -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)) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt index 058f602ad83..6ddc7ee0dbf 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/PositioningStrategies.kt @@ -121,6 +121,11 @@ public object PositioningStrategies { public val DECLARATION_SIGNATURE: PositioningStrategy = object : DeclarationHeader() { override fun mark(element: JetDeclaration): List { 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) } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetConstructor.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetConstructor.java new file mode 100644 index 00000000000..e1ae9bd2e83 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetConstructor.java @@ -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> extends JetDeclarationStub> implements JetFunction { + protected JetConstructor(@NotNull ASTNode node) { + super(node); + } + + protected JetConstructor(@NotNull KotlinPlaceHolderStub 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 getValueParameters() { + JetParameterList list = getValueParameterList(); + return list != null ? list.getParameters() : Collections.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 getTypeConstraints() { + return Collections.emptyList(); + } + + @NotNull + @Override + public List 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(); + } +} diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPrimaryConstructor.java b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPrimaryConstructor.java index 2ebe7ee92dd..df983abf0b9 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPrimaryConstructor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPrimaryConstructor.java @@ -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> { +public class JetPrimaryConstructor extends JetConstructor { public JetPrimaryConstructor(@NotNull ASTNode node) { super(node); } @@ -43,15 +38,15 @@ public class JetPrimaryConstructor extends JetDeclarationStub getValueParameters() { - JetParameterList list = getValueParameterList(); - return list != null ? list.getParameters() : Collections.emptyList(); + @Override + public JetClassOrObject getClassOrObject() { + return getContainingClassOrObject(); } @Override @@ -70,19 +65,4 @@ public class JetPrimaryConstructor extends JetDeclarationStub> implements JetFunction { +public class JetSecondaryConstructor extends JetConstructor { public JetSecondaryConstructor(@NotNull ASTNode node) { super(node); } @@ -44,46 +37,10 @@ public class JetSecondaryConstructor extends JetDeclarationStub getValueParameters() { - JetParameterList list = getValueParameterList(); - return list != null ? list.getParameters() : Collections.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 getTypeConstraints() { - return Collections.emptyList(); - } - - @NotNull - @Override - public List 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) return false if (element.getTypeReference() != null) return false if (getTypeForDeclaration(element).isError() || hasPublicMemberDiagnostic(element)) return false diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeParameterTypeFix.java b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeParameterTypeFix.java index 50dd091e2d4..d5b9e019c08 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeParameterTypeFix.java +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ChangeParameterTypeFix.java @@ -40,7 +40,7 @@ public class ChangeParameterTypeFix extends JetIntentionAction { 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(); }