Skip to main content

logicaffeine_language/
source_format.rs

1//! The canonical LOGOS source formatter.
2//!
3//! One rule set shared by every formatting surface (the LSP's
4//! `textDocument/formatting` provider, its on-type formatter, and
5//! `largo fmt`):
6//!
7//! - code lines reindent to **4 spaces per lexed nesting level** — the
8//!   depth comes from the lexer's own Indent/Dedent reading, so the
9//!   canonical form is the structure the program already has,
10//! - trailing whitespace is removed from code lines; CRLF normalizes to LF,
11//! - multiline-string interiors are **content** — not a byte is touched,
12//! - `## Note`/`## Example` bodies are the author's prose — untouched
13//!   (markdown hard-breaks and nested-list indents survive),
14//! - comment-only lines keep their author indent (tabs still normalize),
15//! - the presence or absence of a final newline is preserved.
16//!
17//! The invariant every rule above keeps, locked by `formatter_locks.rs` in the test suite:
18//! **formatting never changes the token stream** — and it is a fixed point
19//! (`format_source(format_source(s)) == format_source(s)`).
20
21use crate::lexer::Lexer;
22use crate::token::{BlockType, TokenType};
23use logicaffeine_base::Interner;
24
25/// Format a single line: normalize leading whitespace (tab → 4 spaces) and
26/// strip trailing whitespace. Content between the indent and the trailing
27/// whitespace is untouched. Line-local — the on-type formatting rule; the
28/// whole-document [`format_source`] adds structure awareness on top.
29pub fn format_line(line: &str) -> String {
30    let trimmed_start = line.trim_start();
31    let leading = &line[..line.len() - trimmed_start.len()];
32    let mut out = String::with_capacity(line.len());
33    for ch in leading.chars() {
34        if ch == '\t' {
35            out.push_str("    ");
36        } else {
37            out.push(ch);
38        }
39    }
40    out.push_str(trimmed_start.trim_end());
41    // A whitespace-only line reduces to empty.
42    if out.chars().all(|c| c == ' ') {
43        out.clear();
44    }
45    out
46}
47
48/// Per-line formatting plan, derived from one lexer pass.
49#[derive(Clone, Copy, PartialEq)]
50enum LinePlan {
51    /// Code with a known nesting depth: reindent to `depth * 4` spaces.
52    Code(usize),
53    /// Code-adjacent but depthless (blank, comment-only): line-local rules.
54    Plain,
55    /// String interior or documentation prose: not a byte changes.
56    Raw,
57}
58
59/// Format a whole source text: structural reindentation for code, raw
60/// passthrough for string interiors and prose, LF endings, preserved final
61/// newline.
62pub fn format_source(source: &str) -> String {
63    if source.is_empty() {
64        return String::new();
65    }
66
67    let plans = line_plans(source);
68    let mut out: String = source
69        .lines()
70        .enumerate()
71        .map(
72            |(i, line)| match plans.get(i).copied().unwrap_or(LinePlan::Plain) {
73                LinePlan::Raw => line.to_string(),
74                LinePlan::Plain => format_line(line),
75                LinePlan::Code(depth) => {
76                    let content = line.trim_start().trim_end();
77                    if content.is_empty() {
78                        String::new()
79                    } else {
80                        let mut formatted = " ".repeat(depth * 4);
81                        formatted.push_str(content);
82                        formatted
83                    }
84                }
85            },
86        )
87        .collect::<Vec<_>>()
88        .join("\n");
89    if source.ends_with('\n') {
90        out.push('\n');
91    }
92    out
93}
94
95/// One lexer pass over the source decides how every line formats.
96fn line_plans(source: &str) -> Vec<LinePlan> {
97    let line_starts: Vec<usize> = std::iter::once(0)
98        .chain(source.match_indices('\n').map(|(i, _)| i + 1))
99        .collect();
100    let line_of = |offset: usize| match line_starts.binary_search(&offset) {
101        Ok(i) => i,
102        Err(i) => i - 1,
103    };
104    let line_count = source.lines().count();
105    let mut plans = vec![LinePlan::Plain; line_count];
106
107    let mut interner = Interner::new();
108    let mut lexer = Lexer::new(source, &mut interner);
109    let tokens = lexer.tokenize();
110
111    // Pass 1: nesting depth per line — the depth in effect at each line's
112    // first token, from the lexer's own Indent/Dedent reading.
113    let mut depth: usize = 0;
114    let mut current_line: Option<usize> = None;
115    for token in &tokens {
116        match &token.kind {
117            TokenType::Indent => depth += 1,
118            TokenType::Dedent => depth = depth.saturating_sub(1),
119            TokenType::Newline | TokenType::EOF => {}
120            _ => {
121                let line = line_of(token.span.start);
122                if current_line != Some(line) {
123                    current_line = Some(line);
124                    if let Some(plan) = plans.get_mut(line) {
125                        *plan = LinePlan::Code(depth);
126                    }
127                }
128            }
129        }
130    }
131
132    // Pass 2: multiline tokens (string/escape-block interiors) are content.
133    // Every line a token SPANS beyond its first is raw, and so is the first
134    // line of any multi-line token — its trailing side is inside the token.
135    for token in &tokens {
136        let start_line = line_of(token.span.start);
137        let end_line = line_of(token.span.end.saturating_sub(1).max(token.span.start));
138        if end_line > start_line {
139            for line in start_line..=end_line {
140                if let Some(plan) = plans.get_mut(line) {
141                    *plan = LinePlan::Raw;
142                }
143            }
144        }
145    }
146
147    // Pass 3: documentation prose (`## Note` / `## Example` bodies) belongs
148    // to the author — raw from the line after the header to the next block.
149    let mut in_prose = false;
150    let mut prose_from = 0usize;
151    let mark_prose = |plans: &mut Vec<LinePlan>, from: usize, to: usize| {
152        for plan in plans.iter_mut().take(to).skip(from) {
153            *plan = LinePlan::Raw;
154        }
155    };
156    for token in &tokens {
157        if let TokenType::BlockHeader { block_type } = &token.kind {
158            let header_line = line_of(token.span.start);
159            if in_prose {
160                mark_prose(&mut plans, prose_from, header_line);
161            }
162            in_prose = matches!(block_type, BlockType::Note | BlockType::Example);
163            prose_from = header_line + 1;
164        }
165    }
166    if in_prose {
167        mark_prose(&mut plans, prose_from, line_count);
168    }
169
170    plans
171}