From f4378672b8bf1488c56b76602e1024c7aa4cdaa8 Mon Sep 17 00:00:00 2001 From: Smit Barmase Date: Thu, 11 Dec 2025 21:14:15 +0530 Subject: [PATCH] editor: Fix auto-indent cases in Markdown (#44616) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Builds on https://github.com/zed-industries/zed/pull/40794 and https://github.com/zed-industries/zed/pull/44381 - Fixes the case where creating a new line inside a nested list puts the cursor correctly under that nested list item. - Fixes the case where typing a new list item at the expected indent no longer auto-indents or outdents incorrectly. Release Notes: - Fixed an issue in Markdown where new list items weren’t respecting the expected indentation on type. --- crates/editor/src/editor_tests.rs | 71 ++++++++++++++++------- crates/languages/src/markdown/config.toml | 6 +- crates/languages/src/markdown/indents.scm | 2 + 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs index c97607bb256ff4b4e3054d5de4e3057e58798e73..89131e8bc39fc03e54e19c9d8b1f79a7f2d66cb9 100644 --- a/crates/editor/src/editor_tests.rs +++ b/crates/editor/src/editor_tests.rs @@ -27701,6 +27701,7 @@ async fn test_markdown_indents(cx: &mut gpui::TestAppContext) { cx.update_editor(|editor, window, cx| { editor.handle_input("x", window, cx); }); + cx.run_until_parked(); cx.assert_editor_state(indoc! {" - [ ] Item 1 - [ ] Item 1.a @@ -27716,8 +27717,7 @@ async fn test_markdown_indents(cx: &mut gpui::TestAppContext) { - [ ] Item 1.a - [x] Item 2 - [x] Item 2.a - - [x] Item 2.bˇ - " + - [x] Item 2.bˇ" }); cx.update_editor(|editor, window, cx| { editor.newline(&Newline, window, cx); @@ -27728,34 +27728,41 @@ async fn test_markdown_indents(cx: &mut gpui::TestAppContext) { - [x] Item 2 - [x] Item 2.a - [x] Item 2.b - ˇ - " + ˇ" }); // Case 3: Test adding a new nested list item preserves indent + cx.set_state(&indoc! {" + - [ ] Item 1 + - [ ] Item 1.a + - [x] Item 2 + - [x] Item 2.a + - [x] Item 2.b + ˇ" + }); cx.update_editor(|editor, window, cx| { editor.handle_input("-", window, cx); }); + cx.run_until_parked(); cx.assert_editor_state(indoc! {" - [ ] Item 1 - [ ] Item 1.a - [x] Item 2 - [x] Item 2.a - [x] Item 2.b - -ˇ - " + -ˇ" }); cx.update_editor(|editor, window, cx| { editor.handle_input(" [x] Item 2.c", window, cx); }); + cx.run_until_parked(); cx.assert_editor_state(indoc! {" - [ ] Item 1 - [ ] Item 1.a - [x] Item 2 - [x] Item 2.a - [x] Item 2.b - - [x] Item 2.cˇ - " + - [x] Item 2.cˇ" }); // Case 4: Test adding new line after nested ordered list preserves indent of previous line @@ -27764,8 +27771,7 @@ async fn test_markdown_indents(cx: &mut gpui::TestAppContext) { 1. Item 1.a 2. Item 2 1. Item 2.a - 2. Item 2.bˇ - " + 2. Item 2.bˇ" }); cx.update_editor(|editor, window, cx| { editor.newline(&Newline, window, cx); @@ -27776,60 +27782,81 @@ async fn test_markdown_indents(cx: &mut gpui::TestAppContext) { 2. Item 2 1. Item 2.a 2. Item 2.b - ˇ - " + ˇ" }); // Case 5: Adding new ordered list item preserves indent + cx.set_state(indoc! {" + 1. Item 1 + 1. Item 1.a + 2. Item 2 + 1. Item 2.a + 2. Item 2.b + ˇ" + }); cx.update_editor(|editor, window, cx| { editor.handle_input("3", window, cx); }); + cx.run_until_parked(); cx.assert_editor_state(indoc! {" 1. Item 1 1. Item 1.a 2. Item 2 1. Item 2.a 2. Item 2.b - 3ˇ - " + 3ˇ" }); cx.update_editor(|editor, window, cx| { editor.handle_input(".", window, cx); }); + cx.run_until_parked(); cx.assert_editor_state(indoc! {" 1. Item 1 1. Item 1.a 2. Item 2 1. Item 2.a 2. Item 2.b - 3.ˇ - " + 3.ˇ" }); cx.update_editor(|editor, window, cx| { editor.handle_input(" Item 2.c", window, cx); }); + cx.run_until_parked(); cx.assert_editor_state(indoc! {" 1. Item 1 1. Item 1.a 2. Item 2 1. Item 2.a 2. Item 2.b - 3. Item 2.cˇ - " + 3. Item 2.cˇ" }); + // Case 6: Test adding new line after nested ordered list preserves indent of previous line + cx.set_state(indoc! {" + - Item 1 + - Item 1.a + - Item 1.a + ˇ"}); + cx.update_editor(|editor, window, cx| { + editor.handle_input("-", window, cx); + }); + cx.run_until_parked(); + cx.assert_editor_state(indoc! {" + - Item 1 + - Item 1.a + - Item 1.a + -ˇ"}); + // Case 7: Test blockquote newline preserves something cx.set_state(indoc! {" - > Item 1ˇ - " + > Item 1ˇ" }); cx.update_editor(|editor, window, cx| { editor.newline(&Newline, window, cx); }); cx.assert_editor_state(indoc! {" > Item 1 - ˇ - " + ˇ" }); } diff --git a/crates/languages/src/markdown/config.toml b/crates/languages/src/markdown/config.toml index f786c61b8155b0fa4c93b43e2126cc66d86e22e4..84c79d2538a0af470ec16d55fe9cf2d1ae05805b 100644 --- a/crates/languages/src/markdown/config.toml +++ b/crates/languages/src/markdown/config.toml @@ -24,5 +24,9 @@ rewrap_prefixes = [ auto_indent_on_paste = false auto_indent_using_last_non_empty_line = false tab_size = 2 -decrease_indent_pattern = "^.*$" +decrease_indent_patterns = [ + { pattern = "^\\s*-", valid_after = ["list_item"] }, + { pattern = "^\\s*\\d", valid_after = ["list_item"] }, + { pattern = "^\\s*", valid_after = ["list_item"] }, +] prettier_parser_name = "markdown" diff --git a/crates/languages/src/markdown/indents.scm b/crates/languages/src/markdown/indents.scm index 2840e8b4611c05ef4c88e775c970eb79b9b99f4a..dc6dfa6118309c264e146a5af167327947fc6946 100644 --- a/crates/languages/src/markdown/indents.scm +++ b/crates/languages/src/markdown/indents.scm @@ -1 +1,3 @@ (list (list_item) @indent) + +(list_item) @start.list_item