Search in sources :

Example 1 with StructuralSearchException

use of com.intellij.structuralsearch.StructuralSearchException in project intellij-community by JetBrains.

the class ScriptSupport method evaluate.

public String evaluate(MatchResult result, PsiElement context) {
    try {
        final HashMap<String, Object> variableMap = new HashMap<>();
        variableMap.put(ScriptLog.SCRIPT_LOG_VAR_NAME, myScriptLog);
        if (result != null) {
            buildVariableMap(result, variableMap);
            if (context == null) {
                context = result.getMatch();
            }
        }
        context = StructuralSearchUtil.getPresentableElement(context);
        variableMap.put(myName, context);
        variableMap.put(Configuration.CONTEXT_VAR_NAME, context);
        script.setBinding(new Binding(variableMap));
        final Object o = script.run();
        return String.valueOf(o);
    } catch (GroovyRuntimeException ex) {
        throw new StructuralSearchException(SSRBundle.message("groovy.script.error", ex.getMessage()));
    } finally {
        script.setBinding(null);
    }
}
Also used : Binding(groovy.lang.Binding) StructuralSearchException(com.intellij.structuralsearch.StructuralSearchException) HashMap(java.util.HashMap) GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 2 with StructuralSearchException

use of com.intellij.structuralsearch.StructuralSearchException 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)

Aggregations

StructuralSearchException (com.intellij.structuralsearch.StructuralSearchException)2 CountingNodeIterator (com.intellij.dupLocator.iterators.CountingNodeIterator)1 Notification (com.intellij.notification.Notification)1 PsiElement (com.intellij.psi.PsiElement)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 Matcher (com.intellij.structuralsearch.Matcher)1 MatchContext (com.intellij.structuralsearch.impl.matcher.MatchContext)1 SsrFilteringNodeIterator (com.intellij.structuralsearch.impl.matcher.iterators.SsrFilteringNodeIterator)1 ReplaceConfiguration (com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration)1 Configuration (com.intellij.structuralsearch.plugin.ui.Configuration)1 PairProcessor (com.intellij.util.PairProcessor)1 Binding (groovy.lang.Binding)1 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)1 HashMap (java.util.HashMap)1 NotNull (org.jetbrains.annotations.NotNull)1