Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: c# improvements #586

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 211 additions & 2 deletions crates/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15769,8 +15769,7 @@ fn csharp_record_declaration() {
| $assignments
| }
|
| $_
| $_
| $...
|
| public override bool Equals($type other) {
| $equality
Expand Down Expand Up @@ -16025,6 +16024,65 @@ fn csharp_groupby_keyword() {
.unwrap();
}

#[test]
fn csharp_partial_linq_query() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language csharp
|
|`group $item by $item.$key into $g select $k` => `select $k`
|"#
.trim_margin()
.unwrap(),
source: r#"
|var result = from user in users
| group user by user.Age into g
| select new { Key = g.Key, Count = g.Count()};
|"#
.trim_margin()
.unwrap(),
expected: r#"
|var result = from user in users
| select new { Key = g.Key, Count = g.Count()};
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}

#[test]
fn csharp_partial_linq_query_2() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language csharp
|
|`orderby $column $order` => `orderby $column`
|"#
.trim_margin()
.unwrap(),
source: r#"
|var orderedNames = from name in names
| orderby name ascending
| select name;
|"#
.trim_margin()
.unwrap(),
expected: r#"
|var orderedNames = from name in names
| orderby name
| select name;
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}

#[test]
fn csharp_simple_test() {
run_test_expected({
Expand Down Expand Up @@ -16091,3 +16149,154 @@ fn csharp_or_file() {
})
.unwrap();
}

#[test]
fn csharp_empty_namespace() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language csharp
|
|`namespace MyNamespace {
| $body
|}` => `namespace Empty {
|}`
|"#
.trim_margin()
.unwrap(),
source: r#"
|namespace MyNamespace {
| public class MyClass { }
|}
|"#
.trim_margin()
.unwrap(),
expected: r#"
|namespace Empty {
|}
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}

#[test]
fn csharp_operator_overloading_rewrite() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language csharp
|
|`public static MyClass operator >>>(MyClass $a, MyClass $b) {
| return new MyClass();
|}` => `public static MyClass operator <<(MyClass $a, MyClass $b) {
| return new MyClass();
| }`
|"#
.trim_margin()
.unwrap(),
source: r#"
|public class MyClass {
| public static MyClass operator >>>(MyClass a, MyClass b) {
| return new MyClass();
| }
|}
|"#
.trim_margin()
.unwrap(),
expected: r#"
|public class MyClass {
| public static MyClass operator <<(MyClass a, MyClass b) {
| return new MyClass();
| }
|}
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}

#[test]
fn csharp_delegate() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language csharp
|
|`public delegate void MyDelegate(int $x);` => `public delegate void MyDelegate(string $x);`
|"#
.trim_margin()
.unwrap(),
source: r#"
|public delegate void MyDelegate(int x);
|"#
.trim_margin()
.unwrap(),
expected: r#"
|public delegate void MyDelegate(string x);
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}

#[test]
fn csharp_generics_remove_type() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language csharp
|
|`public class GenericClass<T> { }` => `public class GenericClass<T, U> { }`
|"#
.trim_margin()
.unwrap(),
source: r#"
|public class GenericClass<T> { }
|"#
.trim_margin()
.unwrap(),
expected: r#"
|public class GenericClass<T, U> { }
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}

#[test]
fn csharp_attribute_swap() {
run_test_expected({
TestArgExpected {
pattern: r#"
|language csharp
|
|`void $methodName([$attribute1][$attribute2] ref double x) { }` =>
|`void $methodName([$attribute2][$attribute1] ref double x) { }`
|"#
.trim_margin()
.unwrap(),
source: r#"
|void MethodA([In][Out] ref double x) { }
|void MethodB([Out][In] ref double x) { }
|"#
.trim_margin()
.unwrap(),
expected: r#"
|void MethodA([Out][In] ref double x) { }
|void MethodB([In][Out] ref double x) { }
|"#
.trim_margin()
.unwrap(),
}
})
.unwrap();
}
2 changes: 2 additions & 0 deletions crates/language/src/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ impl Language for CSharp {
("GRIT_FUNC(", ");"),
("public class GRIT_CLASS {", "}"),
("", " { }"),
("return from GRIT_VAR in GRIT_VAR ", ";"),
("return from GRIT_VAR in GRIT_VAR ", " select GRIT_VAR;"),
]
}
}
Expand Down
Binary file modified crates/wasm-bindings/wasm_parsers/tree-sitter-c-sharp.wasm
Binary file not shown.
13 changes: 2 additions & 11 deletions resources/language-metavariables/tree-sitter-c-sharp/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading