SSR: script log first pass
authorBas Leijdekkers <basleijdekkers@gmail.com>
Fri, 6 Nov 2015 19:52:01 +0000 (20:52 +0100)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Fri, 6 Nov 2015 19:55:36 +0000 (20:55 +0100)
platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/compiler/PatternCompiler.java
platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptLog.java [new file with mode: 0644]
platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptPredicate.java
platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/ScriptSupport.java
platform/structuralsearch/source/com/intellij/structuralsearch/plugin/replace/impl/ReplacementBuilder.java

index 78744bad6390301a5c189433e9ed48d6f9794161..afe40f03792ce79ec0e874c814eb8a1c2b039afc 100644 (file)
@@ -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 (file)
index 0000000..340b5bb
--- /dev/null
@@ -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);
+  }
+}
index 9310eb9e70a070ac74b055e80c33e2fd55069fb1..c4c09167b99aaf9bca6d68b19fa1c98a924a0bf7 100644 (file)
@@ -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;
 
index cc6f97a295ed7fd7742f33541ae1fea788813b59..f161dabd08d581d9585dc76362707208d6300be3 100644 (file)
@@ -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<String, Object> variableMap = new HashMap<String, Object>();
+      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();
index 2ec2c6ed31753ba63c8db5724d942a4a5a5a7ce6..61c21f26180e9e4deab70c1901f2bfcc44bb5561 100644 (file)
@@ -34,8 +34,10 @@ public final class ReplacementBuilder {
   private final List<ParameterInfo> parameterizations = new ArrayList<ParameterInfo>();
   private final Map<String, ScriptSupport> replacementVarsMap;
   private final ReplaceOptions options;
+  private final Project myProject;
 
   ReplacementBuilder(final Project project,final ReplaceOptions options) {
+    myProject = project;
     replacementVarsMap = new HashMap<String, ScriptSupport>();
     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);