IDEA-196749 Move slow TextEditorImpl init logic to background thread (AsyncEditorLoader)

This commit is contained in:
Dmitry Batrak
2018-08-20 14:32:56 +03:00
parent faa48e1d8b
commit 3ffae65e66
4 changed files with 46 additions and 18 deletions
@@ -15,6 +15,7 @@
*/
package com.intellij.openapi.editor;
import com.intellij.lang.Language;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -156,4 +157,13 @@ public interface EditorSettings {
boolean isShowIntentionBulb();
void setShowIntentionBulb(boolean show);
/**
* Sets the language which determines certain editor settings (right margin and soft margins, 'wrap on reaching right margin').
*
* @see #getRightMargin(Project)
* @see #getSoftMargins()
* @see #isWrapWhenTypingReachesRightMargin(Project)
*/
void setLanguage(@Nullable Language language);
}
@@ -2,6 +2,7 @@
package com.intellij.openapi.fileEditor.impl;
import com.intellij.ide.highlighter.HighlighterFactory;
import com.intellij.lang.Language;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
@@ -14,6 +15,7 @@ import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.fileEditor.*;
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
import com.intellij.openapi.fileEditor.ex.FileEditorWithProvider;
import com.intellij.openapi.fileEditor.impl.text.TextEditorImpl;
import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider;
import com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider;
import com.intellij.openapi.project.Project;
@@ -469,6 +471,8 @@ final class TestEditorManagerImpl extends FileEditorManagerEx implements Disposa
LOG.assertTrue(document != null, psiFile);
editor = EditorFactory.getInstance().createEditor(document, myProject);
final EditorHighlighter highlighter = HighlighterFactory.createHighlighter(myProject, file);
Language language = TextEditorImpl.getDocumentLanguage(editor);
editor.getSettings().setLanguage(language);
((EditorEx) editor).setHighlighter(highlighter);
((EditorEx) editor).setFile(file);
@@ -5,7 +5,6 @@ package com.intellij.openapi.editor.impl;
import com.intellij.application.options.CodeStyle;
import com.intellij.lang.Language;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorKind;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.ex.DocumentEx;
@@ -31,7 +30,7 @@ public class SettingsImpl implements EditorSettings {
private static final Logger LOG = Logger.getInstance(SettingsImpl.class);
@Nullable private final EditorEx myEditor;
@Nullable private final Language myLanguage;
@Nullable private Language myLanguage;
private Boolean myIsCamelWords;
// This group of settings does not have UI
@@ -89,8 +88,6 @@ public class SettingsImpl implements EditorSettings {
SettingsImpl(@Nullable EditorEx editor, @Nullable Project project, @Nullable EditorKind kind) {
myEditor = editor;
myLanguage = editor != null && project != null ? getDocumentLanguage(project, editor.getDocument()) : null;
if (EditorKind.CONSOLE.equals(kind)) {
mySoftWrapAppliancePlace = SoftWrapAppliancePlaces.CONSOLE;
}
@@ -216,19 +213,6 @@ public class SettingsImpl implements EditorSettings {
: CodeStyle.getProjectOrDefaultSettings(project).getRightMargin(myLanguage);
}
@Nullable
private static Language getDocumentLanguage(@NotNull Project project, @NotNull Document document) {
if (!project.isDisposed()) {
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
PsiFile file = documentManager.getPsiFile(document);
if (file != null) return file.getLanguage();
}
else {
LOG.warn("Attempting to get a language for document on a disposed project: " + project.getName());
}
return null;
}
@Override
public boolean isWrapWhenTypingReachesRightMargin(Project project) {
if (myWrapWhenTypingReachesRightMargin != null) return myWrapWhenTypingReachesRightMargin.booleanValue();
@@ -716,4 +700,8 @@ public class SettingsImpl implements EditorSettings {
public void setShowIntentionBulb(boolean show) {
myShowIntentionBulb = show;
}
public void setLanguage(@Nullable Language language) {
myLanguage = language;
}
}
@@ -3,6 +3,8 @@ package com.intellij.openapi.fileEditor.impl.text;
import com.intellij.codeHighlighting.BackgroundEditorHighlighter;
import com.intellij.ide.structureView.StructureViewBuilder;
import com.intellij.lang.Language;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.EditorColorsManager;
@@ -18,7 +20,10 @@ import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.UserDataHolderBase;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import javax.swing.*;
@@ -33,6 +38,8 @@ import java.util.concurrent.TimeoutException;
* @author Vladimir Kondratyev
*/
public class TextEditorImpl extends UserDataHolderBase implements TextEditor {
private static final Logger LOG = Logger.getInstance(TextEditorImpl.class);
private static final Key<TransientEditorState> TRANSIENT_EDITOR_STATE_KEY = Key.create("transientState");
protected final Project myProject;
@@ -65,7 +72,26 @@ public class TextEditorImpl extends UserDataHolderBase implements TextEditor {
EditorHighlighter highlighter = EditorHighlighterFactory.getInstance().createEditorHighlighter(myFile, scheme, myProject);
EditorEx editor = (EditorEx)getEditor();
highlighter.setText(editor.getDocument().getImmutableCharSequence());
return () -> editor.setHighlighter(highlighter);
Language language = getDocumentLanguage(editor);
return () -> {
editor.getSettings().setLanguage(language);
editor.setHighlighter(highlighter);
};
}
@Nullable
public static Language getDocumentLanguage(@NotNull Editor editor) {
Project project = editor.getProject();
LOG.assertTrue(project != null);
if (!project.isDisposed()) {
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
PsiFile file = documentManager.getPsiFile(editor.getDocument());
if (file != null) return file.getLanguage();
}
else {
LOG.warn("Attempting to get a language for document on a disposed project: " + project.getName());
}
return null;
}
@NotNull