[AutoDetectIndent] added registry flag to switch between formatter-based and document-based detector

This commit is contained in:
Yaroslav Lepenkin
2015-06-15 15:02:41 +03:00
parent 3b1c3bad64
commit 4f5aaa647b
2 changed files with 21 additions and 6 deletions
@@ -19,6 +19,7 @@ import com.intellij.lang.Language;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
@@ -38,12 +39,14 @@ public class IndentOptionsDetectorImpl implements IndentOptionsDetector {
private final Project myProject;
private final Document myDocument;
private final Language myLanguage;
private final boolean myUseFormatterBasedLineIndentBuilder;
public IndentOptionsDetectorImpl(@NotNull PsiFile file) {
myFile = file;
myLanguage = file.getLanguage();
myProject = file.getProject();
myDocument = PsiDocumentManager.getInstance(myProject).getDocument(myFile);
myUseFormatterBasedLineIndentBuilder = Registry.is("editor.detect.indent.by.formatter");
}
@Override
@@ -51,17 +54,27 @@ public class IndentOptionsDetectorImpl implements IndentOptionsDetector {
public IndentOptions getIndentOptions() {
IndentOptions indentOptions = (IndentOptions)CodeStyleSettingsManager.getSettings(myProject).getIndentOptions(myFile.getFileType()).clone();
if (myDocument != null) {
List<LineIndentInfo> linesInfo = new FormatterBasedLineIndentInfoBuilder(myFile).build();
if (linesInfo != null) {
IndentUsageStatistics stats = new IndentUsageStatisticsImpl(linesInfo);
adjustIndentOptions(indentOptions, stats);
}
long start = System.currentTimeMillis();
List<LineIndentInfo> linesInfo = calcLineIndentInfo();
long end = System.currentTimeMillis();
LOG.info("Formatter-based: " + myUseFormatterBasedLineIndentBuilder + ". Line info building time: " + (end - start));
if (linesInfo != null) {
IndentUsageStatistics stats = new IndentUsageStatisticsImpl(linesInfo);
adjustIndentOptions(indentOptions, stats);
}
return indentOptions;
}
private List<LineIndentInfo> calcLineIndentInfo() {
if (myDocument == null) return null;
if (myUseFormatterBasedLineIndentBuilder) {
return new FormatterBasedLineIndentInfoBuilder(myFile).build();
}
return new LineIndentInfoBuilder(myDocument.getCharsSequence(), myLanguage).build();
}
private void adjustIndentOptions(@NotNull IndentOptions indentOptions, @NotNull IndentUsageStatistics stats) {
int linesWithTabs = stats.getTotalLinesWithLeadingTabs();
int linesWithWhiteSpaceIndent = stats.getTotalLinesWithLeadingSpaces();
@@ -608,3 +608,5 @@ tfs.set.connection.timeout=false
ide.mac.yosemite.laf=false
ide.mac.yosemite.laf.restartRequired=true
ide.mac.yosemite.laf.description=Replaces Aqua LaF to enhanced IntelliJ LaF
editor.detect.indent.by.formatter=false