SSR: improve "should show problem" api

GitOrigin-RevId: 790be8f55944bbf5db274c762fa8c22dc97b6b2b
This commit is contained in:
Bas Leijdekkers
2019-06-11 18:09:38 +03:00
committed by intellij-monorepo-bot
parent f5ee1db0b8
commit db13b7b767
4 changed files with 15 additions and 13 deletions
@@ -18,8 +18,6 @@ import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.JavaDummyHolder;
import com.intellij.psi.impl.source.PsiCodeFragmentImpl;
import com.intellij.psi.impl.source.tree.JavaElementType;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocToken;
import com.intellij.psi.util.PsiTreeUtil;
@@ -484,7 +482,7 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
}
@Override
public boolean shouldShowProblem(HighlightInfo highlightInfo, PsiFile file) {
public boolean shouldShowProblem(HighlightInfo highlightInfo, PsiFile file, PatternContext context) {
if (!Registry.is("ssr.in.editor.problem.highlighting")) {
return false;
}
@@ -514,7 +512,7 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
return false;
}
final List<PsiStatement> children = PsiTreeUtil.getChildrenOfTypeAsList(file, PsiStatement.class);
if (children.size() == 1 && ((PsiCodeFragmentImpl)file).getContentElementType() == JavaElementType.STATEMENTS) {
if (children.size() == 1 && context == DEFAULT_CONTEXT) {
final PsiStatement child = children.get(0);
if (child == parent && (child instanceof PsiExpressionStatement || child instanceof PsiDeclarationStatement)) {
// search for expression, type, annotation or symbol
@@ -3,9 +3,7 @@ package com.intellij.structuralsearch;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.HighlightInfoFilter;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.structuralsearch.plugin.ui.StructuralSearchDialog;
import org.jetbrains.annotations.NotNull;
@@ -22,13 +20,18 @@ public class StructuralSearchHighlightInfoFilter implements HighlightInfoFilter
return true;
}
final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
if (document == null || document.getUserData(StructuralSearchDialog.STRUCTURAL_SEARCH) == null) {
if (document == null) {
return true;
}
final String contextId = document.getUserData(StructuralSearchDialog.STRUCTURAL_SEARCH_PATTERN_CONTEXT_ID);
if (contextId == null) {
return true;
}
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByPsiElement(file);
if (profile == null) {
return true;
}
return profile.shouldShowProblem(highlightInfo, file);
final PatternContext context = StructuralSearchUtil.findPatternContextByID(contextId, profile);
return profile.shouldShowProblem(highlightInfo, file, context);
}
}
@@ -184,7 +184,7 @@ public abstract class StructuralSearchProfile {
myProblemCallback = new SoftReference<>(callback);
}
public boolean shouldShowProblem(HighlightInfo highlightInfo, PsiFile file) {
public boolean shouldShowProblem(HighlightInfo highlightInfo, PsiFile file, PatternContext context) {
return true;
}
@@ -111,7 +111,7 @@ public class StructuralSearchDialog extends DialogWrapper {
@NonNls private static final String FILTERS_VISIBLE_STATE = "structural.search.filters.visible";
public static final Key<StructuralSearchDialog> STRUCTURAL_SEARCH_DIALOG = Key.create("STRUCTURAL_SEARCH_DIALOG");
public static final Key<Boolean> STRUCTURAL_SEARCH = Key.create("STRUCTURAL_SEARCH");
public static final Key<String> STRUCTURAL_SEARCH_PATTERN_CONTEXT_ID = Key.create("STRUCTURAL_SEARCH_PATTERN_CONTEXT_ID");
public static final String USER_DEFINED = SSRBundle.message("new.template.defaultname");
private final SearchContext mySearchContext;
@@ -196,7 +196,7 @@ public class StructuralSearchDialog extends DialogWrapper {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType);
assert profile != null;
final Document document = UIUtil.createDocument(getProject(), myFileType, myDialect, myPatternContext, "", profile);
document.putUserData(STRUCTURAL_SEARCH, Boolean.TRUE);
document.putUserData(STRUCTURAL_SEARCH_PATTERN_CONTEXT_ID, (myPatternContext == null) ? "" : myPatternContext.getId());
final EditorTextField textField = new EditorTextField(document, getProject(), myFileType, false, false) {
@Override
@@ -542,11 +542,12 @@ public class StructuralSearchDialog extends DialogWrapper {
final Document searchDocument =
UIUtil.createDocument(getProject(), myFileType, myDialect, myPatternContext, mySearchCriteriaEdit.getText(), profile);
mySearchCriteriaEdit.setNewDocumentAndFileType(myFileType, searchDocument);
searchDocument.putUserData(STRUCTURAL_SEARCH, Boolean.TRUE);
final String contextId = (myPatternContext == null) ? "" : myPatternContext.getId();
searchDocument.putUserData(STRUCTURAL_SEARCH_PATTERN_CONTEXT_ID, contextId);
final Document replaceDocument =
UIUtil.createDocument(getProject(), myFileType, myDialect, myPatternContext, myReplaceCriteriaEdit.getText(), profile);
myReplaceCriteriaEdit.setNewDocumentAndFileType(myFileType, replaceDocument);
replaceDocument.putUserData(STRUCTURAL_SEARCH, Boolean.TRUE);
replaceDocument.putUserData(STRUCTURAL_SEARCH_PATTERN_CONTEXT_ID, contextId);
myFilterPanel.setProfile(profile);
initiateValidation();
}