当前位置: 首页>>代码示例>>Java>>正文


Java Template.addVariableSegment方法代码示例

本文整理汇总了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);
}
 
开发者ID:consulo,项目名称:consulo-java,代码行数:39,代码来源:ForIndexedPostfixTemplate.java

示例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);
        }
    }
}
 
开发者ID:nvlad,项目名称:yii2support,代码行数:51,代码来源:ViewLookupElement.java

示例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;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:67,代码来源:TestIntegrationUtils.java

示例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;
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:55,代码来源:TestIntegrationUtils.java

示例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;
}
 
开发者ID:consulo,项目名称:consulo-java,代码行数:55,代码来源:TestIntegrationUtils.java


注:本文中的com.intellij.codeInsight.template.Template.addVariableSegment方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。