@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); } } }); }
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, 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); } } } }
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, 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()); 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().length() == 0) { final int adjustedOffset = style.adjustLineIndent(file, offset); mySegments.replaceSegmentAt(endSegmentNumber, adjustedOffset, adjustedOffset); } } } catch (IncorrectOperationException e) { LOG.error(e); } } } }
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); } } } }