From c1bb7e99e89a69e7a8b150e968d5f8fbf1c5878a Mon Sep 17 00:00:00 2001 From: Ekaterina Berezhko Date: Mon, 24 Aug 2026 15:02:57 +0300 Subject: [PATCH] [Markdown][IJPL-253568] Make the Markdown preview layout setting govern every editor (cherry picked from commit 445bb72854646ef5db413fdf019402d1fe8f3fe8) IJ-CR-220760 GitOrigin-RevId: 399603c2abd08b7ae62b59cdfa6d6c9c598a3798 --- .../ui/preview/MarkdownEditorWithPreview.java | 21 ++++++++ .../MarkdownPreviewLayoutSettingTest.kt | 50 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 plugins/markdown/test/src/org/intellij/plugins/markdown/ui/preview/MarkdownPreviewLayoutSettingTest.kt diff --git a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownEditorWithPreview.java b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownEditorWithPreview.java index 51c872bda568..34a1ab6c2923 100644 --- a/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownEditorWithPreview.java +++ b/plugins/markdown/core/src/org/intellij/plugins/markdown/ui/preview/MarkdownEditorWithPreview.java @@ -6,9 +6,11 @@ import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.event.VisibleAreaEvent; import com.intellij.openapi.editor.event.VisibleAreaListener; import com.intellij.openapi.editor.impl.EditorImpl; +import com.intellij.openapi.fileEditor.FileEditorState; import com.intellij.openapi.fileEditor.TextEditor; import com.intellij.openapi.fileEditor.TextEditorWithPreview; import com.intellij.openapi.project.Project; +import com.intellij.ui.JBSplitter; import org.intellij.plugins.markdown.MarkdownBundle; import org.intellij.plugins.markdown.settings.MarkdownSettings; import org.jetbrains.annotations.NotNull; @@ -16,6 +18,7 @@ import org.jetbrains.annotations.NotNull; import java.awt.Point; public final class MarkdownEditorWithPreview extends TextEditorWithPreview { + private final MarkdownSettings settings; private boolean autoScrollPreview; /** @@ -38,6 +41,8 @@ public final class MarkdownEditorWithPreview extends TextEditorWithPreview { !settings.isVerticalSplit() ); + this.settings = settings; + // allow launching actions while in preview mode; // FIXME: better solution IDEA-354102 editor.getEditor().getContentComponent().putClientProperty(ActionUtil.ALLOW_ACTION_PERFORM_WHEN_HIDDEN, true); @@ -64,6 +69,22 @@ public final class MarkdownEditorWithPreview extends TextEditorWithPreview { editor.getEditor().getScrollingModel().addVisibleAreaListener(new MyVisibleAreaListener(), this); } + @Override + protected @NotNull JBSplitter createSplitter() { + JBSplitter splitter = super.createSplitter(); + // "Preview layout" must apply to a freshly created editor, not just to an already open one. + splitter.setOrientation(!settings.isVerticalSplit()); + return splitter; + } + + @Override + public void setState(@NotNull FileEditorState state) { + super.setState(state); + // "Preview layout" is a global default, so it must win over the per-file orientation + // that super.setState() restores from the editor state. See IJPL-253568. + handleLayoutChange(!settings.isVerticalSplit()); + } + @Override protected void onLayoutChange(Layout oldValue, Layout newValue) { super.onLayoutChange(oldValue, newValue); diff --git a/plugins/markdown/test/src/org/intellij/plugins/markdown/ui/preview/MarkdownPreviewLayoutSettingTest.kt b/plugins/markdown/test/src/org/intellij/plugins/markdown/ui/preview/MarkdownPreviewLayoutSettingTest.kt new file mode 100644 index 000000000000..620a50e6c8c4 --- /dev/null +++ b/plugins/markdown/test/src/org/intellij/plugins/markdown/ui/preview/MarkdownPreviewLayoutSettingTest.kt @@ -0,0 +1,50 @@ +// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.intellij.plugins.markdown.ui.preview + +import com.intellij.openapi.fileEditor.TextEditorWithPreview +import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider +import com.intellij.openapi.util.Disposer +import com.intellij.testFramework.fixtures.BasePlatformTestCase +import com.intellij.ui.JBSplitter +import com.intellij.util.ui.UIUtil +import org.intellij.plugins.markdown.settings.MarkdownSettings + +/** + * see IJPL-253568 + */ +class MarkdownPreviewLayoutSettingTest : BasePlatformTestCase() { + fun testSplitHorizontallySurvivesRestoredPerFileOrientation() { + val settings = MarkdownSettings.getInstance(project) + settings.update { it.isVerticalSplit = false } + + val splitter = splitterOf(settings) + assertTrue("setting must be applied to a freshly created editor", splitter.orientation) + } + + fun testRestoredStateDoesNotOverrideSetting() { + val settings = MarkdownSettings.getInstance(project) + settings.update { it.isVerticalSplit = false } + + val editorWithPreview = createEditor(settings) + val splitter = UIUtil.findComponentOfType(editorWithPreview.component, JBSplitter::class.java)!! + + editorWithPreview.setState( + TextEditorWithPreview.MyFileEditorState(TextEditorWithPreview.Layout.SHOW_EDITOR_AND_PREVIEW, null, null, false) + ) + + assertTrue("global setting must win over restored per-file orientation", splitter.orientation) + } + + private fun splitterOf(settings: MarkdownSettings): JBSplitter = + UIUtil.findComponentOfType(createEditor(settings).component, JBSplitter::class.java)!! + + private fun createEditor(settings: MarkdownSettings): MarkdownEditorWithPreview { + myFixture.configureByText("test.md", "# Heading\n\ntext\n") + val file = myFixture.file.virtualFile + val textEditor = TextEditorProvider.getInstance().getTextEditor(myFixture.editor) + val preview = MarkdownPreviewFileEditor(project, file, myFixture.editor.document) + val editorWithPreview = MarkdownEditorWithPreview(textEditor, preview, project, settings) + Disposer.register(testRootDisposable, editorWithPreview) + return editorWithPreview + } +}