SSR: remove CachedValuesManager usage again as it leaked the project in tests

GitOrigin-RevId: 4abbf8baa29b05b76cef31659d86b70ca4995fcc
This commit is contained in:
Bas Leijdekkers
2020-05-07 19:36:52 +00:00
committed by intellij-monorepo-bot
parent 790698eefe
commit c496a2d345
17 changed files with 60 additions and 64 deletions
@@ -545,7 +545,7 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
final MatchOptions matchOptions = options.getMatchOptions();
final LanguageFileType fileType = matchOptions.getFileType();
final Language dialect = matchOptions.getDialect();
final PatternContext patternContext = matchOptions.getPatternContext(project);
final PatternContext patternContext = matchOptions.getPatternContext();
final PsiElement[] statements =
createPatternTree(matchOptions.getSearchPattern(), PatternTreeContext.Block, fileType, dialect, patternContext.getId(), project, false);
final boolean searchIsExpression = statements.length == 1 && statements[0].getLastChild() instanceof PsiErrorElement;
@@ -291,8 +291,8 @@ public class MatchOptions implements JDOMExternalizable {
myDialect = dialect;
}
public PatternContext getPatternContext(Project project) {
return StructuralSearchUtil.findPatternContextByID(myPatternContextId, getDialect(), project);
public PatternContext getPatternContext() {
return StructuralSearchUtil.findPatternContextByID(myPatternContextId, getDialect());
}
public void setPatternContext(PatternContext patternContext) {
@@ -6,14 +6,8 @@ import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.fileTypes.LanguageFileType;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.ModificationTracker;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.structuralsearch.impl.matcher.GlobalMatchingVisitor;
import com.intellij.structuralsearch.plugin.ui.Configuration;
import com.intellij.util.SmartList;
import one.util.streamex.StreamEx;
@@ -22,10 +16,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -37,39 +28,48 @@ public final class StructuralSearchUtil {
private static final Pattern ACCENTS = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
private static LanguageFileType ourDefaultFileType = null;
public static boolean ourUseUniversalMatchingAlgorithm = false;
private static final ModificationTracker ourMatchingAlgorithmTracker = () -> ourUseUniversalMatchingAlgorithm ? 1 : 0;
private static boolean ourUseUniversalMatchingAlgorithm = false;
private static final Map<Language, StructuralSearchProfile> cache = new HashMap<>();
private static List<Configuration> ourPredefinedConfigurations = null;
static {
StructuralSearchProfile.EP_NAME.addChangeListener(() -> {
ourPredefinedConfigurations = null;
ourDefaultFileType = null;
cache.clear();
}, null);
}
private StructuralSearchUtil() {}
public static void setUseUniversalMatchingAlgorithm(boolean useUniversalMatchingAlgorithm) {
ourUseUniversalMatchingAlgorithm = useUniversalMatchingAlgorithm;
cache.clear();
}
@Nullable
public static StructuralSearchProfile getProfileByPsiElement(@NotNull PsiElement element) {
return getProfileByLanguage(element.getLanguage(), element.getProject());
return getProfileByLanguage(element.getLanguage());
}
@Nullable
public static StructuralSearchProfile getProfileByFileType(LanguageFileType fileType, Project project) {
return getProfileByLanguage(fileType.getLanguage(), project);
public static StructuralSearchProfile getProfileByFileType(LanguageFileType fileType) {
return getProfileByLanguage(fileType.getLanguage());
}
@Nullable
public static StructuralSearchProfile getProfileByLanguage(@NotNull Language language, @NotNull Project project) {
return CachedValuesManager.getManager(project).getCachedValue(language, () -> {
for (StructuralSearchProfile profile : getProfiles()) {
if (profile.isMyLanguage(language)) {
return new CachedValueProvider.Result<>(profile, ourMatchingAlgorithmTracker);
}
public static StructuralSearchProfile getProfileByLanguage(@NotNull Language language) {
if (cache.containsKey(language)) {
return cache.get(language);
}
for (StructuralSearchProfile profile : getProfiles()) {
if (profile.isMyLanguage(language)) {
cache.put(language, profile);
return profile;
}
return new CachedValueProvider.Result<>(null, ourMatchingAlgorithmTracker);
});
}
cache.put(language, null);
return null;
}
@Contract("null -> false")
@@ -226,8 +226,8 @@ public final class StructuralSearchUtil {
return stripAccents(normalizeWhiteSpace(text));
}
public static PatternContext findPatternContextByID(@Nullable String id, @NotNull Language language, @NotNull Project project) {
return findPatternContextByID(id, getProfileByLanguage(language, project));
public static PatternContext findPatternContextByID(@Nullable String id, @NotNull Language language) {
return findPatternContextByID(id, getProfileByLanguage(language));
}
public static PatternContext findPatternContextByID(@Nullable String id, @Nullable StructuralSearchProfile profile) {
@@ -7,7 +7,6 @@ import com.intellij.dupLocator.util.NodeFilter;
import com.intellij.lang.Language;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
@@ -157,15 +156,15 @@ public class GlobalMatchingVisitor extends AbstractMatchingVisitor {
final Language language = element.getLanguage();
PsiElementVisitor visitor = myLanguage2MatchingVisitor.get(language);
if (visitor == null) {
visitor = createMatchingVisitor(language, element.getProject());
visitor = createMatchingVisitor(language);
myLanguage2MatchingVisitor.put(language, visitor);
}
return visitor;
}
@Nullable
private PsiElementVisitor createMatchingVisitor(Language language, Project project) {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language, project);
private PsiElementVisitor createMatchingVisitor(Language language) {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language);
if (profile == null) {
LOG.warn("there is no StructuralSearchProfile for language " + language.getID());
return null;
@@ -23,7 +23,7 @@ public class MatcherImplUtil {
LanguageFileType fileType,
Project project,
boolean physical) {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(fileType.getLanguage(), project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(fileType.getLanguage());
if (profile != null) {
return profile.createPatternTree(text, context, fileType, fileType.getLanguage(), null, project, physical);
}
@@ -39,7 +39,7 @@ public class MatcherImplUtil {
if (language == null) {
language = fileType.getLanguage();
}
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language, project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language);
if (profile != null) {
return profile.createPatternTree(text, contextInfo, fileType, language, project, physical);
}
@@ -112,7 +112,7 @@ public class GlobalCompilingVisitor {
myCodeBlockLevel = 0;
this.context = context;
final StructuralSearchProfile profile =
StructuralSearchUtil.getProfileByFileType(context.getOptions().getFileType(), context.getProject());
StructuralSearchUtil.getProfileByFileType(context.getOptions().getFileType());
assert profile != null;
profile.compile(elements, this);
@@ -218,7 +218,7 @@ public class GlobalCompilingVisitor {
return;
}
final StructuralSearchProfile profile =
StructuralSearchUtil.getProfileByFileType(compileContext.getOptions().getFileType(), compileContext.getProject());
StructuralSearchUtil.getProfileByFileType(compileContext.getOptions().getFileType());
assert profile != null;
if (profile.getReservedWords().contains(word)) return; // skip our special annotations !!!
@@ -60,7 +60,7 @@ public class PatternCompiler {
boolean checkForErrors, boolean optimizeScope)
throws MalformedPatternException, NoMatchFoundException {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(options.getFileType(), project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(options.getFileType());
if (profile == null) {
LOG.warn("no profile found for " + options.getFileType().getDescription());
return null;
@@ -479,7 +479,7 @@ public class PatternCompiler {
}
}
addExtensionPredicates(options, constraint, handler, project);
addExtensionPredicates(options, constraint, handler);
addScriptConstraint(project, name, constraint, handler, variableNames, options, checkForErrors);
if (!StringUtil.isEmptyOrSpaces(constraint.getContainsConstraint())) {
@@ -516,7 +516,7 @@ public class PatternCompiler {
addPredicate(handler, predicate);
}
addExtensionPredicates(options, constraint, handler, project);
addExtensionPredicates(options, constraint, handler);
addScriptConstraint(project, Configuration.CONTEXT_VAR_NAME, constraint, handler, variableNames, options, checkForErrors);
}
@@ -525,7 +525,7 @@ public class PatternCompiler {
final PsiElement[] patternElements;
try {
PatternContextInfo contextInfo = new PatternContextInfo(PatternTreeContext.Block,
options.getPatternContext(project),
options.getPatternContext(),
constraint != null ? constraint.getContextConstraint() : null);
patternElements = MatcherImplUtil.createTreeFromText(buf.toString(), contextInfo, options.getFileType(),
options.getDialect(), project, false);
@@ -555,11 +555,8 @@ public class PatternCompiler {
return elements;
}
private static void addExtensionPredicates(MatchOptions options,
MatchVariableConstraint constraint,
SubstitutionHandler handler,
Project project) {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(options.getFileType(), project);
private static void addExtensionPredicates(MatchOptions options, MatchVariableConstraint constraint, SubstitutionHandler handler) {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(options.getFileType());
assert profile != null;
for (MatchPredicate matchPredicate : profile.getCustomPredicates(constraint, handler.getName(), options)) {
addPredicate(handler, matchPredicate);
@@ -83,12 +83,12 @@ public final class ReplacementBuilder {
}
final LanguageFileType fileType = options.getMatchOptions().getFileType();
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(fileType, project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(fileType);
if (profile != null) {
try {
final PsiElement[] elements = MatcherImplUtil.createTreeFromText(
options.getReplacement(),
new PatternContextInfo(PatternTreeContext.Block, options.getMatchOptions().getPatternContext(project)),
new PatternContextInfo(PatternTreeContext.Block, options.getMatchOptions().getPatternContext()),
fileType,
options.getMatchOptions().getDialect(),
project,
@@ -127,7 +127,7 @@ public final class ReplacementBuilder {
final StringBuilder result = new StringBuilder(replacement);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(type, myProject);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(type);
assert profile != null;
List<ParameterInfo> sorted = new SmartList<>(parameterizations.values());
@@ -43,7 +43,7 @@ public class Replacer {
public Replacer(Project project, ReplaceOptions options) {
this.project = project;
this.options = options;
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(options.getMatchOptions().getFileType(), project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(options.getMatchOptions().getFileType());
assert profile != null;
replaceHandler = profile.getReplaceHandler(project, options);
assert replaceHandler != null;
@@ -301,7 +301,7 @@ public class Replacer {
}
}
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(fileType, project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(fileType);
assert profile != null;
ReadAction.run(() -> profile.checkReplacementPattern(project, options));
} catch (IncorrectOperationException ex) {
@@ -89,7 +89,7 @@ public final class ReplacementPreviewDialog extends DialogWrapper {
protected JComponent createCenterPanel() {
final JComponent centerPanel = new JPanel(new BorderLayout());
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType, project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType);
assert profile != null;
replacement = UIUtil.createEditor(project, myFileType, null, "", false, profile);
@@ -60,14 +60,14 @@ public class FileTypeSelector extends ComboBox<FileTypeInfo> {
private static DefaultComboBoxModel<FileTypeInfo> createModel(Project project) {
final List<LanguageFileType> types = new ArrayList<>();
for (LanguageFileType fileType : StructuralSearchUtil.getSuitableFileTypes()) {
if (StructuralSearchUtil.getProfileByFileType(fileType, project) != null) {
if (StructuralSearchUtil.getProfileByFileType(fileType) != null) {
types.add(fileType);
}
}
types.sort((o1, o2) -> o1.getDescription().compareToIgnoreCase(o2.getDescription()));
final List<FileTypeInfo> infos = new ArrayList<>();
for (LanguageFileType fileType : types) {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(fileType, project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(fileType);
assert profile != null;
final Language language = fileType.getLanguage();
final List<PatternContext> patternContexts = new ArrayList<>(profile.getPatternContexts());
@@ -208,7 +208,7 @@ public class SelectTemplateDialog extends DialogWrapper {
final MatchOptions matchOptions = configuration.getMatchOptions();
UIUtil.setContent(searchPatternEditor, matchOptions.getSearchPattern());
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(matchOptions.getFileType(), project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(matchOptions.getFileType());
if (profile != null) {
TemplateEditorUtil.setHighlighter(searchPatternEditor, UIUtil.getTemplateContextType(profile));
}
@@ -250,7 +250,7 @@ public class StructuralSearchDialog extends DialogWrapper implements ProjectMana
}
private EditorTextField createEditor(boolean replace) {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType, getProject());
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType);
assert profile != null;
final Document document = UIUtil.createDocument(getProject(), myFileType, myDialect, myPatternContext, "", profile);
document.addDocumentListener(this, myDisposable);
@@ -543,7 +543,7 @@ public class StructuralSearchDialog extends DialogWrapper implements ProjectMana
myMatchCase.addActionListener(e -> initiateValidation());
myFileType = UIUtil.detectFileType(mySearchContext);
myDialect = myFileType.getLanguage();
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType, getProject());
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType);
if (profile != null) {
final List<PatternContext> contexts = profile.getPatternContexts();
if (!contexts.isEmpty()) {
@@ -565,7 +565,7 @@ public class StructuralSearchDialog extends DialogWrapper implements ProjectMana
myDialect = item.getDialect();
myPatternContext = item.getContext();
final String contextId = (myPatternContext == null) ? "" : myPatternContext.getId();
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType, getProject());
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType);
assert profile != null;
final Document searchDocument =
@@ -1062,7 +1062,7 @@ public class StructuralSearchDialog extends DialogWrapper implements ProjectMana
myMatchCase.setSelected(matchOptions.isCaseSensitiveMatch());
myFileTypesComboBox.setSelectedItem(matchOptions.getFileType(), matchOptions.getDialect(), matchOptions.getPatternContext(getProject()));
myFileTypesComboBox.setSelectedItem(matchOptions.getFileType(), matchOptions.getDialect(), matchOptions.getPatternContext());
final Editor searchEditor = mySearchCriteriaEdit.getEditor();
if (searchEditor != null) {
searchEditor.putUserData(SubstitutionShortInfoHandler.CURRENT_CONFIGURATION_KEY, myConfiguration);
@@ -1144,7 +1144,7 @@ public class StructuralSearchDialog extends DialogWrapper implements ProjectMana
}
private String getPattern(EditorTextField textField) {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType, getProject());
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType);
assert profile != null;
final Document document = textField.getDocument();
final String pattern = ReadAction.compute(() -> {
@@ -1316,7 +1316,7 @@ public class StructuralSearchDialog extends DialogWrapper implements ProjectMana
final EditorEx editor = super.createEditor();
editor.setHorizontalScrollbarVisible(true);
editor.setVerticalScrollbarVisible(true);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType, getProject());
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(myFileType);
assert profile != null;
TemplateEditorUtil.setHighlighter(editor, UIUtil.getTemplateContextType(profile));
SubstitutionShortInfoHandler.install(editor, variableName -> {
@@ -262,7 +262,7 @@ public class UIUtil {
}
if (context != null) {
final Language language = context.getLanguage();
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language, searchContext.getProject());
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByLanguage(language);
if (profile != null) {
final LanguageFileType fileType = profile.detectFileType(context);
return fileType != null ? fileType : language.getAssociatedFileType();
@@ -171,7 +171,7 @@ public class FilterPanel implements FilterTable {
@Override
@NotNull
public StructuralSearchProfile getProfile() {
final StructuralSearchProfile fileType = StructuralSearchUtil.getProfileByFileType(myFileType, myProject);
final StructuralSearchProfile fileType = StructuralSearchUtil.getProfileByFileType(myFileType);
assert fileType != null;
return fileType;
}
@@ -65,7 +65,7 @@ public abstract class StructuralSearchTestCase extends LightQuickFixTestCase {
public static String checkApplicableConstraints(MatchOptions options, Project project) {
final CompiledPattern compiledPattern = PatternCompiler.compilePattern(project, options, true, false);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(options.getFileType(), project);
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(options.getFileType());
assert profile != null;
for (String varName : options.getVariableConstraintNames()) {
final List<PsiElement> nodes = compiledPattern.getVariableNodes(varName);
@@ -112,7 +112,7 @@ public class GroovyStructuralSearchTest extends StructuralSearchTestCase {
doTest(s, "def $name$ = {\n" +
" '_T+\n" +
"}", 0, 0);
final PatternContext old = options.getPatternContext(getProject());
final PatternContext old = options.getPatternContext();
try {
options.setPatternContext(GroovyStructuralSearchProfile.CLASS_CONTEXT);
doTest(s, "def $name$ = {\n" +