toolchain: Respect currently focused file when querying toolchains (#28875)

Closes #21743


https://github.com/user-attachments/assets/0230f233-58a4-494c-90af-28ce82f9fc1d


Release Notes:

- Virtual environment picker now looks up virtual environment based on
parent directory of active file; this enables having multiple active
virtual environments in a single worktree.
This commit is contained in:
Piotr Osiewicz 2025-04-16 19:05:57 +02:00 committed by GitHub
parent 4f58bdee28
commit 5c2c6d7e5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 77 additions and 21 deletions

View file

@ -3094,6 +3094,9 @@ impl Project {
.map(|lister| lister.term()) .map(|lister| lister.term())
} }
pub fn toolchain_store(&self) -> Option<Entity<ToolchainStore>> {
self.toolchain_store.clone()
}
pub fn activate_toolchain( pub fn activate_toolchain(
&self, &self,
path: ProjectPath, path: ProjectPath,

View file

@ -55,6 +55,7 @@ impl ToolchainStore {
}); });
Self(ToolchainStoreInner::Local(entity, subscription)) Self(ToolchainStoreInner::Local(entity, subscription))
} }
pub(super) fn remote(project_id: u64, client: AnyProtoClient, cx: &mut App) -> Self { pub(super) fn remote(project_id: u64, client: AnyProtoClient, cx: &mut App) -> Self {
Self(ToolchainStoreInner::Remote( Self(ToolchainStoreInner::Remote(
cx.new(|_| RemoteToolchainStore { client, project_id }), cx.new(|_| RemoteToolchainStore { client, project_id }),
@ -285,7 +286,7 @@ struct LocalStore(WeakEntity<LocalToolchainStore>);
struct RemoteStore(WeakEntity<RemoteToolchainStore>); struct RemoteStore(WeakEntity<RemoteToolchainStore>);
#[derive(Clone)] #[derive(Clone)]
pub(crate) enum ToolchainStoreEvent { pub enum ToolchainStoreEvent {
ToolchainActivated, ToolchainActivated,
} }

View file

@ -302,7 +302,7 @@ impl TitleBar {
cx.notify() cx.notify()
}), }),
); );
subscriptions.push(cx.subscribe(&project, |_, _, _, cx| cx.notify())); subscriptions.push(cx.subscribe(&project, |_, _, _: &project::Event, cx| cx.notify()));
subscriptions.push(cx.observe(&active_call, |this, _, cx| this.active_call_changed(cx))); subscriptions.push(cx.observe(&active_call, |this, _, cx| this.active_call_changed(cx)));
subscriptions.push(cx.observe_window_activation(window, Self::window_activation_changed)); subscriptions.push(cx.observe_window_activation(window, Self::window_activation_changed));
subscriptions.push(cx.observe(&user_store, |_, _, cx| cx.notify())); subscriptions.push(cx.observe(&user_store, |_, _, cx| cx.notify()));

View file

@ -1,4 +1,4 @@
use std::sync::Arc; use std::{path::Path, sync::Arc};
use editor::Editor; use editor::Editor;
use gpui::{ use gpui::{
@ -6,7 +6,7 @@ use gpui::{
WeakEntity, Window, div, WeakEntity, Window, div,
}; };
use language::{Buffer, BufferEvent, LanguageName, Toolchain}; use language::{Buffer, BufferEvent, LanguageName, Toolchain};
use project::{Project, ProjectPath, WorktreeId}; use project::{Project, ProjectPath, WorktreeId, toolchain_store::ToolchainStoreEvent};
use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, SharedString, Tooltip}; use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, SharedString, Tooltip};
use workspace::{StatusItemView, Workspace, item::ItemHandle}; use workspace::{StatusItemView, Workspace, item::ItemHandle};
@ -22,6 +22,28 @@ pub struct ActiveToolchain {
impl ActiveToolchain { impl ActiveToolchain {
pub fn new(workspace: &Workspace, window: &mut Window, cx: &mut Context<Self>) -> Self { pub fn new(workspace: &Workspace, window: &mut Window, cx: &mut Context<Self>) -> Self {
if let Some(store) = workspace.project().read(cx).toolchain_store() {
cx.subscribe_in(
&store,
window,
|this, _, _: &ToolchainStoreEvent, window, cx| {
let editor = this
.workspace
.update(cx, |workspace, cx| {
workspace
.active_item(cx)
.and_then(|item| item.downcast::<Editor>())
})
.ok()
.flatten();
if let Some(editor) = editor {
this.active_toolchain.take();
this.update_lister(editor, window, cx);
}
},
)
.detach();
}
Self { Self {
active_toolchain: None, active_toolchain: None,
active_buffer: None, active_buffer: None,
@ -57,12 +79,19 @@ impl ActiveToolchain {
this.term = term; this.term = term;
cx.notify(); cx.notify();
}); });
let worktree_id = active_file let (worktree_id, path) = active_file
.update(cx, |this, cx| Some(this.file()?.worktree_id(cx))) .update(cx, |this, cx| {
this.file().and_then(|file| {
Some((
file.worktree_id(cx),
Arc::<Path>::from(file.path().parent()?),
))
})
})
.ok() .ok()
.flatten()?; .flatten()?;
let toolchain = let toolchain =
Self::active_toolchain(workspace, worktree_id, language_name, cx).await?; Self::active_toolchain(workspace, worktree_id, path, language_name, cx).await?;
let _ = this.update(cx, |this, cx| { let _ = this.update(cx, |this, cx| {
this.active_toolchain = Some(toolchain); this.active_toolchain = Some(toolchain);
@ -101,6 +130,7 @@ impl ActiveToolchain {
fn active_toolchain( fn active_toolchain(
workspace: WeakEntity<Workspace>, workspace: WeakEntity<Workspace>,
worktree_id: WorktreeId, worktree_id: WorktreeId,
relative_path: Arc<Path>,
language_name: LanguageName, language_name: LanguageName,
cx: &mut AsyncWindowContext, cx: &mut AsyncWindowContext,
) -> Task<Option<Toolchain>> { ) -> Task<Option<Toolchain>> {
@ -114,7 +144,7 @@ impl ActiveToolchain {
this.project().read(cx).active_toolchain( this.project().read(cx).active_toolchain(
ProjectPath { ProjectPath {
worktree_id, worktree_id,
path: Arc::from("".as_ref()), path: relative_path.clone(),
}, },
language_name.clone(), language_name.clone(),
cx, cx,
@ -133,7 +163,7 @@ impl ActiveToolchain {
project.read(cx).available_toolchains( project.read(cx).available_toolchains(
ProjectPath { ProjectPath {
worktree_id, worktree_id,
path: Arc::from("".as_ref()), path: relative_path.clone(),
}, },
language_name, language_name,
cx, cx,
@ -144,7 +174,12 @@ impl ActiveToolchain {
if let Some(toolchain) = toolchains.toolchains.first() { if let Some(toolchain) = toolchains.toolchains.first() {
// Since we don't have a selected toolchain, pick one for user here. // Since we don't have a selected toolchain, pick one for user here.
workspace::WORKSPACE_DB workspace::WORKSPACE_DB
.set_toolchain(workspace_id, worktree_id, "".to_owned(), toolchain.clone()) .set_toolchain(
workspace_id,
worktree_id,
relative_path.to_string_lossy().into_owned(),
toolchain.clone(),
)
.await .await
.ok()?; .ok()?;
project project
@ -152,7 +187,7 @@ impl ActiveToolchain {
this.activate_toolchain( this.activate_toolchain(
ProjectPath { ProjectPath {
worktree_id, worktree_id,
path: Arc::from("".as_ref()), path: relative_path,
}, },
toolchain.clone(), toolchain.clone(),
cx, cx,

View file

@ -50,6 +50,7 @@ impl ToolchainSelector {
let language_name = buffer.read(cx).language()?.name(); let language_name = buffer.read(cx).language()?.name();
let worktree_id = buffer.read(cx).file()?.worktree_id(cx); let worktree_id = buffer.read(cx).file()?.worktree_id(cx);
let relative_path: Arc<Path> = Arc::from(buffer.read(cx).file()?.path().parent()?);
let worktree_root_path = project let worktree_root_path = project
.read(cx) .read(cx)
.worktree_for_id(worktree_id, cx)? .worktree_for_id(worktree_id, cx)?
@ -58,8 +59,9 @@ impl ToolchainSelector {
let workspace_id = workspace.database_id()?; let workspace_id = workspace.database_id()?;
let weak = workspace.weak_handle(); let weak = workspace.weak_handle();
cx.spawn_in(window, async move |workspace, cx| { cx.spawn_in(window, async move |workspace, cx| {
let as_str = relative_path.to_string_lossy().into_owned();
let active_toolchain = workspace::WORKSPACE_DB let active_toolchain = workspace::WORKSPACE_DB
.toolchain(workspace_id, worktree_id, language_name.clone()) .toolchain(workspace_id, worktree_id, as_str, language_name.clone())
.await .await
.ok() .ok()
.flatten(); .flatten();
@ -72,6 +74,7 @@ impl ToolchainSelector {
active_toolchain, active_toolchain,
worktree_id, worktree_id,
worktree_root_path, worktree_root_path,
relative_path,
language_name, language_name,
window, window,
cx, cx,
@ -91,6 +94,7 @@ impl ToolchainSelector {
active_toolchain: Option<Toolchain>, active_toolchain: Option<Toolchain>,
worktree_id: WorktreeId, worktree_id: WorktreeId,
worktree_root: Arc<Path>, worktree_root: Arc<Path>,
relative_path: Arc<Path>,
language_name: LanguageName, language_name: LanguageName,
window: &mut Window, window: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
@ -104,6 +108,7 @@ impl ToolchainSelector {
worktree_id, worktree_id,
worktree_root, worktree_root,
project, project,
relative_path,
language_name, language_name,
window, window,
cx, cx,
@ -137,6 +142,7 @@ pub struct ToolchainSelectorDelegate {
workspace: WeakEntity<Workspace>, workspace: WeakEntity<Workspace>,
worktree_id: WorktreeId, worktree_id: WorktreeId,
worktree_abs_path_root: Arc<Path>, worktree_abs_path_root: Arc<Path>,
relative_path: Arc<Path>,
placeholder_text: Arc<str>, placeholder_text: Arc<str>,
_fetch_candidates_task: Task<Option<()>>, _fetch_candidates_task: Task<Option<()>>,
} }
@ -149,6 +155,7 @@ impl ToolchainSelectorDelegate {
worktree_id: WorktreeId, worktree_id: WorktreeId,
worktree_abs_path_root: Arc<Path>, worktree_abs_path_root: Arc<Path>,
project: Entity<Project>, project: Entity<Project>,
relative_path: Arc<Path>,
language_name: LanguageName, language_name: LanguageName,
window: &mut Window, window: &mut Window,
cx: &mut Context<Picker<Self>>, cx: &mut Context<Picker<Self>>,
@ -162,17 +169,26 @@ impl ToolchainSelectorDelegate {
}) })
.ok()? .ok()?
.await?; .await?;
let placeholder_text = format!("Select a {}", term.to_lowercase()).into(); let relative_path = this
.update(cx, |this, _| this.delegate.relative_path.clone())
.ok()?;
let placeholder_text = format!(
"Select a {} for `{}`…",
term.to_lowercase(),
relative_path.to_string_lossy()
)
.into();
let _ = this.update_in(cx, move |this, window, cx| { let _ = this.update_in(cx, move |this, window, cx| {
this.delegate.placeholder_text = placeholder_text; this.delegate.placeholder_text = placeholder_text;
this.refresh_placeholder(window, cx); this.refresh_placeholder(window, cx);
}); });
let available_toolchains = project let available_toolchains = project
.update(cx, |this, cx| { .update(cx, |this, cx| {
this.available_toolchains( this.available_toolchains(
ProjectPath { ProjectPath {
worktree_id, worktree_id,
path: Arc::from("".as_ref()), path: relative_path.clone(),
}, },
language_name, language_name,
cx, cx,
@ -211,6 +227,7 @@ impl ToolchainSelectorDelegate {
worktree_id, worktree_id,
worktree_abs_path_root, worktree_abs_path_root,
placeholder_text, placeholder_text,
relative_path,
_fetch_candidates_task, _fetch_candidates_task,
} }
} }
@ -246,19 +263,18 @@ impl PickerDelegate for ToolchainSelectorDelegate {
{ {
let workspace = self.workspace.clone(); let workspace = self.workspace.clone();
let worktree_id = self.worktree_id; let worktree_id = self.worktree_id;
let path = self.relative_path.clone();
let relative_path = self.relative_path.to_string_lossy().into_owned();
cx.spawn_in(window, async move |_, cx| { cx.spawn_in(window, async move |_, cx| {
workspace::WORKSPACE_DB workspace::WORKSPACE_DB
.set_toolchain(workspace_id, worktree_id, "".to_owned(), toolchain.clone()) .set_toolchain(workspace_id, worktree_id, relative_path, toolchain.clone())
.await .await
.log_err(); .log_err();
workspace workspace
.update(cx, |this, cx| { .update(cx, |this, cx| {
this.project().update(cx, |this, cx| { this.project().update(cx, |this, cx| {
this.activate_toolchain( this.activate_toolchain(
ProjectPath { ProjectPath { worktree_id, path },
worktree_id,
path: Arc::from("".as_ref()),
},
toolchain, toolchain,
cx, cx,
) )

View file

@ -1334,17 +1334,18 @@ impl WorkspaceDb {
&self, &self,
workspace_id: WorkspaceId, workspace_id: WorkspaceId,
worktree_id: WorktreeId, worktree_id: WorktreeId,
relative_path: String,
language_name: LanguageName, language_name: LanguageName,
) -> Result<Option<Toolchain>> { ) -> Result<Option<Toolchain>> {
self.write(move |this| { self.write(move |this| {
let mut select = this let mut select = this
.select_bound(sql!( .select_bound(sql!(
SELECT name, path, raw_json FROM toolchains WHERE workspace_id = ? AND language_name = ? AND worktree_id = ? SELECT name, path, raw_json FROM toolchains WHERE workspace_id = ? AND language_name = ? AND worktree_id = ? AND relative_path = ?
)) ))
.context("Preparing insertion")?; .context("Preparing insertion")?;
let toolchain: Vec<(String, String, String)> = let toolchain: Vec<(String, String, String)> =
select((workspace_id, language_name.as_ref().to_string(), worktree_id.to_usize()))?; select((workspace_id, language_name.as_ref().to_string(), worktree_id.to_usize(), relative_path))?;
Ok(toolchain.into_iter().next().and_then(|(name, path, raw_json)| Some(Toolchain { Ok(toolchain.into_iter().next().and_then(|(name, path, raw_json)| Some(Toolchain {
name: name.into(), name: name.into(),