addPredicate(handler, matchPredicate);
}
- addScriptConstraint(name, constraint, handler);
+ addScriptConstraint(project, name, constraint, handler);
if (!StringUtil.isEmptyOrSpaces(constraint.getContainsConstraint())) {
predicate = new ContainsPredicate(name, constraint.getContainsConstraint());
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()));
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);
}
}
--- /dev/null
+/*
+ * 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);
+ }
+}
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;
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;
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;
*/
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);
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) {
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();
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();
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);