Invalidate cached PSI files in go packages if they become invalid.
Virtual file instances can become stale if the file disappears and
reappears. They'll never become valid again even after refresh.
In these cases we must find the new instances of these files.
@@ -56,6 +56,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.util.Processor;
import java.io.File;
+import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
@@ -258,7 +259,7 @@ public class BlazeGoPackage extends GoPackage {
@Override
public Collection<PsiFile> files() {
- if (cachedGoFiles == null) {
+ if (cachedGoFiles == null || cachedGoFiles.stream().anyMatch(f -> !f.isValid())) {
PsiManager psiManager = PsiManager.getInstance(getProject());
cachedGoFiles =
files.stream()
@@ -306,7 +307,7 @@ public class BlazeGoPackage extends GoPackage {
@Nullable
@Override
public PsiElement getNavigableElement() {
- if (cachedNavigable == null) {
+ if (cachedNavigable == null || !cachedNavigable.isValid()) {
Project project = getProject();
BuildReferenceManager buildReferenceManager = BuildReferenceManager.getInstance(project);
cachedNavigable = buildReferenceManager.resolveLabel(label);
@@ -370,7 +371,10 @@ public class BlazeGoPackage extends GoPackage {
*/
@Nullable
PsiElement[] getImportReferences() {
- if (cachedImportReferences == null) {
+ if (cachedImportReferences == null
+ || Arrays.stream(cachedImportReferences)
+ .filter(Objects::nonNull)
+ .anyMatch(e -> !e.isValid())) {
PsiElement navigable = getNavigableElement();
if (navigable != null) {
cachedImportReferences = getImportReferences(label, navigable, importPath);
