Converted JetPsiUtil.getElementTextWithContext() to Kotlin extension function.

This commit is contained in:
Evgeny Gerashchenko
2015-04-22 13:10:57 +03:00
parent f59506c5e0
commit f5677d8424
31 changed files with 88 additions and 89 deletions
@@ -43,6 +43,7 @@ import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
@@ -321,7 +322,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
if (specifier instanceof JetDelegatorToSuperClass || specifier instanceof JetDelegatorToSuperCall) {
JetType superType = bindingContext.get(BindingContext.TYPE, specifier.getTypeReference());
assert superType != null :
String.format("No type recorded for \n---\n%s\n---\n", JetPsiUtil.getElementTextWithContext(specifier));
String.format("No type recorded for \n---\n%s\n---\n", PsiUtilPackage.getElementTextWithContext(specifier));
ClassifierDescriptor classifierDescriptor = superType.getConstructor().getDeclarationDescriptor();
if (!(classifierDescriptor instanceof ClassDescriptor)) continue;
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.load.java.JvmAbi;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.DescriptorFactory;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
@@ -373,7 +374,7 @@ public class PropertyCodegen {
}
}
throw new IllegalStateException("Delegated property not found in its parent: " + JetPsiUtil.getElementTextWithContext(property));
throw new IllegalStateException("Delegated property not found in its parent: " + PsiUtilPackage.getElementTextWithContext(property));
}
@@ -25,14 +25,13 @@ import org.jetbrains.kotlin.codegen.SamType;
import org.jetbrains.kotlin.codegen.state.GenerationState;
import org.jetbrains.kotlin.codegen.when.WhenByEnumsMapping;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.descriptors.impl.AnonymousFunctionDescriptor;
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.BindingTrace;
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
import org.jetbrains.kotlin.util.slicedMap.BasicWritableSlice;
import org.jetbrains.kotlin.util.slicedMap.Slices;
@@ -197,7 +196,7 @@ public class CodegenBinding {
public static void registerClassNameForScript(@NotNull BindingTrace trace, @NotNull JetScript script, @NotNull Type asmType) {
ScriptDescriptor descriptor = trace.getBindingContext().get(SCRIPT, script);
if (descriptor == null) {
throw new IllegalStateException("Script descriptor is not found for PSI: " + JetPsiUtil.getElementTextWithContext(script));
throw new IllegalStateException("Script descriptor is not found for PSI: " + PsiUtilPackage.getElementTextWithContext(script));
}
String simpleName = asmType.getInternalName().substring(asmType.getInternalName().lastIndexOf('/') + 1);
@@ -26,6 +26,7 @@ import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.kotlin.psi.stubs.KotlinFunctionStub;
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
import org.jetbrains.kotlin.psi.typeRefHelpers.TypeRefHelpersPackage;
@@ -80,7 +81,7 @@ public class JetNamedFunction extends JetTypeParameterListOwnerStub<KotlinFuncti
@NotNull
public PsiElement getFunToken() {
PsiElement element = findChildByType(JetTokens.FUN_KEYWORD);
assert element != null : "'fun' must be present: " + JetPsiUtil.getElementTextWithContext(this);
assert element != null : "'fun' must be present: " + PsiUtilPackage.getElementTextWithContext(this);
return element;
}
@@ -778,33 +778,6 @@ public class JetPsiUtil {
return element;
}
@NotNull
public static String getElementTextWithContext(@NotNull PsiElement element) {
if (element instanceof PsiFile) {
return element.getContainingFile().getText();
}
// Find parent for element among file children
PsiElement inFileParent = PsiTreeUtil.findFirstParent(element, new Condition<PsiElement>() {
@Override
public boolean value(PsiElement parentCandidate) {
return parentCandidate != null && parentCandidate.getParent() instanceof PsiFile;
}
});
assert inFileParent != null : "For non-file element we should always be able to find parent in file children";
int startContextOffset = inFileParent.getTextRange().getStartOffset();
int elementContextOffset = element.getTextRange().getStartOffset();
int inFileParentOffset = elementContextOffset - startContextOffset;
return new StringBuilder(inFileParent.getText())
.insert(inFileParentOffset, "<caret>")
.insert(0, String.format("File name: %s\n", element.getContainingFile().getName()))
.toString();
}
@Nullable
public static JetModifierList replaceModifierList(@NotNull JetModifierListOwner owner, @Nullable JetModifierList modifierList) {
JetModifierList oldModifierList = owner.getModifierList();
@@ -37,7 +37,7 @@ object JetQualifiedExpressionImpl {
}
public fun JetQualifiedExpression.getReceiverExpression(): JetExpression {
return getExpression(false) ?: throw AssertionError("No receiver found: ${JetPsiUtil.getElementTextWithContext(this)}")
return getExpression(false) ?: throw AssertionError("No receiver found: ${getElementTextWithContext()}")
}
public fun JetQualifiedExpression.getSelectorExpression(): JetExpression? {
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.psi.stubs.KotlinClassOrObjectStub
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils
import com.intellij.lang.ASTNode
import com.intellij.openapi.util.Condition
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.PsiComment
import org.jetbrains.kotlin.resolve.calls.CallTransformer.CallForImplicitInvoke
@@ -491,3 +492,23 @@ fun JetNamedDeclaration.getValueParameterList(): JetParameterList? {
else -> null
}
}
public fun PsiElement.getElementTextWithContext(): String {
if (this is PsiFile) {
return getContainingFile().getText()
}
// Find parent for element among file children
val topLevelElement = PsiTreeUtil.findFirstParent(this, { it.getParent() is PsiFile }) ?:
throw AssertionError("For non-file element we should always be able to find parent in file children")
val startContextOffset = topLevelElement.getTextRange().getStartOffset()
val elementContextOffset = getTextRange().getStartOffset()
val inFileParentOffset = elementContextOffset - startContextOffset
return StringBuilder(topLevelElement.getText())
.insert(inFileParentOffset, "<caret>")
.insert(0, "File name: ${getContainingFile().getName()}\n")
.toString()
}
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstant;
@@ -1109,7 +1110,7 @@ public class DescriptorResolver {
if (TypesPackage.isFlexible(type) && !TypesPackage.isDynamic(type)) {
assert jetTypeArguments.size() == 2
: "Flexible type cannot be denoted in Kotlin otherwise than as ft<T1, T2>, but was: "
+ JetPsiUtil.getElementTextWithContext(typeReference);
+ PsiUtilPackage.getElementTextWithContext(typeReference);
// it's really ft<Foo, Bar>
Flexibility flexibility = TypesPackage.flexibility(type);
checkBounds(jetTypeArguments.get(0), flexibility.getLowerBound(), trace);
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.resolve.lazy;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyClassDescriptor;
import org.jetbrains.kotlin.resolve.scopes.JetScope;
@@ -69,7 +70,7 @@ public class DeclarationScopeProviderImpl implements DeclarationScopeProvider {
}
throw new IllegalStateException("Don't call this method for local declarations: " + jetDeclaration + "\n" +
JetPsiUtil.getElementTextWithContext(jetDeclaration));
PsiUtilPackage.getElementTextWithContext(jetDeclaration));
}
@NotNull
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.resolve.BindingTrace;
@@ -77,7 +78,7 @@ public class LazyDeclarationResolver {
throw new IllegalArgumentException(
String.format("Could not find a classifier for %s.\n" +
"Found descriptor: %s (%s).\n",
JetPsiUtil.getElementTextWithContext(classOrObject),
PsiUtilPackage.getElementTextWithContext(classOrObject),
scopeDescriptor != null ? DescriptorRenderer.DEBUG_TEXT.render(scopeDescriptor) : "null",
scopeDescriptor != null ? (scopeDescriptor.getContainingDeclaration().getClass()) : null));
}
@@ -202,12 +203,12 @@ public class LazyDeclarationResolver {
@Override
public DeclarationDescriptor visitJetElement(@NotNull JetElement element, Void data) {
throw new IllegalArgumentException("Unsupported declaration type: " + element + " " +
JetPsiUtil.getElementTextWithContext(element));
PsiUtilPackage.getElementTextWithContext(element));
}
}, null);
if (result == null) {
throw new IllegalStateException("No descriptor resolved for " + declaration + ":\n" +
JetPsiUtil.getElementTextWithContext(declaration));
PsiUtilPackage.getElementTextWithContext(declaration));
}
return result;
}
@@ -35,6 +35,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.JetLanguage;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilPackage;
import java.util.List;
@@ -123,7 +124,7 @@ public abstract class KotlinWrappingLightClass extends AbstractLightClass implem
JetDeclaration declaration = ClsWrapperStubPsiFactory.getOriginalDeclaration(field);
if (declaration instanceof JetEnumEntry) {
assert field instanceof PsiEnumConstant : "Field delegate should be an enum constant (" + field.getName() + "):\n" +
JetPsiUtil.getElementTextWithContext(declaration);
PsiUtilPackage.getElementTextWithContext(declaration);
JetEnumEntry enumEntry = (JetEnumEntry) declaration;
PsiEnumConstant enumConstant = (PsiEnumConstant) field;
FqName enumConstantFqName = new FqName(getFqName().asString() + "." + enumEntry.getName());