-
Notifications
You must be signed in to change notification settings - Fork 551
/
Copy pathmod.rs
87 lines (80 loc) · 2.82 KB
/
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
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
86
87
use cairo_lang_syntax::node::SyntaxNode;
use lsp_types::{
CodeAction, CodeActionOrCommand, CodeActionParams, CodeActionResponse, Diagnostic,
NumberOrString,
};
use tracing::debug;
use crate::lang::db::{AnalysisDatabase, LsSyntaxGroup};
use crate::lang::lsp::{LsProtoGroup, ToCairo};
mod add_missing_trait;
mod expand_macro;
mod fill_struct_members;
mod rename_unused_variable;
/// Compute commands for a given text document and range. These commands are typically code fixes to
/// either fix problems or to beautify/refactor code.
pub fn code_actions(params: CodeActionParams, db: &AnalysisDatabase) -> Option<CodeActionResponse> {
let mut actions = Vec::with_capacity(params.context.diagnostics.len());
let file_id = db.file_for_url(¶ms.text_document.uri)?;
let node = db.find_syntax_node_at_position(file_id, params.range.start.to_cairo())?;
for diagnostic in params.context.diagnostics.iter() {
actions.extend(
get_code_actions_for_diagnostic(db, &node, diagnostic, ¶ms)
.into_iter()
.map(CodeActionOrCommand::from),
);
}
actions.extend(
expand_macro::expand_macro(db, node.clone()).into_iter().map(CodeActionOrCommand::from),
);
actions.extend(
fill_struct_members::fill_struct_members(db, node.clone(), ¶ms)
.into_iter()
.map(CodeActionOrCommand::from),
);
Some(actions)
}
/// Generate code actions for a given diagnostic.
///
/// # Arguments
///
/// * `db` - A reference to the Salsa database.
/// * `node` - The syntax node where the diagnostic is located.
/// * `diagnostic` - The diagnostic for which to generate code actions.
/// * `params` - The parameters for the code action request.
///
/// # Returns
///
/// A vector of [`CodeAction`] objects that can be applied to resolve the diagnostic.
fn get_code_actions_for_diagnostic(
db: &AnalysisDatabase,
node: &SyntaxNode,
diagnostic: &Diagnostic,
params: &CodeActionParams,
) -> Vec<CodeAction> {
let code = match &diagnostic.code {
Some(NumberOrString::String(code)) => code,
Some(NumberOrString::Number(code)) => {
debug!("diagnostic code is not a string: `{code}`");
return vec![];
}
None => {
debug!("diagnostic code is missing");
return vec![];
}
};
match code.as_str() {
"E0001" => {
vec![rename_unused_variable::rename_unused_variable(
db,
node,
diagnostic.clone(),
params.text_document.uri.clone(),
)]
}
"E0002" => add_missing_trait::add_missing_trait(db, node, params.text_document.uri.clone()),
code => {
debug!("no code actions for diagnostic code: {code}");
vec![]
}
}
}