//! Defines `Fixture` -- a convenient way to describe the initial state of
//! rust-analyzer database from a single string.
//!
//! Fixtures are strings containing rust source code with optional metadata.
//! A fixture without metadata is parsed into a single source file.
//! Use this to test functionality local to one file.
//!
//! Simple Example:
//!
//! ```ignore
//! r#"
//! fn main() {
//!     println!("Hello World")
//! }
//! "#
//! ```
//!
//! Metadata can be added to a fixture after a `//-` comment.
//! The basic form is specifying filenames,
//! which is also how to define multiple files in a single test fixture
//!
//! Example using two files in the same crate:
//!
//! ```ignore
//! "
//! //- /main.rs
//! mod foo;
//! fn main() {
//!     foo::bar();
//! }
//!
//! //- /foo.rs
//! pub fn bar() {}
//! "
//! ```
//!
//! Example using two crates with one file each, with one crate depending on the other:
//!
//! ```ignore
//! r#"
//! //- /main.rs crate:a deps:b
//! fn main() {
//!     b::foo();
//! }
//! //- /lib.rs crate:b
//! pub fn b() {
//!     println!("Hello World")
//! }
//! "#
//! ```
//!
//! Metadata allows specifying all settings and variables
//! that are available in a real rust project. See [`Fixture`]
//! for the syntax.
//!
//! Example using some available metadata:
//!
//! ```ignore
//! "
//! //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
//! fn insert_source_code_here() {}
//! "
//! ```

use std::iter;

use rustc_hash::FxHashMap;
use stdx::trim_indent;

#[derive(Debug, Eq, PartialEq)]
pub struct Fixture {
    /// Specifies the path for this file. It must start with "/".
    pub path: String,
    /// Defines a new crate and make this file its root module.
    ///
    /// Version and repository URL of the crate can optionally be specified; if
    /// either one is specified, the other must also be specified.
    ///
    /// Syntax:
    /// - `crate:my_awesome_lib`
    /// - `crate:my_awesome_lib@0.0.1,https://example.com/repo.git`
    pub krate: Option<String>,
    /// Specifies dependencies of this crate. This must be used with `crate` meta.
    ///
    /// Syntax: `deps:hir-def,ide-assists`
    pub deps: Vec<String>,
    /// Limits crates in the extern prelude. The set of crate names must be a
    /// subset of `deps`. This must be used with `crate` meta.
    ///
    /// If this is not specified, all the dependencies will be in the extern prelude.
    ///
    /// Syntax: `extern-prelude:hir-def,ide-assists`
    pub extern_prelude: Option<Vec<String>>,
    /// Specifies configuration options to be enabled. Options may have associated
    /// values.
    ///
    /// Syntax: `cfg:test,dbg=false,opt_level=2`
    pub cfgs: Vec<(String, Option<String>)>,
    /// Specifies the edition of this crate. This must be used with
    /// `crate` meta. If this is not specified,
    /// `base_db::input::Edition::CURRENT` will be used.  This must be
    /// used with `crate` meta.
    ///
    /// Syntax: `edition:2021`
    pub edition: Option<String>,
    /// Specifies environment variables.
    ///
    /// Syntax: `env:PATH=/bin,RUST_LOG=debug`
    pub env: FxHashMap<String, String>,
    /// Specifies extra crate-level attributes injected at the top of the crate root file.
    /// This must be used with `crate` meta.
    ///
    /// Syntax: `crate-attr:no_std crate-attr:features(f16,f128) crate-attr:cfg(target_arch="x86")`
    pub crate_attrs: Vec<String>,
    /// Introduces a new source root. This file **and the following
    /// files** will belong the new source root. This must be used
    /// with `crate` meta.
    ///
    /// Use this if you want to test something that uses `SourceRoot::is_library()`
    /// to check editability.
    ///
    /// Note that files before the first fixture with `new_source_root` meta will
    /// belong to an implicitly defined local source root.
    ///
    /// Syntax:
    /// - `new_source_root:library`
    /// - `new_source_root:local`
    pub introduce_new_source_root: Option<String>,
    /// Explicitly declares this crate as a library outside current workspace. This
    /// must be used with `crate` meta.
    ///
    /// This is implied if this file belongs to a library source root.
    ///
    /// Use this if you want to test something that checks if a crate is a workspace
    /// member via `CrateOrigin`.
    ///
    /// Syntax: `library`
    pub library: bool,
    /// Actual file contents. All meta comments are stripped.
    pub text: String,
    /// The line number in the original fixture of the beginning of this fixture.
    pub line: usize,
}

#[derive(Debug)]
pub struct MiniCore {
    activated_flags: Vec<String>,
    valid_flags: Vec<String>,
}

#[derive(Debug)]
pub struct FixtureWithProjectMeta {
    pub fixture: Vec<Fixture>,
    pub mini_core: Option<MiniCore>,
    pub proc_macro_names: Vec<String>,
    pub toolchain: Option<String>,
    /// Specifies LLVM data layout to be used.
    ///
    /// You probably don't want to manually specify this. See LLVM manual for the
    /// syntax, if you must: <https://llvm.org/docs/LangRef.html#data-layout>
    pub target_data_layout: String,
    /// Specifies the target architecture.
    pub target_arch: String,
}

impl FixtureWithProjectMeta {
    /// Parses text which looks like this:
    ///
    ///  ```text
    ///  //- some meta
    ///  line 1
    ///  line 2
    ///  //- other meta
    ///  ```
    ///
    /// Fixture can also start with a proc_macros and minicore declaration (in that order):
    ///
    /// ```text
    /// //- toolchain: nightly
    /// //- proc_macros: identity
    /// //- minicore: sized
    /// ```
    ///
    /// That will set toolchain to nightly and include predefined proc macros and a subset of
    /// `libcore` into the fixture, see `minicore.rs` for what's available. Note that toolchain
    /// defaults to stable.
    pub fn parse(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> Self {
        let fixture = trim_indent(ra_fixture);
        let mut fixture = fixture.as_str();
        let mut toolchain = None;
        let mut target_data_layout =
            "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128".to_owned();
        let mut target_arch = "x86_64".to_owned();
        let mut mini_core = None;
        let mut res: Vec<Fixture> = Vec::new();
        let mut proc_macro_names = vec![];
        let mut first_row = 0;

        if let Some(meta) = fixture.strip_prefix("//- toolchain:") {
            first_row += 1;
            let (meta, remain) = meta.split_once('\n').unwrap();
            toolchain = Some(meta.trim().to_owned());
            fixture = remain;
        }

        if let Some(meta) = fixture.strip_prefix("//- target_data_layout:") {
            first_row += 1;
            let (meta, remain) = meta.split_once('\n').unwrap();
            meta.trim().clone_into(&mut target_data_layout);
            fixture = remain;
        }

        if let Some(meta) = fixture.strip_prefix("//- target_arch:") {
            first_row += 1;
            let (meta, remain) = meta.split_once('\n').unwrap();
            meta.trim().clone_into(&mut target_arch);
            fixture = remain;
        }

        if let Some(meta) = fixture.strip_prefix("//- proc_macros:") {
            first_row += 1;
            let (meta, remain) = meta.split_once('\n').unwrap();
            proc_macro_names = meta.split(',').map(|it| it.trim().to_owned()).collect();
            fixture = remain;
        }

        if let Some(meta) = fixture.strip_prefix("//- minicore:") {
            first_row += 1;
            let (meta, remain) = meta.split_once('\n').unwrap();
            mini_core = Some(MiniCore::parse(meta));
            fixture = remain;
        }

        let default =
            if fixture.contains("//- /") { None } else { Some((first_row - 1, "//- /main.rs")) };

        for (ix, line) in
            default.into_iter().chain((first_row..).zip(fixture.split_inclusive('\n')))
        {
            if line.contains("//-") {
                assert!(
                    line.starts_with("//-"),
                    "Metadata line {ix} has invalid indentation. \
                     All metadata lines need to have the same indentation.\n\
                     The offending line: {line:?}"
                );
            }

            if let Some(line) = line.strip_prefix("//-") {
                let meta = Self::parse_meta_line(line, (ix + 1).try_into().unwrap());
                res.push(meta);
            } else {
                if matches!(line.strip_prefix("// "), Some(l) if l.trim().starts_with('/')) {
                    panic!("looks like invalid metadata line: {line:?}");
                }

                if let Some(entry) = res.last_mut() {
                    entry.text.push_str(line);
                }
            }
        }

        Self {
            fixture: res,
            mini_core,
            proc_macro_names,
            toolchain,
            target_data_layout,
            target_arch,
        }
    }

    //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
    fn parse_meta_line(meta: &str, line: usize) -> Fixture {
        let meta = meta.trim();
        let mut components = meta.split_ascii_whitespace();

        let path = components.next().expect("fixture meta must start with a path").to_owned();
        assert!(path.starts_with('/'), "fixture path does not start with `/`: {path:?}");

        let mut krate = None;
        let mut deps = Vec::new();
        let mut crate_attrs = Vec::new();
        let mut extern_prelude = None;
        let mut edition = None;
        let mut cfgs = Vec::new();
        let mut env = FxHashMap::default();
        let mut introduce_new_source_root = None;
        let mut library = false;
        for component in components {
            if component == "library" {
                library = true;
                continue;
            }

            let (key, value) =
                component.split_once(':').unwrap_or_else(|| panic!("invalid meta line: {meta:?}"));
            match key {
                "crate" => krate = Some(value.to_owned()),
                "deps" => deps = value.split(',').map(|it| it.to_owned()).collect(),
                "crate-attr" => crate_attrs.push(value.to_owned()),
                "extern-prelude" => {
                    if value.is_empty() {
                        extern_prelude = Some(Vec::new());
                    } else {
                        extern_prelude =
                            Some(value.split(',').map(|it| it.to_owned()).collect::<Vec<_>>());
                    }
                }
                "edition" => edition = Some(value.to_owned()),
                "cfg" => {
                    for entry in value.split(',') {
                        match entry.split_once('=') {
                            Some((k, v)) => cfgs.push((k.to_owned(), Some(v.to_owned()))),
                            None => cfgs.push((entry.to_owned(), None)),
                        }
                    }
                }
                "env" => {
                    for key in value.split(',') {
                        if let Some((k, v)) = key.split_once('=') {
                            env.insert(k.into(), v.into());
                        }
                    }
                }
                "new_source_root" => introduce_new_source_root = Some(value.to_owned()),
                _ => panic!("bad component: {component:?}"),
            }
        }

        for prelude_dep in extern_prelude.iter().flatten() {
            assert!(
                deps.contains(prelude_dep),
                "extern-prelude {extern_prelude:?} must be a subset of deps {deps:?}"
            );
        }

        Fixture {
            path,
            text: String::new(),
            line,
            krate,
            deps,
            crate_attrs,
            extern_prelude,
            cfgs,
            edition,
            env,
            introduce_new_source_root,
            library,
        }
    }
}

impl MiniCore {
    pub const RAW_SOURCE: &'static str = include_str!("./minicore.rs");

    fn has_flag(&self, flag: &str) -> bool {
        self.activated_flags.iter().any(|it| it == flag)
    }

    pub fn from_flags<'a>(flags: impl IntoIterator<Item = &'a str>) -> Self {
        MiniCore {
            activated_flags: flags.into_iter().map(|x| x.to_owned()).collect(),
            valid_flags: Vec::new(),
        }
    }

    #[track_caller]
    fn assert_valid_flag(&self, flag: &str) {
        if !self.valid_flags.iter().any(|it| it == flag) {
            panic!("invalid flag: {flag:?}, valid flags: {:?}", self.valid_flags);
        }
    }

    fn parse(line: &str) -> MiniCore {
        let mut res = MiniCore { activated_flags: Vec::new(), valid_flags: Vec::new() };

        for entry in line.trim().split(", ") {
            if res.has_flag(entry) {
                panic!("duplicate minicore flag: {entry:?}");
            }
            res.activated_flags.push(entry.to_owned());
        }

        res
    }

    pub fn available_flags(raw_source: &str) -> impl Iterator<Item = &str> {
        let lines = raw_source.split_inclusive('\n');
        lines
            .map_while(|x| x.strip_prefix("//!"))
            .skip_while(|line| !line.contains("Available flags:"))
            .skip(1)
            .map(|x| x.split_once(':').unwrap().0.trim())
    }

    /// Strips parts of minicore.rs which are flagged by inactive flags.
    ///
    /// This is probably over-engineered to support flags dependencies.
    pub fn source_code(mut self, raw_source: &str) -> String {
        let raw_source = raw_source.replace("\r\n", "\n").replace('\r', "\n");
        let mut buf = String::new();
        let mut lines = raw_source.split_inclusive('\n');

        let mut implications = Vec::new();

        // Parse `//!` preamble and extract flags and dependencies.
        let trim_doc: fn(&str) -> Option<&str> = |line| match line.strip_prefix("//!") {
            Some(it) => Some(it),
            None => {
                assert!(line.trim().is_empty(), "expected empty line after minicore header");
                None
            }
        };
        for line in lines
            .by_ref()
            .map_while(trim_doc)
            .skip_while(|line| !line.contains("Available flags:"))
            .skip(1)
        {
            let (flag, deps) = line.split_once(':').unwrap();
            let flag = flag.trim();

            self.valid_flags.push(flag.to_owned());
            implications.extend(
                iter::repeat(flag)
                    .zip(deps.split(", ").map(str::trim).filter(|dep| !dep.is_empty())),
            );
        }

        for (_, dep) in &implications {
            self.assert_valid_flag(dep);
        }

        for flag in &self.activated_flags {
            self.assert_valid_flag(flag);
        }

        // Fixed point loop to compute transitive closure of flags.
        loop {
            let mut changed = false;
            for &(u, v) in &implications {
                if self.has_flag(u) && !self.has_flag(v) {
                    self.activated_flags.push(v.to_owned());
                    changed = true;
                }
            }
            if !changed {
                break;
            }
        }

        let mut active_regions = Vec::new();
        let mut inactive_regions = Vec::new();
        let mut seen_regions = Vec::new();
        for line in lines {
            let trimmed = line.trim();
            if let Some(region) = trimmed.strip_prefix("// region:") {
                if let Some(region) = region.strip_prefix('!') {
                    inactive_regions.push(region);
                    continue;
                } else {
                    active_regions.push(region);
                    continue;
                }
            }
            if let Some(region) = trimmed.strip_prefix("// endregion:") {
                let (prev, region) = if let Some(region) = region.strip_prefix('!') {
                    (inactive_regions.pop().unwrap(), region)
                } else {
                    (active_regions.pop().unwrap(), region)
                };
                assert_eq!(prev, region, "unbalanced region pairs");
                continue;
            }

            let mut active_line_region = 0;
            let mut inactive_line_region = 0;
            if let Some(idx) = trimmed.find("// :!") {
                let regions = trimmed[idx + "// :!".len()..].split(", ");
                inactive_line_region += regions.clone().count();
                inactive_regions.extend(regions);
            } else if let Some(idx) = trimmed.find("// :") {
                let regions = trimmed[idx + "// :".len()..].split(", ");
                active_line_region += regions.clone().count();
                active_regions.extend(regions);
            }

            let mut keep = true;
            for &region in &active_regions {
                assert!(!region.starts_with(' '), "region marker starts with a space: {region:?}");
                self.assert_valid_flag(region);
                seen_regions.push(region);
                keep &= self.has_flag(region);
            }
            for &region in &inactive_regions {
                assert!(!region.starts_with(' '), "region marker starts with a space: {region:?}");
                self.assert_valid_flag(region);
                seen_regions.push(region);
                keep &= !self.has_flag(region);
            }

            if keep {
                buf.push_str(line);
            }
            if active_line_region > 0 {
                active_regions.drain(active_regions.len() - active_line_region..);
            }
            if inactive_line_region > 0 {
                inactive_regions.drain(inactive_regions.len() - inactive_line_region..);
            }
        }

        if !active_regions.is_empty() {
            panic!("unclosed regions: {active_regions:?} Add an `endregion` comment");
        }
        if !inactive_regions.is_empty() {
            panic!("unclosed regions: {inactive_regions:?} Add an `endregion` comment");
        }

        for flag in &self.valid_flags {
            if !seen_regions.iter().any(|it| it == flag) {
                panic!("unused minicore flag: {flag:?}");
            }
        }
        buf
    }
}

#[test]
#[should_panic]
fn parse_fixture_checks_further_indented_metadata() {
    FixtureWithProjectMeta::parse(
        r"
        //- /lib.rs
          mod bar;

          fn foo() {}
          //- /bar.rs
          pub fn baz() {}
          ",
    );
}

#[test]
fn parse_fixture_gets_full_meta() {
    let FixtureWithProjectMeta {
        fixture: parsed,
        mini_core,
        proc_macro_names,
        toolchain,
        target_data_layout: _,
        target_arch: _,
    } = FixtureWithProjectMeta::parse(
        r#"
//- toolchain: nightly
//- proc_macros: identity
//- minicore: coerce_unsized
//- /lib.rs crate:foo deps:bar,baz crate-attr:no_std crate-attr:features(f16,f128) crate-attr:cfg(target_arch="x86") cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
mod m;
"#,
    );
    assert_eq!(toolchain, Some("nightly".to_owned()));
    assert_eq!(proc_macro_names, vec!["identity".to_owned()]);
    assert_eq!(mini_core.unwrap().activated_flags, vec!["coerce_unsized".to_owned()]);
    assert_eq!(1, parsed.len());

    let meta = &parsed[0];
    assert_eq!("mod m;\n", meta.text);

    assert_eq!("foo", meta.krate.as_ref().unwrap());
    assert_eq!(
        vec![
            "no_std".to_owned(),
            "features(f16,f128)".to_owned(),
            "cfg(target_arch=\"x86\")".to_owned()
        ],
        meta.crate_attrs
    );
    assert_eq!("/lib.rs", meta.path);
    assert_eq!(2, meta.env.len());
}

#[test]
fn minicore_source_code_normalizes_line_endings() {
    let source = MiniCore::RAW_SOURCE.replace("\r\n", "\n").replace('\r', "\n");
    let source = source.replace('\n', "\r\n");
    let source = MiniCore::from_flags(["option"]).source_code(&source);

    assert!(!source.contains('\r'));
}
