com.intellij.psi.impl.source.codeStyle.CodeStyleManagerImpl Java Examples

The following examples show how to use com.intellij.psi.impl.source.codeStyle.CodeStyleManagerImpl. 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: ReformatAndOptimizeImportsProcessor.java    From idea-android-studio-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Override
@NotNull
public FutureTask<Boolean> preprocessFile(@NotNull PsiFile file, boolean processChangedTextOnly) throws IncorrectOperationException {
    final FutureTask<Boolean> reformatTask = myReformatCodeProcessor.preprocessFile(file, processChangedTextOnly);
    final FutureTask<Boolean> optimizeImportsTask = myOptimizeImportsProcessor.preprocessFile(file, false);
    return new FutureTask<Boolean>(new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            reformatTask.run();
            if (!reformatTask.get() || reformatTask.isCancelled()) {
                return false;
            }

            CodeStyleManagerImpl.setSequentialProcessingAllowed(false);
            try {
                optimizeImportsTask.run();
                return optimizeImportsTask.get() && !optimizeImportsTask.isCancelled();
            }
            finally {
                CodeStyleManagerImpl.setSequentialProcessingAllowed(true);
            }
        }
    });
}
 
Example #2
Source File: OptimizeImportsProcessor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
protected FutureTask<Boolean> prepareTask(@Nonnull PsiFile file, boolean processChangedTextOnly) {
  if (DumbService.isDumb(file.getProject())) {
    return new FutureTask<>(EmptyRunnable.INSTANCE, true);
  }

  final Set<ImportOptimizer> optimizers = LanguageImportStatements.INSTANCE.forFile(file);
  final List<Runnable> runnables = new ArrayList<>();
  List<PsiFile> files = file.getViewProvider().getAllFiles();
  for (ImportOptimizer optimizer : optimizers) {
    for (PsiFile psiFile : files) {
      if (optimizer.supports(psiFile)) {
        runnables.add(optimizer.processFile(psiFile));
      }
    }
  }

  Runnable runnable = !runnables.isEmpty() ? () -> {
    CodeStyleManagerImpl.setSequentialProcessingAllowed(false);
    try {
      for (Runnable runnable1 : runnables) {
        runnable1.run();
        retrieveAndStoreNotificationInfo(runnable1);
      }
      putNotificationInfoIntoCollector();
    }
    finally {
      CodeStyleManagerImpl.setSequentialProcessingAllowed(true);
    }
  } : EmptyRunnable.getInstance();

  return new FutureTask<>(runnable, true);
}
 
Example #3
Source File: PostprocessReformattingAspect.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(@Nonnull FileViewProvider viewProvider) {
  final PsiFile file = viewProvider.getPsi(viewProvider.getBaseLanguage());
  final FormatTextRanges textRanges = myRanges.ensureNonEmpty();
  textRanges.setExtendToContext(true);
  if (ExternalFormatProcessor.useExternalFormatter(file)) {
    CodeStyleManagerImpl.formatRanges(file, myRanges, null);
  }
  else {
    final CodeFormatterFacade codeFormatter = getFormatterFacade(viewProvider);
    codeFormatter.processText(file, textRanges, false);
  }
}
 
Example #4
Source File: ManualCodeStyleManagerDelegator.java    From EclipseCodeFormatter with Apache License 2.0 4 votes vote down vote up
public ManualCodeStyleManagerDelegator(Project p) {
	CodeStyleManagerImpl codeStyleManager = new CodeStyleManagerImpl(p);

	this.eclipseCodeStyleManager = new EclipseCodeStyleManager_IJ_2016_3plus(codeStyleManager, ProjectComponent.getSettings(p));
	this.original = codeStyleManager;
}
 
Example #5
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);
      }
    }
  }
}