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::{path::PathBuf, sync::Arc};
8
9use async_trait::async_trait;
10use collections::HashMap;
11use fs::Fs;
12use gpui::{AsyncApp, SharedString};
13use settings::WorktreeId;
14use task::ShellKind;
15use util::rel_path::RelPath;
16
17use crate::{LanguageName, ManifestName};
18
19/// Represents a single toolchain.
20#[derive(Clone, Eq, Debug)]
21pub struct Toolchain {
22 /// User-facing label
23 pub name: SharedString,
24 /// Absolute path
25 pub path: SharedString,
26 pub language_name: LanguageName,
27 /// Full toolchain data (including language-specific details)
28 pub as_json: serde_json::Value,
29}
30
31/// Declares a scope of a toolchain added by user.
32///
33/// When the user adds a toolchain, we give them an option to see that toolchain in:
34/// - All of their projects
35/// - A project they're currently in.
36/// - Only in the subproject they're currently in.
37#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
38pub enum ToolchainScope {
39 Subproject(WorktreeId, Arc<RelPath>),
40 Project,
41 /// Available in all projects on this box. It wouldn't make sense to show suggestions across machines.
42 Global,
43}
44
45impl ToolchainScope {
46 pub fn label(&self) -> &'static str {
47 match self {
48 ToolchainScope::Subproject(_, _) => "Subproject",
49 ToolchainScope::Project => "Project",
50 ToolchainScope::Global => "Global",
51 }
52 }
53
54 pub fn description(&self) -> &'static str {
55 match self {
56 ToolchainScope::Subproject(_, _) => {
57 "Available only in the subproject you're currently in."
58 }
59 ToolchainScope::Project => "Available in all locations in your current project.",
60 ToolchainScope::Global => "Available in all of your projects on this machine.",
61 }
62 }
63}
64
65impl std::hash::Hash for Toolchain {
66 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
67 let Self {
68 name,
69 path,
70 language_name,
71 as_json: _,
72 } = self;
73 name.hash(state);
74 path.hash(state);
75 language_name.hash(state);
76 }
77}
78
79impl PartialEq for Toolchain {
80 fn eq(&self, other: &Self) -> bool {
81 let Self {
82 name,
83 path,
84 language_name,
85 as_json: _,
86 } = self;
87 // Do not use as_json for comparisons; it shouldn't impact equality, as it's not user-surfaced.
88 // Thus, there could be multiple entries that look the same in the UI.
89 (name, path, language_name).eq(&(&other.name, &other.path, &other.language_name))
90 }
91}
92
93#[async_trait]
94pub trait ToolchainLister: Send + Sync + 'static {
95 /// List all available toolchains for a given path.
96 async fn list(
97 &self,
98 worktree_root: PathBuf,
99 subroot_relative_path: Arc<RelPath>,
100 project_env: Option<HashMap<String, String>>,
101 fs: &dyn Fs,
102 ) -> ToolchainList;
103
104 /// Given a user-created toolchain, resolve lister-specific details.
105 /// Put another way: fill in the details of the toolchain so the user does not have to.
106 async fn resolve(
107 &self,
108 path: PathBuf,
109 project_env: Option<HashMap<String, String>>,
110 fs: &dyn Fs,
111 ) -> anyhow::Result<Toolchain>;
112
113 fn activation_script(&self, toolchain: &Toolchain, shell: ShellKind) -> Vec<String>;
114
115 /// Returns various "static" bits of information about this toolchain lister. This function should be pure.
116 fn meta(&self) -> ToolchainMetadata;
117}
118
119#[derive(Clone, PartialEq, Eq, Hash)]
120pub struct ToolchainMetadata {
121 /// Returns a term which we should use in UI to refer to toolchains produced by a given `[ToolchainLister]`.
122 pub term: SharedString,
123 /// A user-facing placeholder describing the semantic meaning of a path to a new toolchain.
124 pub new_toolchain_placeholder: SharedString,
125 /// The name of the manifest file for this toolchain.
126 pub manifest_name: ManifestName,
127}
128
129#[async_trait(?Send)]
130pub trait LanguageToolchainStore: Send + Sync + 'static {
131 async fn active_toolchain(
132 self: Arc<Self>,
133 worktree_id: WorktreeId,
134 relative_path: Arc<RelPath>,
135 language_name: LanguageName,
136 cx: &mut AsyncApp,
137 ) -> Option<Toolchain>;
138}
139
140pub trait LocalLanguageToolchainStore: Send + Sync + 'static {
141 fn active_toolchain(
142 self: Arc<Self>,
143 worktree_id: WorktreeId,
144 relative_path: &Arc<RelPath>,
145 language_name: LanguageName,
146 cx: &mut AsyncApp,
147 ) -> Option<Toolchain>;
148}
149
150#[async_trait(?Send)]
151impl<T: LocalLanguageToolchainStore> LanguageToolchainStore for T {
152 async fn active_toolchain(
153 self: Arc<Self>,
154 worktree_id: WorktreeId,
155 relative_path: Arc<RelPath>,
156 language_name: LanguageName,
157 cx: &mut AsyncApp,
158 ) -> Option<Toolchain> {
159 self.active_toolchain(worktree_id, &relative_path, language_name, cx)
160 }
161}
162
163type DefaultIndex = usize;
164#[derive(Default, Clone, Debug)]
165pub struct ToolchainList {
166 pub toolchains: Vec<Toolchain>,
167 pub default: Option<DefaultIndex>,
168 pub groups: Box<[(usize, SharedString)]>,
169}
170
171impl ToolchainList {
172 pub fn toolchains(&self) -> &[Toolchain] {
173 &self.toolchains
174 }
175 pub fn default_toolchain(&self) -> Option<Toolchain> {
176 self.default.and_then(|ix| self.toolchains.get(ix)).cloned()
177 }
178 pub fn group_for_index(&self, index: usize) -> Option<(usize, SharedString)> {
179 if index >= self.toolchains.len() {
180 return None;
181 }
182 let first_equal_or_greater = self
183 .groups
184 .partition_point(|(group_lower_bound, _)| group_lower_bound <= &index);
185 self.groups
186 .get(first_equal_or_greater.checked_sub(1)?)
187 .cloned()
188 }
189}