Provides fine-grained control on fallback indent options to be used during formatting

This commit is contained in:
Denis.Zhdanov
2013-01-30 17:22:21 +04:00
parent 084eb952e7
commit d42d92ffdd
2 changed files with 30 additions and 1 deletions
@@ -16,8 +16,11 @@
package com.intellij.formatting;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Denis Zhdanov
@@ -36,4 +39,20 @@ public interface FormattingModelBuilderEx extends FormattingModelBuilder {
*/
@NotNull
FormattingModel createModel(@NotNull final PsiElement element, @NotNull final CodeStyleSettings settings, @NotNull FormattingMode mode);
/**
* Allows to adjust indent options to used during performing formatting operation on the given ranges of the given file.
* <p/>
* Default algorithm is to query given settings for indent options using given file's language as a key.
*
* @param file target file which content is going to be reformatted
* @param ranges given file's ranges to reformat
* @param settings code style settings holder
* @return indent options to use for the target formatting operation (if any adjustment is required);
* <code>null</code> to trigger default algorithm usage
*/
@Nullable
CommonCodeStyleSettings.IndentOptions getIndentOptionsToUse(@NotNull PsiFile file,
@NotNull FormatTextRanges ranges,
@NotNull CodeStyleSettings settings);
}
@@ -44,6 +44,7 @@ import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiLanguageInjectionHost;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.formatter.DocumentBasedFormattingModel;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
@@ -210,7 +211,16 @@ public class CodeFormatterFacade {
if (CodeStyleManager.getInstance(project).isSequentialProcessingAllowed()) {
formatter.setProgressTask(new FormattingProgressTask(project, file, document));
}
formatter.format(model, mySettings, mySettings.getIndentOptions(file.getFileType()), ranges);
CommonCodeStyleSettings.IndentOptions indentOptions = null;
if (builder instanceof FormattingModelBuilderEx) {
indentOptions = ((FormattingModelBuilderEx)builder).getIndentOptionsToUse(file, ranges, mySettings);
}
if (indentOptions == null) {
indentOptions = mySettings.getIndentOptions(file.getFileType());
}
formatter.format(model, mySettings, indentOptions, ranges);
for (FormatTextRanges.FormatTextRange range : textRanges) {
TextRange textRange = range.getTextRange();
wrapLongLinesIfNecessary(file, document, textRange.getStartOffset(), textRange.getEndOffset());