task: Add ZED_PACKAGE task variable in Rust files. (#9491)

This variable is experimental, as I expect it to be superseded by
whatever the extensions can provide (once we get them)

Release Notes:

- Added experimental ZED_PACKAGE task variable which contains name of
the current crate in Rust files.

---------

Co-authored-by: Kirill <kirill@zed.dev>
This commit is contained in:
Piotr Osiewicz 2024-03-18 12:18:42 +01:00 committed by GitHub
parent a69eddc081
commit 07dbee8651
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 53 additions and 2 deletions

View file

@ -317,6 +317,48 @@ impl LspAdapter for RustLspAdapter {
}
}
pub(crate) struct RustContextProvider;
impl LanguageContextProvider 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();
context.package = package_name;
}
}
Ok(context)
}
}
async fn get_cached_server_binary(container_dir: PathBuf) -> Option<LanguageServerBinary> {
async_maybe!({
let mut last = None;