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