Add more investigation info to getFilesForElements

Relates to #EA-209630
This commit is contained in:
Vladimir Dolzhenko
2020-01-06 14:53:09 +01:00
parent 93ce4c003f
commit d644c0125a
3 changed files with 15 additions and 7 deletions
@@ -60,9 +60,9 @@ public class KtElementImpl extends ASTWrapperPsiElement implements KtElement {
public KtFile getContainingKtFile() {
PsiFile file = getContainingFile();
if(!(file instanceof KtFile)) {
String fileString = (file != null && file.isValid()) ? file.getText() : "";
String fileString = (file != null && file.isValid()) ? (" " + file.getText()) : "";
throw new IllegalStateException("KtElement not inside KtFile: " + file + fileString +
"for element " + this + " of type " + this.getClass() + " node = " + getNode());
" for element " + this + " of type " + this.getClass() + " node = " + getNode());
}
return (KtFile) file;
}
@@ -69,9 +69,9 @@ public class KtElementImplStub<T extends StubElement<?>> extends StubBasedPsiEle
PsiFile file = getContainingFile();
if (!(file instanceof KtFile)) {
// KtElementImpl.copy() might be the reason for this exception
String fileString = file.isValid() ? file.getText() : "";
String fileString = file.isValid() ? (" " + file.getText()) : "";
throw new IllegalStateException("KtElement not inside KtFile: " + file + fileString +
"for element " + this + " of type " + this.getClass() + " node = " + getNode());
" for element " + this + " of type " + this.getClass() + " node = " + getNode());
}
return (KtFile) file;
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.caches.resolve
import com.intellij.openapi.diagnostic.ControlFlowException
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.Sdk
@@ -59,6 +60,7 @@ import org.jetbrains.kotlin.psi.psiUtil.contains
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.diagnostics.KotlinSuppressCache
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.sumByLong
@@ -120,9 +122,15 @@ class KotlinCacheServiceImpl(val project: Project) : KotlinCacheService {
private fun getFilesForElements(elements: List<KtElement>): List<KtFile> {
return elements.map {
// in theory `containingKtFile` is `@NotNull` but in practice EA-114080
@Suppress("USELESS_ELVIS")
it.containingKtFile ?: throw IllegalStateException("containingKtFile was null for $it of ${it.javaClass}")
try {
// in theory `containingKtFile` is `@NotNull` but in practice EA-114080
@Suppress("USELESS_ELVIS")
it.containingKtFile ?: throw IllegalStateException("containingKtFile was null for $it of ${it.javaClass}")
} catch (e: Exception) {
if (e is ControlFlowException) throw e
throw KotlinExceptionWithAttachments("Couldn't get containingKtFile for ktElement")
.withAttachment("element.kt", it.text)
}
}
}