feat: Add an assist to extract an expression into a constant

This commit is contained in:
Giga Bowser 2024-12-09 16:23:36 -05:00
parent 17e482b1a9
commit 1979d3f9b5
3 changed files with 847 additions and 189 deletions

File diff suppressed because it is too large Load diff

View file

@ -363,6 +363,7 @@ pub fn test_some_range(a: int) -> bool {
expect![[r#"
Convert integer base
Extract into variable
Extract into constant
Extract into function
Replace if let with match
"#]]
@ -392,6 +393,7 @@ pub fn test_some_range(a: int) -> bool {
expect![[r#"
Convert integer base
Extract into variable
Extract into constant
Extract into function
Replace if let with match
"#]]
@ -406,6 +408,7 @@ pub fn test_some_range(a: int) -> bool {
expect![[r#"
Extract into variable
Extract into constant
Extract into function
"#]]
.assert_eq(&expected);
@ -440,7 +443,7 @@ pub fn test_some_range(a: int) -> bool {
{
let assists = assists(&db, &cfg, AssistResolveStrategy::None, frange.into());
assert_eq!(2, assists.len());
assert_eq!(3, assists.len());
let mut assists = assists.into_iter();
let extract_into_variable_assist = assists.next().unwrap();
@ -459,6 +462,22 @@ pub fn test_some_range(a: int) -> bool {
"#]]
.assert_debug_eq(&extract_into_variable_assist);
let extract_into_constant_assist = assists.next().unwrap();
expect![[r#"
Assist {
id: AssistId(
"extract_constant",
RefactorExtract,
),
label: "Extract into constant",
group: None,
target: 59..60,
source_change: None,
command: None,
}
"#]]
.assert_debug_eq(&extract_into_constant_assist);
let extract_into_function_assist = assists.next().unwrap();
expect![[r#"
Assist {
@ -486,7 +505,7 @@ pub fn test_some_range(a: int) -> bool {
}),
frange.into(),
);
assert_eq!(2, assists.len());
assert_eq!(3, assists.len());
let mut assists = assists.into_iter();
let extract_into_variable_assist = assists.next().unwrap();
@ -505,6 +524,22 @@ pub fn test_some_range(a: int) -> bool {
"#]]
.assert_debug_eq(&extract_into_variable_assist);
let extract_into_constant_assist = assists.next().unwrap();
expect![[r#"
Assist {
id: AssistId(
"extract_constant",
RefactorExtract,
),
label: "Extract into constant",
group: None,
target: 59..60,
source_change: None,
command: None,
}
"#]]
.assert_debug_eq(&extract_into_constant_assist);
let extract_into_function_assist = assists.next().unwrap();
expect![[r#"
Assist {
@ -532,7 +567,7 @@ pub fn test_some_range(a: int) -> bool {
}),
frange.into(),
);
assert_eq!(2, assists.len());
assert_eq!(3, assists.len());
let mut assists = assists.into_iter();
let extract_into_variable_assist = assists.next().unwrap();
@ -594,6 +629,22 @@ pub fn test_some_range(a: int) -> bool {
"#]]
.assert_debug_eq(&extract_into_variable_assist);
let extract_into_constant_assist = assists.next().unwrap();
expect![[r#"
Assist {
id: AssistId(
"extract_constant",
RefactorExtract,
),
label: "Extract into constant",
group: None,
target: 59..60,
source_change: None,
command: None,
}
"#]]
.assert_debug_eq(&extract_into_constant_assist);
let extract_into_function_assist = assists.next().unwrap();
expect![[r#"
Assist {
@ -613,7 +664,7 @@ pub fn test_some_range(a: int) -> bool {
{
let assists = assists(&db, &cfg, AssistResolveStrategy::All, frange.into());
assert_eq!(2, assists.len());
assert_eq!(3, assists.len());
let mut assists = assists.into_iter();
let extract_into_variable_assist = assists.next().unwrap();
@ -675,6 +726,69 @@ pub fn test_some_range(a: int) -> bool {
"#]]
.assert_debug_eq(&extract_into_variable_assist);
let extract_into_constant_assist = assists.next().unwrap();
expect![[r#"
Assist {
id: AssistId(
"extract_constant",
RefactorExtract,
),
label: "Extract into constant",
group: None,
target: 59..60,
source_change: Some(
SourceChange {
source_file_edits: {
FileId(
0,
): (
TextEdit {
indels: [
Indel {
insert: "const",
delete: 45..47,
},
Indel {
insert: "VAR_NAME:",
delete: 48..60,
},
Indel {
insert: "i32",
delete: 61..81,
},
Indel {
insert: "=",
delete: 82..86,
},
Indel {
insert: "5;\n if let 2..6 = VAR_NAME {\n true\n } else {\n false\n }",
delete: 87..108,
},
],
},
Some(
SnippetEdit(
[
(
0,
51..51,
),
],
),
),
),
},
file_system_edits: [],
is_snippet: true,
},
),
command: Some(
Rename,
),
}
"#]]
.assert_debug_eq(&extract_into_constant_assist);
let extract_into_function_assist = assists.next().unwrap();
expect![[r#"
Assist {

View file

@ -932,6 +932,24 @@ enum TheEnum {
)
}
#[test]
fn doctest_extract_constant() {
check_doc_test(
"extract_constant",
r#####"
fn main() {
$0(1 + 2)$0 * 4;
}
"#####,
r#####"
fn main() {
const $0VAR_NAME: i32 = 1 + 2;
VAR_NAME * 4;
}
"#####,
)
}
#[test]
fn doctest_extract_expressions_from_format_string() {
check_doc_test(