Add basic bash and Python tasks (#10548)

Part of https://github.com/zed-industries/zed/issues/5141

* adds "run selection" and "run file" tasks for bash and Python.
* replaces newlines with `\n` symbols in the human-readable task labels
* properly escapes task command arguments when spawning the task in
terminal

Caveats:

* bash tasks will always use user's default shell to spawn the
selections, but they should rather respect the shebang line even if it's
not selected
* Python tasks will always use `python3` to spawn its tasks now, as
there's no proper mechanism in Zed to deal with different Python
executables

Release Notes:

- Added tasks for bash and Python to execute selections and open files
in terminal
This commit is contained in:
Kirill Bulatov 2024-04-15 15:07:21 +02:00 committed by GitHub
parent 1911a9f39b
commit db48c75231
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 100 additions and 17 deletions

1
Cargo.lock generated
View File

@ -9842,6 +9842,7 @@ dependencies = [
"serde_json",
"settings",
"shellexpand",
"shlex",
"smol",
"task",
"terminal",

View File

@ -298,6 +298,7 @@ serde_json_lenient = { version = "0.1", features = [
] }
serde_repr = "0.1"
sha2 = "0.10"
shlex = "1.3"
shellexpand = "2.1.0"
smallvec = { version = "1.6", features = ["union"] }
smol = "1.2"

View File

@ -0,0 +1,18 @@
use language::ContextProviderWithTasks;
use task::{TaskTemplate, TaskTemplates, VariableName};
pub(super) fn bash_task_context() -> ContextProviderWithTasks {
ContextProviderWithTasks::new(TaskTemplates(vec![
TaskTemplate {
label: "execute selection".to_owned(),
command: VariableName::SelectedText.template_value(),
ignore_previously_resolved: true,
..TaskTemplate::default()
},
TaskTemplate {
label: format!("run '{}'", VariableName::File.template_value()),
command: VariableName::File.template_value(),
..TaskTemplate::default()
},
]))
}

View File

@ -8,10 +8,14 @@ use smol::stream::StreamExt;
use std::{str, sync::Arc};
use util::{asset_str, ResultExt};
use crate::{elixir::elixir_task_context, rust::RustContextProvider};
use crate::{
bash::bash_task_context, elixir::elixir_task_context, python::python_task_context,
rust::RustContextProvider,
};
use self::{deno::DenoSettings, elixir::ElixirSettings};
mod bash;
mod c;
mod css;
mod deno;
@ -133,7 +137,7 @@ pub fn init(
);
};
}
language!("bash");
language!("bash", Vec::new(), bash_task_context());
language!("c", vec![Arc::new(c::CLspAdapter) as Arc<dyn LspAdapter>]);
language!("cpp", vec![Arc::new(c::CLspAdapter)]);
language!(
@ -195,7 +199,8 @@ pub fn init(
"python",
vec![Arc::new(python::PythonLspAdapter::new(
node_runtime.clone(),
))]
))],
python_task_context()
);
language!(
"rust",

View File

@ -1,6 +1,6 @@
use anyhow::Result;
use async_trait::async_trait;
use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
use language::{ContextProviderWithTasks, LanguageServerName, LspAdapter, LspAdapterDelegate};
use lsp::LanguageServerBinary;
use node_runtime::NodeRuntime;
use std::{
@ -9,6 +9,7 @@ use std::{
path::{Path, PathBuf},
sync::Arc,
};
use task::{TaskTemplate, TaskTemplates, VariableName};
use util::ResultExt;
const SERVER_PATH: &str = "node_modules/pyright/langserver.index.js";
@ -180,6 +181,30 @@ async fn get_cached_server_binary(
}
}
pub(super) fn python_task_context() -> ContextProviderWithTasks {
ContextProviderWithTasks::new(TaskTemplates(vec![
TaskTemplate {
label: "execute selection".to_owned(),
command: "python3".to_owned(),
args: vec![
"-c".to_owned(),
format!(
"exec(r'''{}''')",
VariableName::SelectedText.template_value()
),
],
ignore_previously_resolved: true,
..TaskTemplate::default()
},
TaskTemplate {
label: format!("run '{}'", VariableName::File.template_value()),
command: "python3".to_owned(),
args: vec![VariableName::File.template_value()],
..TaskTemplate::default()
},
]))
}
#[cfg(test)]
mod tests {
use gpui::{BorrowAppContext, Context, ModelContext, TestAppContext};

View File

@ -214,12 +214,17 @@ impl Inventory {
.flat_map(|task| Some((task_source_kind.as_ref()?, task)));
let mut lru_score = 0_u32;
let mut task_usage = self.last_scheduled_tasks.iter().rev().fold(
let mut task_usage = self
.last_scheduled_tasks
.iter()
.rev()
.filter(|(_, task)| !task.original_task().ignore_previously_resolved)
.fold(
HashMap::default(),
|mut tasks, (task_source_kind, resolved_task)| {
tasks
.entry(&resolved_task.id)
.or_insert_with(|| (task_source_kind, resolved_task, post_inc(&mut lru_score)));
tasks.entry(&resolved_task.id).or_insert_with(|| {
(task_source_kind, resolved_task, post_inc(&mut lru_score))
});
tasks
},
);

View File

@ -44,6 +44,7 @@ impl Project {
.unwrap_or_else(|| Path::new(""));
let (spawn_task, shell) = if let Some(spawn_task) = spawn_task {
log::debug!("Spawning task: {spawn_task:?}");
env.extend(spawn_task.env);
// Activate minimal Python virtual environment
if let Some(python_settings) = &python_settings.as_option() {

View File

@ -39,6 +39,20 @@ pub struct TaskTemplate {
/// Whether to allow multiple instances of the same task to be run, or rather wait for the existing ones to finish.
#[serde(default)]
pub allow_concurrent_runs: bool,
// Tasks like "execute the selection" better have the constant labels (to avoid polluting the history with temporary tasks),
// and always use the latest context with the latest selection.
//
// Current impl will always pick previously spawned tasks on full label conflict in the tasks modal and terminal tabs, never
// getting the latest selection for them.
// This flag inverts the behavior, effectively removing all previously spawned tasks from history,
// if their full labels are the same as the labels of the newly resolved tasks.
// Such tasks are still re-runnable, and will use the old context in that case (unless the rerun task forces this).
//
// Current approach is relatively hacky, a better way is understand when the new resolved tasks needs a rerun,
// and replace the historic task accordingly.
#[doc(hidden)]
#[serde(default)]
pub ignore_previously_resolved: bool,
/// What to do with the terminal pane and tab, after the command was started:
/// * `always` — always show the terminal pane, add and focus the corresponding task's tab in it (default)
/// * `never` — avoid changing current terminal pane focus, but still add/reuse the task's tab there
@ -114,12 +128,22 @@ impl TaskTemplate {
}
.map(PathBuf::from)
.or(cx.cwd.clone());
let shortened_label = substitute_all_template_variables_in_str(
let human_readable_label = substitute_all_template_variables_in_str(
&self.label,
&truncated_variables,
&variable_names,
&mut substituted_variables,
)?;
)?
.lines()
.fold(String::new(), |mut string, line| {
if string.is_empty() {
string.push_str(line);
} else {
string.push_str("\\n");
string.push_str(line);
}
string
});
let full_label = substitute_all_template_variables_in_str(
&self.label,
&task_variables,
@ -162,7 +186,7 @@ impl TaskTemplate {
id,
cwd,
full_label,
label: shortened_label,
label: human_readable_label,
command,
args,
env,

View File

@ -286,6 +286,7 @@ impl Display for TerminalError {
}
}
#[derive(Debug)]
pub struct SpawnTask {
pub id: TaskId,
pub full_label: String,

View File

@ -28,6 +28,7 @@ search.workspace = true
serde.workspace = true
serde_json.workspace = true
settings.workspace = true
shlex.workspace = true
shellexpand.workspace = true
smol.workspace = true
terminal.workspace = true

View File

@ -1,4 +1,4 @@
use std::{ops::ControlFlow, path::PathBuf, sync::Arc};
use std::{borrow::Cow, ops::ControlFlow, path::PathBuf, sync::Arc};
use crate::TerminalView;
use collections::{HashMap, HashSet};
@ -319,6 +319,7 @@ impl TerminalPanel {
let args = std::mem::take(&mut spawn_task.args);
for arg in args {
command.push(' ');
let arg = shlex::try_quote(&arg).unwrap_or(Cow::Borrowed(&arg));
command.push_str(&arg);
}
spawn_task.command = shell;