diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/documentation/render/JavaDocRenderTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/documentation/render/JavaDocRenderTest.java index 972086372833..fb0d5ccfb8fc 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/documentation/render/JavaDocRenderTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/documentation/render/JavaDocRenderTest.java @@ -127,7 +127,7 @@ public class JavaDocRenderTest extends AbstractEditorTest { } private void updateRenderedItems(boolean collapseNewRegions) { - DocRenderPassFactory.Items items = DocRenderPassFactory.calculateItemsToRender(getEditor().getDocument(), getFile()); + DocRenderPassFactory.Items items = DocRenderPassFactory.calculateItemsToRender(getEditor(), getFile()); DocRenderPassFactory.applyItemsToRender(getEditor(), getProject(), items, collapseNewRegions); } diff --git a/platform/lang-impl/src/com/intellij/application/options/editor/EditorAppearanceConfigurable.kt b/platform/lang-impl/src/com/intellij/application/options/editor/EditorAppearanceConfigurable.kt index cd357a82fc50..45b7005251cb 100644 --- a/platform/lang-impl/src/com/intellij/application/options/editor/EditorAppearanceConfigurable.kt +++ b/platform/lang-impl/src/com/intellij/application/options/editor/EditorAppearanceConfigurable.kt @@ -2,7 +2,7 @@ package com.intellij.application.options.editor import com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettings -import com.intellij.codeInsight.documentation.render.DocRenderItem +import com.intellij.codeInsight.documentation.render.DocRenderManager import com.intellij.ide.IdeBundle import com.intellij.ide.ui.LafManager import com.intellij.ide.ui.UISettings @@ -121,7 +121,7 @@ class EditorAppearanceConfigurable : BoundCompositeSearchableConfigurable items = editor.getUserData(OUR_ITEMS); if (items == null) return; - boolean globalSetting = EditorSettingsExternalizable.getInstance().isDocCommentRenderingEnabled(); + boolean editorSetting = DocRenderManager.isDocRenderingEnabled(editor); keepScrollingPositionWhile(editor, () -> { List foldingTasks = new ArrayList<>(); boolean updated = false; for (DocRenderItem item : items) { - if (item.isValid() && (item.inlay == null) == globalSetting) { + if (item.isValid() && (item.inlay == null) == editorSetting) { updated |= item.toggle(foldingTasks); } } @@ -218,24 +215,6 @@ public class DocRenderItem { }); } - public static void resetToDefaultEditorState(@NotNull Editor editor) { - resetToDefaultState(editor); - DocRenderPassFactory.forceRefreshOnNextPass(editor); - for (Project project : ProjectManager.getInstance().getOpenProjects()) { - DaemonCodeAnalyzer.getInstance(project).restart(); - } - } - - public static void resetAllToDefaultState() { - for (Editor editor : EditorFactory.getInstance().getAllEditors()) { - resetToDefaultState(editor); - DocRenderPassFactory.forceRefreshOnNextPass(editor); - } - for (Project project : ProjectManager.getInstance().getOpenProjects()) { - DaemonCodeAnalyzer.getInstance(project).restart(); - } - } - public static EditorCustomElementRenderer createDemoRenderer(@NotNull Editor editor) { DocRenderItem item = new DocRenderItem(editor, new TextRange(0, 0), "Rendered documentation with link"); return new DocRenderer(item); @@ -320,7 +299,7 @@ public class DocRenderItem { } Disposer.dispose(inlay); inlay = null; - if (!EditorSettingsExternalizable.getInstance().isDocCommentRenderingEnabled()) { + if (!DocRenderManager.isDocRenderingEnabled(editor)) { // the value won't be updated by DocRenderPass on document modification, so we shouldn't cache the value textToRender = null; } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/DocRenderManager.java b/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/DocRenderManager.java new file mode 100644 index 000000000000..b30b2c237e04 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/DocRenderManager.java @@ -0,0 +1,72 @@ +// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.codeInsight.documentation.render; + +import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.EditorFactory; +import com.intellij.openapi.editor.ex.EditorSettingsExternalizable; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManager; +import com.intellij.openapi.util.Key; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class DocRenderManager { + private static final Key DOC_RENDER_ENABLED = Key.create("doc.render.enabled"); + + /** + * Allows to override global doc comments rendering setting for a specific editor. Passing {@code null} as {@code value} makes editor use + * the global setting again. + */ + public static void setDocRenderingEnabled(@NotNull Editor editor, @Nullable Boolean value) { + ApplicationManager.getApplication().assertIsDispatchThread(); + boolean enabledBefore = isDocRenderingEnabled(editor); + editor.putUserData(DOC_RENDER_ENABLED, value); + boolean enabledAfter = isDocRenderingEnabled(editor); + if (enabledAfter != enabledBefore) { + resetEditorToDefaultState(editor); + } + } + + /** + * Tells whether doc comment rendering is enabled for a specific editor. + * + * @see #setDocRenderingEnabled(Editor, Boolean) + */ + public static boolean isDocRenderingEnabled(@NotNull Editor editor) { + Boolean value = editor.getUserData(DOC_RENDER_ENABLED); + return value == null ? EditorSettingsExternalizable.getInstance().isDocCommentRenderingEnabled() : value; + } + + /** + * Sets all doc comments to their default state (rendered or not rendered) for all opened editors. + * + * @see #isDocRenderingEnabled(Editor) + */ + public static void resetAllEditorsToDefaultState() { + ApplicationManager.getApplication().assertIsDispatchThread(); + for (Editor editor : EditorFactory.getInstance().getAllEditors()) { + DocRenderItem.resetToDefaultState(editor); + DocRenderPassFactory.forceRefreshOnNextPass(editor); + } + for (Project project : ProjectManager.getInstance().getOpenProjects()) { + DaemonCodeAnalyzer.getInstance(project).restart(); + } + } + + /** + * Sets all doc comments to their default state (rendered or not rendered) in the specified editor. + * + * @see #isDocRenderingEnabled(Editor) + */ + public static void resetEditorToDefaultState(@NotNull Editor editor) { + ApplicationManager.getApplication().assertIsDispatchThread(); + DocRenderItem.resetToDefaultState(editor); + DocRenderPassFactory.forceRefreshOnNextPass(editor); + Project project = editor.getProject(); + if (project != null) { + DaemonCodeAnalyzer.getInstance(project).restart(); + } + } +} diff --git a/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/DocRenderPassFactory.java b/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/DocRenderPassFactory.java index db4622fca893..b70a418b7923 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/DocRenderPassFactory.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/DocRenderPassFactory.java @@ -8,7 +8,6 @@ import com.intellij.codeInsight.documentation.DocumentationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.ex.EditorSettingsExternalizable; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.DumbAware; import com.intellij.openapi.project.IndexNotReadyException; @@ -61,7 +60,7 @@ public class DocRenderPassFactory implements TextEditorHighlightingPassFactoryRe @Override public void doCollectInformation(@NotNull ProgressIndicator progress) { - items = calculateItemsToRender(myDocument, myFile); + items = calculateItemsToRender(myEditor, myFile); } @Override @@ -71,8 +70,9 @@ public class DocRenderPassFactory implements TextEditorHighlightingPassFactoryRe } @NotNull - public static Items calculateItemsToRender(@NotNull Document document, @NotNull PsiFile psiFile) { - boolean enabled = EditorSettingsExternalizable.getInstance().isDocCommentRenderingEnabled(); + public static Items calculateItemsToRender(@NotNull Editor editor, @NotNull PsiFile psiFile) { + boolean enabled = DocRenderManager.isDocRenderingEnabled(editor); + Document document = editor.getDocument(); Items items = new Items(); DocumentationManager.getProviderFromElement(psiFile).collectDocComments(psiFile, comment -> { TextRange range = comment.getTextRange(); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/ToggleRenderAllDocs.java b/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/ToggleRenderAllDocs.java index 611e55646a14..e060f715607e 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/ToggleRenderAllDocs.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/documentation/render/ToggleRenderAllDocs.java @@ -16,6 +16,6 @@ public class ToggleRenderAllDocs extends ToggleAction implements DumbAware { @Override public void setSelected(@NotNull AnActionEvent e, boolean state) { EditorSettingsExternalizable.getInstance().setDocCommentRenderingEnabled(state); - DocRenderItem.resetAllToDefaultState(); + DocRenderManager.resetAllEditorsToDefaultState(); } } diff --git a/platform/lang-impl/src/com/intellij/openapi/fileEditor/impl/text/PsiAwareTextEditorImpl.java b/platform/lang-impl/src/com/intellij/openapi/fileEditor/impl/text/PsiAwareTextEditorImpl.java index fe8be712e824..308b67cacad0 100644 --- a/platform/lang-impl/src/com/intellij/openapi/fileEditor/impl/text/PsiAwareTextEditorImpl.java +++ b/platform/lang-impl/src/com/intellij/openapi/fileEditor/impl/text/PsiAwareTextEditorImpl.java @@ -9,6 +9,7 @@ import com.intellij.codeHighlighting.BackgroundEditorHighlighter; import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; import com.intellij.codeInsight.daemon.impl.TextEditorBackgroundHighlighter; import com.intellij.codeInsight.daemon.impl.focusMode.FocusModePassFactory; +import com.intellij.codeInsight.documentation.render.DocRenderManager; import com.intellij.codeInsight.documentation.render.DocRenderPassFactory; import com.intellij.codeInsight.folding.CodeFoldingManager; import com.intellij.codeInsight.lookup.LookupManager; @@ -18,7 +19,6 @@ import com.intellij.openapi.actionSystem.LangDataKeys; import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.editor.ex.EditorSettingsExternalizable; import com.intellij.openapi.editor.impl.EditorImpl; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.module.ModuleUtilCore; @@ -54,14 +54,13 @@ public class PsiAwareTextEditorImpl extends TextEditorImpl { List focusZones = FocusModePassFactory.calcFocusZones(psiFile); - DocRenderPassFactory.Items items = - document != null && psiFile != null && EditorSettingsExternalizable.getInstance().isDocCommentRenderingEnabled() - ? DocRenderPassFactory.calculateItemsToRender(document, psiFile) - : null; + Editor editor = getEditor(); + DocRenderPassFactory.Items items = document != null && psiFile != null && DocRenderManager.isDocRenderingEnabled(getEditor()) + ? DocRenderPassFactory.calculateItemsToRender(editor, psiFile) + : null; return () -> { baseResult.run(); - Editor editor = getEditor(); if (foldingState != null) { foldingState.setToEditor(editor);