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::{AsyncAppContext, SharedString};
12use settings::WorktreeId;
13
14use crate::LanguageName;
15
16/// Represents a single toolchain.
17#[derive(Clone, Debug, PartialEq)]
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
27#[async_trait]
28pub trait ToolchainLister: Send + Sync {
29    async fn list(
30        &self,
31        worktree_root: PathBuf,
32        project_env: Option<HashMap<String, String>>,
33    ) -> ToolchainList;
34    // Returns a term which we should use in UI to refer to a toolchain.
35    fn term(&self) -> SharedString;
36}
37
38#[async_trait(?Send)]
39pub trait LanguageToolchainStore {
40    async fn active_toolchain(
41        self: Arc<Self>,
42        worktree_id: WorktreeId,
43        language_name: LanguageName,
44        cx: &mut AsyncAppContext,
45    ) -> Option<Toolchain>;
46}
47
48type DefaultIndex = usize;
49#[derive(Default, Clone)]
50pub struct ToolchainList {
51    pub toolchains: Vec<Toolchain>,
52    pub default: Option<DefaultIndex>,
53    pub groups: Box<[(usize, SharedString)]>,
54}
55
56impl ToolchainList {
57    pub fn toolchains(&self) -> &[Toolchain] {
58        &self.toolchains
59    }
60    pub fn default_toolchain(&self) -> Option<Toolchain> {
61        self.default.and_then(|ix| self.toolchains.get(ix)).cloned()
62    }
63    pub fn group_for_index(&self, index: usize) -> Option<(usize, SharedString)> {
64        if index >= self.toolchains.len() {
65            return None;
66        }
67        let first_equal_or_greater = self
68            .groups
69            .partition_point(|(group_lower_bound, _)| group_lower_bound <= &index);
70        self.groups
71            .get(first_equal_or_greater.checked_sub(1)?)
72            .cloned()
73    }
74}