-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathremove_bang_from_call.rs
97 lines (74 loc) · 2.22 KB
/
remove_bang_from_call.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
88
89
90
91
92
93
94
95
96
97
use lsp_types::TextEdit;
use noirc_errors::{Location, Span};
use noirc_frontend::{node_interner::ReferenceId, QuotedType, Type};
use crate::byte_span_to_range;
use super::CodeActionFinder;
impl<'a> CodeActionFinder<'a> {
pub(super) fn remove_bang_from_call(&mut self, span: Span) {
// If we can't find the referenced function, there's nothing we can do
let Some(ReferenceId::Function(func_id)) =
self.interner.find_referenced(Location::new(span, self.file))
else {
return;
};
// If the return type is Quoted, all is good
let func_meta = self.interner.function_meta(&func_id);
if let Type::Quoted(QuotedType::Quoted) = func_meta.return_type() {
return;
}
// The `!` comes right after the name
let byte_span = span.end() as usize..span.end() as usize + 1;
let Some(range) = byte_span_to_range(self.files, self.file, byte_span) else {
return;
};
let title = "Remove `!` from call".to_string();
let text_edit = TextEdit { range, new_text: "".to_string() };
let code_action = self.new_quick_fix(title, text_edit);
self.code_actions.push(code_action);
}
}
#[cfg(test)]
mod tests {
use tokio::test;
use crate::requests::code_action::tests::assert_code_action;
#[test]
async fn test_removes_bang_from_call() {
let title = "Remove `!` from call";
let src = r#"
fn foo() {}
fn main() {
fo>|<o!();
}
"#;
let expected = r#"
fn foo() {}
fn main() {
foo();
}
"#;
assert_code_action(title, src, expected).await;
}
#[test]
async fn test_removes_bang_from_method_call() {
let title = "Remove `!` from call";
let src = r#"
struct Foo {}
impl Foo {
fn foo(self) {}
}
fn bar(foo: Foo) {
foo.fo>|<o!();
}
"#;
let expected = r#"
struct Foo {}
impl Foo {
fn foo(self) {}
}
fn bar(foo: Foo) {
foo.foo();
}
"#;
assert_code_action(title, src, expected).await;
}
}