Skip to content
Open
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
1 change: 0 additions & 1 deletion crates/lib-core/src/parser/segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ impl ErasedSegment {
}

impl ErasedSegment {
#[allow(clippy::new_ret_no_self, clippy::wrong_self_convention)]
#[track_caller]
pub fn new(&self, segments: Vec<ErasedSegment>) -> ErasedSegment {
match &self.value.kind {
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/rules/aliasing/al01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ FROM foo AS voo
as_keyword.clone().into(),
ReflowSequence::from_around_target(
as_keyword,
rule_cx.parent_stack[0].clone(),
&rule_cx.parent_stack[0],
TargetSide::Both,
rule_cx.config,
)
Expand All @@ -142,7 +142,7 @@ FROM foo AS voo
rule_cx.segment.clone().into(),
ReflowSequence::from_around_target(
&identifier,
rule_cx.parent_stack[0].clone(),
&rule_cx.parent_stack[0],
TargetSide::Before,
rule_cx.config,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/convention/cv05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ WHERE a IS NULL

let fixes = ReflowSequence::from_around_target(
&context.segment,
context.parent_stack[0].clone(),
&context.parent_stack[0],
TargetSide::Both,
context.config,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/layout/lt01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ JOIN bar USING (a)
}

fn eval(&self, context: &RuleContext) -> Vec<LintResult> {
let sequence = ReflowSequence::from_root(context.segment.clone(), context.config);
let sequence = ReflowSequence::from_root(&context.segment, context.config);
sequence
.respace(context.tables, false, Filter::All)
.results()
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/layout/lt02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ FROM foo
}

fn eval(&self, context: &RuleContext) -> Vec<LintResult> {
ReflowSequence::from_root(context.segment.clone(), context.config)
ReflowSequence::from_root(&context.segment, context.config)
.reindent(context.tables)
.results()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/layout/lt03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ FROM foo

ReflowSequence::from_around_target(
&context.segment,
context.parent_stack.first().unwrap().clone(),
context.parent_stack.first().unwrap(),
TargetSide::Both,
context.config,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/layout/lt04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ FROM foo

ReflowSequence::from_around_target(
&context.segment,
context.parent_stack.first().unwrap().clone(),
context.parent_stack.first().unwrap(),
TargetSide::Both,
context.config,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/rules/layout/lt05.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ FROM my_table"#
&[RuleGroups::All, RuleGroups::Core, RuleGroups::Layout]
}
fn eval(&self, context: &RuleContext) -> Vec<LintResult> {
let mut results = ReflowSequence::from_root(context.segment.clone(), context.config)
let mut results = ReflowSequence::from_root(&context.segment, context.config)
.break_long_lines(context.tables)
.results();

Expand Down
4 changes: 3 additions & 1 deletion crates/lib/src/rules/layout/lt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl Rule for RuleLT11 {
fn load_from_config(&self, _config: &AHashMap<String, Value>) -> Result<ErasedRule, String> {
Ok(RuleLT11.erased())
}

fn name(&self) -> &'static str {
"layout.set_operators"
}
Expand Down Expand Up @@ -48,10 +49,11 @@ SELECT 'b' AS col
fn groups(&self) -> &'static [RuleGroups] {
&[RuleGroups::All, RuleGroups::Core, RuleGroups::Layout]
}

fn eval(&self, context: &RuleContext) -> Vec<LintResult> {
ReflowSequence::from_around_target(
&context.segment,
context.parent_stack.first().unwrap().clone(),
context.parent_stack.first().unwrap(),
TargetSide::Both,
context.config,
)
Expand Down
10 changes: 5 additions & 5 deletions crates/lib/src/rules/structure/st08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ use crate::utils::reflow::sequence::{Filter, ReflowSequence, TargetSide};
pub struct RuleST08;

impl RuleST08 {
pub fn remove_unneeded_brackets<'a>(
pub fn remove_unneeded_brackets<'a, 'b>(
&self,
context: &RuleContext<'a>,
context: &'b RuleContext<'a>,
bracketed: Segments,
) -> (ErasedSegment, ReflowSequence<'a>) {
) -> (ErasedSegment, ReflowSequence<'a, 'b>) {
let anchor = &bracketed.get(0, None).unwrap();
let seq = ReflowSequence::from_around_target(
anchor,
context.parent_stack[0].clone(),
&context.parent_stack[0],
TargetSide::Before,
context.config,
)
Expand Down Expand Up @@ -121,7 +121,7 @@ SELECT DISTINCT a, b FROM foo
anchor = Some(modifier[0].clone());
seq = Some(ReflowSequence::from_around_target(
&modifier[0],
context.parent_stack[0].clone(),
&context.parent_stack[0],
TargetSide::After,
context.config,
));
Expand Down
18 changes: 11 additions & 7 deletions crates/lib/src/utils/reflow/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl ReflowPoint {
}
}

pub fn get_indent_segment(&self) -> Option<ErasedSegment> {
pub fn get_indent_segment(&self) -> Option<&ErasedSegment> {
let mut indent = None;
for seg in self.segments.iter().rev() {
if seg
Expand All @@ -116,7 +116,7 @@ impl ReflowPoint {
match seg.get_type() {
SyntaxKind::Newline => return indent,
SyntaxKind::Whitespace => {
indent = Some(seg.clone());
indent = Some(seg);
continue;
}
_ => {}
Expand All @@ -126,7 +126,7 @@ impl ReflowPoint {
.unwrap_or_default()
.contains('\n')
{
return Some(seg.clone());
return Some(seg);
}
}
indent
Expand All @@ -150,7 +150,7 @@ impl ReflowPoint {
}

let seg = self.get_indent_segment();
let consumed_whitespace = get_consumed_whitespace(seg.as_ref());
let consumed_whitespace = get_consumed_whitespace(seg);

if let Some(consumed_whitespace) = consumed_whitespace {
return consumed_whitespace
Expand Down Expand Up @@ -198,7 +198,7 @@ impl ReflowPoint {
let idx = self
.segments
.iter()
.position(|seg| seg == &indent_seg)
.position(|seg| seg == indent_seg)
.unwrap();
return (
vec![LintResult::new(
Expand Down Expand Up @@ -229,14 +229,18 @@ impl ReflowPoint {
let idx = self
.segments
.iter()
.position(|it| it == &indent_seg)
.position(|it| it == indent_seg)
.unwrap();

let description = format!("Expected {}.", indent_description(desired_indent));

let lint_result = LintResult::new(
indent_seg.clone().into(),
vec![LintFix::replace(indent_seg, vec![new_indent.clone()], None)],
vec![LintFix::replace(
indent_seg.clone(),
vec![new_indent.clone()],
None,
)],
description.into(),
None,
);
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/utils/reflow/rebreak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ mod tests {

let root = parse_ansi_string(raw_sql_in);
let config = <_>::default();
let seq = ReflowSequence::from_root(root, &config);
let seq = ReflowSequence::from_root(&root, &config);
let new_seq = seq.rebreak(&tables);

assert_eq!(new_seq.raw(), raw_sql_out);
Expand All @@ -609,7 +609,7 @@ mod tests {
let root = parse_ansi_string(raw_sql_in);
let target = &root.get_raw_segments()[target_idx];
let config = <_>::default();
let seq = ReflowSequence::from_around_target(target, root, TargetSide::Both, &config);
let seq = ReflowSequence::from_around_target(target, &root, TargetSide::Both, &config);

assert_eq!(seq.raw(), seq_sql_in);

Expand Down
6 changes: 3 additions & 3 deletions crates/lib/src/utils/reflow/reindent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,12 @@ fn deduce_line_current_indent(
} else {
for segment in elements[0].segments().iter().rev() {
if segment.is_type(SyntaxKind::Whitespace) && !segment.is_templated() {
indent_seg = Some(segment.clone());
indent_seg = Some(segment);
break;
}
}

if let Some(ref seg) = indent_seg
if let Some(seg) = indent_seg
&& !seg.is_type(SyntaxKind::Whitespace)
{
indent_seg = None;
Expand Down Expand Up @@ -1548,7 +1548,7 @@ mod tests {
for (raw_sql_in, elem_idx, indent_out) in cases {
let root = parse_ansi_string(raw_sql_in);
let config = <_>::default();
let seq = ReflowSequence::from_root(root, &config);
let seq = ReflowSequence::from_root(&root, &config);
let elem = seq.elements()[elem_idx].as_point().unwrap();

assert_eq!(indent_out, elem.get_indent().as_deref());
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/utils/reflow/respace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ mod tests {
for (raw_sql_in, (strip_newlines, filter), raw_sql_out) in cases {
let root = parse_ansi_string(raw_sql_in);
let config = <_>::default();
let seq = ReflowSequence::from_root(root, &config);
let seq = ReflowSequence::from_root(&root, &config);

let new_seq = seq.respace(&tables, strip_newlines, filter);
assert_eq!(new_seq.raw(), raw_sql_out);
Expand Down Expand Up @@ -682,7 +682,7 @@ mod tests {

let root = parse_ansi_string(raw_sql_in);
let config = <_>::default();
let seq = ReflowSequence::from_root(root.clone(), &config);
let seq = ReflowSequence::from_root(&root, &config);
let pnt = seq.elements()[point_idx].as_point().unwrap();

let (results, new_pnt) = pnt.respace_point(
Expand Down
30 changes: 15 additions & 15 deletions crates/lib/src/utils/reflow/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use super::reindent::{construct_single_indent, lint_indent_points, lint_line_len
use crate::core::config::FluffConfig;
use crate::core::rules::LintResult;

pub struct ReflowSequence<'a> {
root_segment: ErasedSegment,
pub struct ReflowSequence<'a, 'b> {
root_segment: &'b ErasedSegment,
elements: ReflowSequenceType,
lint_results: Vec<LintResult>,
reflow_config: &'a ReflowConfig,
Expand All @@ -34,7 +34,7 @@ pub enum ReflowInsertPosition {
Before,
}

impl<'a> ReflowSequence<'a> {
impl<'a, 'b> ReflowSequence<'a, 'b> {
pub fn raw(&self) -> String {
self.elements.iter().map(|it| it.raw()).join("")
}
Expand All @@ -50,8 +50,8 @@ impl<'a> ReflowSequence<'a> {
.collect()
}

pub fn from_root(root_segment: ErasedSegment, config: &'a FluffConfig) -> Self {
let depth_map = DepthMap::from_parent(&root_segment).into();
pub fn from_root(root_segment: &'b ErasedSegment, config: &'a FluffConfig) -> Self {
let depth_map = DepthMap::from_parent(root_segment).into();

Self::from_raw_segments(
root_segment.get_raw_segments(),
Expand All @@ -63,13 +63,13 @@ impl<'a> ReflowSequence<'a> {

pub fn from_raw_segments(
segments: Vec<ErasedSegment>,
root_segment: ErasedSegment,
root_segment: &'b ErasedSegment,
config: &'a FluffConfig,
depth_map: Option<DepthMap>,
) -> ReflowSequence<'a> {
) -> ReflowSequence<'a, 'b> {
let reflow_config = config.reflow();
let depth_map = depth_map.unwrap_or_else(|| {
DepthMap::from_raws_and_root(segments.clone().into_iter(), &root_segment)
DepthMap::from_raws_and_root(segments.clone().into_iter(), root_segment)
});
let elements = Self::elements_from_raw_segments(segments, &depth_map, reflow_config);

Expand Down Expand Up @@ -131,10 +131,10 @@ impl<'a> ReflowSequence<'a> {

pub fn from_around_target(
target_segment: &ErasedSegment,
root_segment: ErasedSegment,
root_segment: &'b ErasedSegment,
sides: TargetSide,
config: &'a FluffConfig,
) -> ReflowSequence<'a> {
) -> ReflowSequence<'a, 'b> {
let all_raws = root_segment.get_raw_segments();
let target_raws = target_segment.get_raw_segments();

Expand Down Expand Up @@ -220,7 +220,7 @@ impl<'a> ReflowSequence<'a> {
.unwrap_or_else(|| panic!("Target [{target:?}] not found in ReflowSequence."))
}

pub fn without(self, target: &ErasedSegment) -> ReflowSequence<'a> {
pub fn without(self, target: &ErasedSegment) -> ReflowSequence<'a, 'b> {
let removal_idx = self.find_element_idx_with(target);
if removal_idx == 0 || removal_idx == self.elements.len() - 1 {
panic!("Unexpected removal at one end of a ReflowSequence.");
Expand All @@ -241,7 +241,7 @@ impl<'a> ReflowSequence<'a> {

ReflowSequence {
elements: new_elements,
root_segment: self.root_segment.clone(),
root_segment: self.root_segment,
lint_results: vec![LintResult::new(
target.clone().into(),
vec![LintFix::delete(target.clone())],
Expand All @@ -263,7 +263,7 @@ impl<'a> ReflowSequence<'a> {
tables,
pre,
post,
&self.root_segment,
self.root_segment,
lint_results,
strip_newlines,
"before",
Expand Down Expand Up @@ -314,7 +314,7 @@ impl<'a> ReflowSequence<'a> {
}

// Delegate to the rebreak algorithm
let (elem_buff, lint_results) = rebreak_sequence(tables, self.elements, &self.root_segment);
let (elem_buff, lint_results) = rebreak_sequence(tables, self.elements, self.root_segment);

ReflowSequence {
root_segment: self.root_segment,
Expand Down Expand Up @@ -419,7 +419,7 @@ impl<'a> ReflowSequence<'a> {
let (elements, length_results) = lint_line_length(
tables,
&self.elements,
&self.root_segment,
self.root_segment,
&single_indent,
self.reflow_config.max_line_length,
self.reflow_config.allow_implicit_indents,
Expand Down
Loading