toolchain.rs

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