Skip to content

Visual Studio Code, rebuilt in Rust

Rust
18.7 KB
Raw Blame
//! Workspace recommendations actions (extensionsActions.ts, extensionsViews.ts, extensionRecommendationNotificationService.ts):
//! the startup prompt, Install Workspace Recommended Extensions and the two Configure Recommended Extensions commands.

use std::path::{Path, PathBuf};

use config::json::{ParseOptions, Segment, parse_tree};
use config::json_edit::{FormattingOptions, apply_edits, set_property};
use exthost::gallery::{ExtensionInfo, Gallery};
use gpui::{Context, Window};
use serde_json::{Value, json};

use super::Workbench;
use super::model_events::Cursor;
use crate::editor::Reveal;
use crate::commands::Outcome;
use crate::editor::{Position, Selection, VerticalReveal};
use crate::extension_recommendations as recommendations;
use crate::main_thread_quick_open::Owner;
use crate::notifications::{Action, Notification, Run, Severity};
use crate::quick_input::{Entry, Event, Item, Kind, QuickInput, path_label};

/// extensionsFileTemplate.ts `ExtensionsConfigurationInitialContent`.
const EXTENSIONS_CONFIGURATION_INITIAL_CONTENT: &str = "{\n\t// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.\n\t// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp\n\n\t// List of extensions which should be recommended for users of this workspace.\n\t\"recommendations\": [\n\t\t\n\t],\n\t// List of extensions recommended by {product} that should not be recommended for users of this workspace.\n\t\"unwantedRecommendations\": [\n\t\t\n\t]\n}";
/// extensionRecommendationsService.ts `promptWorkspaceRecommendations` waits 5 s after activation.
const PROMPT_DELAY: std::time::Duration = std::time::Duration::from_secs(5);

impl Workbench {
    pub(super) fn register_workspace_recommendations_commands(&mut self) {
        self.commands.register("workbench.extensions.action.installWorkspaceRecommendedExtensions", |workbench, _, _, cx| {
            workbench.install_workspace_recommendations(cx);
            Outcome::done()
        });
        self.commands.register("workbench.extensions.action.configureWorkspaceRecommendedExtensions", |workbench, _, window, cx| {
            let workspace = workbench.settings.lock().unwrap().workspace.clone();
            let Some(workspace) = workspace else { return Outcome::done() };
            match (&workspace.file, workspace.folders.first()) {
                (Some(file), _) => workbench.open_workspace_file_recommendations(file.clone(), window, cx),
                (None, Some(folder)) => workbench.open_extensions_file(&folder.path, window, cx),
                (None, None) => Outcome::done(),
            }
        });
        self.commands.register("workbench.extensions.action.configureWorkspaceFolderRecommendedExtensions", |workbench, _, window, cx| {
            let workspace = workbench.settings.lock().unwrap().workspace.clone();
            let Some(workspace) = workspace else { return Outcome::done() };
            match workspace.folders.as_slice() {
                [] => Outcome::done(),
                [folder] => workbench.open_extensions_file(&folder.path, window, cx),
                // `PICK_WORKSPACE_FOLDER_COMMAND_ID`.
                folders => {
                    let items = folders
                        .iter()
                        .enumerate()
                        .map(|(handle, folder)| {
                            let parent = folder.path.parent().map(|parent| path_label(parent, Some(&workspace))).unwrap_or_default();
                            let description = (parent != folder.name).then_some(parent);
                            Entry::Item(Item { handle: handle as i64, label: folder.name.clone(), description, pickable: true, ..Item::default() })
                        })
                        .collect();
                    let mut input = QuickInput::new(0, Kind::QuickPick);
                    input.placeholder = Some("Select workspace folder".into());
                    input.match_on_description = true;
                    input.set_items(items);
                    let id = workbench.quick_inputs.add(input, Owner::ExtensionsFolder(folders.iter().map(|folder| folder.path.clone()).collect()));
                    workbench.show_quick_input(id, window, cx);
                    workbench.flush_quick_input_events(window, cx);
                    Outcome::done()
                }
            }
        });
        self.commands.register("_extensions.workspaceRecommendations.dontShowAgain", |workbench, _, _, _| {
            workbench.workspace_storage.lock().unwrap().set(recommendations::DONOT_SHOW_WORKSPACE_RECOMMENDATIONS_KEY, "true".into());
            Outcome::done()
        });
        self.commands.register("_extensions.workspaceRecommendations.install", |workbench, args, window, cx| {
            let ids: Vec<String> = args.iter().filter_map(Value::as_str).map(str::to_owned).collect();
            workbench.search_extensions("@recommended", window, cx);
            for id in ids {
                drop(workbench.install_from_gallery(id, None, false, cx));
            }
            Outcome::done()
        });
    }

    pub fn extensions_folder_pick_event(&mut self, id: i64, event: Event, window: &mut Window, cx: &mut Context<Self>) {
        match event {
            Event::Accept(_) => {
                let Some(handle) = self.quick_inputs.get(id).and_then(|input| input.selected_handles().first().copied()) else { return };
                let Some(Owner::ExtensionsFolder(folders)) = self.quick_inputs.owner(id) else { return };
                let folder = folders[handle as usize].clone();
                self.hide_quick_input_by_id(id, window, cx);
                let outcome = self.open_extensions_file(&folder, window, cx);
                super::quick_access::show_failure("Configure Recommended Extensions (Workspace Folder)".into(), outcome, window, cx);
            }
            Event::Hide => self.quick_inputs.remove(id),
            _ => {}
        }
    }

    /// `promptWorkspaceRecommendations`, 5 s after startup: unless the repository or the settings opted out, the
    /// recommendations not installed yet that Open VSX knows.
    pub(super) fn schedule_workspace_recommendations_prompt(&mut self, cx: &mut Context<Self>) {
        if self.settings.lock().unwrap().workspace.is_none() {
            return;
        }
        cx.spawn(async move |workbench, cx| {
            cx.background_executor().timer(PROMPT_DELAY).await;
            let _ = workbench.update(cx, |workbench, cx| workbench.prompt_workspace_recommendations(cx));
        })
        .detach();
    }

    fn prompt_workspace_recommendations(&mut self, cx: &mut Context<Self>) {
        if self.workspace_storage.lock().unwrap().get(recommendations::DONOT_SHOW_WORKSPACE_RECOMMENDATIONS_KEY) == Some("true") || self.ignores_recommendation_notifications() {
            return;
        }
        let ids = self.uninstalled_workspace_recommendations();
        if ids.is_empty() {
            return;
        }
        let fetch = cx.background_executor().spawn(async move {
            let infos: Vec<ExtensionInfo> = ids.iter().map(|id| ExtensionInfo { id: id.clone(), uuid: None, version: None, pre_release: false }).collect();
            Gallery::open_vsx(&crate::VERSION).get_extensions(&infos, true)
        });
        cx.spawn(async move |workbench, cx| {
            let found = match fetch.await {
                Ok(found) => found,
                Err(error) => return eprintln!("[extensions] workspace recommendations: {error:#}"),
            };
            if found.is_empty() {
                return;
            }
            let _ = workbench.update(cx, |workbench, cx| {
                let named: Vec<(String, String)> = found.iter().map(|gallery| (gallery.display_name.clone(), gallery.publisher_display_name.clone())).collect();
                let ids: Vec<Value> = found.iter().map(|gallery| json!(gallery.id)).collect();
                let mut notification = Notification::new(Severity::Info, recommendations::prompt_message(&named));
                notification.sticky = true;
                notification.primary = vec![
                    Action { id: "workbench.notification.choice.0".into(), label: "Install".into(), run: Run::Command("_extensions.workspaceRecommendations.install".into(), ids) },
                    Action { id: "workbench.notification.choice.1".into(), label: "Show Recommendations".into(), run: Run::Command("workbench.extensions.action.showRecommendedExtensions".into(), Vec::new()) },
                ];
                notification.secondary = vec![Action {
                    id: "workbench.notification.choice.2".into(),
                    label: "Don't Show Again for this Repository".into(),
                    run: Run::Command("_extensions.workspaceRecommendations.dontShowAgain".into(), Vec::new()),
                }];
                workbench.notify(notification);
                cx.notify();
            });
        })
        .detach();
    }

    /// `hasToIgnoreRecommendationNotifications`.
    fn ignores_recommendation_notifications(&self) -> bool {
        self.setting("extensions.ignoreRecommendations") == true || self.setting("extensions.showRecommendationsOnlyOnDemand") == true
    }

    fn uninstalled_workspace_recommendations(&self) -> Vec<String> {
        let local = self.local_extensions();
        self.workspace_recommendations().into_iter().filter(|id| !local.iter().any(|extension| extension.identifier().id.eq_ignore_ascii_case(id))).collect()
    }

    /// `installWorkspaceRecommendations`.
    fn install_workspace_recommendations(&mut self, cx: &mut Context<Self>) {
        let ids = self.uninstalled_workspace_recommendations();
        if ids.is_empty() {
            self.notify(Notification::new(Severity::Info, "There are no extensions to install."));
            cx.notify();
            return;
        }
        for id in ids {
            drop(self.install_from_gallery(id, None, false, cx));
        }
    }

    /// `openExtensionsFile`: the folder's extensions.json, created from the template when missing, with the cursor
    /// after the last recommendation.
    fn open_extensions_file(&mut self, folder: &Path, window: &mut Window, cx: &mut Context<Self>) -> Outcome {
        let path = config::service::folder_config_dir(folder).join("extensions.json");
        let created = !path.exists();
        if created {
            let written = std::fs::create_dir_all(path.parent().expect("a config dir")).and_then(|()| std::fs::write(&path, EXTENSIONS_CONFIGURATION_INITIAL_CONTENT.replace("{product}", crate::product::name_long())));
            if let Err(error) = written {
                return Outcome::error(format!("Unable to create 'extensions.json' file inside the '.vscode' folder ({error})."));
            }
        }
        self.open_at_recommendations(path, &[Segment::Key("recommendations".into())], created, window, cx);
        Outcome::done()
    }

    /// `openWorkspaceConfigurationFile`: adds `extensions.recommendations` to the workspace file when missing.
    fn open_workspace_file_recommendations(&mut self, file: PathBuf, window: &mut Window, cx: &mut Context<Self>) -> Outcome {
        let text = match std::fs::read_to_string(&file) {
            Ok(text) => text,
            Err(error) => return Outcome::error(error.to_string()),
        };
        let has_recommendations = recommendations::read_workspace_file_config(&file).is_some_and(|config| config.recommendations.is_some());
        if !has_recommendations {
            let options = FormattingOptions { tab_size: 4, insert_spaces: false, eol: "\n".into() };
            let edits = set_property(&text, &[Segment::Key("extensions".into())], Some(&json!({ "recommendations": [] })), &options);
            if let Err(error) = std::fs::write(&file, apply_edits(&text, &edits)) {
                return Outcome::error(error.to_string());
            }
        }
        self.open_at_recommendations(file, &[Segment::Key("extensions".into()), Segment::Key("recommendations".into())], false, window, cx);
        Outcome::done()
    }

    /// workspaceExtensionsConfig.ts `toggleRecommendation`: removes the extension from the folders and workspace file
    /// recommending it, else adds it to the picked ones (`pickWorkspaceOrFolders` asks only when there is a choice).
    pub(super) fn toggle_workspace_recommendation(&mut self, id: &str, window: &mut Window, cx: &mut Context<Self>) {
        let id = id.to_lowercase();
        let Some(workspace) = self.settings.lock().unwrap().workspace.clone() else { return };
        let recommends = |config: &recommendations::ExtensionsConfig| config.recommendations.iter().flatten().any(|recommended| recommended.eq_ignore_ascii_case(&id));
        let workspace_recommended = workspace.file.as_deref().and_then(recommendations::read_workspace_file_config).is_some_and(|config| recommends(&config));
        let recommended_folders: Vec<_> = workspace.folders.iter().filter(|folder| recommends(&recommendations::read_folder_config(&folder.path))).collect();
        let add = !workspace_recommended && recommended_folders.is_empty();
        let (folders, workspace_file, placeholder) = match add {
            true => (workspace.folders.iter().collect(), workspace.file.clone(), "Add extension recommendation to"),
            false => (recommended_folders, workspace.file.clone().filter(|_| workspace_recommended), "Remove extension recommendation from"),
        };
        let mut targets: Vec<(PathBuf, &'static [&'static str])> = folders.iter().map(|folder| (folder_extensions_file(&folder.path), &[][..])).collect();
        targets.extend(workspace_file.map(|file| (file, &["extensions"][..])));
        if targets.len() == 1 {
            let (file, prefix) = &targets[0];
            return self.notify_recommendation_write(write_recommendation(file, prefix, &id, add), cx);
        }
        let mut items: Vec<Entry> = folders.iter().enumerate().map(|(handle, folder)| Entry::Item(Item { handle: handle as i64, label: folder.name.clone(), description: Some("Workspace Folder".into()), ..Item::default() })).collect();
        if targets.len() > folders.len() {
            items.push(Entry::Separator(None));
            items.push(Entry::Item(Item { handle: folders.len() as i64, label: "Workspace".into(), ..Item::default() }));
        }
        let mut input = QuickInput::new(0, Kind::QuickPick);
        input.placeholder = Some(placeholder.into());
        input.can_select_many = true;
        input.set_items(items);
        let picked = self.choose(input, window, cx);
        cx.spawn(async move |workbench, cx| {
            let Some(picked) = picked.await.ok().and_then(Result::ok) else { return };
            let handles: Vec<usize> = picked.as_array().into_iter().flatten().filter_map(Value::as_u64).map(|handle| handle as usize).collect();
            let written = handles.iter().try_for_each(|&handle| write_recommendation(&targets[handle].0, targets[handle].1, &id, add));
            drop(workbench.update(cx, |workbench, cx| workbench.notify_recommendation_write(written, cx)));
        })
        .detach();
    }

    fn notify_recommendation_write(&mut self, written: std::io::Result<()>, cx: &mut Context<Self>) {
        if let Err(error) = written {
            self.notify_error(error.to_string());
        }
        cx.notify();
    }

    /// `getSelectionPosition`: after the last entry of the array at `path`, else just inside its bracket.
    fn open_at_recommendations(&mut self, path: PathBuf, json_path: &[Segment], pinned: bool, window: &mut Window, cx: &mut Context<Self>) {
        let text = std::fs::read_to_string(&path).unwrap_or_default();
        self.open_file(path, pinned, window, cx);
        let tree = parse_tree(&text, &mut Vec::new(), ParseOptions { allow_trailing_comma: true, ..ParseOptions::default() });
        let Some(array) = tree.find(tree.root, json_path) else { return };
        let array = tree.node(array);
        let offset = match array.children.last() {
            Some(&last) => tree.node(last).offset + tree.node(last).length,
            None => array.offset + 1,
        };
        let before = &text[..offset];
        let line_start = before.rfind('\n').map_or(0, |index| index + 1);
        let position = Position::new(before.matches('\n').count(), before[line_start..].chars().count());
        let viewport = self.editors.viewport;
        let Some(editor) = self.editors.active_mut() else { return };
        editor.set_selections(vec![Selection::caret(position)]);
        editor.reveal_range(position, position, VerticalReveal::CenterIfOutsideViewport, viewport);
        self.cursor_position_changed(Cursor { moved: true, source: "api", reveal: Reveal::Nothing }, cx);
        cx.notify();
    }
}

/// The folder's extensions.json whose `recommendations` count (`.vsrustium/` wins per key), else where a new one goes.
fn folder_extensions_file(folder: &Path) -> PathBuf {
    let has_recommendations = |file: &PathBuf| recommendations::read_json(file).is_some_and(|value| value.get("recommendations").is_some());
    let mut files = config::service::FOLDER_CONFIG_DIRS.iter().rev().map(|dir| folder.join(dir).join("extensions.json"));
    files.find(has_recommendations).unwrap_or_else(|| config::service::folder_config_dir(folder).join("extensions.json"))
}

/// `addOrRemoveWorkspaceFolderRecommendation` and `addOrRemoveWorkspaceRecommendation` (`prefix` `extensions`):
/// adding appends to `recommendations` and drops it from `unwantedRecommendations`; JSONEditingService creates a
/// missing file.
fn write_recommendation(file: &Path, prefix: &[&str], id: &str, add: bool) -> std::io::Result<()> {
    let mut text = std::fs::read_to_string(file).unwrap_or_else(|_| "{}".into());
    let value = recommendations::read_json(file).unwrap_or_default();
    let config = prefix.iter().fold(&value, |value, key| &value[*key]);
    let path = |key: &str, index: usize| prefix.iter().map(|key| Segment::Key((*key).into())).chain([Segment::Key(key.into()), Segment::Index(index)]).collect::<Vec<_>>();
    let index_in = |key: &str| config[key].as_array().and_then(|ids| ids.iter().position(|entry| entry.as_str().is_some_and(|entry| entry.eq_ignore_ascii_case(id))));
    let mut writes = Vec::new();
    if add {
        writes.push((path("recommendations", usize::MAX), Some(json!(id))));
        writes.extend(index_in("unwantedRecommendations").map(|index| (path("unwantedRecommendations", index), None)));
    } else {
        writes.extend(index_in("recommendations").map(|index| (path("recommendations", index), None)));
    }
    if writes.is_empty() {
        return Ok(());
    }
    let options = FormattingOptions { tab_size: 4, insert_spaces: false, eol: "\n".into() };
    for (path, value) in writes {
        let edits = set_property(&text, &path, value.as_ref(), &options);
        text = apply_edits(&text, &edits);
    }
    if let Some(parent) = file.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(file, text)
}