本文整理汇总了Java中com.intellij.codeInsight.template.Template.addVariableSegment方法的典型用法代码示例。如果您正苦于以下问题:Java Template.addVariableSegment方法的具体用法?Java Template.addVariableSegment怎么用?Java Template.addVariableSegment使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.codeInsight.template.Template
的用法示例。
在下文中一共展示了Template.addVariableSegment方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: expand
import com.intellij.codeInsight.template.Template; //导入方法依赖的package包/类
@Override
public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
PsiExpression expr = JavaPostfixTemplatesUtils.getTopmostExpression(context);
if (expr == null) {
PostfixTemplatesUtils.showErrorHint(context.getProject(), editor);
return;
}
Pair<String, String> bounds = calculateBounds(expr);
if (bounds == null) {
PostfixTemplatesUtils.showErrorHint(context.getProject(), editor);
return;
}
Project project = context.getProject();
Document document = editor.getDocument();
document.deleteString(expr.getTextRange().getStartOffset(), expr.getTextRange().getEndOffset());
TemplateManager manager = TemplateManager.getInstance(project);
Template template = manager.createTemplate("", "");
template.setToReformat(true);
template.addTextSegment("for (" + suggestIndexType(expr) + " ");
MacroCallNode index = new MacroCallNode(new SuggestVariableNameMacro());
String indexVariable = "index";
template.addVariable(indexVariable, index, index, true);
template.addTextSegment(" = " + bounds.first + "; ");
template.addVariableSegment(indexVariable);
template.addTextSegment(getComparativeSign(expr));
template.addTextSegment(bounds.second);
template.addTextSegment("; ");
template.addVariableSegment(indexVariable);
template.addTextSegment(getOperator());
template.addTextSegment(") {\n");
template.addEndVariable();
template.addTextSegment("\n}");
manager.startTemplate(editor, template);
}
示例2: handleInsert
import com.intellij.codeInsight.template.Template; //导入方法依赖的package包/类
@Override
public void handleInsert(InsertionContext context) {
PsiElement element = context.getFile().getViewProvider().findElementAt(context.getSelectionEndOffset());
element = PsiTreeUtil.getParentOfType(element, StringLiteralExpression.class);
if (element == null) {
return;
}
String value = ((StringLiteralExpression) element).getContents();
if (value.contains("/")) {
value = value.substring(value.lastIndexOf('/') + 1);
}
if (!myName.equals(value)) {
String filename = myFile.getName();
if (!filename.equals(value)) {
return;
}
}
MethodReference reference = PsiTreeUtil.getParentOfType(element, MethodReference.class);
if (reference != null) {
reference.putUserData(ViewsUtil.RENDER_VIEW, ((StringLiteralExpression) element).getContents());
reference.putUserData(ViewsUtil.RENDER_VIEW_FILE, myFile);
}
ArrayList<String> params = ViewsUtil.getViewVariables(myFile);
if (params.size() > 0) {
ParameterList parameterList = (ParameterList) element.getParent();
if (parameterList.getParameters().length == 1) {
Project project = context.getProject();
Template template = TemplateManager.getInstance(project).createTemplate("", "");
template.addTextSegment(", [");
boolean addComma = false;
for (String param : params) {
String variableName = "$" + param.toUpperCase() + "$";
if (addComma) {
template.addTextSegment(", ");
}
template.addTextSegment("'" + param + "' => ");
template.addVariable(variableName, "", "\"$" + param + "\"", true);
template.addVariableSegment(variableName);
addComma = true;
}
template.addTextSegment("]");
int offset = parameterList.getParameters()[0].getTextRange().getEndOffset();
context.getEditor().getCaretModel().moveToOffset(offset);
TemplateManager.getInstance(project).startTemplate(context.getEditor(), template);
}
}
}
示例3: createTestMethodTemplate
import com.intellij.codeInsight.template.Template; //导入方法依赖的package包/类
public static Template createTestMethodTemplate(MethodKind methodKind,
TestFramework descriptor,
PsiClass targetClass,
@Nullable String name,
boolean automatic,
Set<String> existingNames) {
FileTemplateDescriptor templateDesc = methodKind.getFileTemplateDescriptor(descriptor);
String templateName = templateDesc.getFileName();
FileTemplate fileTemplate = FileTemplateManager.getInstance(targetClass.getProject()).getCodeTemplate(templateName);
Template template = TemplateManager.getInstance(targetClass.getProject()).createTemplate("", "");
String templateText;
try {
templateText = fileTemplate.getText(new Properties());
}
catch (IOException e) {
LOG.warn(e);
templateText = fileTemplate.getText();
}
if (name == null) name = methodKind.getDefaultName();
if (existingNames != null && !existingNames.add(name)) {
int idx = 1;
while (existingNames.contains(name)) {
final String newName = name + (idx++);
if (existingNames.add(newName)) {
name = newName;
break;
}
}
}
templateText = StringUtil.replace(templateText, "${BODY}", "");
int from = 0;
while (true) {
int index = templateText.indexOf("${NAME}", from);
if (index == -1) break;
template.addTextSegment(templateText.substring(from, index));
if (index > 0 && !Character.isWhitespace(templateText.charAt(index - 1))) {
name = StringUtil.capitalize(name);
}
else {
name = StringUtil.decapitalize(name);
}
if (from == 0) {
Expression nameExpr = new ConstantNode(name);
template.addVariable("name", nameExpr, nameExpr, !automatic);
}
else {
template.addVariableSegment("name");
}
from = index + "${NAME}".length();
}
template.addTextSegment(templateText.substring(from, templateText.length()));
template.setToIndent(true);
template.setToReformat(true);
template.setToShortenLongNames(true);
return template;
}
示例4: createTestMethodTemplate
import com.intellij.codeInsight.template.Template; //导入方法依赖的package包/类
private static Template createTestMethodTemplate(MethodKind methodKind,
TestFramework descriptor,
PsiClass targetClass,
@Nullable String name,
boolean automatic) {
FileTemplateDescriptor templateDesc = methodKind.getFileTemplateDescriptor(descriptor);
String templateName = templateDesc.getFileName();
FileTemplate fileTemplate = FileTemplateManager.getInstance().getCodeTemplate(templateName);
Template template = TemplateManager.getInstance(targetClass.getProject()).createTemplate("", "");
String templateText;
try {
templateText = fileTemplate.getText(new Properties());
}
catch (IOException e) {
LOG.warn(e);
templateText = fileTemplate.getText();
}
if (name == null) name = methodKind.getDefaultName();
templateText = StringUtil.replace(templateText, "${BODY}", "");
int from = 0;
while (true) {
int index = templateText.indexOf("${NAME}", from);
if (index == -1) break;
template.addTextSegment(templateText.substring(from, index));
if (index > 0 && !Character.isWhitespace(templateText.charAt(index - 1))) {
name = StringUtil.capitalize(name);
}
else {
name = StringUtil.decapitalize(name);
}
if (from == 0) {
Expression nameExpr = new ConstantNode(name);
template.addVariable("name", nameExpr, nameExpr, !automatic);
}
else {
template.addVariableSegment("name");
}
from = index + "${NAME}".length();
}
template.addTextSegment(templateText.substring(from, templateText.length()));
template.setToIndent(true);
template.setToReformat(true);
template.setToShortenLongNames(true);
return template;
}
示例5: createTestMethodTemplate
import com.intellij.codeInsight.template.Template; //导入方法依赖的package包/类
private static Template createTestMethodTemplate(MethodKind methodKind,
TestFramework descriptor,
PsiClass targetClass,
@Nullable String name,
boolean automatic) {
FileTemplateDescriptor templateDesc = methodKind.getFileTemplateDescriptor(descriptor);
String templateName = templateDesc.getFileName();
FileTemplate fileTemplate = FileTemplateManager.getInstance(targetClass.getProject()).getCodeTemplate(templateName);
Template template = TemplateManager.getInstance(targetClass.getProject()).createTemplate("", "");
String templateText;
try {
templateText = fileTemplate.getText(new Properties());
}
catch (IOException e) {
LOG.warn(e);
templateText = fileTemplate.getText();
}
if (name == null) name = methodKind.getDefaultName();
templateText = StringUtil.replace(templateText, "${BODY}", "");
int from = 0;
while (true) {
int index = templateText.indexOf("${NAME}", from);
if (index == -1) break;
template.addTextSegment(templateText.substring(from, index));
if (index > 0 && !Character.isWhitespace(templateText.charAt(index - 1))) {
name = StringUtil.capitalize(name);
}
else {
name = StringUtil.decapitalize(name);
}
if (from == 0) {
Expression nameExpr = new ConstantNode(name);
template.addVariable("name", nameExpr, nameExpr, !automatic);
}
else {
template.addVariableSegment("name");
}
from = index + "${NAME}".length();
}
template.addTextSegment(templateText.substring(from, templateText.length()));
template.setToIndent(true);
template.setToReformat(true);
template.setToShortenLongNames(true);
return template;
}