Search in sources :

Example 1 with Configuration

use of com.intellij.structuralsearch.plugin.ui.Configuration in project intellij-community by JetBrains.

the class PredefinedConfigurationUtil method createSearchTemplateInfoSimple.

public static Configuration createSearchTemplateInfoSimple(String name, @NonNls String criteria, String category) {
    final Configuration info = createSearchTemplateInfo(name, criteria, category);
    info.getMatchOptions().setRecursiveSearch(false);
    return info;
}
Also used : Configuration(com.intellij.structuralsearch.plugin.ui.Configuration) SearchConfiguration(com.intellij.structuralsearch.plugin.ui.SearchConfiguration)

Example 2 with Configuration

use of com.intellij.structuralsearch.plugin.ui.Configuration in project intellij-community by JetBrains.

the class SSBasedInspectionTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    SSBasedInspection inspection = new SSBasedInspection();
    List<Configuration> configurations = new ArrayList<>();
    SearchConfiguration configuration = new SearchConfiguration();
    MatchOptions options = new MatchOptions();
    options.setFileType(StdFileTypes.JAVA);
    options.setSearchPattern("int i;");
    configuration.setMatchOptions(options);
    configurations.add(configuration);
    configuration = new SearchConfiguration();
    options = new MatchOptions();
    options.setFileType(StdFileTypes.JAVA);
    options.setSearchPattern("f();");
    configuration.setMatchOptions(options);
    configurations.add(configuration);
    inspection.setConfigurations(configurations, myProject);
    myWrapper = new LocalInspectionToolWrapper(inspection);
}
Also used : Configuration(com.intellij.structuralsearch.plugin.ui.Configuration) SearchConfiguration(com.intellij.structuralsearch.plugin.ui.SearchConfiguration) ArrayList(java.util.ArrayList) SSBasedInspection(com.intellij.structuralsearch.inspection.highlightTemplate.SSBasedInspection) SearchConfiguration(com.intellij.structuralsearch.plugin.ui.SearchConfiguration) LocalInspectionToolWrapper(com.intellij.codeInspection.ex.LocalInspectionToolWrapper)

Example 3 with Configuration

use of com.intellij.structuralsearch.plugin.ui.Configuration in project intellij-community by JetBrains.

the class SSBasedInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    final Map<Configuration, MatchContext> compiledOptions = SSBasedInspectionCompiledPatternsCache.getCompiledOptions(myConfigurations, holder.getProject());
    if (compiledOptions.isEmpty())
        return super.buildVisitor(holder, isOnTheFly);
    return new PsiElementVisitor() {

        final Matcher matcher = new Matcher(holder.getManager().getProject());

        final PairProcessor<MatchResult, Configuration> processor = (matchResult, configuration) -> {
            PsiElement element = matchResult.getMatch();
            String name = configuration.getName();
            LocalQuickFix fix = createQuickFix(holder.getManager().getProject(), matchResult, configuration);
            holder.registerProblem(holder.getManager().createProblemDescriptor(element, name, fix, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly));
            return true;
        };

        @Override
        public void visitElement(PsiElement element) {
            synchronized (LOCK) {
                if (LexicalNodesFilter.getInstance().accepts(element))
                    return;
                final SsrFilteringNodeIterator matchedNodes = new SsrFilteringNodeIterator(element);
                for (Map.Entry<Configuration, MatchContext> entry : compiledOptions.entrySet()) {
                    Configuration configuration = entry.getKey();
                    MatchContext context = entry.getValue();
                    if (MatcherImpl.checkIfShouldAttemptToMatch(context, matchedNodes)) {
                        final int nodeCount = context.getPattern().getNodeCount();
                        try {
                            matcher.processMatchesInElement(context, configuration, new CountingNodeIterator(nodeCount, matchedNodes), processor);
                        } catch (StructuralSearchException e) {
                            if (myProblemsReported.add(configuration.getName())) {
                                // don't overwhelm the user with messages
                                Notifications.Bus.notify(new Notification(SSRBundle.message("structural.search.title"), SSRBundle.message("template.problem", configuration.getName()), e.getMessage(), NotificationType.ERROR), element.getProject());
                            }
                        }
                        matchedNodes.reset();
                    }
                }
            }
        }
    };
}
Also used : ReplaceConfiguration(com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration) Configuration(com.intellij.structuralsearch.plugin.ui.Configuration) Matcher(com.intellij.structuralsearch.Matcher) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) PairProcessor(com.intellij.util.PairProcessor) Notification(com.intellij.notification.Notification) StructuralSearchException(com.intellij.structuralsearch.StructuralSearchException) CountingNodeIterator(com.intellij.dupLocator.iterators.CountingNodeIterator) MatchContext(com.intellij.structuralsearch.impl.matcher.MatchContext) SsrFilteringNodeIterator(com.intellij.structuralsearch.impl.matcher.iterators.SsrFilteringNodeIterator) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with Configuration

use of com.intellij.structuralsearch.plugin.ui.Configuration in project intellij-community by JetBrains.

the class SSBasedInspectionCompiledPatternsCache method getCompiledOptions.

@NotNull
static Map<Configuration, MatchContext> getCompiledOptions(@NotNull List<Configuration> configurations, @NotNull Project project) {
    final Map<Configuration, MatchContext> cache = ObjectUtils.notNull(project.getUserData(COMPILED_OPTIONS_KEY), new HashMap<Configuration, MatchContext>());
    if (!areConfigurationsInCache(configurations, cache)) {
        final Matcher matcher = new Matcher(project);
        matcher.precompileOptions(configurations, cache);
        project.putUserData(COMPILED_OPTIONS_KEY, cache);
    }
    return configurations.stream().collect(Collectors.toMap(Function.identity(), cache::get, (c1, c2) -> c2));
}
Also used : List(java.util.List) MatchContext(com.intellij.structuralsearch.impl.matcher.MatchContext) Map(java.util.Map) Project(com.intellij.openapi.project.Project) Key(com.intellij.openapi.util.Key) Configuration(com.intellij.structuralsearch.plugin.ui.Configuration) ObjectUtils(com.intellij.util.ObjectUtils) HashMap(java.util.HashMap) NotNull(org.jetbrains.annotations.NotNull) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Matcher(com.intellij.structuralsearch.Matcher) Configuration(com.intellij.structuralsearch.plugin.ui.Configuration) MatchContext(com.intellij.structuralsearch.impl.matcher.MatchContext) Matcher(com.intellij.structuralsearch.Matcher) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with Configuration

use of com.intellij.structuralsearch.plugin.ui.Configuration in project intellij-community by JetBrains.

the class MatcherImpl method precompileOptions.

public void precompileOptions(List<Configuration> configurations, final Map<Configuration, MatchContext> out) {
    for (final Configuration configuration : configurations) {
        if (out.containsKey(configuration)) {
            continue;
        }
        final MatchContext matchContext = new MatchContext();
        matchContext.setMatcher(visitor);
        final MatchOptions matchOptions = configuration.getMatchOptions();
        matchContext.setOptions(matchOptions);
        ApplicationManager.getApplication().runReadAction(() -> {
            try {
                final CompiledPattern compiledPattern = PatternCompiler.compilePattern(project, matchOptions);
                matchContext.setPattern(compiledPattern);
                out.put(configuration, matchContext);
            } catch (UnsupportedPatternException | MalformedPatternException ignored) {
            }
        });
    }
}
Also used : Configuration(com.intellij.structuralsearch.plugin.ui.Configuration)

Aggregations

Configuration (com.intellij.structuralsearch.plugin.ui.Configuration)5 Matcher (com.intellij.structuralsearch.Matcher)2 MatchContext (com.intellij.structuralsearch.impl.matcher.MatchContext)2 SearchConfiguration (com.intellij.structuralsearch.plugin.ui.SearchConfiguration)2 NotNull (org.jetbrains.annotations.NotNull)2 LocalInspectionToolWrapper (com.intellij.codeInspection.ex.LocalInspectionToolWrapper)1 CountingNodeIterator (com.intellij.dupLocator.iterators.CountingNodeIterator)1 Notification (com.intellij.notification.Notification)1 Project (com.intellij.openapi.project.Project)1 Key (com.intellij.openapi.util.Key)1 PsiElement (com.intellij.psi.PsiElement)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 StructuralSearchException (com.intellij.structuralsearch.StructuralSearchException)1 SsrFilteringNodeIterator (com.intellij.structuralsearch.impl.matcher.iterators.SsrFilteringNodeIterator)1 SSBasedInspection (com.intellij.structuralsearch.inspection.highlightTemplate.SSBasedInspection)1 ReplaceConfiguration (com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration)1 ObjectUtils (com.intellij.util.ObjectUtils)1 PairProcessor (com.intellij.util.PairProcessor)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1