下面列出了怎么用com.intellij.psi.impl.source.codeStyle.CodeStyleManagerImpl的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
}
});
}
@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);
}
@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);
}
}
public ManualCodeStyleManagerDelegator(Project p) {
CodeStyleManagerImpl codeStyleManager = new CodeStyleManagerImpl(p);
this.eclipseCodeStyleManager = new EclipseCodeStyleManager_IJ_2016_3plus(codeStyleManager, ProjectComponent.getSettings(p));
this.original = codeStyleManager;
}
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);
}
}
}
}