Java Code Examples for com.intellij.psi.codeStyle.CodeStyleManager#reformatText()

The following examples show how to use com.intellij.psi.codeStyle.CodeStyleManager#reformatText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CodeMakerUtil.java    From CodeMaker with Apache License 2.0 5 votes vote down vote up
public static void reformatJavaDoc(PsiElement theElement) {
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(theElement.getProject());
    try {
        int javadocTextOffset = findJavaDocTextOffset(theElement);
        int javaCodeTextOffset = findJavaCodeTextOffset(theElement);
        codeStyleManager.reformatText(theElement.getContainingFile(), javadocTextOffset,
            javaCodeTextOffset + 1);
    } catch (Exception e) {
        LOGGER.error("reformat code failed", e);
    }
}
 
Example 2
Source File: TemplateState.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void reformat(RangeMarker rangeMarkerToReformat) {
  final PsiFile file = getPsiFile();
  if (file != null) {
    CodeStyleManager style = CodeStyleManager.getInstance(myProject);
    DumbService.getInstance(myProject).withAlternativeResolveEnabled(() -> {
      for (TemplateOptionalProcessor optionalProcessor : Extensions.getExtensions(TemplateOptionalProcessor.EP_NAME)) {
        optionalProcessor.processText(myProject, myTemplate, myDocument, myTemplateRange, myEditor);
      }
    });
    PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myDocument);
    // for Python, we need to indent the template even if reformatting is enabled, because otherwise indents would be broken
    // and reformat wouldn't be able to fix them
    if (myTemplate.isToIndent()) {
      if (!myTemplateIndented) {
        LOG.assertTrue(myTemplateRange.isValid(), presentTemplate(myTemplate));
        smartIndent(myTemplateRange.getStartOffset(), myTemplateRange.getEndOffset());
        myTemplateIndented = true;
      }
    }
    if (myTemplate.isToReformat()) {
      try {
        int endSegmentNumber = myTemplate.getEndSegmentNumber();
        PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
        RangeMarker dummyAdjustLineMarkerRange = null;
        int endVarOffset = -1;
        if (endSegmentNumber >= 0) {
          endVarOffset = mySegments.getSegmentStart(endSegmentNumber);
          TextRange range = CodeStyleManagerImpl.insertNewLineIndentMarker(file, myDocument, endVarOffset);
          if (range != null) dummyAdjustLineMarkerRange = myDocument.createRangeMarker(range);
        }
        int reformatStartOffset = myTemplateRange.getStartOffset();
        int reformatEndOffset = myTemplateRange.getEndOffset();
        if (rangeMarkerToReformat != null) {
          reformatStartOffset = rangeMarkerToReformat.getStartOffset();
          reformatEndOffset = rangeMarkerToReformat.getEndOffset();
        }
        if (dummyAdjustLineMarkerRange == null && endVarOffset >= 0) {
          // There is a possible case that indent marker element was not inserted (e.g. because there is no blank line
          // at the target offset). However, we want to reformat white space adjacent to the current template (if any).
          PsiElement whiteSpaceElement = CodeStyleManagerImpl.findWhiteSpaceNode(file, endVarOffset);
          if (whiteSpaceElement != null) {
            TextRange whiteSpaceRange = whiteSpaceElement.getTextRange();
            if (whiteSpaceElement.getContainingFile() != null) {
              // Support injected white space nodes.
              whiteSpaceRange = InjectedLanguageManager.getInstance(file.getProject()).injectedToHost(whiteSpaceElement, whiteSpaceRange);
            }
            reformatStartOffset = Math.min(reformatStartOffset, whiteSpaceRange.getStartOffset());
            reformatEndOffset = Math.max(reformatEndOffset, whiteSpaceRange.getEndOffset());
          }
        }
        style.reformatText(file, reformatStartOffset, reformatEndOffset);
        PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
        PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myDocument);

        if (dummyAdjustLineMarkerRange != null && dummyAdjustLineMarkerRange.isValid()) {
          //[ven] TODO: [max] correct javadoc reformatting to eliminate isValid() check!!!
          mySegments.replaceSegmentAt(endSegmentNumber, dummyAdjustLineMarkerRange.getStartOffset(), dummyAdjustLineMarkerRange.getEndOffset());
          myDocument.deleteString(dummyAdjustLineMarkerRange.getStartOffset(), dummyAdjustLineMarkerRange.getEndOffset());
          PsiDocumentManager.getInstance(myProject).commitDocument(myDocument);
        }
        if (endSegmentNumber >= 0) {
          final int offset = mySegments.getSegmentStart(endSegmentNumber);
          final int lineStart = myDocument.getLineStartOffset(myDocument.getLineNumber(offset));
          // if $END$ is at line start, put it at correct indentation
          if (myDocument.getCharsSequence().subSequence(lineStart, offset).toString().trim().isEmpty()) {
            final int adjustedOffset = style.adjustLineIndent(file, offset);
            mySegments.replaceSegmentAt(endSegmentNumber, adjustedOffset, adjustedOffset);
          }
        }
      }
      catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
  }
}