mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-06 06:21:16 +07:00
IDEA-77745 Please add a "Print parameters to system out" live template
This commit is contained in:
@@ -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<Result, String>() {
|
||||
@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());
|
||||
|
||||
@@ -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> result = new ArrayList<Result>();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Result> myComponents;
|
||||
|
||||
public ListResult(List<Result> components) {
|
||||
myComponents = components;
|
||||
}
|
||||
|
||||
public List<Result> 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) {
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -26,6 +26,13 @@
|
||||
<option name="GROOVY_STATEMENT" value="true" />
|
||||
</context>
|
||||
</template>
|
||||
<template resource-bundle="messages.CodeInsightBundle" key="livetemplate.description.soutp"
|
||||
name="soutp" toReformat="true" toShortenFQNames="true" value="println "$FORMAT$"">
|
||||
<variable alwaysStopAt="false" defaultValue="" expression="groovyScript("_1.collect { it + ' = [$' + it + ']'}.join(', ')", methodParameters())" name="FORMAT" />
|
||||
<context>
|
||||
<option name="GROOVY_STATEMENT" value="true" />
|
||||
</context>
|
||||
</template>
|
||||
<template resource-bundle="messages.CodeInsightBundle" key="livetemplate.description.soutv"
|
||||
name="soutv" toReformat="true" toShortenFQNames="true" value="println "$EXPR_COPY$ = $DOLLAR$$EXPR$"">
|
||||
<variable alwaysStopAt="false" defaultValue=""$"" expression="" name="DOLLAR" />
|
||||
|
||||
+27
-21
@@ -4,20 +4,17 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.lang;
|
||||
|
||||
import com.intellij.codeInsight.lookup.Lookup;
|
||||
import com.intellij.codeInsight.lookup.LookupManager;
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl;
|
||||
import com.intellij.codeInsight.template.impl.TemplateImpl;
|
||||
import com.intellij.codeInsight.template.impl.TemplateManagerImpl;
|
||||
import com.intellij.codeInsight.template.impl.TemplateSettings;
|
||||
import com.intellij.codeInsight.template.impl.actions.ListTemplatesAction;
|
||||
import com.intellij.openapi.application.Result;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.plugins.groovy.util.TestUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import com.intellij.codeInsight.lookup.Lookup
|
||||
import com.intellij.codeInsight.lookup.LookupManager
|
||||
import com.intellij.codeInsight.lookup.impl.LookupImpl
|
||||
import com.intellij.codeInsight.template.impl.TemplateImpl
|
||||
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
|
||||
import com.intellij.codeInsight.template.impl.TemplateSettings
|
||||
import com.intellij.codeInsight.template.impl.actions.ListTemplatesAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
|
||||
import org.jetbrains.plugins.groovy.util.TestUtils
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
@@ -43,17 +40,26 @@ public class GroovyLiveTemplatesTest extends LightCodeInsightFixtureTestCase{
|
||||
public void testSoutv() {
|
||||
myFixture.configureByText("a.groovy", "def x = 2\nsoutv<caret>");
|
||||
expandTemplate(myFixture.getEditor());
|
||||
myFixture.checkResult("def x = 2\nprintln \"x = $x\"");
|
||||
myFixture.checkResult("def x = 2\nprintln \"x = \$x\"");
|
||||
}
|
||||
|
||||
public void testSoutp() {
|
||||
myFixture.configureByText("a.groovy", """
|
||||
void usage(int num, boolean someBoolean, List<String> args){
|
||||
soutp<caret>
|
||||
}
|
||||
""");
|
||||
expandTemplate(myFixture.getEditor());
|
||||
myFixture.checkResult '''
|
||||
void usage(int num, boolean someBoolean, List<String> args){
|
||||
println "num = [$num], someBoolean = [$someBoolean], args = [$args]"
|
||||
}
|
||||
'''
|
||||
}
|
||||
|
||||
public static void expandTemplate(final Editor editor) {
|
||||
new WriteCommandAction(editor.getProject()) {
|
||||
@Override
|
||||
protected void run(Result result) throws Throwable {
|
||||
new ListTemplatesAction().actionPerformedImpl(editor.getProject(), editor);
|
||||
((LookupImpl)LookupManager.getActiveLookup(editor)).finishLookup(Lookup.NORMAL_SELECT_CHAR);
|
||||
}
|
||||
}.execute();
|
||||
new ListTemplatesAction().actionPerformedImpl(editor.getProject(), editor);
|
||||
((LookupImpl)LookupManager.getActiveLookup(editor)).finishLookup(Lookup.NORMAL_SELECT_CHAR);
|
||||
}
|
||||
|
||||
public void testGroovyStatementContext() throws Exception {
|
||||
@@ -876,6 +876,7 @@
|
||||
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.AnnotatedMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.MethodNameMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.MethodParametersMacro"/>
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.MethodReturnTypeMacro"/>
|
||||
|
||||
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.ClassNameMacro"/>
|
||||
|
||||
@@ -32,6 +32,14 @@
|
||||
<option name="GROOVY_STATEMENT" value="false" />
|
||||
</context>
|
||||
</template>
|
||||
<template resource-bundle="messages.CodeInsightBundle" key="livetemplate.description.soutp"
|
||||
name="soutp" toReformat="true" toShortenFQNames="true" value="System.out.println($FORMAT$);">
|
||||
<variable alwaysStopAt="false" defaultValue="" expression="groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", methodParameters())" name="FORMAT" />
|
||||
<context>
|
||||
<option name="JAVA_CODE" value="false" />
|
||||
<option name="JAVA_STATEMENT" value="true" />
|
||||
</context>
|
||||
</template>
|
||||
<template resource-bundle="messages.CodeInsightBundle" key="livetemplate.description.soutv"
|
||||
name="soutv" toReformat="true" toShortenFQNames="true" value="System.out.println("$EXPR_COPY$ = " + $EXPR$);">
|
||||
<variable alwaysStopAt="true" defaultValue=""expr"" expression="variableOfType("")" name="EXPR" />
|
||||
|
||||
Reference in New Issue
Block a user