1//! Provides support for language toolchains.
2//!
3//! A language can have associated toolchains,
4//! which is a set of tools used to interact with the projects written in said language.
5//! For example, a Python project can have an associated virtual environment; a Rust project can have a toolchain override.
6
7use std::{
8 path::{Path, PathBuf},
9 sync::Arc,
10};
11
12use async_trait::async_trait;
13use collections::HashMap;
14use gpui::{AsyncApp, SharedString};
15use settings::WorktreeId;
16
17use crate::LanguageName;
18
19/// Represents a single toolchain.
20#[derive(Clone, Debug)]
21pub struct Toolchain {
22 /// User-facing label
23 pub name: SharedString,
24 pub path: SharedString,
25 pub language_name: LanguageName,
26 /// Full toolchain data (including language-specific details)
27 pub as_json: serde_json::Value,
28}
29
30impl PartialEq for Toolchain {
31 fn eq(&self, other: &Self) -> bool {
32 // Do not use as_json for comparisons; it shouldn't impact equality, as it's not user-surfaced.
33 // Thus, there could be multiple entries that look the same in the UI.
34 (&self.name, &self.path, &self.language_name).eq(&(
35 &other.name,
36 &other.path,
37 &other.language_name,
38 ))
39 }
40}
41
42#[async_trait]
43pub trait ToolchainLister: Send + Sync {
44 async fn list(
45 &self,
46 worktree_root: PathBuf,
47 project_env: Option<HashMap<String, String>>,
48 ) -> ToolchainList;
49 // Returns a term which we should use in UI to refer to a toolchain.
50 fn term(&self) -> SharedString;
51}
52
53#[async_trait(?Send)]
54pub trait LanguageToolchainStore: Send + Sync + 'static {
55 async fn active_toolchain(
56 self: Arc<Self>,
57 worktree_id: WorktreeId,
58 relative_path: Arc<Path>,
59 language_name: LanguageName,
60 cx: &mut AsyncApp,
61 ) -> Option<Toolchain>;
62}
63
64type DefaultIndex = usize;
65#[derive(Default, Clone)]
66pub struct ToolchainList {
67 pub toolchains: Vec<Toolchain>,
68 pub default: Option<DefaultIndex>,
69 pub groups: Box<[(usize, SharedString)]>,
70}
71
72impl ToolchainList {
73 pub fn toolchains(&self) -> &[Toolchain] {
74 &self.toolchains
75 }
76 pub fn default_toolchain(&self) -> Option<Toolchain> {
77 self.default.and_then(|ix| self.toolchains.get(ix)).cloned()
78 }
79 pub fn group_for_index(&self, index: usize) -> Option<(usize, SharedString)> {
80 if index >= self.toolchains.len() {
81 return None;
82 }
83 let first_equal_or_greater = self
84 .groups
85 .partition_point(|(group_lower_bound, _)| group_lower_bound <= &index);
86 self.groups
87 .get(first_equal_or_greater.checked_sub(1)?)
88 .cloned()
89 }
90}