Skip to content

Commit

Permalink
Don't unnecessariliy render expressions in mulitline checking
Browse files Browse the repository at this point in the history
  • Loading branch information
reese authored Aug 19, 2023
1 parent 9b3e904 commit 98d309c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 23 deletions.
10 changes: 0 additions & 10 deletions librubyfmt/src/parser_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,13 +1058,3 @@ impl BaseParserState {
pub fn line_difference_requires_newline(to_line: LineNumber, from_line: LineNumber) -> bool {
(to_line > from_line) && (to_line - from_line >= 2)
}

pub fn will_render_as_multiline<'a>(ps: &BaseParserState, f: RenderFunc) -> bool {
let mut next_ps = BaseParserState::new_with_depth_stack_from(ps);
// Ignore commments when determining line length
next_ps.with_suppress_comments(true, f);
let data = next_ps.render_to_buffer();

let s = str::from_utf8(&data).expect("string is utf8").to_string();
s.trim().contains('\n')
}
16 changes: 3 additions & 13 deletions librubyfmt/src/render_targets.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::delimiters::BreakableDelims;
use crate::file_comments::FileComments;
use crate::format::format_expression;
use crate::line_tokens::{AbstractLineToken, ConcreteLineToken, ConcreteLineTokenAndTargets};
use crate::parser_state::{will_render_as_multiline, BaseParserState, FormattingContext};
use crate::parser_state::FormattingContext;
use crate::ripper_tree_types::{Block, CallChainElement, Expression, StringLiteral};
use crate::types::LineNumber;
use std::collections::HashSet;
Expand Down Expand Up @@ -306,16 +304,8 @@ impl AbstractTokenTarget for BreakableCallChainEntry {

// If the first item in the chain is a multiline expression (like a hash or array),
// ignore it when checking line length
if let Some(CallChainElement::Expression(expr)) = call_chain_to_check.first() {
let is_multiline_expression = will_render_as_multiline(
&BaseParserState::new(FileComments::default()),
Box::new(|ps| {
format_expression(ps, expr.as_ref().clone());
}),
);
if is_multiline_expression {
call_chain_to_check.remove(0);
}
if let Some(CallChainElement::Expression(..)) = call_chain_to_check.first() {
call_chain_to_check.remove(0);
}

let all_element_locations = call_chain_to_check
Expand Down

0 comments on commit 98d309c

Please sign in to comment.