toolchains: Use language-specific terms in UI (#20985)

Closes #ISSUE

Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2024-11-21 15:57:22 +01:00 committed by GitHub
parent 75c545aa1e
commit 0b373d43dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 76 additions and 9 deletions

View file

@ -4,14 +4,15 @@ use gpui::{
ViewContext, WeakModel, WeakView,
};
use language::{Buffer, BufferEvent, LanguageName, Toolchain};
use project::WorktreeId;
use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, Tooltip};
use project::{Project, WorktreeId};
use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, SharedString, Tooltip};
use workspace::{item::ItemHandle, StatusItemView, Workspace};
use crate::ToolchainSelector;
pub struct ActiveToolchain {
active_toolchain: Option<Toolchain>,
term: SharedString,
workspace: WeakView<Workspace>,
active_buffer: Option<(WorktreeId, WeakModel<Buffer>, Subscription)>,
_update_toolchain_task: Task<Option<()>>,
@ -22,6 +23,7 @@ impl ActiveToolchain {
Self {
active_toolchain: None,
active_buffer: None,
term: SharedString::new_static("Toolchain"),
workspace: workspace.weak_handle(),
_update_toolchain_task: Self::spawn_tracker_task(cx),
@ -44,7 +46,17 @@ impl ActiveToolchain {
.update(&mut cx, |this, _| Some(this.language()?.name()))
.ok()
.flatten()?;
let term = workspace
.update(&mut cx, |workspace, cx| {
let languages = workspace.project().read(cx).languages();
Project::toolchain_term(languages.clone(), language_name.clone())
})
.ok()?
.await?;
let _ = this.update(&mut cx, |this, cx| {
this.term = term;
cx.notify();
});
let worktree_id = active_file
.update(&mut cx, |this, cx| Some(this.file()?.worktree_id(cx)))
.ok()
@ -133,6 +145,7 @@ impl ActiveToolchain {
impl Render for ActiveToolchain {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
div().when_some(self.active_toolchain.as_ref(), |el, active_toolchain| {
let term = self.term.clone();
el.child(
Button::new("change-toolchain", active_toolchain.name.clone())
.label_size(LabelSize::Small)
@ -143,7 +156,7 @@ impl Render for ActiveToolchain {
});
}
}))
.tooltip(|cx| Tooltip::text("Select Toolchain", cx)),
.tooltip(move |cx| Tooltip::text(format!("Select {}", &term), cx)),
)
})
}

View file

@ -126,6 +126,7 @@ pub struct ToolchainSelectorDelegate {
workspace: WeakView<Workspace>,
worktree_id: WorktreeId,
worktree_abs_path_root: Arc<Path>,
placeholder_text: Arc<str>,
_fetch_candidates_task: Task<Option<()>>,
}
@ -144,6 +145,17 @@ impl ToolchainSelectorDelegate {
let _fetch_candidates_task = cx.spawn({
let project = project.clone();
move |this, mut cx| async move {
let term = project
.update(&mut cx, |this, _| {
Project::toolchain_term(this.languages().clone(), language_name.clone())
})
.ok()?
.await?;
let placeholder_text = format!("Select a {}", term.to_lowercase()).into();
let _ = this.update(&mut cx, move |this, cx| {
this.delegate.placeholder_text = placeholder_text;
this.refresh_placeholder(cx);
});
let available_toolchains = project
.update(&mut cx, |this, cx| {
this.available_toolchains(worktree_id, language_name, cx)
@ -153,6 +165,7 @@ impl ToolchainSelectorDelegate {
let _ = this.update(&mut cx, move |this, cx| {
this.delegate.candidates = available_toolchains;
if let Some(active_toolchain) = active_toolchain {
if let Some(position) = this
.delegate
@ -170,7 +183,7 @@ impl ToolchainSelectorDelegate {
Some(())
}
});
let placeholder_text = "Select a toolchain…".to_string().into();
Self {
toolchain_selector: language_selector,
candidates: Default::default(),
@ -179,6 +192,7 @@ impl ToolchainSelectorDelegate {
workspace,
worktree_id,
worktree_abs_path_root,
placeholder_text,
_fetch_candidates_task,
}
}
@ -196,7 +210,7 @@ impl PickerDelegate for ToolchainSelectorDelegate {
type ListItem = ListItem;
fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
"Select a toolchain...".into()
self.placeholder_text.clone()
}
fn match_count(&self) -> usize {