Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Fix auto-indent cases in Markdown (#44616)

Smit Barmasef4378672b8bf
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index c97607bb256..89131e8bc39 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 f786c61b815..84c79d2538a 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 2840e8b4611..dc6dfa61183 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