sway_lsp/capabilities/code_actions/diagnostic/
mod.rs

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
mod auto_import;
mod qualify;

use crate::capabilities::{code_actions::CodeActionContext, diagnostic::DiagnosticData};
use lsp_types::CodeActionOrCommand;

use self::auto_import::import_code_action;
use self::qualify::qualify_code_action;

/// Returns a list of [CodeActionOrCommand] based on the relevant compiler diagnostics.
pub(crate) fn code_actions(ctx: &CodeActionContext) -> Option<Vec<CodeActionOrCommand>> {
    // Find diagnostics that have attached metadata.
    let diagnostics_with_data = ctx.diagnostics.iter().filter_map(|diag| {
        if let Some(data) = diag.clone().data {
            if let Ok(data) = serde_json::from_value::<DiagnosticData>(data) {
                return Some((diag.range, data));
            }
        }
        None
    });

    import_code_action(ctx, &mut diagnostics_with_data.clone())
        .into_iter()
        .chain(qualify_code_action(ctx, &mut diagnostics_with_data.clone()))
        .reduce(|mut combined, mut curr| {
            combined.append(&mut curr);
            combined
        })
}