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, ManifestName};
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 subroot_relative_path: Option<Arc<Path>>,
48 project_env: Option<HashMap<String, String>>,
49 ) -> ToolchainList;
50 // Returns a term which we should use in UI to refer to a toolchain.
51 fn term(&self) -> SharedString;
52 /// Returns the name of the manifest file for this toolchain.
53 fn manifest_name(&self) -> ManifestName;
54}
55
56#[async_trait(?Send)]
57pub trait LanguageToolchainStore: Send + Sync + 'static {
58 async fn active_toolchain(
59 self: Arc<Self>,
60 worktree_id: WorktreeId,
61 relative_path: Arc<Path>,
62 language_name: LanguageName,
63 cx: &mut AsyncApp,
64 ) -> Option<Toolchain>;
65}
66
67type DefaultIndex = usize;
68#[derive(Default, Clone)]
69pub struct ToolchainList {
70 pub toolchains: Vec<Toolchain>,
71 pub default: Option<DefaultIndex>,
72 pub groups: Box<[(usize, SharedString)]>,
73}
74
75impl ToolchainList {
76 pub fn toolchains(&self) -> &[Toolchain] {
77 &self.toolchains
78 }
79 pub fn default_toolchain(&self) -> Option<Toolchain> {
80 self.default.and_then(|ix| self.toolchains.get(ix)).cloned()
81 }
82 pub fn group_for_index(&self, index: usize) -> Option<(usize, SharedString)> {
83 if index >= self.toolchains.len() {
84 return None;
85 }
86 let first_equal_or_greater = self
87 .groups
88 .partition_point(|(group_lower_bound, _)| group_lower_bound <= &index);
89 self.groups
90 .get(first_equal_or_greater.checked_sub(1)?)
91 .cloned()
92 }
93}