Skip to content

Visual Studio Code, rebuilt in Rust

Rust
9.0 KB
Raw Blame
//! Debuggers contributed by extensions (debugger.ts `Debugger` and its `merge`), the launch configurations of
//! launch.json (debugConfigurationManager.ts `Launch`), and the `${command:...}` and `${input:...}` references
//! configurationResolver.ts resolves interactively (workbench/configuration_resolver.rs).

use serde_json::Value;

use exthost::scan::ScannedExtension;

pub use config::debuggers::Debugger;

/// `config::debuggers::debuggers` over the scanned extensions; an untyped contribution is an error message of its
/// extension (debugAdapterManager.ts `debuggersExtPoint.setHandler`).
pub fn debuggers(extensions: &[ScannedExtension]) -> Vec<Debugger> {
    for extension in extensions {
        if extension.description["contributes"]["debuggers"].as_array().into_iter().flatten().any(|debugger| !debugger["type"].as_str().is_some_and(|kind| !kind.is_empty())) {
            crate::perf::extension_point_message(&extension.id, crate::notifications::Severity::Error, "Debugger 'type' can not be omitted and must be of type 'string'.".into());
        }
    }
    let described: Vec<(&str, &Value)> = extensions.iter().map(|extension| (extension.id.as_str(), &extension.description)).collect();
    config::debuggers::debuggers(&described)
}

/// A launch.json of a folder: its `configurations` and `compounds`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Launch {
    pub configurations: Vec<Value>,
    pub compounds: Vec<Value>,
    pub inputs: Vec<Value>,
}

impl Launch {
    /// The `launch` section as `Launch.getConfig` reads it; anything malformed counts as missing.
    pub fn from_section(section: Option<&Value>) -> Launch {
        let list = |key: &str| section.and_then(|section| section[key].as_array()).cloned().unwrap_or_default();
        let named = |value: &Value| value["name"].is_string();
        Launch { configurations: list("configurations").into_iter().filter(named).collect(), compounds: list("compounds").into_iter().filter(named).collect(), inputs: list("inputs") }
    }

    /// `getConfiguration(name)`: the first configuration with that name, a copy.
    pub fn configuration(&self, name: &str) -> Option<Value> {
        self.configurations.iter().find(|configuration| configuration["name"] == name).cloned()
    }

    pub fn compound(&self, name: &str) -> Option<&Value> {
        self.compounds.iter().find(|compound| compound["name"] == name)
    }

    /// `getConfigurationNames`: configurations not `presentation.hidden`, sorted as debugUtils.ts `getVisibleAndSorted`,
    /// then the compounds.
    pub fn configuration_names(&self) -> Vec<String> {
        let mut visible: Vec<&Value> = self.configurations.iter().chain(&self.compounds).filter(|config| config["presentation"]["hidden"] != true).collect();
        visible.sort_by(|first, second| compare_presentation(&first["presentation"], &second["presentation"]));
        visible.iter().filter_map(|config| config["name"].as_str().map(str::to_owned)).collect()
    }
}

/// debugUtils.ts `getVisibleAndSorted` comparison: entries with a presentation first, then by group and order.
pub fn compare_presentation(first: &Value, second: &Value) -> std::cmp::Ordering {
    use std::cmp::Ordering;
    match (first.is_object(), second.is_object()) {
        (false, false) => return Ordering::Equal,
        (false, true) => return Ordering::Greater,
        (true, false) => return Ordering::Less,
        _ => {}
    }
    match (first["group"].as_str(), second["group"].as_str()) {
        (None, Some(_)) => return Ordering::Greater,
        (Some(_), None) => return Ordering::Less,
        (Some(a), Some(b)) if a != b => return a.cmp(b),
        _ => {}
    }
    match (first["order"].as_f64(), second["order"].as_f64()) {
        (None, Some(_)) => Ordering::Greater,
        (Some(_), None) => Ordering::Less,
        (Some(a), Some(b)) => a.partial_cmp(&b).unwrap_or(Ordering::Equal),
        (None, None) => Ordering::Equal,
    }
}

/// A `${command:...}` or `${input:...}` the configuration holds, in the order they appear.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Interactive {
    Command(String),
    Input(String),
}

impl Interactive {
    pub fn variable(&self) -> String {
        match self {
            Interactive::Command(name) => format!("${{command:{name}}}"),
            Interactive::Input(name) => format!("${{input:{name}}}"),
        }
    }
}

/// configurationResolver.ts `findVariables`: every interactive variable in the configuration's strings, once.
pub fn interactive_variables(config: &Value) -> Vec<Interactive> {
    let mut found = Vec::new();
    collect_variables(config, &mut found);
    found
}

fn collect_variables(value: &Value, found: &mut Vec<Interactive>) {
    match value {
        Value::String(text) => {
            let mut rest = text.as_str();
            while let Some(start) = rest.find("${") {
                let Some(end) = rest[start..].find('}') else { break };
                let inner = &rest[start + 2..start + end];
                let variable = match inner.split_once(':') {
                    Some(("command", name)) => Some(Interactive::Command(name.to_owned())),
                    Some(("input", name)) => Some(Interactive::Input(name.to_owned())),
                    _ => None,
                };
                if let Some(variable) = variable.filter(|variable| !found.contains(variable)) {
                    found.push(variable);
                }
                rest = &rest[start + end + 1..];
            }
        }
        Value::Array(items) => items.iter().for_each(|item| collect_variables(item, found)),
        Value::Object(members) => members.values().for_each(|member| collect_variables(member, found)),
        _ => {}
    }
}

/// Replaces each resolved variable in every string; a string that is exactly one variable takes the value as it
/// is (a command may answer a non-string, which the resolver refuses before this).
pub fn replace_variables(value: &mut Value, resolved: &[(String, String)]) {
    match value {
        Value::String(text) => {
            for (variable, replacement) in resolved {
                if text.contains(variable.as_str()) {
                    *text = text.replace(variable.as_str(), replacement);
                }
            }
        }
        Value::Array(items) => items.iter_mut().for_each(|item| replace_variables(item, resolved)),
        Value::Object(members) => members.values_mut().for_each(|member| replace_variables(member, resolved)),
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    #[test]
    fn interactive_variables_are_found_once_in_order() {
        let config = json!({ "program": "${workspaceFolder}/${input:file}", "args": ["${command:pickProcess}", "${input:file}"], "env": { "A": "${env:HOME}" } });
        assert_eq!(interactive_variables(&config), vec![Interactive::Input("file".into()), Interactive::Command("pickProcess".into())]);
    }

    #[test]
    fn replaced_variables_reach_nested_strings() {
        let mut config = json!({ "program": "${input:file}.js", "args": ["--pid=${command:pick}"] });
        replace_variables(&mut config, &[("${input:file}".into(), "main".into()), ("${command:pick}".into(), "42".into())]);
        assert_eq!(config, json!({ "program": "main.js", "args": ["--pid=42"] }));
    }

    #[test]
    fn debuggers_merge_by_type_and_keep_the_built_in_values() {
        let extension = |id: &str, builtin: bool, debuggers: Value| ScannedExtension {
            id: id.into(),
            location: std::path::PathBuf::from("/x"),
            description: json!({ "isBuiltin": builtin, "contributes": { "debuggers": debuggers } }),
        };
        let extensions = vec![
            extension("a.main", true, json!([{ "type": "node", "label": "Node.js", "languages": ["javascript"] }])),
            extension("b.extra", false, json!([{ "type": "node", "label": "Other", "variables": { "PickProcess": "extension.pickNodeProcess" } }])),
        ];
        let found = debuggers(&extensions);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].label(), "Node.js");
        assert!(found[0].interested_in_language("javascript"));
        assert_eq!(found[0].contribution["variables"]["PickProcess"], "extension.pickNodeProcess");
        assert_eq!(found[0].extension_id, "b.extra");
        debuggers(&[extension("c.untyped", false, json!([{ "label": "No Type" }]))]);
        assert_eq!(crate::perf::messages("c.untyped"), [(crate::notifications::Severity::Error, "Debugger 'type' can not be omitted and must be of type 'string'.".to_owned())]);
    }

    #[test]
    fn configuration_names_put_presentation_groups_first() {
        let launch = Launch::from_section(Some(&json!({
            "configurations": [{ "name": "plain" }, { "name": "b", "presentation": { "group": "2" } }, { "name": "a", "presentation": { "group": "1", "order": 2 } }, { "name": "hidden", "presentation": { "hidden": true } }],
            "compounds": [{ "name": "both", "configurations": ["a", "b"] }],
        })));
        assert_eq!(launch.configuration_names(), vec!["a", "b", "plain", "both"]);
        assert!(launch.configuration("missing").is_none());
    }
}