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::{path::PathBuf, sync::Arc};
 8
 9use async_trait::async_trait;
10use collections::HashMap;
11use gpui::{AsyncApp, SharedString};
12use settings::WorktreeId;
13
14use crate::LanguageName;
15
16/// Represents a single toolchain.
17#[derive(Clone, Debug)]
18pub struct Toolchain {
19    /// User-facing label
20    pub name: SharedString,
21    pub path: SharedString,
22    pub language_name: LanguageName,
23    /// Full toolchain data (including language-specific details)
24    pub as_json: serde_json::Value,
25}
26
27impl PartialEq for Toolchain {
28    fn eq(&self, other: &Self) -> bool {
29        // Do not use as_json for comparisons; it shouldn't impact equality, as it's not user-surfaced.
30        // Thus, there could be multiple entries that look the same in the UI.
31        (&self.name, &self.path, &self.language_name).eq(&(
32            &other.name,
33            &other.path,
34            &other.language_name,
35        ))
36    }
37}
38
39#[async_trait]
40pub trait ToolchainLister: Send + Sync {
41    async fn list(
42        &self,
43        worktree_root: PathBuf,
44        project_env: Option<HashMap<String, String>>,
45    ) -> ToolchainList;
46    // Returns a term which we should use in UI to refer to a toolchain.
47    fn term(&self) -> SharedString;
48}
49
50#[async_trait(?Send)]
51pub trait LanguageToolchainStore {
52    async fn active_toolchain(
53        self: Arc<Self>,
54        worktree_id: WorktreeId,
55        language_name: LanguageName,
56        cx: &mut AsyncApp,
57    ) -> Option<Toolchain>;
58}
59
60type DefaultIndex = usize;
61#[derive(Default, Clone)]
62pub struct ToolchainList {
63    pub toolchains: Vec<Toolchain>,
64    pub default: Option<DefaultIndex>,
65    pub groups: Box<[(usize, SharedString)]>,
66}
67
68impl ToolchainList {
69    pub fn toolchains(&self) -> &[Toolchain] {
70        &self.toolchains
71    }
72    pub fn default_toolchain(&self) -> Option<Toolchain> {
73        self.default.and_then(|ix| self.toolchains.get(ix)).cloned()
74    }
75    pub fn group_for_index(&self, index: usize) -> Option<(usize, SharedString)> {
76        if index >= self.toolchains.len() {
77            return None;
78        }
79        let first_equal_or_greater = self
80            .groups
81            .partition_point(|(group_lower_bound, _)| group_lower_bound <= &index);
82        self.groups
83            .get(first_equal_or_greater.checked_sub(1)?)
84            .cloned()
85    }
86}