extension_settings.rs

 1use collections::{HashMap, HashSet};
 2use extension::{
 3    DownloadFileCapability, ExtensionCapability, NpmInstallPackageCapability, ProcessExecCapability,
 4};
 5use settings::{RegisterSetting, Settings};
 6use std::sync::Arc;
 7
 8#[derive(Debug, Default, Clone, RegisterSetting)]
 9pub struct ExtensionSettings {
10    /// The extensions that should be automatically installed by Zed.
11    ///
12    /// This is used to make functionality provided by extensions (e.g., language support)
13    /// available out-of-the-box.
14    ///
15    /// Default: { "html": true }
16    pub auto_install_extensions: HashMap<Arc<str>, bool>,
17    pub auto_update_extensions: HashMap<Arc<str>, bool>,
18    pub granted_capabilities: Vec<ExtensionCapability>,
19    /// The extension language model providers that are allowed to read API keys
20    /// from environment variables. Each entry is in the format
21    /// "extension_id:provider_id:ENV_VAR_NAME".
22    pub allowed_env_var_providers: HashSet<Arc<str>>,
23}
24
25impl ExtensionSettings {
26    /// Returns whether the given extension should be auto-installed.
27    pub fn should_auto_install(&self, extension_id: &str) -> bool {
28        self.auto_install_extensions
29            .get(extension_id)
30            .copied()
31            .unwrap_or(true)
32    }
33
34    pub fn should_auto_update(&self, extension_id: &str) -> bool {
35        self.auto_update_extensions
36            .get(extension_id)
37            .copied()
38            .unwrap_or(true)
39    }
40}
41
42impl Settings for ExtensionSettings {
43    fn from_settings(content: &settings::SettingsContent) -> Self {
44        Self {
45            auto_install_extensions: content.extension.auto_install_extensions.clone(),
46            auto_update_extensions: content.extension.auto_update_extensions.clone(),
47            granted_capabilities: content
48                .extension
49                .granted_extension_capabilities
50                .clone()
51                .unwrap_or_default()
52                .into_iter()
53                .map(|capability| match capability {
54                    settings::ExtensionCapabilityContent::ProcessExec { command, args } => {
55                        ExtensionCapability::ProcessExec(ProcessExecCapability { command, args })
56                    }
57                    settings::ExtensionCapabilityContent::DownloadFile { host, path } => {
58                        ExtensionCapability::DownloadFile(DownloadFileCapability { host, path })
59                    }
60                    settings::ExtensionCapabilityContent::NpmInstallPackage { package } => {
61                        ExtensionCapability::NpmInstallPackage(NpmInstallPackageCapability {
62                            package,
63                        })
64                    }
65                })
66                .collect(),
67            allowed_env_var_providers: content
68                .extension
69                .allowed_env_var_providers
70                .clone()
71                .unwrap_or_default()
72                .into_iter()
73                .collect(),
74        }
75    }
76}