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.

rust: Add runnable icon for `#[cfg(test)] mod tests` (#12017)

Remco Smits1e5389a2be36
diff --git a/crates/languages/src/rust.rs b/crates/languages/src/rust.rs
index 8b2e48ff159..fa13957d1d1 100644
--- a/crates/languages/src/rust.rs
+++ b/crates/languages/src/rust.rs
@@ -389,6 +389,22 @@ impl ContextProvider for RustContextProvider {
tags: vec!["rust-test".to_owned()],
..TaskTemplate::default()
},
+ TaskTemplate {
+ label: format!(
+ "cargo test -p {} {}",
+ RUST_PACKAGE_TASK_VARIABLE.template_value(),
+ VariableName::Stem.template_value(),
+ ),
+ command: "cargo".into(),
+ args: vec![
+ "test".into(),
+ "-p".into(),
+ RUST_PACKAGE_TASK_VARIABLE.template_value(),
+ VariableName::Stem.template_value(),
+ ],
+ tags: vec!["rust-mod-test".to_owned()],
+ ..TaskTemplate::default()
+ },
TaskTemplate {
label: format!(
"cargo test -p {}",
diff --git a/crates/languages/src/rust/runnables.scm b/crates/languages/src/rust/runnables.scm
index a0409623b87..f722fb00d70 100644
--- a/crates/languages/src/rust/runnables.scm
+++ b/crates/languages/src/rust/runnables.scm
@@ -1,3 +1,8 @@
+(mod_item
+ name: (_) @run
+ (#eq? @run "tests")
+) @rust-mod-test
+
(
(attribute_item (attribute
[((identifier) @_attribute)
diff --git a/crates/project/src/task_inventory.rs b/crates/project/src/task_inventory.rs
index 9f16de1f927..fbed35e3828 100644
--- a/crates/project/src/task_inventory.rs
+++ b/crates/project/src/task_inventory.rs
@@ -550,7 +550,17 @@ impl ContextProvider for BasicContextProvider {
task_variables.insert(VariableName::SelectedText, selected_text);
}
if let Some(path) = current_file {
- task_variables.insert(VariableName::File, path);
+ task_variables.insert(VariableName::File, path.clone());
+
+ let path = Path::new(&path);
+
+ if let Some(filename) = path.file_name().and_then(|f| f.to_str()) {
+ task_variables.insert(VariableName::Filename, String::from(filename));
+ }
+
+ if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
+ task_variables.insert(VariableName::Stem, String::from(stem));
+ }
}
let worktree_abs_path = buffer
diff --git a/crates/task/src/lib.rs b/crates/task/src/lib.rs
index d1c9aa22ccd..3d7a4e3a5b4 100644
--- a/crates/task/src/lib.rs
+++ b/crates/task/src/lib.rs
@@ -124,6 +124,10 @@ impl ResolvedTask {
pub enum VariableName {
/// An absolute path of the currently opened file.
File,
+ /// the currently opened filename.
+ Filename,
+ /// stem (filename without extension) of the currently opened file.
+ Stem,
/// An absolute path of the currently opened worktree, that contains the file.
WorktreeRoot,
/// A symbol text, that contains latest cursor/selection position.
@@ -160,6 +164,8 @@ impl std::fmt::Display for VariableName {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::File => write!(f, "{ZED_VARIABLE_NAME_PREFIX}FILE"),
+ Self::Filename => write!(f, "{ZED_VARIABLE_NAME_PREFIX}FILENAME"),
+ Self::Stem => write!(f, "{ZED_VARIABLE_NAME_PREFIX}STEM"),
Self::WorktreeRoot => write!(f, "{ZED_VARIABLE_NAME_PREFIX}WORKTREE_ROOT"),
Self::Symbol => write!(f, "{ZED_VARIABLE_NAME_PREFIX}SYMBOL"),
Self::Row => write!(f, "{ZED_VARIABLE_NAME_PREFIX}ROW"),
diff --git a/crates/tasks_ui/src/lib.rs b/crates/tasks_ui/src/lib.rs
index 8b91ccd2686..bbc3161cd7c 100644
--- a/crates/tasks_ui/src/lib.rs
+++ b/crates/tasks_ui/src/lib.rs
@@ -260,6 +260,8 @@ mod tests {
cwd: Some("/dir".into()),
task_variables: TaskVariables::from_iter([
(VariableName::File, "/dir/rust/b.rs".into()),
+ (VariableName::Filename, "b.rs".into()),
+ (VariableName::Stem, "b".into()),
(VariableName::WorktreeRoot, "/dir".into()),
(VariableName::Row, "1".into()),
(VariableName::Column, "1".into()),
@@ -276,6 +278,8 @@ mod tests {
cwd: Some("/dir".into()),
task_variables: TaskVariables::from_iter([
(VariableName::File, "/dir/rust/b.rs".into()),
+ (VariableName::Filename, "b.rs".into()),
+ (VariableName::Stem, "b".into()),
(VariableName::WorktreeRoot, "/dir".into()),
(VariableName::Row, "1".into()),
(VariableName::Column, "15".into()),
@@ -293,6 +297,8 @@ mod tests {
cwd: Some("/dir".into()),
task_variables: TaskVariables::from_iter([
(VariableName::File, "/dir/a.ts".into()),
+ (VariableName::Filename, "a.ts".into()),
+ (VariableName::Stem, "a".into()),
(VariableName::WorktreeRoot, "/dir".into()),
(VariableName::Row, "1".into()),
(VariableName::Column, "1".into()),
diff --git a/docs/src/tasks.md b/docs/src/tasks.md
index 5a9a81adc5f..a89f5e9436d 100644
--- a/docs/src/tasks.md
+++ b/docs/src/tasks.md
@@ -43,10 +43,12 @@ These variables allow you to pull information from the current editor and use it
- `ZED_COLUMN`: current line column
- `ZED_ROW`: current line row
-- `ZED_FILE`: absolute path to the file
+- `ZED_FILE`: absolute path of the currently opened file (e.g. `/Users/my-user/path/to/project/src/main.rs`)
+- `ZED_FILENAME`: filename of the currently opened file (e.g. `main.rs`)
+- `ZED_STEM`: stem (filename without extension) of the currently opened file (e.g. `main`)
- `ZED_SYMBOL`: currently selected symbol; should match the last symbol shown in a symbol breadcrumb (e.g. `mod tests > fn test_task_contexts`)
- `ZED_SELECTED_TEXT`: currently selected text
-- `ZED_WORKTREE_ROOT`: absolute path to the root of the current worktree.
+- `ZED_WORKTREE_ROOT`: absolute path to the root of the current worktree. (e.g. `/Users/my-user/path/to/project`)
- `ZED_CUSTOM_RUST_PACKAGE`: (Rust-specific) name of the parent package of $ZED_FILE source file.
To use a variable in a task, prefix it with a dollar sign (`$`):