Use non-physical files with JS code style preview

This commit is contained in:
Rustam Vishnyakov
2017-10-06 12:50:59 +03:00
parent 9116e86e3b
commit 286e0a0dea
2 changed files with 16 additions and 1 deletions
@@ -21,8 +21,11 @@ import com.intellij.lang.ASTFactory;
import com.intellij.lang.ASTNode;
import com.intellij.lang.Language;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.TokenType;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.*;
@@ -489,4 +492,15 @@ public class FormatterUtil {
public static boolean isFormatterCalledExplicitly() {
return FORMATTER_ACTION_NAMES.contains(CommandProcessor.getInstance().getCurrentCommandName());
}
@Nullable
public static Document getFileDocument(@NotNull PsiFile file) {
if (file.isValid()) {
Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
if (document != null) return document;
return file.getViewProvider().getDocument();
}
return null;
}
}
@@ -46,6 +46,7 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.formatter.DocumentBasedFormattingModel;
import com.intellij.psi.formatter.FormatterUtil;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil;
@@ -167,7 +168,7 @@ public class CodeFormatterFacade {
public void processText(PsiFile file, final FormatTextRanges ranges, boolean doPostponedFormatting) {
final Project project = file.getProject();
Document document = PsiDocumentManager.getInstance(project).getDocument(file);
Document document = FormatterUtil.getFileDocument(file);
final List<FormatTextRange> textRanges = ranges.getRanges();
if (document instanceof DocumentWindow) {
file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);