Skip to content

Add partial selection for generate_getter_or_setter #20353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion crates/ide-assists/src/handlers/generate_getter_or_setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ fn extract_and_parse_record_fields(
let info_of_record_fields_in_selection = ele
.fields()
.filter_map(|record_field| {
if selection_range.contains_range(record_field.syntax().text_range()) {
let field_range = record_field.syntax().text_range();
if selection_range.intersect(field_range).is_some_and(|rng| !rng.is_empty()) {
let record_field_info = parse_record_field(record_field, assist_type)?;
field_names.push(record_field_info.fn_name.clone());
return Some(record_field_info);
Expand Down Expand Up @@ -894,6 +895,37 @@ struct Context {
count: usize,
}

impl Context {
fn data(&self) -> &Data {
&self.data
}

fn $0count(&self) -> &usize {
&self.count
}
}
"#,
);
}

#[test]
fn test_generate_multiple_getters_from_partial_selection() {
check_assist(
generate_getter,
r#"
struct Context {
data$0: Data,
count$0: usize,
other: usize,
}
"#,
r#"
struct Context {
data: Data,
count: usize,
other: usize,
}

impl Context {
fn data(&self) -> &Data {
&self.data
Expand Down
Loading