|
| 1 | +//! Where lines break: before a token, and after one that asks for a break. |
| 2 | +
|
| 3 | +use crate::layout::_helpers::plan::Plan; |
| 4 | +use crate::layout::_helpers::tokens::{Token, TokenKind}; |
| 5 | +use crate::layout::constants::{ |
| 6 | + BY_CLAUSE_WORDS, CLAUSE_WORDS, INDENT, INLINE_WIDTH, JOIN_MODIFIERS, NON_CALL_WORDS, |
| 7 | + SELECT_MODIFIERS, SET_OPERATION_MODIFIERS, SET_OPERATIONS, |
| 8 | +}; |
| 9 | +use crate::layout::models::{Case, Clause, Frame, Walk}; |
| 10 | + |
| 11 | +/// The walk over a statement, which breaks its lines when `broken`. |
| 12 | +pub(crate) fn start(broken: bool) -> Walk { |
| 13 | + Walk { |
| 14 | + frames: vec![Frame { |
| 15 | + query: true, |
| 16 | + broken, |
| 17 | + inline: !broken, |
| 18 | + call: false, |
| 19 | + base: 0, |
| 20 | + close: 0, |
| 21 | + clause: Clause::Other, |
| 22 | + item_pending: false, |
| 23 | + }], |
| 24 | + cases: Vec::new(), |
| 25 | + line_indent: 0, |
| 26 | + forced: None, |
| 27 | + laid_out: String::new(), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Walk { |
| 32 | + /// Appends `tokens[index]`, on a new line at `level` or after its own spacing. |
| 33 | + pub(crate) fn push_token(&mut self, tokens: &[Token<'_>], index: usize, level: Option<usize>) { |
| 34 | + let token = &tokens[index]; |
| 35 | + match (index, level) { |
| 36 | + (0, _) => {} |
| 37 | + (_, Some(level)) => { |
| 38 | + self.laid_out.push('\n'); |
| 39 | + self.laid_out.push_str(&INDENT.repeat(level)); |
| 40 | + self.line_indent = level; |
| 41 | + } |
| 42 | + (_, None) if token.spaced => self.laid_out.push(' '), |
| 43 | + (_, None) => {} |
| 44 | + } |
| 45 | + self.laid_out.push_str(token.text); |
| 46 | + } |
| 47 | + |
| 48 | + /// The indentation level of the new line `tokens[index]` starts, or `None` to keep its line. |
| 49 | + pub(crate) fn line_break(&mut self, tokens: &[Token<'_>], index: usize) -> Option<usize> { |
| 50 | + if let Some((_, level)) = self.forced.filter(|(at, _)| *at == index) { |
| 51 | + return Some(level); |
| 52 | + } |
| 53 | + let token = &tokens[index]; |
| 54 | + let depth = self.frames.len(); |
| 55 | + let case = self.cases.last().filter(|case| case.depth == depth); |
| 56 | + let frame = self.frames.last_mut()?; |
| 57 | + if token.is_symbol(")") { |
| 58 | + return (depth > 1 && frame.query && frame.broken).then_some(frame.close); |
| 59 | + } |
| 60 | + if frame.inline { |
| 61 | + return None; |
| 62 | + } |
| 63 | + if let Some(level) = case.and_then(|case| case_break(token, case)) { |
| 64 | + return Some(level); |
| 65 | + } |
| 66 | + if !frame.query || !frame.broken { |
| 67 | + return None; |
| 68 | + } |
| 69 | + if frame.item_pending { |
| 70 | + return frame.first_item_break(tokens, index); |
| 71 | + } |
| 72 | + (starts_clause(tokens, index) || is_set_operation(tokens, index)).then_some(frame.base) |
| 73 | + } |
| 74 | + |
| 75 | + /// Opens or closes what `tokens[index]` opens or closes, and the break it asks for after it. |
| 76 | + pub(crate) fn after_token( |
| 77 | + &mut self, |
| 78 | + tokens: &[Token<'_>], |
| 79 | + index: usize, |
| 80 | + plan: &Plan, |
| 81 | + ) -> Option<(usize, usize)> { |
| 82 | + let token = &tokens[index]; |
| 83 | + match token { |
| 84 | + token if token.is_symbol("(") => self.open_frame(tokens, index, plan), |
| 85 | + token if token.is_symbol(")") => { |
| 86 | + if self.frames.len() > 1 { |
| 87 | + self.frames.pop(); |
| 88 | + } |
| 89 | + None |
| 90 | + } |
| 91 | + token if token.is("CASE") => { |
| 92 | + let frame = self.frames.last()?; |
| 93 | + let broken = !frame.inline |
| 94 | + && !frame.call |
| 95 | + && plan.width(index, plan.case_end(index)) > INLINE_WIDTH; |
| 96 | + self.cases.push(Case { |
| 97 | + broken, |
| 98 | + indent: self.line_indent, |
| 99 | + depth: self.frames.len(), |
| 100 | + }); |
| 101 | + None |
| 102 | + } |
| 103 | + token if token.is("END") => { |
| 104 | + self.cases.pop(); |
| 105 | + None |
| 106 | + } |
| 107 | + token if token.kind == TokenKind::LineComment => Some((index + 1, self.line_indent)), |
| 108 | + _ => self.frames.last_mut()?.clause_break(tokens, index), |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// Opens the frame of the parenthesis at `tokens[index]`, breaking inside a long query. |
| 113 | + fn open_frame( |
| 114 | + &mut self, |
| 115 | + tokens: &[Token<'_>], |
| 116 | + index: usize, |
| 117 | + plan: &Plan, |
| 118 | + ) -> Option<(usize, usize)> { |
| 119 | + let frame = self.frames.last()?; |
| 120 | + let query = plan.opens_query(tokens, index); |
| 121 | + let broken = query && !frame.inline && plan.width(index, plan.close(index)) > INLINE_WIDTH; |
| 122 | + let base = self.line_indent + 1; |
| 123 | + let opened = Frame { |
| 124 | + query, |
| 125 | + broken, |
| 126 | + inline: frame.inline || (query && !broken), |
| 127 | + call: !query && (frame.call || calls(tokens, index)), |
| 128 | + base, |
| 129 | + close: self.line_indent, |
| 130 | + clause: Clause::Other, |
| 131 | + item_pending: false, |
| 132 | + }; |
| 133 | + self.frames.push(opened); |
| 134 | + broken.then_some((index + 1, base)) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl Frame { |
| 139 | + /// The line the select list's first item starts, after any `DISTINCT` or `DISTINCT ON (…)`. |
| 140 | + fn first_item_break(&mut self, tokens: &[Token<'_>], index: usize) -> Option<usize> { |
| 141 | + let token = &tokens[index]; |
| 142 | + let modifier = SELECT_MODIFIERS.iter().any(|word| token.is(word)) |
| 143 | + || (token.is_symbol("(") && tokens[index - 1].is("ON")); |
| 144 | + if modifier { |
| 145 | + return None; |
| 146 | + } |
| 147 | + self.item_pending = false; |
| 148 | + Some(self.base + 1) |
| 149 | + } |
| 150 | + |
| 151 | + /// The clause `tokens[index]` moves a broken query into, and the break its commas ask for. |
| 152 | + fn clause_break(&mut self, tokens: &[Token<'_>], index: usize) -> Option<(usize, usize)> { |
| 153 | + let token = &tokens[index]; |
| 154 | + if !self.query || !self.broken { |
| 155 | + return None; |
| 156 | + } |
| 157 | + match token { |
| 158 | + token if token.is("SELECT") => { |
| 159 | + (self.clause, self.item_pending) = (Clause::Select, true); |
| 160 | + None |
| 161 | + } |
| 162 | + token if token.is("WITH") && self.clause == Clause::Other => { |
| 163 | + self.clause = Clause::With; |
| 164 | + None |
| 165 | + } |
| 166 | + token if token.is_symbol(",") => match self.clause { |
| 167 | + Clause::Select => Some((index + 1, self.base + 1)), |
| 168 | + Clause::With => Some((index + 1, self.base)), |
| 169 | + Clause::Other => None, |
| 170 | + }, |
| 171 | + _ if is_set_operation(tokens, index) => { |
| 172 | + self.clause = Clause::Other; |
| 173 | + Some((after_modifiers(tokens, index), self.base)) |
| 174 | + } |
| 175 | + _ if starts_clause(tokens, index) => { |
| 176 | + self.clause = Clause::Other; |
| 177 | + None |
| 178 | + } |
| 179 | + _ => None, |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +/// The level of the line `token` starts within `case`, when the `CASE` breaks. |
| 185 | +fn case_break(token: &Token<'_>, case: &Case) -> Option<usize> { |
| 186 | + match case.broken { |
| 187 | + true if token.is("WHEN") || token.is("ELSE") => Some(case.indent + 1), |
| 188 | + true if token.is("END") => Some(case.indent), |
| 189 | + _ => None, |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +/// The index of the first token after the set operation at `index` and its modifiers. |
| 194 | +fn after_modifiers(tokens: &[Token<'_>], index: usize) -> usize { |
| 195 | + (index + 1..tokens.len()) |
| 196 | + .find(|at| !is_set_operation_modifier(&tokens[*at])) |
| 197 | + .unwrap_or(tokens.len()) |
| 198 | +} |
| 199 | + |
| 200 | +/// Whether `token` is `ALL`, `DISTINCT`, `BY` or `NAME` after a set operation. |
| 201 | +fn is_set_operation_modifier(token: &Token<'_>) -> bool { |
| 202 | + SET_OPERATION_MODIFIERS.iter().any(|word| token.is(word)) |
| 203 | +} |
| 204 | + |
| 205 | +/// Whether `token` can come before `JOIN` in a join. |
| 206 | +fn is_join_modifier(token: &Token<'_>) -> bool { |
| 207 | + JOIN_MODIFIERS.iter().any(|word| token.is(word)) |
| 208 | +} |
| 209 | + |
| 210 | +/// Whether `tokens[index]` starts a clause: a clause word, `GROUP BY`, `ORDER BY` or a join. |
| 211 | +fn starts_clause(tokens: &[Token<'_>], index: usize) -> bool { |
| 212 | + let token = &tokens[index]; |
| 213 | + let previous = index.checked_sub(1).map(|at| &tokens[at]); |
| 214 | + let next = tokens.get(index + 1); |
| 215 | + if token.is("FROM") && previous.is_some_and(|previous| previous.is("DISTINCT")) { |
| 216 | + return false; |
| 217 | + } |
| 218 | + let joins = (token.is("JOIN") || is_join_modifier(token)) |
| 219 | + && !previous.is_some_and(is_join_modifier) |
| 220 | + && tokens[index..] |
| 221 | + .iter() |
| 222 | + .find(|token| !is_join_modifier(token)) |
| 223 | + .is_some_and(|token| token.is("JOIN")); |
| 224 | + CLAUSE_WORDS.iter().any(|word| token.is(word)) |
| 225 | + || (BY_CLAUSE_WORDS.iter().any(|word| token.is(word)) |
| 226 | + && next.is_some_and(|next| next.is("BY"))) |
| 227 | + || joins |
| 228 | +} |
| 229 | + |
| 230 | +/// Whether `tokens[index]` is a set operation, not the `EXCEPT` of a `* EXCEPT (…)` column list. |
| 231 | +fn is_set_operation(tokens: &[Token<'_>], index: usize) -> bool { |
| 232 | + SET_OPERATIONS.iter().any(|word| tokens[index].is(word)) |
| 233 | + && !(tokens[index].is("EXCEPT") && index > 0 && tokens[index - 1].is_symbol("*")) |
| 234 | +} |
| 235 | + |
| 236 | +/// Whether the parenthesis at `tokens[index]` calls a function rather than grouping. |
| 237 | +fn calls(tokens: &[Token<'_>], index: usize) -> bool { |
| 238 | + let Some(previous) = index.checked_sub(1).map(|at| &tokens[at]) else { |
| 239 | + return false; |
| 240 | + }; |
| 241 | + match previous.kind { |
| 242 | + TokenKind::Word => !NON_CALL_WORDS.iter().any(|word| previous.is(word)), |
| 243 | + TokenKind::Quoted => true, |
| 244 | + _ => previous.is_symbol("]") || previous.is_symbol(")"), |
| 245 | + } |
| 246 | +} |
0 commit comments