Added soutp live template.
This commit is contained in:
@@ -28,4 +28,11 @@
|
||||
<option name="KOTLIN_STATEMENT" value="true" />
|
||||
</context>
|
||||
</template>
|
||||
<template resource-bundle="org.jetbrains.jet.plugin.JetBundle" key="livetemplate.description.soutp"
|
||||
name="soutp" toReformat="true" toShortenFQNames="true" value="println($FORMAT$)">
|
||||
<variable alwaysStopAt="false" defaultValue="" expression="groovyScript("'\"' + _1.collect { it + ' = [${' + it + '}]'}.join(', ') + '\"'", functionParameters())" name="FORMAT" />
|
||||
<context>
|
||||
<option name="KOTLIN_STATEMENT" value="true"/>
|
||||
</context>
|
||||
</template>
|
||||
</templateSet>
|
||||
|
||||
@@ -70,6 +70,7 @@
|
||||
<liveTemplateContext implementation="org.jetbrains.jet.plugin.liveTemplates.JetTemplateContextType$Statement"/>
|
||||
<defaultLiveTemplatesProvider implementation="org.jetbrains.jet.plugin.liveTemplates.JetLiveTemplatesProvider"/>
|
||||
<liveTemplateMacro implementation="org.jetbrains.jet.plugin.liveTemplates.macro.JetVariableOfTypeMacro"/>
|
||||
<liveTemplateMacro implementation="org.jetbrains.jet.plugin.liveTemplates.macro.JetFunctionParametersMacro"/>
|
||||
|
||||
<annotator language="jet" implementationClass="org.jetbrains.jet.plugin.annotations.SoftKeywordsAnnotator"/>
|
||||
<annotator language="jet" implementationClass="org.jetbrains.jet.plugin.annotations.LabelsAnnotator"/>
|
||||
|
||||
@@ -28,4 +28,6 @@ change.to.backing.field=Change reference to backing field
|
||||
implement.members=Implement members
|
||||
|
||||
livetemplate.description.main=main() function
|
||||
macro.variable.of.type=kotlinVariableOfType(Type)
|
||||
livetemplate.description.soutp=Prints function parameter names and values to System.out
|
||||
macro.variable.of.type=kotlinVariableOfType(Type)
|
||||
macro.fun.parameters=functionParameters()
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.jetbrains.jet.plugin.liveTemplates.macro;
|
||||
|
||||
import com.intellij.codeInsight.template.*;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.psi.JetFunction;
|
||||
import org.jetbrains.jet.lang.psi.JetParameter;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Evgeny Gerashchenko
|
||||
* @since 1/30/12
|
||||
*/
|
||||
public class JetFunctionParametersMacro extends Macro {
|
||||
public String getName() {
|
||||
return "functionParameters";
|
||||
}
|
||||
|
||||
public String getPresentableName() {
|
||||
return JetBundle.message("macro.fun.parameters");
|
||||
}
|
||||
|
||||
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());
|
||||
if (file == null) return null;
|
||||
PsiElement place = file.findElementAt(offset);
|
||||
while (place != null){
|
||||
if (place instanceof JetFunction) {
|
||||
List<Result> result = new ArrayList<Result>();
|
||||
for (JetParameter param : ((JetFunction) place).getValueParameters()) {
|
||||
String name = param.getName();
|
||||
assert name != null;
|
||||
result.add(new TextResult(name));
|
||||
}
|
||||
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