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.

languages: Fix broken whitespace for C++ (#53709)

Phillip Davis8b1520467b3a
diff --git a/crates/grammars/src/cpp/indents.scm b/crates/grammars/src/cpp/indents.scm
index 0b55631e5ca..ebd5afb7c74 100644
--- a/crates/grammars/src/cpp/indents.scm
+++ b/crates/grammars/src/cpp/indents.scm
@@ -13,6 +13,14 @@
"{"
"}" @end) @indent
+(field_declaration_list
+ (access_specifier) @start
+ "}" @end) @indent
+
+(field_declaration_list
+ (access_specifier)
+ (access_specifier) @outdent)
+
(_
"("
")" @end) @indent
diff --git a/crates/languages/Cargo.toml b/crates/languages/Cargo.toml
index 93c70d4b27a..4188c97a097 100644
--- a/crates/languages/Cargo.toml
+++ b/crates/languages/Cargo.toml
@@ -67,6 +67,7 @@ util.workspace = true
[dev-dependencies]
fs = { workspace = true, features = ["test-support"] }
pretty_assertions.workspace = true
+settings = { workspace = true, features = ["test-support"] }
theme = { workspace = true, features = ["test-support"] }
tree-sitter-bash.workspace = true
tree-sitter-c.workspace = true
diff --git a/crates/languages/src/cpp.rs b/crates/languages/src/cpp.rs
index 5985baa5480..0577a558260 100644
--- a/crates/languages/src/cpp.rs
+++ b/crates/languages/src/cpp.rs
@@ -16,6 +16,332 @@ mod tests {
use std::num::NonZeroU32;
use unindent::Unindent;
+ #[gpui::test]
+ async fn test_cpp_autoindent_access_specifier(cx: &mut TestAppContext) {
+ cx.update(|cx| {
+ let test_settings = SettingsStore::test(cx);
+ cx.set_global(test_settings);
+ cx.update_global::<SettingsStore, _>(|store, cx| {
+ store.update_user_settings(cx, |s| {
+ s.project.all_languages.defaults.tab_size = NonZeroU32::new(2);
+ });
+ });
+ });
+ let language = crate::language("cpp", tree_sitter_cpp::LANGUAGE.into());
+
+ cx.new(|cx| {
+ let mut buffer = Buffer::local("", cx).with_language(language, cx);
+
+ buffer.edit(
+ [(
+ 0..0,
+ r#"
+ class Foo {
+ public:
+ void bar();
+ private:
+ int x;
+ };
+ "#
+ .unindent(),
+ )],
+ Some(AutoindentMode::EachLine),
+ cx,
+ );
+ assert_eq!(
+ buffer.text(),
+ r#"
+ class Foo {
+ public:
+ void bar();
+ private:
+ int x;
+ };
+ "#
+ .unindent(),
+ "members after access specifiers should be indented one level deeper than the specifier"
+ );
+
+ buffer
+ });
+ }
+
+ #[gpui::test]
+ async fn test_cpp_autoindent_access_specifier_next_line(cx: &mut TestAppContext) {
+ cx.update(|cx| {
+ let test_settings = SettingsStore::test(cx);
+ cx.set_global(test_settings);
+ cx.update_global::<SettingsStore, _>(|store, cx| {
+ store.update_user_settings(cx, |s| {
+ s.project.all_languages.defaults.tab_size = NonZeroU32::new(2);
+ });
+ });
+ });
+ let language = crate::language("cpp", tree_sitter_cpp::LANGUAGE.into());
+
+ cx.new(|cx| {
+ let mut buffer = Buffer::local("", cx).with_language(language, cx);
+
+ buffer.edit(
+ [(
+ 0..0,
+ r#"
+ class Foo {
+ public:
+ void bar();
+ void baz();
+ private:
+ int x;
+ };
+ "#
+ .unindent(),
+ )],
+ Some(AutoindentMode::EachLine),
+ cx,
+ );
+ assert_eq!(
+ buffer.text(),
+ r#"
+ class Foo {
+ public:
+ void bar();
+ void baz();
+ private:
+ int x;
+ };
+ "#
+ .unindent(),
+ "members after access specifiers should be indented one level deeper than the specifier"
+ );
+
+ buffer
+ });
+ }
+
+ #[gpui::test]
+ async fn test_cpp_autoindent_nested_class_access_specifiers(cx: &mut TestAppContext) {
+ cx.update(|cx| {
+ let test_settings = SettingsStore::test(cx);
+ cx.set_global(test_settings);
+ cx.update_global::<SettingsStore, _>(|store, cx| {
+ store.update_user_settings(cx, |s| {
+ s.project.all_languages.defaults.tab_size = NonZeroU32::new(2);
+ });
+ });
+ });
+ let language = crate::language("cpp", tree_sitter_cpp::LANGUAGE.into());
+
+ cx.new(|cx| {
+ let mut buffer = Buffer::local("", cx).with_language(language, cx);
+
+ buffer.edit(
+ [(
+ 0..0,
+ r#"
+ class Outer {
+ public:
+ class Inner {
+ public:
+ void inner_pub();
+ private:
+ int inner_priv;
+ };
+ private:
+ int outer_priv;
+ };
+ "#
+ .unindent(),
+ )],
+ Some(AutoindentMode::EachLine),
+ cx,
+ );
+ assert_eq!(
+ buffer.text(),
+ r#"
+ class Outer {
+ public:
+ class Inner {
+ public:
+ void inner_pub();
+ private:
+ int inner_priv;
+ };
+ private:
+ int outer_priv;
+ };
+ "#
+ .unindent(),
+ "nested class access specifiers should indent independently at each nesting level"
+ );
+
+ buffer
+ });
+ }
+
+ #[gpui::test]
+ async fn test_cpp_autoindent_consecutive_access_specifiers(cx: &mut TestAppContext) {
+ cx.update(|cx| {
+ let test_settings = SettingsStore::test(cx);
+ cx.set_global(test_settings);
+ cx.update_global::<SettingsStore, _>(|store, cx| {
+ store.update_user_settings(cx, |s| {
+ s.project.all_languages.defaults.tab_size = NonZeroU32::new(2);
+ });
+ });
+ });
+ let language = crate::language("cpp", tree_sitter_cpp::LANGUAGE.into());
+
+ cx.new(|cx| {
+ let mut buffer = Buffer::local("", cx).with_language(language, cx);
+
+ buffer.edit(
+ [(
+ 0..0,
+ r#"
+ class Foo {
+ public:
+ protected:
+ private:
+ int x;
+ };
+ "#
+ .unindent(),
+ )],
+ Some(AutoindentMode::EachLine),
+ cx,
+ );
+ assert_eq!(
+ buffer.text(),
+ r#"
+ class Foo {
+ public:
+ protected:
+ private:
+ int x;
+ };
+ "#
+ .unindent(),
+ "consecutive access specifiers with no members between them should all align at class level"
+ );
+
+ buffer
+ });
+ }
+
+ #[gpui::test]
+ async fn test_cpp_autoindent_indented_access_specifiers(cx: &mut TestAppContext) {
+ cx.update(|cx| {
+ let test_settings = SettingsStore::test(cx);
+ cx.set_global(test_settings);
+ cx.update_global::<SettingsStore, _>(|store, cx| {
+ store.update_user_settings(cx, |s| {
+ s.project.all_languages.defaults.tab_size = NonZeroU32::new(2);
+ });
+ });
+ });
+ let language = crate::language("cpp", tree_sitter_cpp::LANGUAGE.into());
+
+ cx.new(|cx| {
+ let mut buffer = Buffer::local("", cx).with_language(language, cx);
+
+ buffer.edit(
+ [(
+ 0..0,
+ r#"
+ class Foo {
+ int default_member;
+ public:
+ void pub_method();
+ private:
+ int priv_member;
+ };
+ "#
+ .unindent(),
+ )],
+ Some(AutoindentMode::EachLine),
+ cx,
+ );
+ assert_eq!(
+ buffer.text(),
+ r#"
+ class Foo {
+ int default_member;
+ public:
+ void pub_method();
+ private:
+ int priv_member;
+ };
+ "#
+ .unindent(),
+ "access specifiers should be indented one level inside class braces"
+ );
+
+ buffer
+ });
+ }
+
+ #[gpui::test]
+ async fn test_cpp_autoindent_access_specifier_with_method_bodies(cx: &mut TestAppContext) {
+ cx.update(|cx| {
+ let test_settings = SettingsStore::test(cx);
+ cx.set_global(test_settings);
+ cx.update_global::<SettingsStore, _>(|store, cx| {
+ store.update_user_settings(cx, |s| {
+ s.project.all_languages.defaults.tab_size = NonZeroU32::new(2);
+ });
+ });
+ });
+ let language = crate::language("cpp", tree_sitter_cpp::LANGUAGE.into());
+
+ cx.new(|cx| {
+ let mut buffer = Buffer::local("", cx).with_language(language, cx);
+
+ buffer.edit(
+ [(
+ 0..0,
+ r#"
+ class Foo {
+ public:
+ void bar() {
+ if (x)
+ y++;
+ }
+ private:
+ int get_x() {
+ return x;
+ }
+ int x;
+ };
+ "#
+ .unindent(),
+ )],
+ Some(AutoindentMode::EachLine),
+ cx,
+ );
+ assert_eq!(
+ buffer.text(),
+ r#"
+ class Foo {
+ public:
+ void bar() {
+ if (x)
+ y++;
+ }
+ private:
+ int get_x() {
+ return x;
+ }
+ int x;
+ };
+ "#
+ .unindent(),
+ "method bodies inside access specifier sections should compose brace and specifier indent"
+ );
+
+ buffer
+ });
+ }
+
#[gpui::test]
async fn test_cpp_autoindent_basic(cx: &mut TestAppContext) {
cx.update(|cx| {