From d7f9b94114b1035a74c2735d2833737d41f49656 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 6 Nov 2015 20:52:01 +0100 Subject: [PATCH] SSR: script log first pass --- .../matcher/compiler/PatternCompiler.java | 8 +-- .../impl/matcher/predicates/ScriptLog.java | 60 +++++++++++++++++++ .../matcher/predicates/ScriptPredicate.java | 6 +- .../matcher/predicates/ScriptSupport.java | 11 +++- .../replace/impl/ReplacementBuilder.java | 4 +- 5 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptLog.java diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/PatternCompiler.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/PatternCompiler.java index 78744bad6390..afe40f03792c 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/PatternCompiler.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/PatternCompiler.java @@ -437,7 +437,7 @@ public class PatternCompiler { addPredicate(handler, matchPredicate); } - addScriptConstraint(name, constraint, handler); + addScriptConstraint(project, name, constraint, handler); if (!StringUtil.isEmptyOrSpaces(constraint.getContainsConstraint())) { predicate = new ContainsPredicate(name, constraint.getContainsConstraint()); @@ -474,7 +474,7 @@ public class PatternCompiler { addPredicate(handler,predicate); } - addScriptConstraint(Configuration.CONTEXT_VAR_NAME, constraint, handler); + addScriptConstraint(project, Configuration.CONTEXT_VAR_NAME, constraint, handler); } buf.append(text.substring(prevOffset,text.length())); @@ -505,12 +505,12 @@ public class PatternCompiler { return elements; } - private static void addScriptConstraint(String name, MatchVariableConstraint constraint, SubstitutionHandler handler) { + private static void addScriptConstraint(Project project, String name, MatchVariableConstraint constraint, SubstitutionHandler handler) { if (constraint.getScriptCodeConstraint()!= null && constraint.getScriptCodeConstraint().length() > 2) { final String script = StringUtil.stripQuotesAroundValue(constraint.getScriptCodeConstraint()); final String s = ScriptSupport.checkValidScript(script); if (s != null) throw new MalformedPatternException("Script constraint for " + constraint.getName() + " has problem "+s); - MatchPredicate predicate = new ScriptPredicate(name, script); + MatchPredicate predicate = new ScriptPredicate(project, name, script); addPredicate(handler,predicate); } } diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptLog.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptLog.java new file mode 100644 index 000000000000..340b5bb45dc4 --- /dev/null +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptLog.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.structuralsearch.impl.matcher.predicates; + +import com.intellij.notification.NotificationGroup; +import com.intellij.notification.NotificationType; +import com.intellij.openapi.project.Project; +import com.intellij.structuralsearch.SSRBundle; + +/** + * @author Bas Leijdekkers + */ +public class ScriptLog { + + private final NotificationGroup myEventLog; + private final Project myProject; + + public ScriptLog(Project project) { + myProject = project; + myEventLog = NotificationGroup.logOnlyGroup(SSRBundle.message("structural.search.title")); + } + + public void info(Object message) { + log(message, NotificationType.INFORMATION); + } + + public void warn(Object message) { + log(message, NotificationType.WARNING); + } + + public void error(Object message) { + log(message, NotificationType.ERROR); + } + + private void log(Object message, NotificationType type) { + final StackTraceElement[] stackTrace = new Throwable().getStackTrace(); + String location = ""; + for (StackTraceElement e : stackTrace) { + final String methodName = e.getMethodName(); + if ("run".equals(methodName)) { + location = "(" + e.getFileName() + ":" + e.getLineNumber() + ") "; + break; + } + } + myEventLog.createNotification(location + String.valueOf(message), type).notify(myProject); + } +} diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptPredicate.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptPredicate.java index 9310eb9e70a0..c4c09167b99a 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptPredicate.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptPredicate.java @@ -1,5 +1,6 @@ package com.intellij.structuralsearch.impl.matcher.predicates; +import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import com.intellij.structuralsearch.impl.matcher.MatchContext; @@ -9,11 +10,12 @@ import com.intellij.structuralsearch.impl.matcher.MatchContext; public class ScriptPredicate extends AbstractStringBasedPredicate { private final ScriptSupport scriptSupport; - public ScriptPredicate(String name, String within) { + public ScriptPredicate(Project project, String name, String within) { super(name, within); - scriptSupport = new ScriptSupport(within, name); + scriptSupport = new ScriptSupport(project, within, name); } + @Override public boolean match(PsiElement node, PsiElement match, int start, int end, MatchContext context) { if (match == null) return false; diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptSupport.java b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptSupport.java index cc6f97a295ed..f161dabd08d5 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptSupport.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptSupport.java @@ -1,11 +1,13 @@ package com.intellij.structuralsearch.impl.matcher.predicates; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; import com.intellij.structuralsearch.MatchResult; import com.intellij.structuralsearch.SSRBundle; import com.intellij.structuralsearch.StructuralSearchException; import com.intellij.structuralsearch.StructuralSearchUtil; +import com.intellij.structuralsearch.plugin.ui.Configuration; import groovy.lang.Binding; import groovy.lang.GroovyRuntimeException; import groovy.lang.GroovyShell; @@ -32,12 +34,14 @@ import java.util.Map; */ public class ScriptSupport { private final Script script; + private final ScriptLog myScriptLog; - public ScriptSupport(String text, String name) { + public ScriptSupport(Project project, String text, String name) { + myScriptLog = new ScriptLog(project); File scriptFile = new File(text); GroovyShell shell = new GroovyShell(); try { - script = scriptFile.exists() ? shell.parse(scriptFile):shell.parse(text, name); + script = scriptFile.exists() ? shell.parse(scriptFile) : shell.parse(text, name); } catch (Exception ex) { Logger.getInstance(getClass().getName()).error(ex); throw new RuntimeException(ex); @@ -78,6 +82,7 @@ public class ScriptSupport { public String evaluate(MatchResult result, PsiElement context) { try { final HashMap variableMap = new HashMap(); + variableMap.put("__log__", myScriptLog); if (result != null) { buildVariableMap(result, variableMap); if (context == null) { @@ -87,7 +92,7 @@ public class ScriptSupport { final Binding binding = new Binding(variableMap); context = StructuralSearchUtil.getPresentableElement(context); - binding.setVariable("__context__", context); + binding.setVariable(Configuration.CONTEXT_VAR_NAME, context); script.setBinding(binding); final Object o = script.run(); diff --git a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementBuilder.java b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementBuilder.java index 2ec2c6ed3175..61c21f26180e 100644 --- a/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementBuilder.java +++ b/platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementBuilder.java @@ -34,8 +34,10 @@ public final class ReplacementBuilder { private final List parameterizations = new ArrayList(); private final Map replacementVarsMap; private final ReplaceOptions options; + private final Project myProject; ReplacementBuilder(final Project project,final ReplaceOptions options) { + myProject = project; replacementVarsMap = new HashMap(); this.options = options; String _replacement = options.getReplacement(); @@ -177,7 +179,7 @@ public final class ReplacementBuilder { if (scriptSupport == null) { String constraint = options.getVariableDefinition(info.getName()).getScriptCodeConstraint(); - scriptSupport = new ScriptSupport(StringUtil.stripQuotesAroundValue(constraint), info.getName()); + scriptSupport = new ScriptSupport(myProject, StringUtil.stripQuotesAroundValue(constraint), info.getName()); replacementVarsMap.put(info.getName(), scriptSupport); } return scriptSupport.evaluate(match, null); -- 2.32.0