Added API for changing type ref for function/variable and refactored code in some quickfixes to use it
This commit is contained in:
@@ -27,4 +27,7 @@ public interface JetCallableDeclaration extends JetNamedDeclaration, JetTypePara
|
||||
|
||||
@Nullable
|
||||
JetTypeReference getReturnTypeRef();
|
||||
|
||||
@Nullable
|
||||
JetTypeReference setReturnTypeRef(@Nullable JetTypeReference typeRef);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.intellij.psi.tree.IElementType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.JetNodeTypes;
|
||||
import org.jetbrains.jet.lang.psi.typeRefHelpers.TypeRefHelpersPackage;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
|
||||
import java.util.Collections;
|
||||
@@ -77,20 +78,13 @@ abstract public class JetFunctionNotStubbed extends JetTypeParameterListOwnerNot
|
||||
@Override
|
||||
@Nullable
|
||||
public JetTypeReference getReturnTypeRef() {
|
||||
boolean colonPassed = false;
|
||||
PsiElement child = getFirstChild();
|
||||
while (child != null) {
|
||||
IElementType tt = child.getNode().getElementType();
|
||||
if (tt == JetTokens.COLON) {
|
||||
colonPassed = true;
|
||||
}
|
||||
if (colonPassed && child instanceof JetTypeReference) {
|
||||
return (JetTypeReference) child;
|
||||
}
|
||||
child = child.getNextSibling();
|
||||
}
|
||||
return TypeRefHelpersPackage.getTypeRef(this);
|
||||
}
|
||||
|
||||
return null;
|
||||
@Nullable
|
||||
@Override
|
||||
public JetTypeReference setReturnTypeRef(@Nullable JetTypeReference typeRef) {
|
||||
return TypeRefHelpersPackage.setTypeRef(this, getValueParameterList(), typeRef);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.intellij.psi.tree.TokenSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.JetNodeTypes;
|
||||
import org.jetbrains.jet.lang.psi.typeRefHelpers.TypeRefHelpersPackage;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
|
||||
@@ -34,7 +35,12 @@ public class JetMultiDeclarationEntry extends JetNamedDeclarationNotStubbed impl
|
||||
|
||||
@Override
|
||||
public JetTypeReference getTypeRef() {
|
||||
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
|
||||
return TypeRefHelpersPackage.getTypeRef(this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public JetTypeReference setTypeRef(@Nullable JetTypeReference typeRef) {
|
||||
return TypeRefHelpersPackage.setTypeRef(this, getNameIdentifier(), typeRef);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.stubs.PsiJetFunctionStub;
|
||||
import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
|
||||
import org.jetbrains.jet.lang.psi.typeRefHelpers.TypeRefHelpersPackage;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
|
||||
import java.util.Collections;
|
||||
@@ -174,25 +175,13 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<PsiJetFuncti
|
||||
}
|
||||
return typeReferences.get(returnTypeIndex);
|
||||
}
|
||||
return getReturnTypeRefByPsi();
|
||||
return TypeRefHelpersPackage.getTypeRef(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
private JetTypeReference getReturnTypeRefByPsi() {
|
||||
boolean colonPassed = false;
|
||||
PsiElement child = getFirstChild();
|
||||
while (child != null) {
|
||||
IElementType tt = child.getNode().getElementType();
|
||||
if (tt == JetTokens.COLON) {
|
||||
colonPassed = true;
|
||||
}
|
||||
if (colonPassed && child instanceof JetTypeReference) {
|
||||
return (JetTypeReference) child;
|
||||
}
|
||||
child = child.getNextSibling();
|
||||
}
|
||||
|
||||
return null;
|
||||
public JetTypeReference setReturnTypeRef(@Nullable JetTypeReference typeRef) {
|
||||
return TypeRefHelpersPackage.setTypeRef(this, getValueParameterList(), typeRef);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.stubs.PsiJetParameterStub;
|
||||
import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
|
||||
import org.jetbrains.jet.lang.psi.typeRefHelpers.TypeRefHelpersPackage;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
|
||||
public class JetParameter extends JetNamedDeclarationStub<PsiJetParameterStub> {
|
||||
@@ -40,11 +41,17 @@ public class JetParameter extends JetNamedDeclarationStub<PsiJetParameterStub> {
|
||||
return visitor.visitParameter(this, data);
|
||||
}
|
||||
|
||||
//TODO: rename it to getTypeRef for consistency
|
||||
@Nullable
|
||||
public JetTypeReference getTypeReference() {
|
||||
return getStubOrPsiChild(JetStubElementTypes.TYPE_REFERENCE);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public JetTypeReference setTypeRef(@Nullable JetTypeReference typeRef) {
|
||||
return TypeRefHelpersPackage.setTypeRef(this, getNameIdentifier(), typeRef);
|
||||
}
|
||||
|
||||
public boolean hasDefaultValue() {
|
||||
PsiJetParameterStub stub = getStub();
|
||||
if (stub != null) {
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.JetNodeTypes;
|
||||
import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyStub;
|
||||
import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
|
||||
import org.jetbrains.jet.lang.psi.typeRefHelpers.TypeRefHelpersPackage;
|
||||
import org.jetbrains.jet.lexer.JetTokens;
|
||||
|
||||
import java.util.List;
|
||||
@@ -121,6 +122,12 @@ public class JetProperty extends JetTypeParameterListOwnerStub<PsiJetPropertyStu
|
||||
return getTypeRef();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public JetTypeReference setReturnTypeRef(@Nullable JetTypeReference typeRef) {
|
||||
return setTypeRef(typeRef);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public JetTypeReference getTypeRef() {
|
||||
@@ -139,25 +146,12 @@ public class JetProperty extends JetTypeParameterListOwnerStub<PsiJetPropertyStu
|
||||
return typeReferences.get(returnTypeRefPositionInPsi);
|
||||
}
|
||||
}
|
||||
return getTypeRefByTree();
|
||||
return TypeRefHelpersPackage.getTypeRef(this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private JetTypeReference getTypeRefByTree() {
|
||||
ASTNode node = getNode().getFirstChildNode();
|
||||
boolean passedColon = false;
|
||||
while (node != null) {
|
||||
IElementType tt = node.getElementType();
|
||||
if (tt == JetTokens.COLON) {
|
||||
passedColon = true;
|
||||
}
|
||||
else if (tt == JetNodeTypes.TYPE_REFERENCE && passedColon) {
|
||||
return (JetTypeReference) node.getPsi();
|
||||
}
|
||||
node = node.getTreeNext();
|
||||
}
|
||||
|
||||
return null;
|
||||
public JetTypeReference setTypeRef(@Nullable JetTypeReference typeRef) {
|
||||
return TypeRefHelpersPackage.setTypeRef(this, getNameIdentifier(), typeRef);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -27,4 +27,7 @@ public interface JetVariableDeclaration extends JetDeclaration, JetWithExpressio
|
||||
|
||||
@Nullable
|
||||
JetTypeReference getTypeRef();
|
||||
|
||||
@Nullable
|
||||
JetTypeReference setTypeRef(@Nullable JetTypeReference typeRef);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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.typeRefHelpers
|
||||
|
||||
import org.jetbrains.jet.lang.psi.JetTypeReference
|
||||
import org.jetbrains.jet.lexer.JetTokens
|
||||
import org.jetbrains.jet.lang.psi.JetDeclaration
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.siblings
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory
|
||||
import com.intellij.psi.PsiWhiteSpace
|
||||
import org.jetbrains.jet.lang.psi.JetNamedDeclaration
|
||||
|
||||
fun getTypeRef(declaration: JetDeclaration): JetTypeReference? {
|
||||
return declaration.getFirstChild()!!.siblings(forward = true)
|
||||
.dropWhile { it.getNode()!!.getElementType() != JetTokens.COLON }
|
||||
.filterIsInstance(javaClass<JetTypeReference>())
|
||||
.firstOrNull()
|
||||
}
|
||||
|
||||
fun setTypeRef(declaration: JetNamedDeclaration, addAfter: PsiElement?, typeRef: JetTypeReference?): JetTypeReference? {
|
||||
val oldTypeRef = getTypeRef(declaration)
|
||||
if (typeRef != null) {
|
||||
if (oldTypeRef != null) {
|
||||
return oldTypeRef.replace(typeRef) as JetTypeReference
|
||||
}
|
||||
else {
|
||||
var anchor = addAfter ?: declaration.getNameIdentifier()
|
||||
val newTypeRef = declaration.addAfter(typeRef, anchor) as JetTypeReference
|
||||
declaration.addAfter(JetPsiFactory(declaration.getProject()).createColon(), anchor)
|
||||
return newTypeRef
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (oldTypeRef != null) {
|
||||
val colon = declaration.getNode()!!.findChildByType(JetTokens.COLON)!!.getPsi()!!
|
||||
val removeFrom = colon.getPrevSibling() as? PsiWhiteSpace ?: colon
|
||||
declaration.deleteChildRange(removeFrom, oldTypeRef)
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user