@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
@NotNull
protected 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);
}
}
});
}
项目:intellij-ce-playground
文件:TemplateState.java
private void reformat(RangeMarker rangeMarkerToReformat) {
final PsiFile file = getPsiFile();
if (file != null) {
CodeStyleManager style = CodeStyleManager.getInstance(myProject);
DumbService.getInstance(myProject).withAlternativeResolveEnabled(new Runnable() {
@Override
public void run() {
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,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);
}
}
}
}
项目:tools-idea
文件:TemplateState.java
private void reformat(RangeMarker rangeMarkerToReformat) {
final PsiFile file = getPsiFile();
if (file != null) {
CodeStyleManager style = CodeStyleManager.getInstance(myProject);
for (TemplateOptionalProcessor optionalProcessor : Extensions.getExtensions(TemplateOptionalProcessor.EP_NAME)) {
optionalProcessor.processtext(myProject,myEditor);
}
PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myDocument);
// for Python,because otherwise indents would be broken
// and reformat wouldn't be able to fix them
if (myTemplate.isToIndent()) {
if (!myTemplateIndented) {
LOG.assertTrue(myTemplaterange.isValid());
smartIndent(myTemplaterange.getStartOffset(),offset).toString().trim().length() == 0) {
final int adjustedOffset = style.adjustLineIndent(file,adjustedOffset);
}
}
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
}
项目:consulo
文件:TemplateState.java
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,myEditor);
}
});
PsiDocumentManager.getInstance(myProject).doPostponedOperationsAndUnblockDocument(myDocument);
// for Python,adjustedOffset);
}
}
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
}