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
21 changes: 21 additions & 0 deletions fixtures/small/other_requires_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ThingToCall.call(
require: "not a regular require",
other: "value"
)

require = bees!
foo(require: bar)

{
:require => foo,
:other => 1
}

"require#{foo}"

x = {require: 1, other: 2}
y = {require:, other:}

def thing(require:)
require
end
21 changes: 21 additions & 0 deletions fixtures/small/other_requires_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ThingToCall.call(
require: "not a regular require",
other: "value"
)

require = bees!
foo(require: bar)

{
:require => foo,
:other => 1
}

"require#{foo}"

x = {require: 1, other: 2}
y = {require:, other:}

def thing(require:)
require
end
15 changes: 14 additions & 1 deletion librubyfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,19 @@ pub fn format_ident(ps: &mut ParserState, ident: Ident) {
}
}

pub fn format_method_ident(ps: &mut ParserState, ident: Ident) {
if ps.at_start_of_line() {
ps.emit_indent();
}

ps.on_line(ident.2.0);
ps.emit_method_name(ident.1);

if ps.at_start_of_line() {
ps.emit_newline();
}
}

pub fn format_const(ps: &mut ParserState, c: Const) {
if ps.at_start_of_line() {
ps.emit_indent();
Expand Down Expand Up @@ -2496,7 +2509,7 @@ fn format_call_chain_elements(
if ident.1 == ".()" {
ps.emit_ident(".");
} else {
format_ident(ps, ident);
format_method_ident(ps, ident);
}
ps.shift_comments();
}
Expand Down
7 changes: 2 additions & 5 deletions librubyfmt/src/format_prism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,11 +1747,8 @@ fn format_call_node<'src>(
} else {
method_name
};
handle_string_at_offset(
ps,
method_ident,
start_loc_for_call_node_in_chain(&call_node),
);
ps.at_offset(start_loc_for_call_node_in_chain(&call_node));
ps.emit_method_name(method_ident);
}

// Calls with only empty parens (`foo ()`) are treated as calls without args
Expand Down
4 changes: 2 additions & 2 deletions librubyfmt/src/intermediary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ impl<'src> Intermediary<'src> {
prev.set_gets_indented()
}
}
ConcreteLineToken::DirectPart { part } => {
if part == "require" && self.tokens.last().map(|t| t.is_indent()).unwrap_or(false) {
ConcreteLineToken::MethodName { name } => {
if name == "require" && self.tokens.last().map(|t| t.is_indent()).unwrap_or(false) {
self.current_line_metadata.set_has_require();
}
}
Expand Down
6 changes: 5 additions & 1 deletion librubyfmt/src/line_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum ConcreteLineToken<'src> {
ModKeyword { contents: &'static str },
ConditionalKeyword { contents: &'static str },
DirectPart { part: Cow<'src, str> },
MethodName { name: Cow<'src, str> },
CommaSpace,
Comma,
Space,
Expand Down Expand Up @@ -81,6 +82,7 @@ impl<'src> ConcreteLineToken<'src> {
Self::DefKeyword => Cow::Borrowed("def"),
Self::ModuleKeyword => Cow::Borrowed("module"),
Self::DirectPart { part } => part,
Self::MethodName { name } => name,
Self::CommaSpace => Cow::Borrowed(", "),
Self::Comma => Cow::Borrowed(","),
Self::Space => Cow::Borrowed(" "),
Expand Down Expand Up @@ -126,7 +128,9 @@ impl<'src> ConcreteLineToken<'src> {
Keyword { keyword: contents }
| ModKeyword { contents }
| ConditionalKeyword { contents } => contents.len(),
Op { op: contents } | DirectPart { part: contents } => contents.len(),
Op { op: contents } | DirectPart { part: contents } | MethodName { name: contents } => {
contents.len()
}
LTStringContent { content: contents }
| Comment { contents }
| HeredocClose { symbol: contents } => contents.len(),
Expand Down
4 changes: 4 additions & 0 deletions librubyfmt/src/parser_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ impl<'src> ParserState<'src> {
self.push_concrete_token(ConcreteLineToken::DirectPart { part: ident.into() });
}

pub(crate) fn emit_method_name(&mut self, name: impl Into<Cow<'src, str>>) {
self.push_concrete_token(ConcreteLineToken::MethodName { name: name.into() });
}

pub(crate) fn emit_newline(&mut self) {
self.shift_comments();
self.push_concrete_token(ConcreteLineToken::HardNewLine);
Expand Down