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.

Surround selection with backticks in Rust, C, and C++ comments (#58674)

Lee ByeongJuncb498ace96ab
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index fb34017052d..e69bee21561 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -14185,6 +14185,46 @@ async fn test_autoclose_quotes_with_multibyte_characters(cx: &mut TestAppContext
"#});
}
+#[gpui::test]
+async fn test_surround_backticks_in_rust(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let mut cx = EditorTestContext::new(cx).await;
+ cx.update_buffer(|buffer, cx| {
+ let language = languages::language("rust", tree_sitter_rust::LANGUAGE.into());
+ buffer.set_language(Some(language), cx)
+ });
+
+ // Surround a selection inside a doc comment with backticks
+ cx.set_state(indoc! {"
+ /// «Aˇ»
+ fn main() {}
+ "});
+ cx.update_editor(|editor, window, cx| {
+ editor.handle_input("`", window, cx);
+ });
+ cx.assert_editor_state(indoc! {"
+ /// `«Aˇ»`
+ fn main() {}
+ "});
+
+ // When inside a string literal, the backtick pair is disabled so typing a
+ // backtick should replace the selection instead of surrounding it.
+ cx.set_state(indoc! {r#"
+ fn main() {
+ let name = "«Jesper Kouthoofdˇ»";
+ }
+ "#});
+ cx.update_editor(|editor, window, cx| {
+ editor.handle_input("`", window, cx);
+ });
+ cx.assert_editor_state(indoc! {r#"
+ fn main() {
+ let name = "`ˇ";
+ }
+ "#});
+}
+
#[gpui::test]
async fn test_surround_with_pair(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/grammars/src/c/config.toml b/crates/grammars/src/c/config.toml
index 9977bb90373..aae8cf5a8fb 100644
--- a/crates/grammars/src/c/config.toml
+++ b/crates/grammars/src/c/config.toml
@@ -12,6 +12,7 @@ brackets = [
{ start = "[", end = "]", close = true, newline = true },
{ start = "(", end = ")", close = true, newline = true },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
+ { start = "`", end = "`", close = false, newline = false, not_in = ["string"] },
{ start = "'", end = "'", close = true, newline = false, not_in = ["string", "comment"] },
{ start = "/*", end = " */", close = true, newline = false, not_in = ["string", "comment"] },
]
diff --git a/crates/grammars/src/cpp/config.toml b/crates/grammars/src/cpp/config.toml
index b0992c5c120..dd437ddffb2 100644
--- a/crates/grammars/src/cpp/config.toml
+++ b/crates/grammars/src/cpp/config.toml
@@ -14,6 +14,7 @@ brackets = [
{ start = "[", end = "]", close = true, newline = true },
{ start = "(", end = ")", close = true, newline = true },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
+ { start = "`", end = "`", close = false, newline = false, not_in = ["string"] },
{ start = "'", end = "'", close = true, newline = false, not_in = ["string", "comment"] },
{ start = "/*", end = " */", close = true, newline = false, not_in = ["string", "comment"] },
]
diff --git a/crates/grammars/src/rust/config.toml b/crates/grammars/src/rust/config.toml
index 6c5763fa642..39ab34b678c 100644
--- a/crates/grammars/src/rust/config.toml
+++ b/crates/grammars/src/rust/config.toml
@@ -13,6 +13,7 @@ brackets = [
{ start = "(", end = ")", close = true, newline = true },
{ start = "<", end = ">", close = false, newline = true, not_in = ["string", "comment"] },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
+ { start = "`", end = "`", close = false, newline = false, not_in = ["string"] },
{ start = "/*", end = " */", close = true, newline = false, not_in = ["string", "comment"] },
]
collapsed_placeholder = " /* ... */ "