Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { readdir } from "fs/promises";
import * as path from "path";

class Test {
    readonly name: string;
    readonly promise: Promise<void>;

    constructor(name: string, promise: Promise<void>) {
        this.name = name;
        this.promise = promise;
    }
}

class Suite {
    tests: Test[];

    constructor() {
        this.tests = [];
    }

    public addTest(name: string, f: () => Promise<void>): void {
        const test = new Test(name, f());
        this.tests.push(test);
    }

    public async run(): Promise<void> {
        let failed = 0;
        for (const test of this.tests) {
            try {
                await test.promise;
                ok(`  ✔ ${test.name}`);
            } catch (e) {
                error(`  ✖︎ ${test.name}\n  ${e.stack}`);
                failed += 1;
            }
        }
        if (failed) {
            const plural = failed > 1 ? "s" : "";
            throw new Error(`${failed} failed test${plural}`);
        }
    }
}

export class Context {
    public async suite(name: string, f: (ctx: Suite) => void): Promise<void> {
        const ctx = new Suite();
        f(ctx);
        try {
            ok(`⌛︎ ${name}`);
            await ctx.run();
            ok(`✔ ${name}`);
        } catch (e) {
            error(`✖︎ ${name}\n  ${e.stack}`);
            throw e;
        }
    }
}

export async function run(): Promise<void> {
    const context = new Context();

    const testFiles = (await readdir(path.resolve(__dirname))).filter((name) =>
        name.endsWith(".test.js")
    );
    for (const testFile of testFiles) {
        try {
            const testModule = require(path.resolve(__dirname, testFile));
            await testModule.getTests(context);
        } catch (e) {
            error(`${e}`);
            throw e;
        }
    }
}

function ok(message: string): void {
    // eslint-disable-next-line no-console
    console.log(`\x1b[32m${message}\x1b[0m`);
}

function error(message: string): void {
    // eslint-disable-next-line no-console
    console.error(`\x1b[31m${message}\x1b[0m`);
}
t_with(", ", |(idx, field), f| {
        if snippet_cap.is_some() {
            f(&format_args!("{}: ${{{}:()}}", field.name(db), idx + 1))
        } else {
            f(&format_args!("{}: ()", field.name(db)))
        }
    });

    let types = fields.iter().format_with(", ", |field, f| {
        f(&format_args!("{}: {}", field.name(db), field.ty(db).display(db)))
    });

    RenderedLiteral {
        literal: format!("{path} {{ {completions} }}"),
        detail: format!("{path} {{ {types} }}"),
    }
}

/// Render a tuple type (or sub-type) to a `RenderedCompound`. Use `None` for
/// the `name` argument for an anonymous type.
pub(crate) fn render_tuple_lit(
    db: &dyn HirDatabase,
    snippet_cap: Option<SnippetCap>,
    fields: &[hir::Field],
    path: &str,
) -> RenderedLiteral {
    if snippet_cap.is_none() {
        return RenderedLiteral { literal: path.to_string(), detail: path.to_string() };
    }
    let completions = fields.iter().enumerate().format_with(", ", |(idx, _), f| {
        if snippet_cap.is_some() {
            f(&format_args!("${{{}:()}}", idx + 1))
        } else {
            f(&format_args!("()"))
        }
    });

    let types = fields.iter().format_with(", ", |field, f| f(&field.ty(db).display(db)));

    RenderedLiteral {
        literal: format!("{path}({completions})"),
        detail: format!("{path}({types})"),
    }
}

/// Find all the visible fields in a given list. Returns the list of visible
/// fields, plus a boolean for whether the list is comprehensive (contains no
/// private fields and its item is not marked `#[non_exhaustive]`).
pub(crate) fn visible_fields(
    ctx: &CompletionContext<'_>,
    fields: &[hir::Field],
    item: impl HasAttrs + HasCrate + Copy,
) -> Option<(Vec<hir::Field>, bool)> {
    let module = ctx.module;
    let n_fields = fields.len();
    let fields = fields
        .iter()
        .filter(|field| field.is_visible_from(ctx.db, module))
        .copied()
        .collect::<Vec<_>>();
    let has_invisible_field = n_fields - fields.len() > 0;
    let is_foreign_non_exhaustive = item.attrs(ctx.db).by_key("non_exhaustive").exists()
        && item.krate(ctx.db) != module.krate();
    let fields_omitted = has_invisible_field || is_foreign_non_exhaustive;
    Some((fields, fields_omitted))
}

/// Format a struct, etc. literal option for display in the completions menu.
pub(crate) fn format_literal_label(
    name: &str,
    kind: StructKind,
    snippet_cap: Option<SnippetCap>,
) -> SmolStr {
    if snippet_cap.is_none() {
        return name.into();
    }
    match kind {
        StructKind::Tuple => SmolStr::from_iter([name, "(…)"]),
        StructKind::Record => SmolStr::from_iter([name, " {…}"]),
        StructKind::Unit => name.into(),
    }
}

/// Format a struct, etc. literal option for lookup used in completions filtering.
pub(crate) fn format_literal_lookup(name: &str, kind: StructKind) -> SmolStr {
    match kind {
        StructKind::Tuple => SmolStr::from_iter([name, "()"]),
        StructKind::Record => SmolStr::from_iter([name, "{}"]),
        StructKind::Unit => name.into(),
    }
}