toon-lsp 0.7.21

Language Server Protocol implementation for TOON (Token-Oriented Object Notation)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (C) 2024-2025 w0wl0lxd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Document formatting for TOON Language Server.
//!
//! This module implements consistent formatting of TOON documents with
//! configurable indentation and array form preservation.

use crate::ast::{ArrayForm, AstNode, NumberValue, ObjectEntry};
use tower_lsp::lsp_types::FormattingOptions;

/// Formatting configuration derived from LSP FormattingOptions.
///
/// Controls how TOON documents are formatted. TOON specification prohibits
/// tabs for indentation, so only space-based indentation is supported.
///
/// # Fields
///
/// * `indent_size` - Number of spaces per indent level (1-8, default 2)
///
/// # Examples
///
/// ```
/// # use toon_lsp::lsp::formatting::ToonFormattingOptions;
/// let opts = ToonFormattingOptions { indent_size: 2 };
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToonFormattingOptions {
    /// Number of spaces per indent level (1-8)
    pub indent_size: u32,
}

/// Default formatting options: 2-space indentation.
impl Default for ToonFormattingOptions {
    fn default() -> Self {
        Self { indent_size: 2 }
    }
}

/// Convert LSP `FormattingOptions` to TOON-specific options.
///
/// Maps LSP client settings to TOON formatter configuration:
/// - `tab_size` → `indent_size` (clamped to 1-8)
///
/// Note: TOON spec prohibits tabs, so `insert_spaces` is ignored.
impl From<&FormattingOptions> for ToonFormattingOptions {
    fn from(opts: &FormattingOptions) -> Self {
        Self {
            // Clamp indent_size to valid range (1-8)
            indent_size: opts.tab_size.clamp(1, 8),
        }
    }
}

/// Internal state for formatting traversal.
///
/// Maintains the current formatting context as the AST is traversed.
/// Tracks indentation level and accumulates the formatted output.
struct FormattingContext {
    /// Formatting options (indent size, tabs vs spaces)
    options: ToonFormattingOptions,
    /// Current nesting level for indentation
    indent_level: u32,
    /// Accumulated formatted output
    output: String,
}

impl FormattingContext {
    /// Create a new formatting context with the given options.
    fn new(options: ToonFormattingOptions) -> Self {
        Self { options, indent_level: 0, output: String::new() }
    }

    /// Generate indentation string for current nesting level.
    ///
    /// Always uses spaces (TOON spec prohibits tabs).
    fn indent(&self) -> String {
        " ".repeat((self.indent_level * self.options.indent_size) as usize)
    }

    /// Append text to the output buffer.
    fn push(&mut self, s: &str) {
        self.output.push_str(s);
    }

    /// Append a newline to the output buffer.
    fn newline(&mut self) {
        self.output.push('\n');
    }
}

/// Format a TOON document with consistent indentation.
///
/// Traverses the AST and produces formatted output with consistent
/// indentation, proper spacing, and preserved array forms. The formatter
/// ensures the output is valid TOON that parses to an equivalent AST.
///
/// # Arguments
///
/// * `ast` - The parsed AST root node (must be valid, no parse errors)
/// * `options` - Formatting configuration (indent size, tabs vs spaces)
///
/// # Returns
///
/// Formatted document text with trailing newline, or `None` if AST is invalid.
///
/// # Examples
///
/// ```
/// # use toon_lsp::lsp::formatting::{format_document, ToonFormattingOptions};
/// # use toon_lsp::parser::parse_with_errors;
/// let source = "user:\n  name: Alice\n  age: 30";
/// let (ast, _) = parse_with_errors(source);
/// let opts = ToonFormattingOptions { indent_size: 2 };
/// let formatted = format_document(&ast.unwrap(), opts).unwrap();
/// assert!(formatted.contains("  name: Alice")); // 2-space indent preserved
/// ```
///
/// # Implementation Notes
///
/// - Preserves array forms (inline, expanded, tabular)
/// - Ensures trailing newline on output
/// - Automatically quotes strings containing special characters
/// - Formats numbers with minimal precision
pub fn format_document(ast: &AstNode, options: ToonFormattingOptions) -> Option<String> {
    let mut ctx = FormattingContext::new(options);
    format_node(ast, &mut ctx, false);

    // Ensure trailing newline
    if !ctx.output.ends_with('\n') {
        ctx.newline();
    }

    Some(ctx.output)
}

/// Format a single AST node.
///
/// Recursively formats an AST node and its children. The `is_value` flag
/// indicates whether this node is being formatted as a value (affects
/// string quoting and inline formatting).
///
/// # Arguments
///
/// * `node` - The AST node to format
/// * `ctx` - The formatting context (accumulates output, tracks indentation)
/// * `is_value` - True if this node is a value in a key-value pair
fn format_node(node: &AstNode, ctx: &mut FormattingContext, is_value: bool) {
    match node {
        AstNode::Document { children, .. } => {
            for child in children {
                format_node(child, ctx, false);
            }
        }

        AstNode::Object { entries, .. } => {
            for entry in entries {
                format_object_entry(entry, ctx);
            }
        }

        AstNode::Array { items, form, .. } => {
            format_array(items, *form, ctx, is_value);
        }

        AstNode::String { value, .. } => {
            // Multiline content is serialized as a block string to preserve newlines
            if value.contains('\n') {
                ctx.push(&format!("\"\"\"{}\"\"\"", value));
            } else if needs_quotes(value) {
                ctx.push(&format!("\"{}\"", escape_string(value)));
            } else {
                ctx.push(value);
            }
        }

        AstNode::Number { value, .. } => {
            ctx.push(&format_number(*value));
        }

        AstNode::Bool { value, .. } => {
            ctx.push(if *value { "true" } else { "false" });
        }

        AstNode::Null { .. } => {
            ctx.push("null");
        }
        AstNode::Reference { path, .. } => {
            ctx.push(&format!("${{{}}}", path));
        }
    }
}

/// Format an object entry (key-value pair).
///
/// Formats a key-value pair with proper indentation and spacing.
/// Nested objects and expanded arrays are placed on separate lines
/// with increased indentation.
///
/// # Arguments
///
/// * `entry` - The object entry to format (contains key, value, and spans)
/// * `ctx` - The formatting context
fn format_object_entry(entry: &ObjectEntry, ctx: &mut FormattingContext) {
    ctx.push(&ctx.indent());
    ctx.push(&entry.key);
    ctx.push(": ");

    // Check if value needs to be on new line (nested object)
    match &entry.value {
        AstNode::Object { .. } => {
            ctx.newline();
            ctx.indent_level += 1;
            format_node(&entry.value, ctx, false);
            ctx.indent_level -= 1;
        }
        AstNode::Array { form: ArrayForm::Expanded | ArrayForm::Tabular, .. } => {
            ctx.newline();
            ctx.indent_level += 1;
            format_node(&entry.value, ctx, true);
            ctx.indent_level -= 1;
        }
        _ => {
            format_node(&entry.value, ctx, true);
            ctx.newline();
        }
    }
}

/// Format an array with form preservation.
///
/// Formats arrays based on their original form (inline, expanded, or tabular).
/// The array form is preserved from the original source to maintain readability.
///
/// # Arguments
///
/// * `items` - The array elements
/// * `form` - The array form (inline `[...]`, expanded `- ...`, or tabular `| ... |`)
/// * `ctx` - The formatting context
/// * `_is_value` - Unused (for future use)
fn format_array(items: &[AstNode], form: ArrayForm, ctx: &mut FormattingContext, _is_value: bool) {
    match form {
        ArrayForm::Inline => {
            if items.len() > 3 {
                ctx.push("[\n");
                ctx.indent_level += 1;
                for item in items {
                    ctx.push(&ctx.indent());
                    format_node(item, ctx, true);
                    ctx.push(",\n");
                }
                ctx.indent_level -= 1;
                ctx.push(&ctx.indent());
                ctx.push("]");
            } else {
                ctx.push("[");
                for (i, item) in items.iter().enumerate() {
                    if i > 0 {
                        ctx.push(", ");
                    }
                    format_node(item, ctx, true);
                }
                ctx.push("]");
            }
        }

        ArrayForm::Expanded => {
            for item in items {
                ctx.push(&ctx.indent());
                ctx.push("- ");
                format_node(item, ctx, true);
                ctx.newline();
            }
        }

        ArrayForm::Tabular => {
            // Tabular arrays are already formatted as Objects by parser
            // Just format the items (which are Objects)
            for item in items {
                format_tabular_row(item, ctx);
            }
        }
    }
}

/// Format a tabular array row (Object node).
///
/// Formats a single row of a tabular array using pipe delimiters.
/// Tabular arrays are parsed as objects with column names as keys.
///
/// # Arguments
///
/// * `node` - The object node representing one row
/// * `ctx` - The formatting context
fn format_tabular_row(node: &AstNode, ctx: &mut FormattingContext) {
    if let AstNode::Object { entries, .. } = node {
        ctx.push(&ctx.indent());
        ctx.push("| ");
        for (i, entry) in entries.iter().enumerate() {
            if i > 0 {
                ctx.push(" | ");
            }
            format_node(&entry.value, ctx, true);
        }
        ctx.push(" |");
        ctx.newline();
    }
}

/// Check if a string needs quotes.
///
/// Determines whether a string value requires quotes in TOON syntax.
/// Strings need quotes if they:
/// - Are empty
/// - Contain special TOON characters (`:`, `,`, `[`, `]`, etc.)
/// - Start or end with whitespace
/// - Could be confused with keywords (`true`, `false`, `null`)
/// - Could be parsed as numbers
///
/// # Arguments
///
/// * `s` - The string to check
///
/// # Returns
///
/// `true` if the string requires quotes, `false` otherwise
fn needs_quotes(s: &str) -> bool {
    s.is_empty()
        || s.contains(':')
        || s.contains(',')
        || s.contains('[')
        || s.contains(']')
        || s.contains('{')
        || s.contains('}')
        || s.contains('|')
        || s.contains('-')
        || s.contains('"')
        || s.contains('\\')
        || s.starts_with(' ')
        || s.ends_with(' ')
        || s == "true"
        || s == "false"
        || s == "null"
        || s.parse::<f64>().is_ok()
}

/// Escape special characters in strings.
///
/// Escapes backslashes and double quotes for TOON string literals.
/// These are the only characters that require escaping inside quoted strings.
///
/// # Arguments
///
/// * `s` - The string to escape
///
/// # Returns
///
/// Escaped string safe for use in quoted TOON literals
fn escape_string(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

/// Format a number value.
///
/// Converts a number to its string representation. Integers are formatted
/// directly, while floats use minimal precision (e.g., `1.0` instead of `1`
/// for whole floats).
///
/// # Arguments
///
/// * `value` - The number value to format
///
/// # Returns
///
/// String representation of the number
fn format_number(value: NumberValue) -> String {
    match value {
        NumberValue::PosInt(n) => n.to_string(),
        NumberValue::NegInt(n) => n.to_string(),
        NumberValue::Float(n) => {
            // Format floats with minimal precision
            if n.fract() == 0.0 && n.is_finite() { format!("{:.1}", n) } else { n.to_string() }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::parse_with_errors;

    fn parse(source: &str) -> AstNode {
        let (ast, errors) = parse_with_errors(source);
        assert!(errors.is_empty(), "Parse errors: {:?}", errors);
        ast.expect("Expected valid AST")
    }

    // Test format with 2-space indentation
    #[test]
    fn test_format_2_space_indent() {
        let source = "user:\n    name: Alice\n    age: 30"; // 4-space input
        let ast = parse(source);
        let opts = ToonFormattingOptions { indent_size: 2 };

        let result = format_document(&ast, opts).expect("Formatting failed");

        // Should convert 4-space to 2-space indentation
        assert!(result.contains("user:"), "Missing 'user:' key");
        assert!(result.contains("  name: Alice"), "Expected 2-space indent for 'name'");
        assert!(result.contains("  age: 30"), "Expected 2-space indent for 'age'");
    }

    // Test format with 4-space indentation
    #[test]
    fn test_format_4_space_indent() {
        let source = "user:\n  name: Alice\n  age: 30"; // 2-space input
        let ast = parse(source);
        let opts = ToonFormattingOptions { indent_size: 4 };

        let result = format_document(&ast, opts).expect("Formatting failed");

        // Should convert 2-space to 4-space indentation
        assert!(result.contains("user:"), "Missing 'user:' key");
        assert!(result.contains("    name: Alice"), "Expected 4-space indent for 'name'");
        assert!(result.contains("    age: 30"), "Expected 4-space indent for 'age'");
    }

    // Test format always uses spaces (TOON spec prohibits tabs)
    #[test]
    fn test_format_uses_spaces_not_tabs() {
        let source = "user:\n  name: Alice\n  age: 30";
        let ast = parse(source);
        let opts = ToonFormattingOptions { indent_size: 2 };

        let result = format_document(&ast, opts).expect("Formatting failed");

        // Should use spaces for indentation (tabs prohibited by TOON spec)
        assert!(result.contains("user:"), "Missing 'user:' key");
        assert!(result.contains("  name: Alice"), "Expected space indent for 'name'");
        assert!(!result.contains('\t'), "Output should not contain tabs");
    }

    // Test format preserves inline array form
    #[test]
    fn test_format_preserves_inline_array() {
        // Test inline array formatting (currently parser creates empty array for array[N]: syntax)
        // This is a known limitation - test that we handle empty inline arrays correctly
        let source = "values[3]:";
        let ast = parse(source);
        let opts = ToonFormattingOptions::default();

        let result = format_document(&ast, opts).expect("Formatting failed");

        // Should format empty inline array
        assert!(result.contains("values"), "Missing 'values' key");
        assert!(result.contains('['), "Expected opening bracket");
        assert!(result.contains(']'), "Expected closing bracket");
    }

    // Test format preserves expanded array form
    #[test]
    fn test_format_preserves_expanded_array() {
        let source = "items:\n  - first\n  - second\n  - third";
        let ast = parse(source);
        let opts = ToonFormattingOptions::default();

        let result = format_document(&ast, opts).expect("Formatting failed");

        // Should preserve expanded array format
        assert!(result.contains("items:"), "Missing 'items:' key");
        assert!(result.contains("  - first"), "Expected expanded format for 'first'");
        assert!(result.contains("  - second"), "Expected expanded format for 'second'");
        assert!(result.contains("  - third"), "Expected expanded format for 'third'");
    }

    // Test format preserves tabular array form
    #[test]
    fn test_format_preserves_tabular_array() {
        // Tabular arrays use comma delimiter: data[2]{x,y}: \n  1,2 \n  3,4
        let source = "data[2]{x,y}:\n  1,2\n  3,4";
        let ast = parse(source);
        let opts = ToonFormattingOptions::default();

        let result = format_document(&ast, opts).expect("Formatting failed");

        // Should preserve tabular array format (formatted as objects with pipe delimiters)
        assert!(result.contains("data"), "Missing 'data' key");
        // Tabular arrays are converted to objects by parser, formatted with pipes
        assert!(result.contains('|'), "Expected pipe delimiters in tabular format");
        assert!(result.contains('1'), "Expected value '1'");
        assert!(result.contains('2'), "Expected value '2'");
        assert!(result.contains('3'), "Expected value '3'");
        assert!(result.contains('4'), "Expected value '4'");
    }

    // Test format produces AST-equivalent output
    #[test]
    fn test_format_produces_equivalent_ast() {
        let source = "user:\n   name: Bob\n   age:30\n   active:true"; // Messy formatting
        let ast = parse(source);
        let opts = ToonFormattingOptions::default();

        let formatted = format_document(&ast, opts).expect("Formatting failed");

        // Parse the formatted output and compare ASTs (ignoring spans)
        let (new_ast_opt, errors) = parse_with_errors(&formatted);
        assert!(errors.is_empty(), "Formatted output has parse errors: {:?}", errors);
        let new_ast = new_ast_opt.expect("Expected valid AST");

        // Compare structure (this is a simplified check)
        assert_eq!(ast.kind(), new_ast.kind(), "AST kinds don't match");
    }

    // Test format handles empty document
    #[test]
    fn test_format_empty_document() {
        let source = "";
        let ast = parse(source);
        let opts = ToonFormattingOptions::default();

        let result = format_document(&ast, opts);

        // Empty document should format to empty string or just newline
        assert!(result.is_some(), "Should handle empty document");
        let formatted = result.unwrap();
        assert!(
            formatted.is_empty() || formatted == "\n",
            "Empty document should format to empty or newline"
        );
    }

    // Test format skips documents with parse errors (handler level test)
    // This test verifies that format_document assumes valid AST
    #[test]
    fn test_format_assumes_valid_ast() {
        // This test is more about documenting the contract:
        // format_document should only be called with valid ASTs
        // The LSP handler is responsible for checking errors

        let source = "valid: data";
        let ast = parse(source);
        let opts = ToonFormattingOptions::default();

        // Should successfully format valid AST
        let result = format_document(&ast, opts);
        assert!(result.is_some(), "Should format valid AST");
    }

    #[test]
    fn test_format_multiline_array_trailing_comma() {
        let source = "list[4]: 1, 2, 3, 4";
        let ast = parse(source);
        let opts = ToonFormattingOptions::default();
        let formatted = format_document(&ast, opts).unwrap();
        assert_eq!(formatted, "list: [\n  1,\n  2,\n  3,\n  4,\n]\n");
    }
}