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


Java CodeStyleManager.reformat方法代码示例

本文整理汇总了Java中com.intellij.psi.codeStyle.CodeStyleManager.reformat方法的典型用法代码示例。如果您正苦于以下问题:Java CodeStyleManager.reformat方法的具体用法?Java CodeStyleManager.reformat怎么用?Java CodeStyleManager.reformat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.intellij.psi.codeStyle.CodeStyleManager的用法示例。


在下文中一共展示了CodeStyleManager.reformat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createParameter

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@NotNull
@Override
public PsiParameter createParameter(@NotNull final String name, @NotNull final PsiType type) throws IncorrectOperationException {
  PsiUtil.checkIsIdentifier(myManager, name);
  if (PsiType.NULL.equals(type)) {
    throw new IncorrectOperationException("Cannot create parameter with type \"null\".");
  }

  final String text = type.getCanonicalText(true) + " " + name;
  PsiParameter parameter = createParameterFromText(text, null);
  final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myManager.getProject());
  PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL,
                              JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalParameters());
  GeneratedMarkerVisitor.markGenerated(parameter);
  parameter = (PsiParameter)JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(parameter);
  return (PsiParameter)codeStyleManager.reformat(parameter);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:18,代码来源:PsiElementFactoryImpl.java

示例2: replaceByStatement

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
public PsiElement replaceByStatement(final PsiMethod extractedMethod, final PsiMethodCallExpression methodCallExpression, final PsiVariable outputVariable) throws IncorrectOperationException {
  PsiStatement statement = null;
  if (outputVariable != null) {
    ReturnValue returnValue = getOutputVariableValue(outputVariable);
    if (returnValue == null && outputVariable instanceof PsiField) {
      returnValue = new FieldReturnValue((PsiField)outputVariable);
    }
    if (returnValue == null) return null;
    statement = returnValue.createReplacement(extractedMethod, methodCallExpression);
  }
  else if (getReturnValue() != null) {
    statement = getReturnValue().createReplacement(extractedMethod, methodCallExpression);
  }
  if (statement == null) {
    final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(methodCallExpression.getProject()).getElementFactory();
    PsiExpressionStatement expressionStatement = (PsiExpressionStatement) elementFactory.createStatementFromText("x();", null);
    final CodeStyleManager styleManager = CodeStyleManager.getInstance(methodCallExpression.getManager());
    expressionStatement = (PsiExpressionStatement)styleManager.reformat(expressionStatement);
    expressionStatement.getExpression().replace(methodCallExpression);
    statement = expressionStatement;
  }
  return replaceWith(statement);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:24,代码来源:Match.java

示例3: surroundExpression

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Override
public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
  PsiManager manager = expr.getManager();
  PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);

  PsiPrefixExpression prefixExpr = (PsiPrefixExpression)factory.createExpressionFromText("!(a instanceof Type)", null);
  prefixExpr = (PsiPrefixExpression)codeStyleManager.reformat(prefixExpr);
  PsiParenthesizedExpression parenthExpr = (PsiParenthesizedExpression)prefixExpr.getOperand();
  PsiInstanceOfExpression instanceofExpr = (PsiInstanceOfExpression)parenthExpr.getExpression();
  instanceofExpr.getOperand().replace(expr);
  prefixExpr = (PsiPrefixExpression)expr.replace(prefixExpr);
  parenthExpr = (PsiParenthesizedExpression)prefixExpr.getOperand();
  instanceofExpr = (PsiInstanceOfExpression)parenthExpr.getExpression();
  instanceofExpr = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(instanceofExpr);
  TextRange range = instanceofExpr.getCheckType().getTextRange();
  editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
  return new TextRange(range.getStartOffset(), range.getStartOffset());
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:20,代码来源:JavaWithNotInstanceofSurrounder.java

示例4: surroundExpression

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Override
public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
  PsiManager manager = expr.getManager();
  PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);

  @NonNls String text = "if(a){\nst;\n}else{\n}";
  PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText(text, null);
  ifStatement = (PsiIfStatement)codeStyleManager.reformat(ifStatement);

  ifStatement.getCondition().replace(expr);

  PsiExpressionStatement statement = (PsiExpressionStatement)expr.getParent();
  ifStatement = (PsiIfStatement)statement.replace(ifStatement);

  PsiCodeBlock block = ((PsiBlockStatement)ifStatement.getThenBranch()).getCodeBlock();

  PsiStatement afterStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(block.getStatements()[0]);

  TextRange range = afterStatement.getTextRange();
  editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
  return new TextRange(range.getStartOffset(), range.getStartOffset());
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:24,代码来源:JavaWithIfElseExpressionSurrounder.java

示例5: format

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@NotNull
@CheckReturnValue
public String format(@Nonnull String text) {
    final String name = Extensions.append(NAME, fileType);
    final PsiFile file = fileFactory.createFileFromText(name, fileType, text);
    final CodeStyleManager styleManager = CodeStyleManager.getInstance(file.getProject());
    styleManager.reformat(file);
    return file.getText();
}
 
开发者ID:t28hub,项目名称:json2java4idea,代码行数:10,代码来源:Formatter.java

示例6: copyClass

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@NotNull
@Override
public PsiClass copyClass(@NotNull final MoveInnerOptions options) {
  PsiClass innerClass = options.getInnerClass();

  PsiClass newClass;
  if (options.getTargetContainer() instanceof PsiDirectory) {
    newClass = createNewClass(options);
    PsiDocComment defaultDocComment = newClass.getDocComment();
    if (defaultDocComment != null && innerClass.getDocComment() == null) {
      final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(defaultDocComment.getProject());
      innerClass = (PsiClass)codeStyleManager.reformat(innerClass.addAfter(defaultDocComment, null).getParent());
    }

    newClass = (PsiClass)newClass.replace(innerClass);
    PsiUtil.setModifierProperty(newClass, PsiModifier.STATIC, false);
    PsiUtil.setModifierProperty(newClass, PsiModifier.PRIVATE, false);
    PsiUtil.setModifierProperty(newClass, PsiModifier.PROTECTED, false);
    final boolean makePublic = needPublicAccess(options.getOuterClass(), options.getTargetContainer());
    if (makePublic) {
      PsiUtil.setModifierProperty(newClass, PsiModifier.PUBLIC, true);
    }
  }
  else {
    newClass = (PsiClass)options.getTargetContainer().add(innerClass);
  }

  newClass.setName(options.getNewClassName());

  return newClass;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:32,代码来源:MoveJavaInnerHandler.java

示例7: replaceExpressionAndShorten

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
public static PsiElement replaceExpressionAndShorten(@NotNull PsiExpression expression, @NotNull @NonNls String newExpressionText) {
  final Project project = expression.getProject();
  final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
  final PsiElementFactory factory = psiFacade.getElementFactory();
  final PsiExpression newExpression = factory.createExpressionFromText(newExpressionText, expression);
  final PsiElement replacementExp = expression.replace(newExpression);
  final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
  javaCodeStyleManager.shortenClassReferences(replacementExp);
  final CodeStyleManager styleManager = CodeStyleManager.getInstance(project);
  return styleManager.reformat(replacementExp);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:12,代码来源:PsiReplacementUtil.java

示例8: addParenthToReplacedChild

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
public static TreeElement addParenthToReplacedChild(@NotNull IElementType parenthType,
                                                    @NotNull TreeElement newChild,
                                                    @NotNull PsiManager manager) {
  CompositeElement parenthExpr = ASTFactory.composite(parenthType);

  TreeElement dummyExpr = (TreeElement)newChild.clone();
  final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(newChild);
  new DummyHolder(manager, parenthExpr, null, charTableByTree);
  parenthExpr.putUserData(CharTable.CHAR_TABLE_KEY, charTableByTree);
  parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.LPARENTH, "("));
  parenthExpr.rawAddChildren(dummyExpr);
  parenthExpr.rawAddChildren(ASTFactory.leaf(JavaTokenType.RPARENTH, ")"));

  try {
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
    PsiElement formatted = codeStyleManager.reformat(SourceTreeToPsiMap.treeToPsiNotNull(parenthExpr));
    parenthExpr = (CompositeElement)SourceTreeToPsiMap.psiToTreeNotNull(formatted);
  }
  catch (IncorrectOperationException e) {
    LOG.error(e); // should not happen
  }

  newChild.putUserData(CharTable.CHAR_TABLE_KEY, SharedImplUtil.findCharTableByTree(newChild));
  dummyExpr.getTreeParent().replaceChild(dummyExpr, newChild);

  // TODO remove explicit caches drop since this should be ok if we will use ChangeUtil for the modification
  TreeUtil.clearCaches(TreeUtil.getFileElement(parenthExpr));
  return parenthExpr;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:30,代码来源:JavaSourceUtil.java

示例9: doFix

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Override
public void doFix(Project project, ProblemDescriptor descriptor)
  throws IncorrectOperationException {
  final PsiElement classIdentifier = descriptor.getPsiElement();
  final PsiClass aClass = (PsiClass)classIdentifier.getParent();
  final PsiElementFactory factory =
    JavaPsiFacade.getElementFactory(project);
  final PsiMethod constructor = factory.createConstructor();
  final PsiModifierList modifierList = constructor.getModifierList();
  if (aClass == null) {
    return;
  }
  if (aClass.hasModifierProperty(PsiModifier.PRIVATE)) {
    modifierList.setModifierProperty(PsiModifier.PUBLIC, false);
    modifierList.setModifierProperty(PsiModifier.PRIVATE, true);
  }
  else if (aClass.hasModifierProperty(PsiModifier.PROTECTED)) {
    modifierList.setModifierProperty(PsiModifier.PUBLIC, false);
    modifierList.setModifierProperty(PsiModifier.PROTECTED, true);
  }
  else if (aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
    modifierList.setModifierProperty(PsiModifier.PUBLIC, false);
    modifierList.setModifierProperty(PsiModifier.PROTECTED, true);
  }
  else if (!aClass.hasModifierProperty(PsiModifier.PUBLIC)) {
    modifierList.setModifierProperty(PsiModifier.PUBLIC, false);
  }
  aClass.add(constructor);
  final CodeStyleManager styleManager =
    CodeStyleManager.getInstance(project);
  styleManager.reformat(constructor);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:33,代码来源:ClassWithoutConstructorInspection.java

示例10: replaceWithStatements

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
private static void replaceWithStatements(final PsiStatement orig, final PsiStatement statement) throws IncorrectOperationException {
  if (statement == null) {
    orig.delete();
    return;
  }
  PsiElement parent = orig.getParent();
  if (parent == null) return;
  if (statement instanceof PsiBlockStatement && parent instanceof PsiCodeBlock) {
    // See IDEADEV-24277
    // Code block can only be inlined into another (parent) code block.
    // Code blocks, which are if or loop statement branches should not be inlined.
    PsiCodeBlock codeBlock = ((PsiBlockStatement)statement).getCodeBlock();
    PsiJavaToken lBrace = codeBlock.getLBrace();
    PsiJavaToken rBrace = codeBlock.getRBrace();
    if (lBrace == null || rBrace == null) return;


    final PsiElement[] children = codeBlock.getChildren();
    if (children.length > 2) {
      final PsiElement added =
        parent.addRangeBefore(
          children[1],
          children[children.length - 2],
          orig);
      final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(orig.getManager());
      codeStyleManager.reformat(added);
    }
    orig.delete();
  }
  else {
    orig.replace(statement);
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:34,代码来源:SimplifyBooleanExpressionFix.java

示例11: surroundStatements

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Override
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException{
  PsiManager manager = PsiManager.getInstance(project);
  PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);

  statements = SurroundWithUtil.moveDeclarationsOut(container, statements, false);
  if (statements.length == 0){
    return null;
  }

  @NonNls String text = "do{\n}while(true);";
  PsiDoWhileStatement doWhileStatement = (PsiDoWhileStatement)factory.createStatementFromText(text, null);
  doWhileStatement = (PsiDoWhileStatement)codeStyleManager.reformat(doWhileStatement);

  doWhileStatement = (PsiDoWhileStatement)container.addAfter(doWhileStatement, statements[statements.length - 1]);

  PsiStatement body = doWhileStatement.getBody();
  if (!(body instanceof PsiBlockStatement)) {
    return null;
  }
  PsiCodeBlock bodyBlock = ((PsiBlockStatement)body).getCodeBlock();
  SurroundWithUtil.indentCommentIfNecessary(bodyBlock, statements);
  bodyBlock.addRange(statements[0], statements[statements.length - 1]);
  container.deleteChildRange(statements[0], statements[statements.length - 1]);

  PsiExpression condition = doWhileStatement.getCondition();
  return condition == null ? null : condition.getTextRange();
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:30,代码来源:JavaWithDoWhileSurrounder.java

示例12: reformat

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
private static PsiMethod reformat(Project project, PsiMethod result) throws IncorrectOperationException {
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
  result = (PsiMethod) codeStyleManager.reformat(result);

  JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
  result = (PsiMethod) javaCodeStyleManager.shortenClassReferences(result);
  return result;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:9,代码来源:AddMethodFix.java

示例13: createReplacement

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
public PsiStatement createReplacement(final PsiMethod extractedMethod, final PsiMethodCallExpression methodCallExpression) throws IncorrectOperationException {

    final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(methodCallExpression.getProject()).getElementFactory();
    final CodeStyleManager styleManager = CodeStyleManager.getInstance(methodCallExpression.getProject());
    PsiExpressionStatement expressionStatement;
    expressionStatement = (PsiExpressionStatement)elementFactory.createStatementFromText("x = y();", null);
    expressionStatement = (PsiExpressionStatement)styleManager.reformat(expressionStatement);
    final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)expressionStatement.getExpression();
    assignmentExpression.getLExpression().replace(elementFactory.createExpressionFromText(myField.getName(), myField));
    assignmentExpression.getRExpression().replace(methodCallExpression);
    return expressionStatement;

  }
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:14,代码来源:FieldReturnValue.java

示例14: surroundExpression

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Override
public TextRange surroundExpression(Project project, Editor editor, PsiExpression expr) throws IncorrectOperationException {
  PsiManager manager = expr.getManager();
  PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);

  PsiPrefixExpression prefixExpr = (PsiPrefixExpression)factory.createExpressionFromText("!(a)", null);
  prefixExpr = (PsiPrefixExpression)codeStyleManager.reformat(prefixExpr);
  ((PsiParenthesizedExpression)prefixExpr.getOperand()).getExpression().replace(expr);
  expr = (PsiExpression)IntroduceVariableBase.replace(expr, prefixExpr, project);
  int offset = expr.getTextRange().getEndOffset();
  return new TextRange(offset, offset);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:14,代码来源:JavaWithNotSurrounder.java

示例15: surroundStatements

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Override
public TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) throws IncorrectOperationException{
  PsiManager manager = PsiManager.getInstance(project);
  PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);

  statements = SurroundWithUtil.moveDeclarationsOut(container, statements, false);
  if (statements.length == 0){
    return null;
  }

  @NonNls String text = "synchronized(a){\n}";
  PsiSynchronizedStatement synchronizedStatement = (PsiSynchronizedStatement)factory.createStatementFromText(text, null);
  synchronizedStatement = (PsiSynchronizedStatement)codeStyleManager.reformat(synchronizedStatement);

  synchronizedStatement = (PsiSynchronizedStatement)container.addAfter(synchronizedStatement, statements[statements.length - 1]);

  PsiCodeBlock synchronizedBlock = synchronizedStatement.getBody();
  if (synchronizedBlock == null) {
    return null;
  }
  SurroundWithUtil.indentCommentIfNecessary(synchronizedBlock, statements);
  synchronizedBlock.addRange(statements[0], statements[statements.length - 1]);
  container.deleteChildRange(statements[0], statements[statements.length - 1]);

  synchronizedStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(synchronizedStatement);
  PsiExpression lockExpression = synchronizedStatement.getLockExpression();
  if (lockExpression == null) {
    return null;
  }
  TextRange range = lockExpression.getTextRange();
  editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
  return new TextRange(range.getStartOffset(), range.getStartOffset());
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:35,代码来源:JavaWithSynchronizedSurrounder.java


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