Skip to content

Commit dcd7df9

Browse files
giacomocavalierilpil
authored andcommitted
make sure to remove all echos
1 parent cfd6d29 commit dcd7df9

4 files changed

+74
-21
lines changed

compiler-core/src/language_server/code_action.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -5467,7 +5467,8 @@ pub struct RemoveEcho<'a> {
54675467
module: &'a Module,
54685468
params: &'a CodeActionParams,
54695469
edits: TextEdits<'a>,
5470-
echo_span_to_delete: Option<SrcSpan>,
5470+
is_hovering_echo: bool,
5471+
echo_spans_to_delete: Vec<SrcSpan>,
54715472
// We need to keep a reference to the two latest pipeline assignments we
54725473
// run into to properly delete an echo that's inside a pipeline.
54735474
latest_pipe_step: Option<SrcSpan>,
@@ -5484,7 +5485,8 @@ impl<'a> RemoveEcho<'a> {
54845485
module,
54855486
params,
54865487
edits: TextEdits::new(line_numbers),
5487-
echo_span_to_delete: None,
5488+
is_hovering_echo: false,
5489+
echo_spans_to_delete: vec![],
54885490
latest_pipe_step: None,
54895491
second_to_latest_pipe_step: None,
54905492
}
@@ -5493,14 +5495,18 @@ impl<'a> RemoveEcho<'a> {
54935495
pub fn code_actions(mut self) -> Vec<CodeAction> {
54945496
self.visit_typed_module(&self.module.ast);
54955497

5496-
let Some(echo_span_to_delete) = self.echo_span_to_delete else {
5498+
// We only want to trigger the action if we're over one of the echos in
5499+
// the module
5500+
if !self.is_hovering_echo {
54975501
return vec![];
54985502
};
54995503

5500-
self.edits.delete(echo_span_to_delete);
5504+
for span in self.echo_spans_to_delete {
5505+
self.edits.delete(span);
5506+
}
55015507

55025508
let mut action = Vec::with_capacity(1);
5503-
CodeActionBuilder::new("Remove `echo`")
5509+
CodeActionBuilder::new("Remove all `echo`s from this module")
55045510
.kind(CodeActionKind::REFACTOR_REWRITE)
55055511
.changes(self.params.text_document.uri.clone(), self.edits.edits)
55065512
.preferred(false)
@@ -5527,15 +5533,15 @@ impl<'ast> ast::visit::Visit<'ast> for RemoveEcho<'ast> {
55275533
// ```
55285534
//
55295535
let echo_range = self.edits.src_span_to_lsp_range(*location);
5530-
if !within(self.params.range, echo_range) {
5531-
return;
5536+
if within(self.params.range, echo_range) {
5537+
self.is_hovering_echo = true;
55325538
}
55335539

55345540
if let Some(expression) = expression {
55355541
// If there's an expression we delete everything we find until its
55365542
// start (excluded).
55375543
let span_to_delete = SrcSpan::new(location.start, expression.location().start);
5538-
self.echo_span_to_delete = Some(span_to_delete);
5544+
self.echo_spans_to_delete.push(span_to_delete);
55395545
} else {
55405546
// Othwerise we know we're inside a pipeline, we take the closest step
55415547
// that is not echo itself and delete everything from its end until the
@@ -5551,7 +5557,7 @@ impl<'ast> ast::visit::Visit<'ast> for RemoveEcho<'ast> {
55515557
.or(self.second_to_latest_pipe_step);
55525558
if let Some(step_preceding_echo) = step_preceding_echo {
55535559
let span_to_delete = SrcSpan::new(step_preceding_echo.end, location.start + 4);
5554-
self.echo_span_to_delete = Some(span_to_delete);
5560+
self.echo_spans_to_delete.push(span_to_delete);
55555561
}
55565562
}
55575563

compiler-core/src/language_server/tests/action.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const INLINE_VARIABLE: &str = "Inline variable";
8080
const CONVERT_TO_PIPE: &str = "Convert to pipe";
8181
const INTERPOLATE_STRING: &str = "Interpolate string";
8282
const FILL_UNUSED_FIELDS: &str = "Fill unused fields";
83-
const REMOVE_ECHO: &str = "Remove `echo`";
83+
const REMOVE_ALL_ECHOS_FROM_THIS_MODULE: &str = "Remove all `echo`s from this module";
8484

8585
macro_rules! assert_code_action {
8686
($title:expr, $code:literal, $range:expr $(,)?) => {
@@ -204,7 +204,7 @@ pub fn main() {
204204
#[test]
205205
fn remove_echo() {
206206
assert_code_action!(
207-
REMOVE_ECHO,
207+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
208208
"pub fn main() {
209209
echo 1 + 2
210210
}",
@@ -215,7 +215,7 @@ fn remove_echo() {
215215
#[test]
216216
fn remove_echo_selecting_expression() {
217217
assert_code_action!(
218-
REMOVE_ECHO,
218+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
219219
"pub fn main() {
220220
echo 1 + 2
221221
}",
@@ -226,7 +226,7 @@ fn remove_echo_selecting_expression() {
226226
#[test]
227227
fn remove_echo_as_function_arg() {
228228
assert_code_action!(
229-
REMOVE_ECHO,
229+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
230230
"pub fn main() {
231231
wibble([], echo 1 + 2)
232232
}",
@@ -237,7 +237,7 @@ fn remove_echo_as_function_arg() {
237237
#[test]
238238
fn remove_echo_in_pipeline_step() {
239239
assert_code_action!(
240-
REMOVE_ECHO,
240+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
241241
"pub fn main() {
242242
[1, 2, 3]
243243
|> echo
@@ -250,7 +250,7 @@ fn remove_echo_in_pipeline_step() {
250250
#[test]
251251
fn remove_echo_in_single_line_pipeline_step() {
252252
assert_code_action!(
253-
REMOVE_ECHO,
253+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
254254
"pub fn main() {
255255
[1, 2, 3] |> echo |> wibble
256256
}",
@@ -261,7 +261,7 @@ fn remove_echo_in_single_line_pipeline_step() {
261261
#[test]
262262
fn remove_echo_last_in_long_pipeline_step() {
263263
assert_code_action!(
264-
REMOVE_ECHO,
264+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
265265
"pub fn main() {
266266
[1, 2, 3]
267267
|> wibble
@@ -274,7 +274,7 @@ fn remove_echo_last_in_long_pipeline_step() {
274274
#[test]
275275
fn remove_echo_last_in_short_pipeline_step() {
276276
assert_code_action!(
277-
REMOVE_ECHO,
277+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
278278
"pub fn main() {
279279
[1, 2, 3]
280280
|> echo
@@ -286,7 +286,7 @@ fn remove_echo_last_in_short_pipeline_step() {
286286
#[test]
287287
fn remove_echo_before_pipeline() {
288288
assert_code_action!(
289-
REMOVE_ECHO,
289+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
290290
"pub fn main() {
291291
echo [1, 2, 3] |> wibble
292292
}",
@@ -297,7 +297,7 @@ fn remove_echo_before_pipeline() {
297297
#[test]
298298
fn remove_echo_before_pipeline_selecting_step() {
299299
assert_code_action!(
300-
REMOVE_ECHO,
300+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
301301
"pub fn main() {
302302
echo [1, 2, 3] |> wibble
303303
}",
@@ -306,16 +306,29 @@ fn remove_echo_before_pipeline_selecting_step() {
306306
}
307307

308308
#[test]
309-
fn remove_echo_removes_closest_one() {
309+
fn remove_echo_removes_all_echos() {
310310
assert_code_action!(
311-
REMOVE_ECHO,
311+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
312312
"pub fn main() {
313313
echo wibble(echo 1, 2)
314314
}",
315315
find_position_of("echo").nth_occurrence(2).to_selection()
316316
);
317317
}
318318

319+
#[test]
320+
fn remove_echo_removes_all_echos_1() {
321+
assert_code_action!(
322+
REMOVE_ALL_ECHOS_FROM_THIS_MODULE,
323+
"pub fn main() {
324+
echo 1 |> echo |> echo |> wibble |> echo
325+
echo wibble(echo 1, echo 2)
326+
echo 1
327+
}",
328+
find_position_of("echo").nth_occurrence(2).to_selection()
329+
);
330+
}
331+
319332
#[test]
320333
fn split_string() {
321334
assert_code_action!(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "pub fn main() {\n echo wibble(echo 1, 2)\n}"
4+
---
5+
----- BEFORE ACTION
6+
pub fn main() {
7+
echo wibble(echo 1, 2)
8+
9+
}
10+
11+
12+
----- AFTER ACTION
13+
pub fn main() {
14+
wibble(1, 2)
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
source: compiler-core/src/language_server/tests/action.rs
3+
expression: "pub fn main() {\n echo 1 |> echo |> echo |> wibble |> echo\n echo wibble(echo 1, echo 2)\n echo 1\n}"
4+
---
5+
----- BEFORE ACTION
6+
pub fn main() {
7+
echo 1 |> echo |> echo |> wibble |> echo
8+
9+
echo wibble(echo 1, echo 2)
10+
echo 1
11+
}
12+
13+
14+
----- AFTER ACTION
15+
pub fn main() {
16+
1 |> wibble
17+
wibble(1, 2)
18+
1
19+
}

0 commit comments

Comments
 (0)