Extensions registering tasks (#9572)

This PR also introduces built-in tasks for Rust and Elixir. Note that
this is not a precedent for future PRs to include tasks for more
languages; we simply want to find the rough edges with tasks & language
integrations before proceeding to task contexts provided by extensions.

As is, we'll load tasks for all loaded languages, so in order to get
Elixir tasks, you have to open an Elixir buffer first. I think it sort
of makes sense (though it's not ideal), as in the future where
extensions do provide their own tasks.json, we'd like to limit the # of
tasks surfaced to the user to make them as relevant to the project at
hand as possible.

Release Notes:

- Added built-in tasks for Rust and Elixir files.
This commit is contained in:
Piotr Osiewicz 2024-03-22 16:18:33 +01:00 committed by GitHub
parent cb4f868815
commit 4dc61f7ccd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 416 additions and 169 deletions

View file

@ -11,6 +11,10 @@ use regex::Regex;
use settings::Settings;
use smol::fs::{self, File};
use std::{any::Any, borrow::Cow, env::consts, path::PathBuf, sync::Arc};
use task::{
static_source::{Definition, TaskDefinitions},
TaskVariables,
};
use util::{
async_maybe,
fs::remove_matching,
@ -319,44 +323,77 @@ impl LspAdapter for RustLspAdapter {
pub(crate) struct RustContextProvider;
impl LanguageContextProvider for RustContextProvider {
impl ContextProvider for RustContextProvider {
fn build_context(
&self,
location: Location,
cx: &mut gpui::AppContext,
) -> Result<LanguageContext> {
let mut context = DefaultContextProvider.build_context(location.clone(), cx)?;
if context.package.is_none() {
if let Some(path) = location.buffer.read(cx).file().and_then(|file| {
let local_file = file.as_local()?.abs_path(cx);
local_file.parent().map(PathBuf::from)
}) {
// src/
// main.rs
// lib.rs
// foo/
// bar/
// baz.rs <|>
// /bin/
// bin_1.rs
//
let Some(pkgid) = std::process::Command::new("cargo")
.current_dir(path)
.arg("pkgid")
.output()
.log_err()
else {
return Ok(context);
};
let package_name = String::from_utf8(pkgid.stdout)
.map(|name| name.trim().to_owned())
.ok();
) -> Result<TaskVariables> {
let mut context = SymbolContextProvider.build_context(location.clone(), cx)?;
context.package = package_name;
if let Some(path) = location.buffer.read(cx).file().and_then(|file| {
let local_file = file.as_local()?.abs_path(cx);
local_file.parent().map(PathBuf::from)
}) {
let Some(pkgid) = std::process::Command::new("cargo")
.current_dir(path)
.arg("pkgid")
.output()
.log_err()
else {
return Ok(context);
};
let package_name = String::from_utf8(pkgid.stdout)
.map(|name| name.trim().to_owned())
.ok();
if let Some(package_name) = package_name {
context.0.insert("ZED_PACKAGE".to_owned(), package_name);
}
}
Ok(context)
}
fn associated_tasks(&self) -> Option<TaskDefinitions> {
Some(TaskDefinitions(vec![
Definition {
label: "Rust: Test current crate".to_owned(),
command: "cargo".into(),
args: vec!["test".into(), "-p".into(), "$ZED_PACKAGE".into()],
..Default::default()
},
Definition {
label: "Rust: Test current function".to_owned(),
command: "cargo".into(),
args: vec![
"test".into(),
"-p".into(),
"$ZED_PACKAGE".into(),
"--".into(),
"$ZED_SYMBOL".into(),
],
..Default::default()
},
Definition {
label: "Rust: cargo run".into(),
command: "cargo".into(),
args: vec!["run".into()],
..Default::default()
},
Definition {
label: "Rust: cargo check current crate".into(),
command: "cargo".into(),
args: vec!["check".into(), "-p".into(), "$ZED_PACKAGE".into()],
..Default::default()
},
Definition {
label: "Rust: cargo check workspace".into(),
command: "cargo".into(),
args: vec!["check".into(), "--workspace".into()],
..Default::default()
},
]))
}
}
async fn get_cached_server_binary(container_dir: PathBuf) -> Option<LanguageServerBinary> {