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
85
use lsp_types::{CodeAction, CodeActionKind, CodeActionOrCommand};
use psl::{parser_database::walkers::CompleteInlineRelationWalker, schema_ast::ast::SourceConfig, Configuration};

pub(crate) fn edit_referential_integrity(
    actions: &mut Vec<CodeActionOrCommand>,
    params: &lsp_types::CodeActionParams,
    schema: &str,
    source: &SourceConfig,
) {
    let prop = match source.properties.iter().find(|p| p.name.name == "referentialIntegrity") {
        Some(prop) => prop,
        None => return,
    };

    let span_diagnostics = match super::diagnostics_for_span(schema, &params.context.diagnostics, source.span) {
        Some(sd) => sd,
        None => return,
    };

    let diagnostics =
        match super::filter_diagnostics(span_diagnostics, "The `referentialIntegrity` attribute is deprecated.") {
            Some(value) => value,
            None => return,
        };

    let edit = super::create_text_edit(schema, "relationMode".to_owned(), false, prop.name.span, params);

    let action = CodeAction {
        title: String::from("Rename property to relationMode"),
        kind: Some(CodeActionKind::QUICKFIX),
        edit: Some(edit),
        diagnostics: Some(diagnostics),
        ..Default::default()
    };

    actions.push(CodeActionOrCommand::CodeAction(action))
}

pub(crate) fn replace_set_default_mysql(
    actions: &mut Vec<CodeActionOrCommand>,
    params: &lsp_types::CodeActionParams,
    schema: &str,
    relation: CompleteInlineRelationWalker<'_>,
    config: &Configuration,
) {
    let datasource = match config.datasources.first() {
        Some(ds) => ds,
        None => return,
    };

    if datasource.active_connector.provider_name() != "mysql" {
        return;
    }

    let span = match relation.on_update_span() {
        Some(span) => span,
        None => return,
    };

    let span_diagnostics = match super::diagnostics_for_span(schema, &params.context.diagnostics, span) {
        Some(sd) => sd,
        None => return,
    };

    let diagnostics = match
        super::filter_diagnostics(
            span_diagnostics,
            "MySQL does not actually support the `SetDefault` referential action, so using it may result in unexpected errors.") {
            Some(value) => value,
            None => return,
        };

    let edit = super::create_text_edit(schema, "NoAction".to_owned(), false, span, params);

    let action = CodeAction {
        title: r#"Replace SetDefault with NoAction"#.to_owned(),

        kind: Some(CodeActionKind::QUICKFIX),
        edit: Some(edit),
        diagnostics: Some(diagnostics),
        ..Default::default()
    };

    actions.push(CodeActionOrCommand::CodeAction(action))
}