From 3fc65693dd6b436a87f25c108e8c5eab4a598445 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 23 Dec 2011 19:50:46 +0100 Subject: [PATCH] IDEA-77745 Please add a "Print parameters to system out" live template --- .../template/macro/GroovyScriptMacro.java | 15 +++- .../template/macro/MethodParametersMacro.java | 69 +++++++++++++++++++ .../codeInsight/template/ListResult.java | 51 ++++++++++++++ .../src/messages/CodeInsightBundle.properties | 2 + .../groovy/resources/liveTemplates/Groovy.xml | 7 ++ ...st.java => GroovyLiveTemplatesTest.groovy} | 48 +++++++------ resources/src/META-INF/IdeaPlugin.xml | 1 + resources/src/liveTemplates/output.xml | 8 +++ 8 files changed, 179 insertions(+), 22 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInsight/template/macro/MethodParametersMacro.java create mode 100644 platform/lang-impl/src/com/intellij/codeInsight/template/ListResult.java rename plugins/groovy/test/org/jetbrains/plugins/groovy/lang/{GroovyLiveTemplatesTest.java => GroovyLiveTemplatesTest.groovy} (80%) diff --git a/java/java-impl/src/com/intellij/codeInsight/template/macro/GroovyScriptMacro.java b/java/java-impl/src/com/intellij/codeInsight/template/macro/GroovyScriptMacro.java index f7a2ea8c74a6..83f2215b512b 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/macro/GroovyScriptMacro.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/macro/GroovyScriptMacro.java @@ -20,6 +20,8 @@ import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.codeInsight.template.*; import com.intellij.openapi.util.text.StringUtil; +import com.intellij.util.Function; +import com.intellij.util.containers.ContainerUtil; import groovy.lang.Binding; import groovy.lang.GroovyShell; import groovy.lang.Script; @@ -60,7 +62,18 @@ public class GroovyScriptMacro extends Macro { for(int i = 1; i < params.length; ++i) { Result paramResult = params[i].calculateResult(context); - binding.setVariable("_"+i, paramResult != null ? paramResult.toString():null); + Object value = null; + if (paramResult instanceof ListResult) { + value = ContainerUtil.map2List(((ListResult)paramResult).getComponents(), new Function() { + @Override + public String fun(Result result) { + return result.toString(); + } + }); + } else if (paramResult != null) { + value = paramResult.toString(); + } + binding.setVariable("_"+i, value); } binding.setVariable("_editor", context.getEditor()); diff --git a/java/java-impl/src/com/intellij/codeInsight/template/macro/MethodParametersMacro.java b/java/java-impl/src/com/intellij/codeInsight/template/macro/MethodParametersMacro.java new file mode 100644 index 000000000000..71d6fcd15ec0 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/template/macro/MethodParametersMacro.java @@ -0,0 +1,69 @@ +/* + * Copyright 2000-2011 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.codeInsight.template.macro; + +import com.intellij.codeInsight.CodeInsightBundle; +import com.intellij.codeInsight.template.*; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import org.jetbrains.annotations.NotNull; + +import java.util.ArrayList; +import java.util.List; + +public class MethodParametersMacro extends Macro { + + public String getName() { + return "methodParameters"; + } + + public String getPresentableName() { + return CodeInsightBundle.message("macro.method.parameters"); + } + + @NotNull + public String getDefaultValue() { + return "a"; + } + + public Result calculateResult(@NotNull Expression[] params, final ExpressionContext context) { + Project project = context.getProject(); + int templateStartOffset = context.getTemplateStartOffset(); + final int offset = templateStartOffset > 0 ? context.getTemplateStartOffset() - 1 : context.getTemplateStartOffset(); + + PsiDocumentManager.getInstance(project).commitAllDocuments(); + + PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument()); + PsiElement place = file.findElementAt(offset); + while(place != null){ + if (place instanceof PsiMethod){ + List result = new ArrayList(); + for (PsiParameter parameter : ((PsiMethod)place).getParameterList().getParameters()) { + result.add(new TextResult(parameter.getName())); + } + return new ListResult(result); + } + place = place.getParent(); + } + return null; + } + + @Override + public boolean isAcceptableInContext(TemplateContextType context) { + return context instanceof JavaCodeContextType; + } + +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/ListResult.java b/platform/lang-impl/src/com/intellij/codeInsight/template/ListResult.java new file mode 100644 index 000000000000..91992ac44045 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/ListResult.java @@ -0,0 +1,51 @@ +/* + * Copyright 2000-2011 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.codeInsight.template; + +import com.intellij.openapi.editor.Document; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; + +import java.util.List; + +/** + * @author peter + */ +public class ListResult implements Result { + private final List myComponents; + + public ListResult(List components) { + myComponents = components; + } + + public List getComponents() { + return myComponents; + } + + @Override + public String toString() { + return myComponents.toString(); + } + + @Override + public boolean equalsToText(String text, PsiElement context) { + return false; + } + + @Override + public void handleFocused(PsiFile psiFile, Document document, int segmentStart, int segmentEnd) { + } +} diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index 3fa41abf3020..fc7fbe6340e3 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -325,6 +325,7 @@ macro.iterable.component.type=iterableComponentType(ArrayOrIterable) macro.iterable.variable=iterableVariable() macro.linenumber=lineNumber() macro.methodname=methodName() +macro.method.parameters=methodParameters() macro.qualified.class.name=qualifiedClassName() macro.right.side.type=rightSideType() macro.suggest.index.name=suggestIndexName() @@ -359,6 +360,7 @@ livetemplate.description.serr=Prints a string to System.err livetemplate.description.sout=Prints a string to System.out livetemplate.description.souf=Prints a formatted string to System.out livetemplate.description.soutm=Prints current class and method names to System.out +livetemplate.description.soutp=Prints method parameter names and values to System.out livetemplate.description.soutv=Prints a value to System.out livetemplate.description.st=String livetemplate.description.psf=public static final diff --git a/plugins/groovy/resources/liveTemplates/Groovy.xml b/plugins/groovy/resources/liveTemplates/Groovy.xml index 9a57cc2e1831..99780ecbcb62 100644 --- a/plugins/groovy/resources/liveTemplates/Groovy.xml +++ b/plugins/groovy/resources/liveTemplates/Groovy.xml @@ -26,6 +26,13 @@