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.

grammars: Support linked editing of custom elements in JSX and TSX (#58578)

Kunall Banerjee6109c2e6d83b
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 980a5892444..61c3312d4cd 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -34059,6 +34059,69 @@ async fn test_linked_edits_on_typing_punctuation(cx: &mut TestAppContext) {
cx.assert_editor_state("<Animated.Vˇ></Animated.V>");
}
+#[gpui::test]
+async fn test_linked_edits_on_typing_dash_in_custom_element_name(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let mut cx = EditorTestContext::new(cx).await;
+ let language = Arc::new(Language::new(
+ LanguageConfig {
+ name: "TSX".into(),
+ matcher: (LanguageMatcher {
+ path_suffixes: vec!["tsx".to_string()],
+ ..LanguageMatcher::default()
+ })
+ .into(),
+ brackets: BracketPairConfig {
+ pairs: vec![BracketPair {
+ start: "<".into(),
+ end: ">".into(),
+ close: true,
+ ..Default::default()
+ }],
+ ..Default::default()
+ },
+ // Mirrors crates/grammars/src/{tsx,javascript}/config.toml: a dash is a valid
+ // JSXIdentifier character, so custom element names like `<custom-el>` must keep
+ // the closing tag linked past the dash. Regression test for #43060 / #58553.
+ linked_edit_characters: HashSet::from_iter(['.', '-']),
+ ..Default::default()
+ },
+ Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
+ ));
+ cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
+
+ // Typing the dash while renaming a tag must extend the linked pair rather than
+ // dropping it (the bug produced `<custom-el></custom>`).
+ cx.set_state("<customˇ></custom>");
+ cx.update_editor(|editor, _, cx| {
+ set_linked_edit_ranges(
+ (Point::new(0, 1), Point::new(0, 7)),
+ (Point::new(0, 10), Point::new(0, 16)),
+ editor,
+ cx,
+ );
+ });
+ cx.update_editor(|editor, window, cx| {
+ editor.handle_input("-", window, cx);
+ });
+ cx.assert_editor_state("<custom-ˇ></custom->");
+
+ // Characters typed after the dash continue to mirror into the closing tag.
+ cx.update_editor(|editor, _, cx| {
+ set_linked_edit_ranges(
+ (Point::new(0, 1), Point::new(0, 8)),
+ (Point::new(0, 11), Point::new(0, 18)),
+ editor,
+ cx,
+ );
+ });
+ cx.update_editor(|editor, window, cx| {
+ editor.handle_input("el", window, cx);
+ });
+ cx.assert_editor_state("<custom-elˇ></custom-el>");
+}
+
#[gpui::test]
async fn test_linked_edits_on_typing_dot_without_language_override(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/grammars/src/javascript/config.toml b/crates/grammars/src/javascript/config.toml
index 66956f1ed60..5b8eaaf49df 100644
--- a/crates/grammars/src/javascript/config.toml
+++ b/crates/grammars/src/javascript/config.toml
@@ -32,7 +32,7 @@ jsx_element_node_name = "jsx_element"
tag_name_node_name = "identifier"
[overrides.default]
-linked_edit_characters = ["."]
+linked_edit_characters = [".", "-"]
[overrides.element]
line_comments = { remove = true }
diff --git a/crates/grammars/src/tsx/config.toml b/crates/grammars/src/tsx/config.toml
index efc180f5cf3..72a0e55aaf5 100644
--- a/crates/grammars/src/tsx/config.toml
+++ b/crates/grammars/src/tsx/config.toml
@@ -31,7 +31,7 @@ tag_name_node_name = "identifier"
tag_name_node_name_alternates = ["member_expression"]
[overrides.default]
-linked_edit_characters = ["."]
+linked_edit_characters = [".", "-"]
[overrides.element]
line_comments = { remove = true }