TextRange (@NotNull CodeFormattingData formattingData, final @NotNull ASTNode node, @NotNull TextRange range) { TextRange result = range; PsiElement psi = node.getPsi(); if (!psi.isValid()) { return result; } PsiFile file = psi.getContainingFile(); final Set<TextRange> injectedFileRangesSet = formattingData.getInjectedRanges(range); if (!injectedFileRangesSet.isEmpty()) { List<TextRange> ranges = new ArrayList<>(injectedFileRangesSet); Collections.reverse(ranges); for (TextRange injectedFileRange : ranges) { int startHostOffset = injectedFileRange.getStartOffset(); int endHostOffset = injectedFileRange.getEndOffset(); if (startHostOffset >= range.getStartOffset() && endHostOffset <= range.getEndOffset()) { PsiFile injected = InjectedLanguageUtilBase.findInjectedPsiNoCommit(file, startHostOffset); if (injected != null) { final TextRange initialInjectedRange = TextRange.create(0, injected.getTextLength()); TextRange injectedRange = initialInjectedRange; for (PreFormatProcessor processor : PreFormatProcessor.EP_NAME.getExtensionList()) { if (processor.changesWhitespacesOnly() || !myCanChangeWhitespaceOnly) { injectedRange = processor.process(injected.getNode(), injectedRange); } } // Allow only range expansion (not reduction) for injected context. if ((initialInjectedRange.getStartOffset() > injectedRange.getStartOffset() && initialInjectedRange.getStartOffset() > 0) || (initialInjectedRange.getEndOffset() < injectedRange.getEndOffset() && initialInjectedRange.getEndOffset() < injected.getTextLength())) { range = TextRange.create( range.getStartOffset() + injectedRange.getStartOffset() - initialInjectedRange.getStartOffset(), range.getEndOffset() + initialInjectedRange.getEndOffset() - injectedRange.getEndOffset()); } } } } } if (!mySettings.FORMATTER_TAGS_ENABLED) { for (PreFormatProcessor processor: PreFormatProcessor.EP_NAME.getExtensionList()) { if (processor.changesWhitespacesOnly() || !myCanChangeWhitespaceOnly) { result = processor.process(node, result); } } } else { result = preprocessEnabledRanges(node, result); } return result; }