Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
go: Add ability to run testable examples (#39390)
Lev Zakharov81b98cdd4d89
diff --git a/crates/languages/src/go.rs b/crates/languages/src/go.rsindex 8b9a4649e4b..13a4cec85ff 100644--- a/crates/languages/src/go.rs+++ b/crates/languages/src/go.rs@@ -635,6 +635,22 @@ impl ContextProvider for GoContextProvider {cwd: package_cwd.clone(),..TaskTemplate::default()},+ TaskTemplate {+ label: format!(+ "go test {} -run {}",+ GO_PACKAGE_TASK_VARIABLE.template_value(),+ VariableName::Symbol.template_value(),+ ),+ command: "go".into(),+ args: vec![+ "test".into(),+ "-run".into(),+ format!("\\^{}\\$", VariableName::Symbol.template_value(),),+ ],+ tags: vec!["go-example".to_owned()],+ cwd: package_cwd.clone(),+ ..TaskTemplate::default()+ },TaskTemplate {label: format!("go test {}", GO_PACKAGE_TASK_VARIABLE.template_value()),command: "go".into(),@@ -992,6 +1008,43 @@ mod tests {);}+ #[gpui::test]+ fn test_go_example_test_detection(cx: &mut TestAppContext) {+ let language = language("go", tree_sitter_go::LANGUAGE.into());++ let example_test = r#"+ package main++ import "fmt"++ func Example() {+ fmt.Println("Hello, world!")+ // Output: Hello, world!+ }+ "#;++ let buffer =+ cx.new(|cx| crate::Buffer::local(example_test, cx).with_language(language.clone(), cx));+ cx.executor().run_until_parked();++ let runnables: Vec<_> = buffer.update(cx, |buffer, _| {+ let snapshot = buffer.snapshot();+ snapshot.runnable_ranges(0..example_test.len()).collect()+ });++ let tag_strings: Vec<String> = runnables+ .iter()+ .flat_map(|r| &r.runnable.tags)+ .map(|tag| tag.0.to_string())+ .collect();++ assert!(+ tag_strings.contains(&"go-example".to_string()),+ "Should find go-example tag, found: {:?}",+ tag_strings+ );+ }+#[gpui::test]fn test_go_table_test_slice_detection(cx: &mut TestAppContext) {let language = language("go", tree_sitter_go::LANGUAGE.into());diff --git a/crates/languages/src/go/runnables.scm b/crates/languages/src/go/runnables.scmindex 15fafa11cb2..d3002a06cce 100644--- a/crates/languages/src/go/runnables.scm+++ b/crates/languages/src/go/runnables.scm@@ -71,6 +71,15 @@(#set! tag go-subtest))+; Functions names start with `Example`+(+ (+ (function_declaration name: (_) @run @_name+ (#match? @_name "^Example.*"))+ ) @_+ (#set! tag go-example)+)+; Functions names start with `Benchmark`((