mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-09 20:42:52 +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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user