mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
replace recursion with traverser
This commit is contained in:
@@ -999,14 +999,8 @@ public class PsiTreeUtil {
|
||||
return nextLeaf;
|
||||
}
|
||||
|
||||
public static boolean hasErrorElements(@NotNull final PsiElement element) {
|
||||
if (element instanceof PsiErrorElement) return true;
|
||||
|
||||
for (PsiElement child : element.getChildren()) {
|
||||
if (hasErrorElements(child)) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
public static boolean hasErrorElements(@NotNull PsiElement element) {
|
||||
return !SyntaxTraverser.psiTraverser(element).traverse().filter(PsiErrorElement.class).isEmpty();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiErrorElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.SyntaxTraverser;
|
||||
import com.intellij.psi.impl.PsiManagerEx;
|
||||
import com.intellij.psi.util.CachedValue;
|
||||
import com.intellij.psi.util.CachedValueProvider;
|
||||
@@ -38,47 +39,33 @@ public class PsiErrorElementUtil {
|
||||
private PsiErrorElementUtil() {}
|
||||
|
||||
public static boolean hasErrors(@NotNull final Project project, @NotNull final VirtualFile virtualFile) {
|
||||
return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
|
||||
@Override
|
||||
public Boolean compute() {
|
||||
if (project.isDisposed() || !virtualFile.isValid()) {
|
||||
return false;
|
||||
}
|
||||
PsiManagerEx psiManager = PsiManagerEx.getInstanceEx(project);
|
||||
PsiFile psiFile = psiManager.getFileManager().findFile(virtualFile);
|
||||
return psiFile != null && hasErrors(psiFile);
|
||||
}
|
||||
return ApplicationManager.getApplication().runReadAction((Computable<Boolean>)() -> {
|
||||
if (project.isDisposed() || !virtualFile.isValid()) return false;
|
||||
|
||||
PsiManagerEx psiManager = PsiManagerEx.getInstanceEx(project);
|
||||
PsiFile psiFile = psiManager.getFileManager().findFile(virtualFile);
|
||||
return psiFile != null && hasErrors(psiFile);
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean hasErrors(@NotNull final PsiFile psiFile) {
|
||||
CachedValuesManager cachedValuesManager = CachedValuesManager.getManager(psiFile.getProject());
|
||||
return cachedValuesManager.getCachedValue(
|
||||
psiFile,
|
||||
CONTAINS_ERROR_ELEMENT,
|
||||
() -> {
|
||||
boolean error = hasErrorElements(psiFile);
|
||||
return CachedValueProvider.Result.create(error, psiFile);
|
||||
},
|
||||
psiFile, CONTAINS_ERROR_ELEMENT,
|
||||
() -> CachedValueProvider.Result.create(hasErrorElements(psiFile), psiFile),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private static boolean hasErrorElements(@NotNull final PsiElement element) {
|
||||
if (element instanceof PsiErrorElement) {
|
||||
HighlightErrorFilter[] errorFilters = Extensions.getExtensions(HighlightErrorFilter.EP_NAME, element.getProject());
|
||||
for (HighlightErrorFilter errorFilter : errorFilters) {
|
||||
if (!errorFilter.shouldHighlightErrorElement((PsiErrorElement)element)) {
|
||||
private static boolean hasErrorElements(@NotNull PsiElement element) {
|
||||
HighlightErrorFilter[] filters = Extensions.getExtensions(HighlightErrorFilter.EP_NAME, element.getProject());
|
||||
for (PsiErrorElement error : SyntaxTraverser.psiTraverser(element).traverse().filter(PsiErrorElement.class)) {
|
||||
for (HighlightErrorFilter filter : filters) {
|
||||
if (!filter.shouldHighlightErrorElement(error)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
for (PsiElement child : element.getChildren()) {
|
||||
if (hasErrorElements(child)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user