Improve diagnostics in KtElementImplStub::getContainingKtFile

- If containing file is not a `PsiFileImpl`, `getNode()` will
throw a CCE (which will prevent collecting the diagnostics)
- Relates to #EA-209630
This commit is contained in:
Roman Golyshev
2020-09-03 14:52:42 +03:00
parent 5b53663eb8
commit 909d418dd1
2 changed files with 14 additions and 4 deletions
@@ -20,6 +20,7 @@ import com.intellij.extapi.psi.StubBasedPsiElementBase;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.lang.Language; import com.intellij.lang.Language;
import com.intellij.psi.*; import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiFileImpl;
import com.intellij.psi.stubs.IStubElementType; import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.StubElement; import com.intellij.psi.stubs.StubElement;
import com.intellij.util.IncorrectOperationException; import com.intellij.util.IncorrectOperationException;
@@ -70,8 +71,12 @@ public class KtElementImplStub<T extends StubElement<?>> extends StubBasedPsiEle
if (!(file instanceof KtFile)) { if (!(file instanceof KtFile)) {
// KtElementImpl.copy() might be the reason for this exception // 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 + // getNode() will fail if getContainingFile() returns not PsiFileImpl instance
" for element " + this + " of type " + this.getClass() + " node = " + getNode()); String nodeString = (file instanceof PsiFileImpl ? (" node = " + getNode()) : "");
throw new IllegalStateException("KtElement not inside KtFile: " +
file + fileString + " of type " + file.getClass() +
" for element " + this + " of type " + this.getClass() + nodeString);
} }
return (KtFile) file; return (KtFile) file;
} }
@@ -9,6 +9,7 @@ import com.intellij.extapi.psi.StubBasedPsiElementBase;
import com.intellij.lang.ASTNode; import com.intellij.lang.ASTNode;
import com.intellij.lang.Language; import com.intellij.lang.Language;
import com.intellij.psi.*; import com.intellij.psi.*;
import com.intellij.psi.impl.source.PsiFileImpl;
import com.intellij.psi.stubs.IStubElementType; import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.StubElement; import com.intellij.psi.stubs.StubElement;
import com.intellij.util.IncorrectOperationException; import com.intellij.util.IncorrectOperationException;
@@ -59,8 +60,12 @@ public class KtElementImplStub<T extends StubElement<?>> extends StubBasedPsiEle
if (!(file instanceof KtFile)) { if (!(file instanceof KtFile)) {
// KtElementImpl.copy() might be the reason for this exception // 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 + // getNode() will fail if getContainingFile() returns not PsiFileImpl instance
" for element " + this + " of type " + this.getClass() + " node = " + getNode()); String nodeString = (file instanceof PsiFileImpl ? (" node = " + getNode()) : "");
throw new IllegalStateException("KtElement not inside KtFile: " +
file + fileString + " of type " + file.getClass() +
" for element " + this + " of type " + this.getClass() + nodeString);
} }
return (KtFile) file; return (KtFile) file;
} }