當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。