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 gpui::{AsyncApp, SharedString};
 16use settings::WorktreeId;
 17use task::ShellKind;
 18
 19use crate::{LanguageName, ManifestName};
 20
 21/// Represents a single toolchain.
 22#[derive(Clone, Eq, Debug)]
 23pub struct Toolchain {
 24    /// User-facing label
 25    pub name: SharedString,
 26    pub path: SharedString,
 27    pub language_name: LanguageName,
 28    /// Full toolchain data (including language-specific details)
 29    pub as_json: serde_json::Value,
 30}
 31
 32impl std::hash::Hash for Toolchain {
 33    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
 34        let Self {
 35            name,
 36            path,
 37            language_name,
 38            as_json: _,
 39        } = self;
 40        name.hash(state);
 41        path.hash(state);
 42        language_name.hash(state);
 43    }
 44}
 45
 46impl PartialEq for Toolchain {
 47    fn eq(&self, other: &Self) -> bool {
 48        let Self {
 49            name,
 50            path,
 51            language_name,
 52            as_json: _,
 53        } = self;
 54        // Do not use as_json for comparisons; it shouldn't impact equality, as it's not user-surfaced.
 55        // Thus, there could be multiple entries that look the same in the UI.
 56        (name, path, language_name).eq(&(&other.name, &other.path, &other.language_name))
 57    }
 58}
 59
 60#[async_trait]
 61pub trait ToolchainLister: Send + Sync {
 62    async fn list(
 63        &self,
 64        worktree_root: PathBuf,
 65        subroot_relative_path: Arc<Path>,
 66        project_env: Option<HashMap<String, String>>,
 67    ) -> ToolchainList;
 68    // Returns a term which we should use in UI to refer to a toolchain.
 69    fn term(&self) -> SharedString;
 70    /// Returns the name of the manifest file for this toolchain.
 71    fn manifest_name(&self) -> ManifestName;
 72    async fn activation_script(
 73        &self,
 74        toolchain: &Toolchain,
 75        shell: ShellKind,
 76        fs: &dyn Fs,
 77    ) -> Vec<String>;
 78}
 79
 80#[async_trait(?Send)]
 81pub trait LanguageToolchainStore: Send + Sync + 'static {
 82    async fn active_toolchain(
 83        self: Arc<Self>,
 84        worktree_id: WorktreeId,
 85        relative_path: Arc<Path>,
 86        language_name: LanguageName,
 87        cx: &mut AsyncApp,
 88    ) -> Option<Toolchain>;
 89}
 90
 91pub trait LocalLanguageToolchainStore: Send + Sync + 'static {
 92    fn active_toolchain(
 93        self: Arc<Self>,
 94        worktree_id: WorktreeId,
 95        relative_path: &Arc<Path>,
 96        language_name: LanguageName,
 97        cx: &mut AsyncApp,
 98    ) -> Option<Toolchain>;
 99}
100
101#[async_trait(?Send)]
102impl<T: LocalLanguageToolchainStore> LanguageToolchainStore for T {
103    async fn active_toolchain(
104        self: Arc<Self>,
105        worktree_id: WorktreeId,
106        relative_path: Arc<Path>,
107        language_name: LanguageName,
108        cx: &mut AsyncApp,
109    ) -> Option<Toolchain> {
110        self.active_toolchain(worktree_id, &relative_path, language_name, cx)
111    }
112}
113
114type DefaultIndex = usize;
115#[derive(Default, Clone, Debug)]
116pub struct ToolchainList {
117    pub toolchains: Vec<Toolchain>,
118    pub default: Option<DefaultIndex>,
119    pub groups: Box<[(usize, SharedString)]>,
120}
121
122impl ToolchainList {
123    pub fn toolchains(&self) -> &[Toolchain] {
124        &self.toolchains
125    }
126    pub fn default_toolchain(&self) -> Option<Toolchain> {
127        self.default.and_then(|ix| self.toolchains.get(ix)).cloned()
128    }
129    pub fn group_for_index(&self, index: usize) -> Option<(usize, SharedString)> {
130        if index >= self.toolchains.len() {
131            return None;
132        }
133        let first_equal_or_greater = self
134            .groups
135            .partition_point(|(group_lower_bound, _)| group_lower_bound <= &index);
136        self.groups
137            .get(first_equal_or_greater.checked_sub(1)?)
138            .cloned()
139    }
140}