Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions fixtures/small/attr_write_array_comments_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
a.items = [
# First item comment
1,
# Second item comment
2
]

x.list = [
"value"
# trailing
]

foo.bar.values = [
# inside array
:a,
:b
]
17 changes: 17 additions & 0 deletions fixtures/small/attr_write_array_comments_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
a.items = [
# First item comment
1,
# Second item comment
2
]

x.list = [
"value"
# trailing
]

foo.bar.values = [
# inside array
:a,
:b
]
32 changes: 32 additions & 0 deletions fixtures/small/attr_write_hash_comments_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
a.bees = {
# Stuff
honey: "hive"
}

x.config = {
# First setting
debug: true,
# Second setting
verbose: false
}

y.options = {
enabled: true
# trailing comment
}

obj.data = {
# Comment line 1
# Comment line 2
key: "value"
}

foo.bar.baz = {
# inside hash
item: 123
}

maybe&.setting = {
# safe navigation comment
value: nil
}
32 changes: 32 additions & 0 deletions fixtures/small/attr_write_hash_comments_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
a.bees = {
# Stuff
honey: "hive"
}

x.config = {
# First setting
debug: true,
# Second setting
verbose: false
}

y.options = {
enabled: true
# trailing comment
}

obj.data = {
# Comment line 1
# Comment line 2
key: "value"
}

foo.bar.baz = {
# inside hash
item: 123
}

maybe&.setting = {
# safe navigation comment
value: nil
}
12 changes: 11 additions & 1 deletion librubyfmt/src/format_prism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,7 +1716,17 @@ fn format_call_node<'src>(
skip_attr_write_value: bool,
) {
let method_name = const_to_str(call_node.name());
let end_offset = call_node.location().end_offset();
// When we skip the attr_write value (because it will be formatted separately),
// we should only wind to the end of the method name, not the full call.
// Otherwise we'd extract comments from inside the value prematurely.
let end_offset = if skip_attr_write_value {
call_node
.message_loc()
.expect("Attribute writes must have a message")
.end_offset()
} else {
call_node.location().end_offset()
};
let is_dot_call = method_name == "call" && call_node.message_loc().is_none(); // e.g. `a.()`

// Only treat [] and []= as aref syntax when there's no explicit call operator.
Expand Down