Skip to content

Commit 0b373d4

Browse files
authored
toolchains: Use language-specific terms in UI (#20985)
Closes #ISSUE Release Notes: - N/A
1 parent 75c545a commit 0b373d4

File tree

6 files changed

+76
-9
lines changed

6 files changed

+76
-9
lines changed

crates/language/src/toolchain.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub trait ToolchainLister: Send + Sync {
3131
worktree_root: PathBuf,
3232
project_env: Option<HashMap<String, String>>,
3333
) -> ToolchainList;
34+
// Returns a term which we should use in UI to refer to a toolchain.
35+
fn term(&self) -> SharedString;
3436
}
3537

3638
#[async_trait(?Send)]

crates/languages/src/python.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use anyhow::ensure;
22
use anyhow::{anyhow, Result};
33
use async_trait::async_trait;
44
use collections::HashMap;
5-
use gpui::AsyncAppContext;
65
use gpui::{AppContext, Task};
6+
use gpui::{AsyncAppContext, SharedString};
77
use language::language_settings::language_settings;
88
use language::LanguageName;
99
use language::LanguageToolchainStore;
@@ -498,8 +498,17 @@ fn python_module_name_from_relative_path(relative_path: &str) -> String {
498498
.to_string()
499499
}
500500

501-
#[derive(Default)]
502-
pub(crate) struct PythonToolchainProvider {}
501+
pub(crate) struct PythonToolchainProvider {
502+
term: SharedString,
503+
}
504+
505+
impl Default for PythonToolchainProvider {
506+
fn default() -> Self {
507+
Self {
508+
term: SharedString::new_static("Virtual Environment"),
509+
}
510+
}
511+
}
503512

504513
static ENV_PRIORITY_LIST: &'static [PythonEnvironmentKind] = &[
505514
// Prioritize non-Conda environments.
@@ -604,6 +613,9 @@ impl ToolchainLister for PythonToolchainProvider {
604613
groups: Default::default(),
605614
}
606615
}
616+
fn term(&self) -> SharedString {
617+
self.term.clone()
618+
}
607619
}
608620

609621
pub struct EnvironmentApi<'a> {

crates/picker/src/picker.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,19 @@ impl<D: PickerDelegate> Picker<D> {
425425
self.cancel(&menu::Cancel, cx);
426426
}
427427

428+
pub fn refresh_placeholder(&mut self, cx: &mut WindowContext<'_>) {
429+
match &self.head {
430+
Head::Editor(view) => {
431+
let placeholder = self.delegate.placeholder_text(cx);
432+
view.update(cx, |this, cx| {
433+
this.set_placeholder_text(placeholder, cx);
434+
cx.notify();
435+
});
436+
}
437+
Head::Empty(_) => {}
438+
}
439+
}
440+
428441
pub fn refresh(&mut self, cx: &mut ViewContext<Self>) {
429442
let query = self.query(cx);
430443
self.update_matches(query, cx);

crates/project/src/project.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,6 +2464,19 @@ impl Project {
24642464
Task::ready(None)
24652465
}
24662466
}
2467+
2468+
pub async fn toolchain_term(
2469+
languages: Arc<LanguageRegistry>,
2470+
language_name: LanguageName,
2471+
) -> Option<SharedString> {
2472+
languages
2473+
.language_for_name(&language_name.0)
2474+
.await
2475+
.ok()?
2476+
.toolchain_lister()
2477+
.map(|lister| lister.term())
2478+
}
2479+
24672480
pub fn activate_toolchain(
24682481
&self,
24692482
worktree_id: WorktreeId,

crates/toolchain_selector/src/active_toolchain.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ use gpui::{
44
ViewContext, WeakModel, WeakView,
55
};
66
use language::{Buffer, BufferEvent, LanguageName, Toolchain};
7-
use project::WorktreeId;
8-
use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, Tooltip};
7+
use project::{Project, WorktreeId};
8+
use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, SharedString, Tooltip};
99
use workspace::{item::ItemHandle, StatusItemView, Workspace};
1010

1111
use crate::ToolchainSelector;
1212

1313
pub struct ActiveToolchain {
1414
active_toolchain: Option<Toolchain>,
15+
term: SharedString,
1516
workspace: WeakView<Workspace>,
1617
active_buffer: Option<(WorktreeId, WeakModel<Buffer>, Subscription)>,
1718
_update_toolchain_task: Task<Option<()>>,
@@ -22,6 +23,7 @@ impl ActiveToolchain {
2223
Self {
2324
active_toolchain: None,
2425
active_buffer: None,
26+
term: SharedString::new_static("Toolchain"),
2527
workspace: workspace.weak_handle(),
2628

2729
_update_toolchain_task: Self::spawn_tracker_task(cx),
@@ -44,7 +46,17 @@ impl ActiveToolchain {
4446
.update(&mut cx, |this, _| Some(this.language()?.name()))
4547
.ok()
4648
.flatten()?;
47-
49+
let term = workspace
50+
.update(&mut cx, |workspace, cx| {
51+
let languages = workspace.project().read(cx).languages();
52+
Project::toolchain_term(languages.clone(), language_name.clone())
53+
})
54+
.ok()?
55+
.await?;
56+
let _ = this.update(&mut cx, |this, cx| {
57+
this.term = term;
58+
cx.notify();
59+
});
4860
let worktree_id = active_file
4961
.update(&mut cx, |this, cx| Some(this.file()?.worktree_id(cx)))
5062
.ok()
@@ -133,6 +145,7 @@ impl ActiveToolchain {
133145
impl Render for ActiveToolchain {
134146
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
135147
div().when_some(self.active_toolchain.as_ref(), |el, active_toolchain| {
148+
let term = self.term.clone();
136149
el.child(
137150
Button::new("change-toolchain", active_toolchain.name.clone())
138151
.label_size(LabelSize::Small)
@@ -143,7 +156,7 @@ impl Render for ActiveToolchain {
143156
});
144157
}
145158
}))
146-
.tooltip(|cx| Tooltip::text("Select Toolchain", cx)),
159+
.tooltip(move |cx| Tooltip::text(format!("Select {}", &term), cx)),
147160
)
148161
})
149162
}

crates/toolchain_selector/src/toolchain_selector.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ pub struct ToolchainSelectorDelegate {
126126
workspace: WeakView<Workspace>,
127127
worktree_id: WorktreeId,
128128
worktree_abs_path_root: Arc<Path>,
129+
placeholder_text: Arc<str>,
129130
_fetch_candidates_task: Task<Option<()>>,
130131
}
131132

@@ -144,6 +145,17 @@ impl ToolchainSelectorDelegate {
144145
let _fetch_candidates_task = cx.spawn({
145146
let project = project.clone();
146147
move |this, mut cx| async move {
148+
let term = project
149+
.update(&mut cx, |this, _| {
150+
Project::toolchain_term(this.languages().clone(), language_name.clone())
151+
})
152+
.ok()?
153+
.await?;
154+
let placeholder_text = format!("Select a {}…", term.to_lowercase()).into();
155+
let _ = this.update(&mut cx, move |this, cx| {
156+
this.delegate.placeholder_text = placeholder_text;
157+
this.refresh_placeholder(cx);
158+
});
147159
let available_toolchains = project
148160
.update(&mut cx, |this, cx| {
149161
this.available_toolchains(worktree_id, language_name, cx)
@@ -153,6 +165,7 @@ impl ToolchainSelectorDelegate {
153165

154166
let _ = this.update(&mut cx, move |this, cx| {
155167
this.delegate.candidates = available_toolchains;
168+
156169
if let Some(active_toolchain) = active_toolchain {
157170
if let Some(position) = this
158171
.delegate
@@ -170,7 +183,7 @@ impl ToolchainSelectorDelegate {
170183
Some(())
171184
}
172185
});
173-
186+
let placeholder_text = "Select a toolchain…".to_string().into();
174187
Self {
175188
toolchain_selector: language_selector,
176189
candidates: Default::default(),
@@ -179,6 +192,7 @@ impl ToolchainSelectorDelegate {
179192
workspace,
180193
worktree_id,
181194
worktree_abs_path_root,
195+
placeholder_text,
182196
_fetch_candidates_task,
183197
}
184198
}
@@ -196,7 +210,7 @@ impl PickerDelegate for ToolchainSelectorDelegate {
196210
type ListItem = ListItem;
197211

198212
fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
199-
"Select a toolchain...".into()
213+
self.placeholder_text.clone()
200214
}
201215

202216
fn match_count(&self) -> usize {

0 commit comments

Comments
 (0)