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