Add language toolchains (#19576)
This PR adds support for selecting toolchains for a given language (e.g. Rust toolchains or Python virtual environments) with support for SSH projects provided out of the box. For Python we piggy-back off of [PET](https://github.com/microsoft/python-environment-tools), a library maintained by Microsoft. Closes #16421 Closes #7646 Release Notes: - Added toolchain selector to the status bar (with initial support for Python virtual environments)
This commit is contained in:
parent
03bd95405b
commit
cdddb4d360
33 changed files with 2221 additions and 133 deletions
24
crates/toolchain_selector/Cargo.toml
Normal file
24
crates/toolchain_selector/Cargo.toml
Normal file
|
@ -0,0 +1,24 @@
|
|||
[package]
|
||||
name = "toolchain_selector"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[dependencies]
|
||||
editor.workspace = true
|
||||
fuzzy.workspace = true
|
||||
gpui.workspace = true
|
||||
language.workspace = true
|
||||
picker.workspace = true
|
||||
project.workspace = true
|
||||
ui.workspace = true
|
||||
util.workspace = true
|
||||
workspace.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
path = "src/toolchain_selector.rs"
|
||||
doctest = false
|
1
crates/toolchain_selector/LICENSE-GPL
Symbolic link
1
crates/toolchain_selector/LICENSE-GPL
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../LICENSE-GPL
|
173
crates/toolchain_selector/src/active_toolchain.rs
Normal file
173
crates/toolchain_selector/src/active_toolchain.rs
Normal file
|
@ -0,0 +1,173 @@
|
|||
use editor::Editor;
|
||||
use gpui::{
|
||||
div, AsyncWindowContext, EventEmitter, IntoElement, ParentElement, Render, Subscription, Task,
|
||||
View, ViewContext, WeakModel, WeakView,
|
||||
};
|
||||
use language::{Buffer, BufferEvent, LanguageName, Toolchain};
|
||||
use project::WorktreeId;
|
||||
use ui::{Button, ButtonCommon, Clickable, FluentBuilder, LabelSize, Tooltip};
|
||||
use workspace::{item::ItemHandle, StatusItemView, Workspace};
|
||||
|
||||
use crate::ToolchainSelector;
|
||||
|
||||
pub struct ActiveToolchain {
|
||||
active_toolchain: Option<Toolchain>,
|
||||
workspace: WeakView<Workspace>,
|
||||
active_buffer: Option<(WorktreeId, WeakModel<Buffer>, Subscription)>,
|
||||
_observe_language_changes: Subscription,
|
||||
_update_toolchain_task: Task<Option<()>>,
|
||||
}
|
||||
|
||||
struct LanguageChanged;
|
||||
|
||||
impl EventEmitter<LanguageChanged> for ActiveToolchain {}
|
||||
|
||||
impl ActiveToolchain {
|
||||
pub fn new(workspace: &Workspace, cx: &mut ViewContext<Self>) -> Self {
|
||||
let view = cx.view().clone();
|
||||
Self {
|
||||
active_toolchain: None,
|
||||
active_buffer: None,
|
||||
workspace: workspace.weak_handle(),
|
||||
_observe_language_changes: cx.subscribe(&view, |this, _, _: &LanguageChanged, cx| {
|
||||
this._update_toolchain_task = Self::spawn_tracker_task(cx);
|
||||
}),
|
||||
_update_toolchain_task: Self::spawn_tracker_task(cx),
|
||||
}
|
||||
}
|
||||
fn spawn_tracker_task(cx: &mut ViewContext<Self>) -> Task<Option<()>> {
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
let active_file = this
|
||||
.update(&mut cx, |this, _| {
|
||||
this.active_buffer
|
||||
.as_ref()
|
||||
.map(|(_, buffer, _)| buffer.clone())
|
||||
})
|
||||
.ok()
|
||||
.flatten()?;
|
||||
let workspace = this
|
||||
.update(&mut cx, |this, _| this.workspace.clone())
|
||||
.ok()?;
|
||||
|
||||
let language_name = active_file
|
||||
.update(&mut cx, |this, _| Some(this.language()?.name()))
|
||||
.ok()
|
||||
.flatten()?;
|
||||
|
||||
let worktree_id = active_file
|
||||
.update(&mut cx, |this, cx| Some(this.file()?.worktree_id(cx)))
|
||||
.ok()
|
||||
.flatten()?;
|
||||
let toolchain =
|
||||
Self::active_toolchain(workspace, worktree_id, language_name, cx.clone()).await?;
|
||||
let _ = this.update(&mut cx, |this, cx| {
|
||||
this.active_toolchain = Some(toolchain);
|
||||
|
||||
cx.notify();
|
||||
});
|
||||
Some(())
|
||||
})
|
||||
}
|
||||
|
||||
fn update_lister(&mut self, editor: View<Editor>, cx: &mut ViewContext<Self>) {
|
||||
let editor = editor.read(cx);
|
||||
if let Some((_, buffer, _)) = editor.active_excerpt(cx) {
|
||||
if let Some(worktree_id) = buffer.read(cx).file().map(|file| file.worktree_id(cx)) {
|
||||
let subscription = cx.subscribe(&buffer, |_, _, event: &BufferEvent, cx| {
|
||||
if let BufferEvent::LanguageChanged = event {
|
||||
cx.emit(LanguageChanged)
|
||||
}
|
||||
});
|
||||
self.active_buffer = Some((worktree_id, buffer.downgrade(), subscription));
|
||||
cx.emit(LanguageChanged);
|
||||
}
|
||||
}
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn active_toolchain(
|
||||
workspace: WeakView<Workspace>,
|
||||
worktree_id: WorktreeId,
|
||||
language_name: LanguageName,
|
||||
cx: AsyncWindowContext,
|
||||
) -> Task<Option<Toolchain>> {
|
||||
cx.spawn(move |mut cx| async move {
|
||||
let workspace_id = workspace
|
||||
.update(&mut cx, |this, _| this.database_id())
|
||||
.ok()
|
||||
.flatten()?;
|
||||
let selected_toolchain = workspace
|
||||
.update(&mut cx, |this, cx| {
|
||||
this.project()
|
||||
.read(cx)
|
||||
.active_toolchain(worktree_id, language_name.clone(), cx)
|
||||
})
|
||||
.ok()?
|
||||
.await;
|
||||
if let Some(toolchain) = selected_toolchain {
|
||||
Some(toolchain)
|
||||
} else {
|
||||
let project = workspace
|
||||
.update(&mut cx, |this, _| this.project().clone())
|
||||
.ok()?;
|
||||
let toolchains = cx
|
||||
.update(|cx| {
|
||||
project
|
||||
.read(cx)
|
||||
.available_toolchains(worktree_id, language_name, cx)
|
||||
})
|
||||
.ok()?
|
||||
.await?;
|
||||
if let Some(toolchain) = toolchains.toolchains.first() {
|
||||
// Since we don't have a selected toolchain, pick one for user here.
|
||||
workspace::WORKSPACE_DB
|
||||
.set_toolchain(workspace_id, worktree_id, toolchain.clone())
|
||||
.await
|
||||
.ok()?;
|
||||
project
|
||||
.update(&mut cx, |this, cx| {
|
||||
this.activate_toolchain(worktree_id, toolchain.clone(), cx)
|
||||
})
|
||||
.ok()?
|
||||
.await;
|
||||
}
|
||||
|
||||
toolchains.toolchains.first().cloned()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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| {
|
||||
el.child(
|
||||
Button::new("change-toolchain", active_toolchain.name.clone())
|
||||
.label_size(LabelSize::Small)
|
||||
.on_click(cx.listener(|this, _, cx| {
|
||||
if let Some(workspace) = this.workspace.upgrade() {
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
ToolchainSelector::toggle(workspace, cx)
|
||||
});
|
||||
}
|
||||
}))
|
||||
.tooltip(|cx| Tooltip::text("Select Toolchain", cx)),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl StatusItemView for ActiveToolchain {
|
||||
fn set_active_pane_item(
|
||||
&mut self,
|
||||
active_pane_item: Option<&dyn ItemHandle>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
if let Some(editor) = active_pane_item.and_then(|item| item.act_as::<Editor>(cx)) {
|
||||
self.active_toolchain.take();
|
||||
self.update_lister(editor, cx);
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
}
|
343
crates/toolchain_selector/src/toolchain_selector.rs
Normal file
343
crates/toolchain_selector/src/toolchain_selector.rs
Normal file
|
@ -0,0 +1,343 @@
|
|||
mod active_toolchain;
|
||||
|
||||
pub use active_toolchain::ActiveToolchain;
|
||||
use editor::Editor;
|
||||
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
|
||||
use gpui::{
|
||||
actions, AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, Model,
|
||||
ParentElement, Render, Styled, Task, View, ViewContext, VisualContext, WeakView,
|
||||
};
|
||||
use language::{LanguageName, Toolchain, ToolchainList};
|
||||
use picker::{Picker, PickerDelegate};
|
||||
use project::{Project, WorktreeId};
|
||||
use std::{path::Path, sync::Arc};
|
||||
use ui::{prelude::*, HighlightedLabel, ListItem, ListItemSpacing};
|
||||
use util::ResultExt;
|
||||
use workspace::{ModalView, Workspace};
|
||||
|
||||
actions!(toolchain, [Select]);
|
||||
|
||||
pub fn init(cx: &mut AppContext) {
|
||||
cx.observe_new_views(ToolchainSelector::register).detach();
|
||||
}
|
||||
|
||||
pub struct ToolchainSelector {
|
||||
picker: View<Picker<ToolchainSelectorDelegate>>,
|
||||
}
|
||||
|
||||
impl ToolchainSelector {
|
||||
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
|
||||
workspace.register_action(move |workspace, _: &Select, cx| {
|
||||
Self::toggle(workspace, cx);
|
||||
});
|
||||
}
|
||||
|
||||
fn toggle(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<()> {
|
||||
let (_, buffer, _) = workspace
|
||||
.active_item(cx)?
|
||||
.act_as::<Editor>(cx)?
|
||||
.read(cx)
|
||||
.active_excerpt(cx)?;
|
||||
let project = workspace.project().clone();
|
||||
|
||||
let language_name = buffer.read(cx).language()?.name();
|
||||
let worktree_id = buffer.read(cx).file()?.worktree_id(cx);
|
||||
let worktree_root_path = project
|
||||
.read(cx)
|
||||
.worktree_for_id(worktree_id, cx)?
|
||||
.read(cx)
|
||||
.abs_path();
|
||||
let workspace_id = workspace.database_id()?;
|
||||
let weak = workspace.weak_handle();
|
||||
cx.spawn(move |workspace, mut cx| async move {
|
||||
let active_toolchain = workspace::WORKSPACE_DB
|
||||
.toolchain(workspace_id, worktree_id, language_name.clone())
|
||||
.await
|
||||
.ok()
|
||||
.flatten();
|
||||
workspace
|
||||
.update(&mut cx, |this, cx| {
|
||||
this.toggle_modal(cx, move |cx| {
|
||||
ToolchainSelector::new(
|
||||
weak,
|
||||
project,
|
||||
active_toolchain,
|
||||
worktree_id,
|
||||
worktree_root_path,
|
||||
language_name,
|
||||
cx,
|
||||
)
|
||||
});
|
||||
})
|
||||
.ok();
|
||||
})
|
||||
.detach();
|
||||
|
||||
Some(())
|
||||
}
|
||||
|
||||
fn new(
|
||||
workspace: WeakView<Workspace>,
|
||||
project: Model<Project>,
|
||||
active_toolchain: Option<Toolchain>,
|
||||
worktree_id: WorktreeId,
|
||||
worktree_root: Arc<Path>,
|
||||
language_name: LanguageName,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Self {
|
||||
let view = cx.view().downgrade();
|
||||
let picker = cx.new_view(|cx| {
|
||||
let delegate = ToolchainSelectorDelegate::new(
|
||||
active_toolchain,
|
||||
view,
|
||||
workspace,
|
||||
worktree_id,
|
||||
worktree_root,
|
||||
project,
|
||||
language_name,
|
||||
cx,
|
||||
);
|
||||
Picker::uniform_list(delegate, cx)
|
||||
});
|
||||
Self { picker }
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ToolchainSelector {
|
||||
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||
v_flex().w(rems(34.)).child(self.picker.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl FocusableView for ToolchainSelector {
|
||||
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
|
||||
self.picker.focus_handle(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<DismissEvent> for ToolchainSelector {}
|
||||
impl ModalView for ToolchainSelector {}
|
||||
|
||||
pub struct ToolchainSelectorDelegate {
|
||||
toolchain_selector: WeakView<ToolchainSelector>,
|
||||
candidates: ToolchainList,
|
||||
matches: Vec<StringMatch>,
|
||||
selected_index: usize,
|
||||
workspace: WeakView<Workspace>,
|
||||
worktree_id: WorktreeId,
|
||||
worktree_abs_path_root: Arc<Path>,
|
||||
_fetch_candidates_task: Task<Option<()>>,
|
||||
}
|
||||
|
||||
impl ToolchainSelectorDelegate {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn new(
|
||||
active_toolchain: Option<Toolchain>,
|
||||
language_selector: WeakView<ToolchainSelector>,
|
||||
workspace: WeakView<Workspace>,
|
||||
worktree_id: WorktreeId,
|
||||
worktree_abs_path_root: Arc<Path>,
|
||||
project: Model<Project>,
|
||||
language_name: LanguageName,
|
||||
cx: &mut ViewContext<Picker<Self>>,
|
||||
) -> Self {
|
||||
let _fetch_candidates_task = cx.spawn({
|
||||
let project = project.clone();
|
||||
move |this, mut cx| async move {
|
||||
let available_toolchains = project
|
||||
.update(&mut cx, |this, cx| {
|
||||
this.available_toolchains(worktree_id, language_name, cx)
|
||||
})
|
||||
.ok()?
|
||||
.await?;
|
||||
|
||||
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
|
||||
.candidates
|
||||
.toolchains
|
||||
.iter()
|
||||
.position(|toolchain| *toolchain == active_toolchain)
|
||||
{
|
||||
this.delegate.set_selected_index(position, cx);
|
||||
}
|
||||
}
|
||||
this.update_matches(this.query(cx), cx);
|
||||
});
|
||||
|
||||
Some(())
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
toolchain_selector: language_selector,
|
||||
candidates: Default::default(),
|
||||
matches: vec![],
|
||||
selected_index: 0,
|
||||
workspace,
|
||||
worktree_id,
|
||||
worktree_abs_path_root,
|
||||
_fetch_candidates_task,
|
||||
}
|
||||
}
|
||||
fn relativize_path(path: SharedString, worktree_root: &Path) -> SharedString {
|
||||
Path::new(&path.as_ref())
|
||||
.strip_prefix(&worktree_root)
|
||||
.ok()
|
||||
.map(|suffix| Path::new(".").join(suffix))
|
||||
.and_then(|path| path.to_str().map(String::from).map(SharedString::from))
|
||||
.unwrap_or(path)
|
||||
}
|
||||
}
|
||||
|
||||
impl PickerDelegate for ToolchainSelectorDelegate {
|
||||
type ListItem = ListItem;
|
||||
|
||||
fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
|
||||
"Select a toolchain...".into()
|
||||
}
|
||||
|
||||
fn match_count(&self) -> usize {
|
||||
self.matches.len()
|
||||
}
|
||||
|
||||
fn confirm(&mut self, _: bool, cx: &mut ViewContext<Picker<Self>>) {
|
||||
if let Some(string_match) = self.matches.get(self.selected_index) {
|
||||
let toolchain = self.candidates.toolchains[string_match.candidate_id].clone();
|
||||
if let Some(workspace_id) = self
|
||||
.workspace
|
||||
.update(cx, |this, _| this.database_id())
|
||||
.ok()
|
||||
.flatten()
|
||||
{
|
||||
let workspace = self.workspace.clone();
|
||||
let worktree_id = self.worktree_id;
|
||||
cx.spawn(|_, mut cx| async move {
|
||||
workspace::WORKSPACE_DB
|
||||
.set_toolchain(workspace_id, worktree_id, toolchain.clone())
|
||||
.await
|
||||
.log_err();
|
||||
workspace
|
||||
.update(&mut cx, |this, cx| {
|
||||
this.project().update(cx, |this, cx| {
|
||||
this.activate_toolchain(worktree_id, toolchain, cx)
|
||||
})
|
||||
})
|
||||
.ok()?
|
||||
.await;
|
||||
Some(())
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
self.dismissed(cx);
|
||||
}
|
||||
|
||||
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
|
||||
self.toolchain_selector
|
||||
.update(cx, |_, cx| cx.emit(DismissEvent))
|
||||
.log_err();
|
||||
}
|
||||
|
||||
fn selected_index(&self) -> usize {
|
||||
self.selected_index
|
||||
}
|
||||
|
||||
fn set_selected_index(&mut self, ix: usize, _: &mut ViewContext<Picker<Self>>) {
|
||||
self.selected_index = ix;
|
||||
}
|
||||
|
||||
fn update_matches(
|
||||
&mut self,
|
||||
query: String,
|
||||
cx: &mut ViewContext<Picker<Self>>,
|
||||
) -> gpui::Task<()> {
|
||||
let background = cx.background_executor().clone();
|
||||
let candidates = self.candidates.clone();
|
||||
let worktree_root_path = self.worktree_abs_path_root.clone();
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
let matches = if query.is_empty() {
|
||||
candidates
|
||||
.toolchains
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(index, candidate)| {
|
||||
let path = Self::relativize_path(candidate.path, &worktree_root_path);
|
||||
let string = format!("{}{}", candidate.name, path);
|
||||
StringMatch {
|
||||
candidate_id: index,
|
||||
string,
|
||||
positions: Vec::new(),
|
||||
score: 0.0,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
let candidates = candidates
|
||||
.toolchains
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(candidate_id, toolchain)| {
|
||||
let path = Self::relativize_path(toolchain.path, &worktree_root_path);
|
||||
let string = format!("{}{}", toolchain.name, path);
|
||||
StringMatchCandidate::new(candidate_id, string)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
match_strings(
|
||||
&candidates,
|
||||
&query,
|
||||
false,
|
||||
100,
|
||||
&Default::default(),
|
||||
background,
|
||||
)
|
||||
.await
|
||||
};
|
||||
|
||||
this.update(&mut cx, |this, cx| {
|
||||
let delegate = &mut this.delegate;
|
||||
delegate.matches = matches;
|
||||
delegate.selected_index = delegate
|
||||
.selected_index
|
||||
.min(delegate.matches.len().saturating_sub(1));
|
||||
cx.notify();
|
||||
})
|
||||
.log_err();
|
||||
})
|
||||
}
|
||||
|
||||
fn render_match(
|
||||
&self,
|
||||
ix: usize,
|
||||
selected: bool,
|
||||
_: &mut ViewContext<Picker<Self>>,
|
||||
) -> Option<Self::ListItem> {
|
||||
let mat = &self.matches[ix];
|
||||
let toolchain = &self.candidates.toolchains[mat.candidate_id];
|
||||
|
||||
let label = toolchain.name.clone();
|
||||
let path = Self::relativize_path(toolchain.path.clone(), &self.worktree_abs_path_root);
|
||||
let (name_highlights, mut path_highlights) = mat
|
||||
.positions
|
||||
.iter()
|
||||
.cloned()
|
||||
.partition::<Vec<_>, _>(|index| *index < label.len());
|
||||
path_highlights.iter_mut().for_each(|index| {
|
||||
*index -= label.len();
|
||||
});
|
||||
Some(
|
||||
ListItem::new(ix)
|
||||
.inset(true)
|
||||
.spacing(ListItemSpacing::Sparse)
|
||||
.selected(selected)
|
||||
.child(HighlightedLabel::new(label, name_highlights))
|
||||
.child(
|
||||
HighlightedLabel::new(path, path_highlights)
|
||||
.size(LabelSize::Small)
|
||||
.color(Color::Muted),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue