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


Java CodeStyleManager.getInstance方法代码示例

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


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

示例1: addFieldInitialization

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Nullable
private static PsiExpressionStatement addFieldInitialization(@NotNull PsiMethod constructor,
                                                             @NotNull PsiField field,
                                                             @NotNull Project project) {
  PsiCodeBlock methodBody = constructor.getBody();
  if (methodBody == null) return null;

  final String fieldName = field.getName();
  String stmtText = fieldName + " = " + suggestInitValue(field) + ";";
  if (methodContainsParameterWithName(constructor, fieldName)) {
    stmtText = "this." + stmtText;
  }

  final PsiManager psiManager = PsiManager.getInstance(project);
  final PsiElementFactory factory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory();
  final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);

  return (PsiExpressionStatement)methodBody.add(codeStyleManager.reformat(factory.createStatementFromText(stmtText, methodBody)));
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:20,代码来源:InitializeFinalFieldInConstructorFix.java

示例2: replaceWthConditionalExpression

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@NotNull
private static PsiConditionalExpression replaceWthConditionalExpression(@NotNull Project project,
                                                                        @NotNull String condition,
                                                                        @NotNull PsiExpression expression,
                                                                        @NotNull String defaultValue) {
  final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();

  final PsiElement parent = expression.getParent();
  final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)factory.createExpressionFromText(
    condition + " ? " + expression.getText() + " : " + defaultValue,
    parent
  );

  final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
  return (PsiConditionalExpression)expression.replace( codeStyleManager.reformat(conditionalExpression));
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:17,代码来源:ReplaceWithTernaryOperatorFix.java

示例3: doFix

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Override
public void doFix(Project project, ProblemDescriptor descriptor)
  throws IncorrectOperationException {
  final PsiElement methodName = descriptor.getPsiElement();
  final PsiMethod method = (PsiMethod)methodName.getParent();
  if (method == null) {
    return;
  }
  final PsiCodeBlock body = method.getBody();
  if (body == null) {
    return;
  }
  final PsiElementFactory factory =
    JavaPsiFacade.getElementFactory(project);
  final PsiStatement newStatement =
    factory.createStatementFromText("super.tearDown();", null);
  final CodeStyleManager styleManager =
    CodeStyleManager.getInstance(project);
  final PsiJavaToken brace = body.getRBrace();
  body.addBefore(newStatement, brace);
  styleManager.reformat(body);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:23,代码来源:TeardownCallsSuperTeardownInspection.java

示例4: 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

示例5: setUp

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
@Override
public void setUp() throws Exception {
  super.setUp();
  myWorkingDirectory = TestFileStructure.createDirectory(getProject(), getSourceRoot(), TEMP_DIR_NAME);

  myRealCodeStyleManger = CodeStyleManager.getInstance(getProject());
  myMockCodeStyleManager = new MockCodeStyleManager();
  registerCodeStyleManager(myMockCodeStyleManager);

  myMockPlainTextFormattingModelBuilder = new MockPlainTextFormattingModelBuilder();
  LanguageFormatting.INSTANCE.addExplicitExtension(PlainTextLanguage.INSTANCE, myMockPlainTextFormattingModelBuilder);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:13,代码来源:ReformatFilesWithFiltersTest.java

示例6: qualifyReference

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
public static PsiReferenceExpression qualifyReference(@NotNull PsiReferenceExpression referenceExpression,
                                                      @NotNull PsiMember member,
                                                      @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException {
  PsiManager manager = referenceExpression.getManager();
  PsiMethodCallExpression methodCallExpression = PsiTreeUtil.getParentOfType(referenceExpression, PsiMethodCallExpression.class, true);
  while (methodCallExpression != null) {
    if (isSuperOrThisMethodCall(methodCallExpression)) {
      return referenceExpression;
    }
    methodCallExpression = PsiTreeUtil.getParentOfType(methodCallExpression, PsiMethodCallExpression.class, true);
  }
  PsiReferenceExpression expressionFromText;
  final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
  if (qualifyingClass == null) {
    PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
    final PsiClass containingClass = member.getContainingClass();
    if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
      while (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
        parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true);
      }
      LOG.assertTrue(parentClass != null);
      expressionFromText = (PsiReferenceExpression)factory.createExpressionFromText("A.this." + member.getName(), null);
      ((PsiThisExpression)expressionFromText.getQualifierExpression()).getQualifier().replace(factory.createClassReferenceElement(parentClass));
    }
    else {
      expressionFromText = (PsiReferenceExpression)factory.createExpressionFromText("this." + member.getName(), null);
    }
  }
  else {
    expressionFromText = (PsiReferenceExpression)factory.createExpressionFromText("A." + member.getName(), null);
    expressionFromText.setQualifierExpression(factory.createReferenceExpression(qualifyingClass));
  }
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
  expressionFromText = (PsiReferenceExpression)codeStyleManager.reformat(expressionFromText);
  return (PsiReferenceExpression)referenceExpression.replace(expressionFromText);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:37,代码来源:RefactoringChangeUtil.java

示例7: 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(getExpression());
  assignmentExpression.getRExpression().replace(methodCallExpression);
  return expressionStatement;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:13,代码来源:ExpressionReturnValue.java

示例8: createReplacement

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

  final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(methodCallExpression.getProject()).getElementFactory();
  final CodeStyleManager styleManager = CodeStyleManager.getInstance(methodCallExpression.getProject());
  statement = (PsiDeclarationStatement)styleManager.reformat(
    elementFactory.createVariableDeclarationStatement(myVariable.getName(), myVariable.getType(), methodCallExpression)
  );
  ((PsiVariable)statement.getDeclaredElements()[0]).getModifierList().replace(myVariable.getModifierList());
  return statement;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:12,代码来源:VariableReturnValue.java

示例9: 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

示例10: getReformatRunnable

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
private ThrowableRunnable getReformatRunnable(@NotNull PsiFile file, @NotNull List<TextRange> ranges) {
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(getProject());
  return () -> CommandProcessor.getInstance().executeCommand(
    getProject(),
    () -> ApplicationManager.getApplication().runWriteAction(
      () -> codeStyleManager.reformatTextWithContext(file, ranges)
    ),
    null,
    null);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:11,代码来源:JavaSmartReformatPerformanceTest.java

示例11: indentEachLine

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
private static void indentEachLine(Project project, Editor editor, int startOffset, int endOffset) {
  PsiDocumentManager.getInstance(project).commitAllDocuments();
  PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());

  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
  final CharSequence text = editor.getDocument().getCharsSequence();
  if (startOffset > 0 && endOffset > startOffset + 1 && text.charAt(endOffset - 1) == '\n' && text.charAt(startOffset - 1) == '\n') {
    // There is a possible situation that pasted text ends by a line feed. We don't want to proceed it when a text is
    // pasted at the first line column.
    // Example:
    //    text to paste:
    //'if (true) {
    //'
    //    source:
    // if (true) {
    //     int i = 1;
    //     int j = 1;
    // }
    //
    //
    // We get the following on paste then:
    // if (true) {
    //     if (true) {
    //         int i = 1;
    //     int j = 1;
    // }
    //
    // We don't want line 'int i = 1;' to be indented here.
    endOffset--;
  }
  try {
    codeStyleManager.adjustLineIndent(file, new TextRange(startOffset, endOffset));
  }
  catch (IncorrectOperationException e) {
    LOG.error(e);
  }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:38,代码来源:PasteHandler.java

示例12: 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 = "try{\n}finally{\n\n}";
  PsiTryStatement tryStatement = (PsiTryStatement)factory.createStatementFromText(text, null);
  tryStatement = (PsiTryStatement)codeStyleManager.reformat(tryStatement);

  tryStatement = (PsiTryStatement)container.addAfter(tryStatement, statements[statements.length - 1]);

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

  PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
  if (finallyBlock == null) {
    return null;
  }
  Document document = editor.getDocument();
  PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document);
  TextRange finallyBlockRange = finallyBlock.getTextRange();
  int newLineOffset = finallyBlockRange.getStartOffset() + 2;
  editor.getCaretModel().moveToOffset(newLineOffset);
  editor.getSelectionModel().removeSelection();
  CodeStyleManager.getInstance(project).adjustLineIndent(document, newLineOffset);
  PsiDocumentManager.getInstance(project).commitDocument(document);
  return new TextRange(editor.getCaretModel().getOffset(), editor.getCaretModel().getOffset());
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:40,代码来源:JavaWithTryFinallySurrounder.java

示例13: qualifyReference

import com.intellij.psi.codeStyle.CodeStyleManager; //导入方法依赖的package包/类
public static GrReferenceExpression qualifyReference(GrReferenceExpression referenceExpression,
                                                     final PsiMember member,
                                                     @Nullable final PsiClass qualifyingClass) throws IncorrectOperationException {
  PsiManager manager = referenceExpression.getManager();
  GrReferenceExpression expressionFromText;
  final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(referenceExpression.getProject());
  if (qualifyingClass == null) {
    PsiClass parentClass = PsiTreeUtil.getParentOfType(referenceExpression, PsiClass.class);
    final PsiClass containingClass = member.getContainingClass();
    if (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
      while (parentClass != null && !InheritanceUtil.isInheritorOrSelf(parentClass, containingClass, true)) {
        parentClass = PsiTreeUtil.getParentOfType(parentClass, PsiClass.class, true);
      }
      LOG.assertTrue(parentClass != null);
      expressionFromText = factory.createReferenceExpressionFromText("A.this." + member.getName());
      //noinspection ConstantConditions
      ((GrReferenceExpression)expressionFromText.getQualifier()).getQualifier().replace(factory.createReferenceElementForClass(parentClass));
    }
    else {
      expressionFromText = (GrReferenceExpression)factory.createExpressionFromText("this." + member.getName());
    }
  }
  else {
    expressionFromText = (GrReferenceExpression)factory.createExpressionFromText("A." + member.getName());
    expressionFromText.setQualifier(factory.createReferenceElementForClass(qualifyingClass));
  }
  CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
  expressionFromText = (GrReferenceExpression)codeStyleManager.reformat(expressionFromText);
  return (GrReferenceExpression)referenceExpression.replace(expressionFromText);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:31,代码来源:FieldConflictsResolver.java

示例14: 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 = "if(a){\n}else{\n}";
  PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText(text, null);
  ifStatement = (PsiIfStatement)codeStyleManager.reformat(ifStatement);

  ifStatement = (PsiIfStatement)container.addAfter(ifStatement, statements[statements.length - 1]);

  PsiStatement thenBranch = ifStatement.getThenBranch();
  if (!(thenBranch instanceof PsiBlockStatement)) {
    return null;
  }
  PsiCodeBlock thenBlock = ((PsiBlockStatement)thenBranch).getCodeBlock();
  SurroundWithUtil.indentCommentIfNecessary(thenBlock, statements);
  thenBlock.addRange(statements[0], statements[statements.length - 1]);
  container.deleteChildRange(statements[0], statements[statements.length - 1]);
  ifStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(ifStatement);
  PsiExpression condition = ifStatement.getCondition();
  if (condition == null) {
    return null;
  }
  TextRange range = condition.getTextRange();
  editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
  return new TextRange(range.getStartOffset(), range.getStartOffset());
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:35,代码来源:JavaWithIfElseSurrounder.java

示例15: 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}";
  PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText(text, null);
  ifStatement = (PsiIfStatement)codeStyleManager.reformat(ifStatement);

  PsiExpression condition = ifStatement.getCondition();
  if (condition != null) {
    condition.replace(expr);
  }

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

  PsiStatement thenBranch = ifStatement.getThenBranch();
  if (thenBranch != null && thenBranch instanceof PsiBlockStatement) {
    PsiCodeBlock block = ((PsiBlockStatement)thenBranch).getCodeBlock();
    block = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(block);
    TextRange range = block.getStatements()[0].getTextRange();
    editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    return TextRange.from(range.getStartOffset(), 0);
  }
  return TextRange.from(editor.getCaretModel().getOffset(), 0);
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:29,代码来源:JavaWithIfExpressionSurrounder.java


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