Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

gleam / pretty-arena / src / lib.rs
63 kB 1529 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2026 The Gleam contributors 3 4//! This module implements the functionality described in 5//! ["Strictly Pretty" (2000) by Christian Lindig][0], with a few 6//! extensions. 7//! 8//! This module is heavily influenced by Elixir's Inspect.Algebra and 9//! JavaScript's Prettier. 10//! 11//! [0]: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.34.2200 12//! 13//! ## Extensions 14//! 15//! - `ForcedBreak` from Elixir. 16//! - `FlexBreak` from Elixir. 17//! 18//! The way this module works is fairly simple conceptually, however the actual 19//! behaviour in practice can be hard to wrap one's head around. 20//! 21//! The basic premise is the `Document` type, which is a tree structure, 22//! containing some text as well as information on how it can be formatted. 23//! Once the document is constructed, it can be printed using the 24//! `to_pretty_string` function. 25//! 26//! It will then traverse the tree, and construct 27//! a string, attempting to wrap lines to that they do not exceed the line length 28//! limit specified. Where and when it wraps lines is determined by the structure 29//! of the `Document` itself. 30//! 31#![allow(clippy::wrong_self_convention)] 32 33use std::{cell::RefCell, rc::Rc}; 34 35use ecow::{EcoString, eco_format}; 36use num_bigint::BigInt; 37use typed_arena::Arena; 38use unicode_segmentation::UnicodeSegmentation; 39 40/// Join multiple documents together in a vector. This macro calls the `to_doc` 41/// method on each element, providing a concise way to write a document sequence. 42/// For example: 43/// 44/// ```rust:norun 45/// docvec!["Hello", line(), "world!"] 46/// ``` 47/// 48/// Note: each document in a docvec is not separated in any way: the formatter 49/// will never break a line unless a `Document::Break` or `Document::Line` 50/// is used. Therefore, `docvec!["a", "b", "c"]` is equivalent to 51/// `"abc".to_doc()`. 52/// 53#[macro_export] 54macro_rules! docvec { 55 // When we're joining exactly 3 or 4 documents we use the specialised 56 // `join3` and `join4` to be a bit faster! 57 ($arena:expr, $first:expr, $second:expr, $third:expr) => { 58 $arena.join3($first, $second, $third) 59 }; 60 61 ($arena:expr, $first:expr, $second:expr, $third:expr, $fourth:expr) => { 62 $arena.join4($first, $second, $third, $fourth) 63 }; 64 65 // Otherwise we just append all the documents together. 66 ($arena:expr, $first:expr, $($rest:expr),+ $(,)?) => { 67 { 68 $first.to_doc(&$arena) 69 $(.append(&$arena, $rest))+ 70 } 71 }; 72} 73 74/// A trait that allows for objects to observe the cursor position as it is being formatted. 75/// This is useful for any operations that need to track the exact position a document is 76/// being written to in a buffer such as for source mapping. 77pub trait CursorPositionObserver: std::fmt::Debug { 78 fn observe_cursor_position(&mut self, line: isize, width: isize); 79} 80 81// To the outside world a document is an opaque data structure. 82// This is a newtype wrapper around a reference that is gonna be stored in a 83// document arena. 84#[derive(Debug, Clone, Copy)] 85pub struct Document<'string, 'doc>(&'doc PrintableDocument<'string, 'doc>); 86 87/// Coerce a value into a Document. 88/// Note we do not implement this for String as a slight pressure to favour str 89/// over String. 90pub trait Documentable<'string, 'doc> { 91 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc>; 92} 93 94impl<'string, 'doc> Documentable<'string, 'doc> for char { 95 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 96 eco_format!("{self}").to_doc(arena) 97 } 98} 99 100impl<'string, 'doc> Documentable<'string, 'doc> for &'string str { 101 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 102 Document(arena.documents.alloc(PrintableDocument::Str { 103 graphemes: self.graphemes(true).count() as isize, 104 string: self, 105 })) 106 } 107} 108 109impl<'string, 'doc> Documentable<'string, 'doc> for EcoString { 110 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 111 Document(arena.documents.alloc(PrintableDocument::EcoString { 112 graphemes: self.graphemes(true).count() as isize, 113 string: self, 114 })) 115 } 116} 117 118impl<'string, 'doc> Documentable<'string, 'doc> for &EcoString { 119 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 120 self.clone().to_doc(arena) 121 } 122} 123 124impl<'string, 'doc> Documentable<'string, 'doc> for isize { 125 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 126 eco_format!("{self}").to_doc(arena) 127 } 128} 129 130impl<'string, 'doc> Documentable<'string, 'doc> for i64 { 131 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 132 eco_format!("{self}").to_doc(arena) 133 } 134} 135 136impl<'string, 'doc> Documentable<'string, 'doc> for usize { 137 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 138 eco_format!("{self}").to_doc(arena) 139 } 140} 141 142impl<'string, 'doc> Documentable<'string, 'doc> for f64 { 143 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 144 eco_format!("{self:?}").to_doc(arena) 145 } 146} 147 148impl<'string, 'doc> Documentable<'string, 'doc> for u64 { 149 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 150 eco_format!("{self:?}").to_doc(arena) 151 } 152} 153 154impl<'string, 'doc> Documentable<'string, 'doc> for u32 { 155 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 156 eco_format!("{self}").to_doc(arena) 157 } 158} 159 160impl<'string, 'doc> Documentable<'string, 'doc> for u16 { 161 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 162 eco_format!("{self}").to_doc(arena) 163 } 164} 165 166impl<'string, 'doc> Documentable<'string, 'doc> for u8 { 167 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 168 eco_format!("{self}").to_doc(arena) 169 } 170} 171 172impl<'string, 'doc> Documentable<'string, 'doc> for BigInt { 173 fn to_doc(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 174 eco_format!("{self}").to_doc(arena) 175 } 176} 177 178impl<'string, 'doc> Documentable<'string, 'doc> for Document<'string, 'doc> { 179 fn to_doc(self, _arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 180 self 181 } 182} 183 184impl<'string, 'doc> Document<'string, 'doc> { 185 /// Groups a document. When pretty printing a group, the formatter will 186 /// first attempt to fit the entire group on one line. If it fails, all 187 /// `break_` documents in the group will render broken. 188 /// 189 /// Nested groups are handled separately to their parents, so if the 190 /// outermost group is broken, any sub-groups might be rendered broken 191 /// or unbroken, depending on whether they fit on a single line. 192 pub fn group(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 193 match self.0 { 194 // Grouping a group doesn't change how it will be formatted so we 195 // can avoid boxing it. 196 PrintableDocument::Group(_) 197 // Same for an empty document. 198 | PrintableDocument::Empty 199 // Grouping a literal string will never change how it's formatted, 200 // we can avoid boxing it. 201 | PrintableDocument::Str { .. } 202 | PrintableDocument::EcoString { .. } 203 | PrintableDocument::ZeroWidthString { .. } 204 | PrintableDocument::ZeroWidthStr { .. } 205 | PrintableDocument::CursorPositionObserver { .. } => self, 206 207 PrintableDocument::Line(_) 208 | PrintableDocument::ForceBroken(_) 209 | PrintableDocument::NextBreakFits(..) 210 | PrintableDocument::Break { .. } 211 | PrintableDocument::Join(..) 212 | PrintableDocument::Join3(..) 213 | PrintableDocument::Join4(..) 214 | PrintableDocument::Nest(..) => Document(arena.documents.alloc(PrintableDocument::Group(self))), 215 } 216 } 217 218 /// Sets the indentation level of a document. 219 pub fn set_nesting( 220 self, 221 arena: &'doc DocumentArena<'string, 'doc>, 222 indent: isize, 223 ) -> Document<'string, 'doc> { 224 Document(arena.documents.alloc(PrintableDocument::Nest( 225 indent, 226 NestMode::Set, 227 NestCondition::Always, 228 self, 229 ))) 230 } 231 232 /// Nests a document by a certain indentation. When rending linebreaks, the 233 /// formatter will print a new line followed by the current indentation. 234 pub fn nest( 235 self, 236 arena: &'doc DocumentArena<'string, 'doc>, 237 indent: isize, 238 ) -> Document<'string, 'doc> { 239 Document(arena.documents.alloc(PrintableDocument::Nest( 240 indent, 241 NestMode::Increase, 242 NestCondition::Always, 243 self, 244 ))) 245 } 246 247 /// Nests a document by a certain indentation, but only if the current 248 /// group is broken. 249 pub fn nest_if_broken( 250 self, 251 arena: &'doc DocumentArena<'string, 'doc>, 252 indent: isize, 253 ) -> Document<'string, 'doc> { 254 Document(arena.documents.alloc(PrintableDocument::Nest( 255 indent, 256 NestMode::Increase, 257 NestCondition::IfBroken, 258 self, 259 ))) 260 } 261 262 /// Forces all `break_` and `flex_break` documents in the current group 263 /// to render broken. 264 pub fn force_break(self, arena: &'doc DocumentArena<'string, 'doc>) -> Document<'string, 'doc> { 265 Document(arena.documents.alloc(PrintableDocument::ForceBroken(self))) 266 } 267 268 /// Force the next `Break` to render unbroken, regardless of whether it 269 /// fits on the line or not. 270 pub fn next_break_fits( 271 self, 272 arena: &'doc DocumentArena<'string, 'doc>, 273 mode: NextBreakFitsMode, 274 ) -> Document<'string, 'doc> { 275 Document( 276 arena 277 .documents 278 .alloc(PrintableDocument::NextBreakFits(self, mode)), 279 ) 280 } 281 282 /// Appends one document to another. Equivalent to `docvec![self, second]`, 283 /// except that it `self` is already a `Document::Vec`, it will append 284 /// directly to it instead of allocating a new vector. 285 /// 286 /// Useful when chaining multiple documents together in a fashion where 287 /// they cannot be put all into one `docvec!` macro. 288 pub fn append( 289 self, 290 arena: &'doc DocumentArena<'string, 'doc>, 291 new: impl Documentable<'string, 'doc>, 292 ) -> Document<'string, 'doc> { 293 let new = new.to_doc(arena); 294 if let PrintableDocument::Empty = new.0 { 295 return self; 296 } 297 298 match self.0 { 299 PrintableDocument::Empty => new, 300 PrintableDocument::Join(first, second) => Document( 301 arena 302 .documents 303 .alloc(PrintableDocument::Join3(*first, *second, new)), 304 ), 305 PrintableDocument::Join3(first, second, third) => Document( 306 arena 307 .documents 308 .alloc(PrintableDocument::Join4(*first, *second, *third, new)), 309 ), 310 PrintableDocument::Line(..) 311 | PrintableDocument::Join4(..) 312 | PrintableDocument::ForceBroken(..) 313 | PrintableDocument::NextBreakFits(..) 314 | PrintableDocument::Break { .. } 315 | PrintableDocument::Nest(..) 316 | PrintableDocument::Group(..) 317 | PrintableDocument::Str { .. } 318 | PrintableDocument::EcoString { .. } 319 | PrintableDocument::ZeroWidthString { .. } 320 | PrintableDocument::ZeroWidthStr { .. } 321 | PrintableDocument::CursorPositionObserver { .. } => { 322 Document(arena.documents.alloc(PrintableDocument::Join(self, new))) 323 } 324 } 325 } 326 327 /// Surrounds a document in two delimiters. Equivalent to 328 /// `docvec![option, self, closed]`. 329 pub fn surround( 330 self, 331 arena: &'doc DocumentArena<'string, 'doc>, 332 open: impl Documentable<'string, 'doc>, 333 closed: impl Documentable<'string, 'doc>, 334 ) -> Document<'string, 'doc> { 335 open.to_doc(arena) 336 .append(arena, self) 337 .append(arena, closed.to_doc(arena)) 338 } 339 340 /// Returns true when the document contains no printable characters 341 /// (whitespace and newlines are considered printable characters). 342 pub fn is_empty(&self) -> bool { 343 match self.0 { 344 PrintableDocument::Empty => true, 345 PrintableDocument::Line(n) => *n == 0, 346 PrintableDocument::EcoString { string, .. } => string.is_empty(), 347 PrintableDocument::Str { string, .. } => string.is_empty(), 348 // assuming `broken` and `unbroken` are equivalent 349 PrintableDocument::Break { broken, .. } => broken.is_empty(), 350 PrintableDocument::ForceBroken(document) 351 | PrintableDocument::Nest(_, _, _, document) 352 | PrintableDocument::Group(document) 353 | PrintableDocument::NextBreakFits(document, _) => document.is_empty(), 354 PrintableDocument::Join(first, second) => first.is_empty() && second.is_empty(), 355 PrintableDocument::Join3(first, second, third) => { 356 first.is_empty() && second.is_empty() && third.is_empty() 357 } 358 PrintableDocument::Join4(first, second, third, fourth) => { 359 first.is_empty() && second.is_empty() && third.is_empty() && fourth.is_empty() 360 } 361 // Zero-width strings don't count towards line length, but they are 362 // still printed and so are not empty. (Unless their string contents 363 // is also empty) 364 PrintableDocument::ZeroWidthString { string } => string.is_empty(), 365 PrintableDocument::ZeroWidthStr { string } => string.is_empty(), 366 PrintableDocument::CursorPositionObserver { .. } => true, 367 } 368 } 369 370 /// Prints a document into a `String`, attempting to limit lines to `limit` 371 /// characters in length. 372 pub fn to_pretty_string(self, limit: isize) -> String { 373 let mut buffer = String::new(); 374 self.pretty_print(limit, &mut buffer) 375 .expect("Writing to string buffer failed"); 376 buffer 377 } 378 379 /// Prints a document into `writer`, attempting to limit lines to `limit` 380 /// characters in length. 381 pub fn pretty_print(self, limit: isize, writer: &mut impl std::fmt::Write) -> std::fmt::Result { 382 let docs = im::vector![(0, Mode::Unbroken, self)]; 383 format(writer, limit, docs)?; 384 Ok(()) 385 } 386} 387 388/// A pretty printable document. A tree structure, made up of text and other 389/// elements which determine how it can be formatted. 390/// 391/// The variants of this enum should probably not be constructed directly, 392/// rather use the helper functions on the DocumentArena to construct them. 393/// 394#[derive(Debug, Clone)] 395enum PrintableDocument<'string, 'doc> { 396 /// A mandatory linebreak. This is always printed as a string of newlines, 397 /// equal in length to the number specified. 398 Line(usize), 399 400 /// Forces the breaks of the wrapped document to be considered as not 401 /// fitting on a single line. Used in combination with a `Group` it can be 402 /// used to force its `Break`s to always break. 403 ForceBroken(Document<'string, 'doc>), 404 405 /// Ignore the next break, forcing it to render as unbroken. 406 NextBreakFits(Document<'string, 'doc>, NextBreakFitsMode), 407 408 /// A document after which the formatter can insert a newline. This determines 409 /// where line breaks can occur, outside of hardcoded `Line`s. 410 /// See `break_` and `flex_break` for usage. 411 Break { 412 broken: &'string str, 413 unbroken: &'string str, 414 kind: BreakKind, 415 }, 416 417 /// Join multiple documents together. The documents are not separated in any 418 /// way: the formatter will only print newlines if `Document::Break` or 419 /// `Document::Line` is used. 420 // maybe experiment with Slice(&'doc [Document<'string, 'doc>]), 421 Join(Document<'string, 'doc>, Document<'string, 'doc>), 422 Join3( 423 Document<'string, 'doc>, 424 Document<'string, 'doc>, 425 Document<'string, 'doc>, 426 ), 427 Join4( 428 Document<'string, 'doc>, 429 Document<'string, 'doc>, 430 Document<'string, 'doc>, 431 Document<'string, 'doc>, 432 ), 433 434 /// Nests the given document by the given indent, depending on the specified 435 /// condition. See `Document::nest`, `Document::set_nesting` and 436 /// `Document::nest_if_broken` for usages. 437 Nest(isize, NestMode, NestCondition, Document<'string, 'doc>), 438 439 /// Groups a document. When pretty printing a group, the formatter will 440 /// first attempt to fit the entire group on one line. If it fails, all 441 /// `break_` documents in the group will render broken. 442 /// 443 /// Nested groups are handled separately to their parents, so if the 444 /// outermost group is broken, any sub-groups might be rendered broken 445 /// or unbroken, depending on whether they fit on a single line. 446 Group(Document<'string, 'doc>), 447 448 /// Renders a string slice. This will always render the string verbatim, 449 /// without any line breaks or other modifications to it. 450 Str { 451 string: &'string str, 452 /// The number of extended grapheme clusters in the string. 453 /// This is what the pretty printer uses as the width of the string as it 454 /// is closes to what a human would consider the "length" of a string. 455 /// 456 /// Since computing the number of grapheme clusters requires walking over 457 /// the string we precompute it to avoid iterating through a string over 458 /// and over again in the pretty printing algorithm. 459 /// 460 graphemes: isize, 461 }, 462 463 /// Renders an `EcoString`. This will always render the string verbatim, 464 /// without any line breaks or other modifications to it. 465 EcoString { 466 string: EcoString, 467 /// The number of extended grapheme clusters in the string. 468 /// This is what the pretty printer uses as the width of the string as it 469 /// is closes to what a human would consider the "length" of a string. 470 /// 471 /// Since computing the number of grapheme clusters requires walking over 472 /// the string we precompute it to avoid iterating through a string over 473 /// and over again in the pretty printing algorithm. 474 /// 475 graphemes: isize, 476 }, 477 478 /// A string that is not taken into account when determining line length. 479 /// This is useful for additional formatting text which won't be rendered 480 /// in the final output, such as ANSI codes or HTML elements. 481 ZeroWidthString { 482 string: EcoString, 483 }, 484 ZeroWidthStr { 485 string: &'string str, 486 }, 487 488 /// A node that gets notified of the cursor position as it is being formatted. 489 /// This allows for processes outside of the final output to be notified of 490 /// the cursor position and perform actions based on it, such as recording 491 /// the span of the node in the generated source code for a source mapping. 492 CursorPositionObserver { 493 observer: Rc<RefCell<dyn CursorPositionObserver>>, 494 }, 495 Empty, 496} 497 498/// The kind of line break this `Document::Break` is. 499#[derive(Debug, Clone, Copy, PartialEq, Eq)] 500pub enum BreakKind { 501 /// A `flex_break`. 502 Flex, 503 /// A `break_`. 504 Strict, 505} 506 507#[derive(Debug, Clone, Copy, PartialEq, Eq)] 508enum Mode { 509 /// The mode used when a group doesn't fit on a single line: when `Broken` 510 /// the `Break`s inside it will be rendered as newlines, splitting the 511 /// group. 512 Broken, 513 514 /// The default mode used when a group can fit on a single line: all its 515 /// `Break`s will be rendered as their unbroken string and kept on a single 516 /// line. 517 Unbroken, 518 519 /// This mode is used by the `NextBreakFit` document to force a break to be 520 /// considered as broken. 521 ForcedBroken, 522 523 /// This mode is used to disable a `NextBreakFit` document. 524 ForcedUnbroken, 525} 526 527/// A flag that can be used to enable or disable a `NextBreakFit` document. 528#[derive(Debug, Clone, Copy, PartialEq, Eq)] 529pub enum NextBreakFitsMode { 530 Enabled, 531 Disabled, 532} 533 534/// A flag that can be used to conditionally disable a `Nest` document. 535#[derive(Debug, Clone, Copy, PartialEq, Eq)] 536pub enum NestCondition { 537 /// This always applies the nesting. This is a sensible default that will 538 /// work for most of the cases. 539 Always, 540 /// Only applies the nesting if the wrapping `Group` couldn't fit on a 541 /// single line and has been broken. 542 IfBroken, 543} 544 545/// Used to change the way nesting of documents work. 546#[derive(Debug, Clone, Copy, PartialEq, Eq)] 547pub enum NestMode { 548 /// If the nesting mode is `Increase`, the current indentation will be 549 /// increased by the specified value. 550 Increase, 551 /// If the nesting mode is `Set`, the current indentation is going to be set 552 /// to exactly the specified value. 553 /// 554 /// `doc.nest(2).set_nesting(0)` 555 /// "wibble 556 /// wobble <- no indentation is added! 557 /// wubble" 558 Set, 559} 560 561macro_rules! const_str { 562 ($name:ident, $string:expr, $graphemes:expr) => { 563 pub const $name: $crate::Document<'static, 'static> = { 564 $crate::Document(&$crate::PrintableDocument::Str { 565 string: $string, 566 graphemes: $graphemes, 567 }) 568 }; 569 }; 570} 571 572macro_rules! const_zero_width_str { 573 ($name:ident, $string:expr) => { 574 pub const $name: $crate::Document<'static, 'static> = 575 { $crate::Document(&$crate::PrintableDocument::ZeroWidthStr { string: $string }) }; 576 }; 577} 578 579macro_rules! const_break { 580 ($name:ident, $broken:expr, $unbroken:expr) => { 581 pub const $name: $crate::Document<'static, 'static> = { 582 $crate::Document(&$crate::PrintableDocument::Break { 583 broken: $broken, 584 unbroken: $unbroken, 585 kind: $crate::BreakKind::Strict, 586 }) 587 }; 588 }; 589} 590 591/// The empty document that renders as nothing. 592pub const EMPTY_DOCUMENT: Document<'static, 'static> = Document(&PrintableDocument::Empty); 593 594pub const LINE_DOCUMENT: Document<'static, 'static> = Document(&PrintableDocument::Line(1)); 595 596pub const TWO_LINES_DOCUMENT: Document<'static, 'static> = Document(&PrintableDocument::Line(2)); 597 598pub const FLEX_COMMA_DOCUMENT: Document<'static, 'static> = Document(&PrintableDocument::Break { 599 broken: ",", 600 unbroken: ", ", 601 kind: BreakKind::Flex, 602}); 603 604const_str!(SPACE_DOCUMENT, " ", 1); 605const_str!(MODULE_COMMENT_DOCUMENT, "////", 4); 606const_str!(DOC_COMMENT_DOCUMENT, "///", 3); 607const_str!(CONST_SPACE_DOCUMENT, "const ", 5); 608const_str!(OPAQUE_TYPE_SPACE_DOCUMENT, "opaque type ", 12); 609const_str!(TYPE_SPACE_DOCUMENT, "type ", 5); 610const_str!(IMPORT_SPACE_DOCUMENT, "import ", 7); 611const_str!(SPACE_AS_SPACE_DOCUMENT, " as ", 4); 612const_str!(SPACE_EQUAL_SPACE_DOCUMENT, " = ", 3); 613const_str!(TODO_DOCUMENT, "todo", 4); 614const_str!(CONCAT_DOCUMENT, "<>", 2); 615const_str!(DOT_DOCUMENT, ".", 1); 616const_str!(DOT_DOT_DOCUMENT, "..", 2); 617const_str!(COLON_SPACE_DOCUMENT, ": ", 2); 618const_str!(OPEN_CURLY_DOCUMENT, "{", 1); 619const_str!(CLOSE_CURLY_DOCUMENT, "}", 1); 620const_str!(OPEN_SQUARE_DOCUMENT, "[", 1); 621const_str!(CLOSE_SQUARE_DOCUMENT, "]", 1); 622const_str!(OPEN_CLOSE_SQUARE_DOCUMENT, "[]", 2); 623const_str!(COMMENT_DOCUMENT, "//", 2); 624const_str!(ECHO_SPACE_DOCUMENT, "echo ", 5); 625const_str!(AS_DOCUMENT, "as", 2); 626const_str!(SPACE_AS_DOCUMENT, " as", 2); 627const_str!(OPEN_PAREN_DOCUMENT, "(", 1); 628const_str!(CLOSE_PAREN_DOCUMENT, ")", 1); 629const_str!(OPEN_CLOSE_PAREN_DOCUMENT, "()", 2); 630const_str!(USE_DOCUMENT, "use", 3); 631const_str!(USE_AND_ARROW_DOCUMENT, "use <-", 6); 632const_str!(LEFT_ARROW_DOCUMENT, "<-", 2); 633const_str!(MINUS_SPACE_DOCUMENT, "- ", 2); 634const_str!(EXCLAMATION_MARK_DOCUMENT, "!", 1); 635const_str!(ASSERT_SPACE_DOCUMENT, "assert ", 7); 636const_str!(PIPE_SPACE_DOCUMENT, "|> ", 3); 637const_str!(COLON_DOCUMENT, ":", 1); 638const_str!(HASHTAG_DOCUMENT, "#", 1); 639const_str!(SPACE_OPEN_CURLY_DOCUMENT, " {", 2); 640const_str!(EMPTY_TUPLE_DOCUMENT, "#()", 3); 641const_str!(OPEN_TUPLE_DOCUMENT, "#(", 2); 642const_str!(FN_DOCUMENT, "fn", 2); 643const_str!(FN_SPACE_DOCUMENT, "fn ", 3); 644const_str!(PANIC_DOCUMENT, "panic", 5); 645const_str!(IF_SPACE_DOCUMENT, "if ", 2); 646const_str!(SPACE_RIGHT_ARROW_DOCUMENT, " ->", 3); 647const_str!(SPACE_RIGHT_ARROW_SPACE_DOCUMENT, " -> ", 4); 648const_str!(LET_SPACE_DOCUMENT, "let ", 4); 649const_str!(LET_ASSERT_SPACE_DOCUMENT, "let assert ", 11); 650const_str!(SPACE_EQUAL_DOCUMENT, " =", 2); 651const_str!(UNDERSCORE_DOCUMENT, "_", 1); 652const_str!(DOUBLE_QUOTE_DOCUMENT, "\"", 1); 653const_str!(OPEN_PAREN_DOT_DOT_CLOSE_PAREN_DOCUMENT, "(..)", 4); 654const_str!(VERTICAL_BAR_SPACE_DOCUMENT, "| ", 2); 655const_str!(SPACE_CONCAT_SPACE_DOCUMENT, " <> ", 4); 656const_str!(SPACE_PLUS_SPACE_DOCUMENT, " + ", 3); 657const_str!(SPACE_MINUS_SPACE_DOCUMENT, " - ", 3); 658const_str!(SPACE_TIMES_SPACE_DOCUMENT, " * ", 3); 659const_str!(SPACE_MODULE_SPACE_DOCUMENT, " % ", 3); 660const_str!(SPACE_SLASH_SPACE_DOCUMENT, " / ", 3); 661const_str!(SPACE_CLOSE_CURLY_DOCUMENT, " }", 2); 662const_str!(OPEN_CURLY_SPACE_DOCUMENT, "{ ", 2); 663const_str!(CLOSE_BIT_ARRAY_DOCUMENT, ">>", 2); 664const_str!(ECHO_DOCUMENT, "echo", 4); 665const_str!(EMPTY_BIT_ARRAY_DOCUMENT, "<<>>", 4); 666const_str!(OPEN_BIT_ARRAY_DOCUMENT, "<<", 2); 667const_str!(RIGHT_ARROW_DOCUMENT, "->", 2); 668const_str!(PUB_SPACE_DOCUMENT, "pub ", 4); 669const_str!(SIZE_DOCUMENT, "size", 4); 670const_str!(BYTES_DOCUMENT, "bytes", 5); 671const_str!(BITS_DOCUMENT, "bits", 4); 672const_str!(INT_DOCUMENT, "int", 3); 673const_str!(FLOAT_DOCUMENT, "float", 5); 674const_str!(UTF8_DOCUMENT, "utf8", 4); 675const_str!(UTF16_DOCUMENT, "utf16", 5); 676const_str!(UTF32_DOCUMENT, "utf32", 5); 677const_str!(UTF8_CODEPOINT_DOCUMENT, "utf8_codepoint", 14); 678const_str!(UTF16_CODEPOINT_DOCUMENT, "utf16_codepoint", 15); 679const_str!(UTF32_CODEPOINT_DOCUMENT, "utf32_codepoint", 15); 680const_str!(SIGNED_DOCUMENT, "signed", 6); 681const_str!(UNSIGNED_DOCUMENT, "unsigned", 8); 682const_str!(BIG_DOCUMENT, "big", 3); 683const_str!(LITTLE_DOCUMENT, "little", 6); 684const_str!(NATIVE_DOCUMENT, "native", 6); 685const_str!(UNIT_DOCUMENT, "unit", 4); 686const_str!(AND_DOCUMENT, "&&", 2); 687const_str!(OR_DOCUMENT, "||", 2); 688const_str!(LT_INT_DOCUMENT, "<", 1); 689const_str!(LT_EQ_INT_DOCUMENT, "<=", 2); 690const_str!(LT_FLOAT_DOCUMENT, "<.", 2); 691const_str!(LT_EQ_FLOAT_DOCUMENT, "<=.", 3); 692const_str!(EQ_DOCUMENT, "==", 2); 693const_str!(NOT_EQ_DOCUMENT, "!=", 2); 694const_str!(GT_EQ_INT_DOCUMENT, ">=", 2); 695const_str!(GT_INT_DOCUMENT, ">", 1); 696const_str!(GT_EQ_FLOAT_DOCUMENT, ">=.", 3); 697const_str!(GT_FLOAT_DOCUMENT, ">.", 2); 698const_str!(ADD_INT_DOCUMENT, "+", 1); 699const_str!(ADD_FLOAT_DOCUMENT, "+.", 2); 700const_str!(SUB_INT_DOCUMENT, "-", 1); 701const_str!(SUB_FLOAT_DOCUMENT, "-.", 2); 702const_str!(MULT_INT_DOCUMENT, "*", 1); 703const_str!(MULT_FLOAT_DOCUMENT, "*.", 2); 704const_str!(DIV_INT_DOCUMENT, "/", 1); 705const_str!(DIV_FLOAT_DOCUMENT, "/.", 2); 706const_str!(REMAINDER_INT_DOCUMENT, "%", 1); 707const_str!(DEPRECATED_ATTRIBUTE_QUOTE_DOCUMENT, "@deprecated(\"", 13); 708const_str!(QUOTE_CLOSE_PAREN_DOCUMENT, "\")", 2); 709const_str!(EXTERNAL_ERLANG_QUOTE_DOCUMENT, "@external(erlang, \"", 19); 710const_str!( 711 EXTERNAL_JAVASCRIPT_QUOTE_DOCUMENT, 712 "@external(javascript, \"", 713 23 714); 715const_str!(QUOTE_COMMA_SPACE_QUOTE_DOCUMENT, "\", \"", 4); 716const_str!(INTERNAL_ATTRIBUTE_DOCUMENT, "@internal", 9); 717const_str!(INTERNAL_ATTRIBUTE_SPACE_DOCUMENT, "@internal ", 9); 718const_str!(TRUE_LOWERCASE_DOCUMENT, "true", 4); 719const_str!(FALSE_LOWERCASE_DOCUMENT, "false", 5); 720const_str!(OPEN_CLOSE_CURLY_DOCUMENT, "{}", 2); 721const_str!(SPACE_TIMES_AT_IGNORE_DOCUMENT, " * @ignore", 10); 722const_str!(SLASH_TIMES_TIMES_DOCUMENT, "/**", 3); 723const_str!(SPACE_TIMES_SLASH_DOCUMENT, " */", 3); 724const_str!(CONST_FILEPATH_EQUALS_DOCUMENT, "const FILEPATH = ", 17); 725const_str!(SEMICOLON_DOCUMENT, ";", 1); 726const_str!(FUNCTION_SPACE_DOCUMENT, "function ", 9); 727const_str!(EXPORT_FUNCTION_SPACE_DOCUMENT, "export function ", 16); 728const_str!(EXPORT_CONST_SPACE_DOCUMENT, "export const ", 13); 729const_str!(SUPER_CALL_SEMICOLON_DOCUMENT, "super();", 8); 730const_str!(CONSTRUCTOR_OPEN_PAREN_DOCUMENT, "constructor(", 12); 731const_str!(CLOSE_PAREN_SPACE_OPEN_CURLY_DOCUMENT, ") {", 3); 732const_str!(THIS_DOT_DOCUMENT, "this.", 5); 733const_str!(THIS_OPEN_SQUARE_DOCUMENT, "this[", 5); 734const_str!(CLOSE_SQUARE_SPACE_EQUAL_SPACE_DOCUMENT, "] = ", 4); 735const_str!( 736 SPACE_EXTENDS_CUSTOM_TYPE_OPEN_CURLY_DOCUMENT, 737 " extends $CustomType {", 738 22 739); 740const_str!(CLASS_SPACE_DOCUMENT, "class ", 6); 741const_str!(EXPORT_CLASS_SPACE_DOCUMENT, "export class ", 13); 742const_str!(VALUE_DOT_DOCUMENT, "value.", 6); 743const_str!(SPACE_EQUAL_SPACE_VALUE_SPACE_ARROW, " = (value) =>", 13); 744const_str!( 745 SPACE_EQUAL_OPEN_CLOSE_PAREN_SPACE_ARROW_DOCUMENT, 746 " = () =>", 747 8 748); 749const_str!(CLOSE_PAREN_SEMICOLON_DOCUMENT, ");", 2); 750const_str!(NEW_SPACE_DOCUMENT, "new ", 4); 751const_str!(VALUE_OPEN_SQUARE_DOCUMENT, "value[", 6); 752const_str!(CLOSE_SQUARE_SEMICOLON_DOCUMENT, "];", 2); 753const_str!(DOLLAR_IS_DOCUMENT, "$is", 3); 754const_str!(VALUE_INSTANCE_OF_SPACE_DOCUMENT, "value instanceof ", 17); 755const_str!(DOLLAR_DOCUMENT, "$", 1); 756const_str!(CLOSE_PAREN_ARROW_DOCUMENT, ") =>", 4); 757const_str!(CLOSE_PAREN_SLIM_ARROW_DOCUMENT, ") ->", 4); 758const_str!(SPACE_EQUAL_SPACE_OPEN_PAREN_DOCUMENT, " = (", 4); 759const_str!(DOLLAR_CONST_SEMICOLON_DOCUMENT, "$const;", 7); 760const_str!(DOLLAR_CONST_DOCUMENT, "$const", 6); 761const_str!(OPEN_CLOSE_PAREN_SEMICOLON_DOCUMENT, "();", 3); 762const_str!(EXPORT_SPACE_OPEN_CLOSE_CURLY_DOCUMENT, "export {}", 9); 763const_str!(EXPORT_SPACE_OPEN_CURLY_DOCUMENT, "export {", 8); 764const_str!(EXPORT_TYPE_SPACE_OPEN_CURLY_DOCUMENT, "export type {", 13); 765const_str!(DOT_MJS_DOT_MAP_DOCUMENT, ".mjs.map", 8); 766const_str!( 767 DOT_D_DOT_MTS_CLOSE_QUOTE_CLOSE_TAG_DOCUMENT, 768 ".d.mts\" />", 769 10 770); 771const_str!( 772 SOURCE_MAPPING_URL_EQUAL_DOCUMENT, 773 "//# sourceMappingURL=", 774 21 775); 776const_str!(REFERENCE_TYPES_DOCUMENT, "/// <reference types=\"./", 24); 777const_str!(CLOSE_CURLY_SEMICOLON_DOCUMENT, "};", 2); 778const_str!(TIMES_SPACE_AS_SPACE_DOCUMENT, "* as ", 5); 779const_str!(SPACE_FROM_SPACE_DOUBLE_QUOTE_DOCUMENT, " from \"", 7); 780const_str!(DOUBLE_QUOTE_SEMICOLON_DOCUMENT, "\";", 2); 781const_str!( 782 CLOSE_CURLY_SPACE_FROM_SPACE_DOUBLE_QUOTE_DOCUMENT, 783 "} from \"", 784 8 785); 786const_str!(EXPORT_TYPE_SPACE_DOCUMENT, "export type ", 12); 787const_str!(X_DOCUMENT, "x", 1); 788const_str!(ANY_DOCUMENT, "any", 3); 789const_str!(EXPORT_SPACE_DOCUMENT, "export ", 7); 790const_str!(DECLARE_SPACE_DOCUMENT, "declare ", 8); 791const_str!( 792 SPACE_EXTENDS_UNDERSCORE_CUSTOM_TYPE_DOCUMENT, 793 " extends _.CustomType {", 794 23 795); 796const_str!(CONSTRUCTOR_DOCUMENT, "constructor", 11); 797const_str!( 798 DEPRECATED_MULTILINE_COMMENT_DOCUMENT, 799 "/** @deprecated */", 800 18 801); 802const_str!(CLOSE_PAREN_COLON_SPACE_DOCUMENT, "): ", 3); 803const_str!(VALUE_COLON_SPACE_ANY_DOCUMENT, "value: any", 10); 804const_str!( 805 CLOSE_PAREN_COLON_SPACE_VALUE_IS_SPACE_DOCUMENT, 806 "): value is ", 807 12 808); 809const_str!(LT_INT_UNKNOWN_DOCUMENT, "<unknown", 8); 810const_str!(COMMA_SPACE_UNKNOWN_DOCUMENT, ", unknown", 9); 811const_str!(VALUE_COLON_SPACE_DOCUMENT, "value: ", 7); 812const_str!(UNDEFINED_DOCUMENT, "undefined", 9); 813const_str!(NUMBER_DOCUMENT, "number", 6); 814const_str!(UNDERSCORE_DOT_UTF_CODEPOINT_DOCUMENT, "_.UtfCodepoint", 14); 815const_str!(STRING_DOCUMENT, "string", 6); 816const_str!(BOOLEAN_DOCUMENT, "boolean", 7); 817const_str!(UNDERSCORE_DOT_BIT_ARRAY_DOCUMENT, "_.BitArray", 10); 818const_str!(UNDERSCORE_DOT_LIST_DOCUMENT, "_.List", 6); 819const_str!(UNDERSCORE_DOT_RESULT_DOCUMENT, "_.Result", 8); 820const_str!(SPACE_EQUAL_RIGHT_ARROW_SPACE_DOCUMENT, " => ", 4); 821const_str!(IF_SPACE_OPEN_PAREN_DOCUMENT, "if (", 4); 822const_str!(CLOSE_PAREN_SPACE_DOCUMENT, ") ", 2); 823const_str!(DOUBLE_CLOSE_PAREN_SPACE_DOCUMENT, ")) ", 3); 824const_str!( 825 IF_SPACE_OPEN_PAREN_EXCLAMATION_OPEN_PAREN_DOCUMENT, 826 "if (!(", 827 6 828); 829const_str!(SPACE_ELSE_SPACE_DOCUMENT, " else ", 6); 830const_str!(DOT_CHAR_CODE_AT_ZERO_DOCUMENT, ".charCodeAt(0)", 14); 831const_str!(SPACE_TRIPLE_EQUAL_SPACE_DOCUMENT, " === ", 5); 832const_str!(TRIPLE_EQUAL_DOCUMENT, "===", 3); 833const_str!(DOT_STARTS_WITH_OPEN_PAREN_DOCUMENT, ".startsWith(", 12); 834const_str!(ZERO_DOCUMENT, "0", 1); 835const_str!(DOT_BIT_SIZE_MODULO_8_DOCUMENT, ".bitSize % 8", 12); 836const_str!(DOT_BIT_SIZE_MINUS_SPACE_DOCUMENT, ".bitSize - ", 11); 837const_str!(DOT_BIT_SIZE_DOCUMENT, ".bitSize", 8); 838const_str!(CLOSE_PAREN_MODULO_8_DOCUMENT, ") % 8", 5); 839const_str!(SPACE_GT_EQ_ZERO_DOCUMENT, " >= 0", 5); 840const_str!(SPACE_GT_EQ_SPACE_DOCUMENT, " >= ", 4); 841const_str!( 842 NUMBER_DOT_IS_FINITE_OPEN_PAREN_DOCUMENT, 843 "Number.isFinite(", 844 16 845); 846const_str!(SPACE_INSTANCE_OF_SPACE_DOCUMENT, " instanceof ", 12); 847const_str!( 848 SPACE_INSTANCE_OF_NON_EMPTY_DOCUMENT, 849 " instanceof $NonEmpty", 850 21 851); 852const_str!(SPACE_INSTANCE_OF_EMPTY_DOCUMENT, " instanceof $Empty", 18); 853const_str!(DOT_BYTE_AT_OPEN_PAREN_DOCUMENT, ".byteAt(", 8); 854const_str!(COMMA_SPACE_DOCUMENT, ", ", 2); 855const_str!( 856 BIT_ARRAY_SLICE_TO_INT_OPEN_PAREN_DOCUMENT, 857 "bitArraySliceToInt(", 858 19 859); 860const_str!( 861 BIT_ARRAY_SLICE_TO_FLOAT_OPEN_PAREN_DOCUMENT, 862 "bitArraySliceToFloat(", 863 21 864); 865const_str!(BIT_ARRAY_SLICE_OPEN_PAREN_DOCUMENT, "bitArraySlice(", 14); 866const_str!(RETURN_SPACE_DOCUMENT, "return ", 7); 867const_str!(SPACE_EQUAL_LOOP_DOLLAR_DOCUMENT, " = loop$", 8); 868const_str!(WHILE_TRUE_OPEN_PAREN_DOCUMENT, "while (true) {", 14); 869const_str!( 870 DOLLAR_LIST_DOLLAR_EMPTY_DOLLAR_CONST_DOCUMENT, 871 "$List$Empty$const", 872 17 873); 874const_str!(SIZED_INT_OPEN_PAREN_DOCUMENT, "sizedInt(", 9); 875const_str!(SIZED_FLOAT_OPEN_PAREN_DOCUMENT, "sizedFloat(", 11); 876const_str!(STRING_BITS_OPEN_PAREN_DOCUMENT, "stringBits(", 11); 877const_str!(STRING_TO_UTF16_OPEN_PAREN_DOCUMENT, "stringToUtf16(", 14); 878const_str!(STRING_TO_UTF32_OPEN_PAREN_DOCUMENT, "stringToUtf32(", 14); 879const_str!( 880 CODEPOINT_TO_UTF16_OPEN_PAREN_DOCUMENT, 881 "codepointToUtf16(", 882 17 883); 884const_str!( 885 CODEPOINT_TO_UTF32_OPEN_PAREN_DOCUMENT, 886 "codepointToUtf32(", 887 17 888); 889const_str!(TO_BIT_ARRAY_OPEN_PAREN_DOCUMENT, "toBitArray(", 11); 890const_str!(COMMA_ZERO_COMMA_SPACE, ", 0, ", 5); 891const_str!(THROW_MAKE_ERROR_DOCUMENT, "throw makeError", 15); 892const_str!(FILEPATH_UPPERCASE_DOCUMENT, "FILEPATH", 8); 893const_str!(SPACE_EQUAL_ARROW_SPACE_OPEN_CURLY_DOCUMENT, " => {", 5); 894const_str!(INFINITY_DOCUMENT, "Infinity", 8); 895const_str!(MINUS_INFINITY_DOCUMENT, "-Infinity", 9); 896const_str!(NAN_DOCUMENT, "NaN", 3); 897const_str!(TO_LIST_OPEN_PAREN_DOCUMENT, "toList(", 7); 898const_str!(LIST_PREPEND_DOCUMENT, "listPrepend", 11); 899const_str!(CLOSE_CURLY_CLOSE_PAREN_OPEN_CLOSE_PAREN_DOCUMENT, "})()", 4); 900const_str!( 901 OPEN_PAREN_OPEN_CLOSE_PAREN_EQUAL_ARROW_OPEN_CURLY_DOCUMENT, 902 "(() => {", 903 8 904); 905const_str!(CODEPOINT_BITS_OPEN_PAREN_DOCUMENT, "codepointBits(", 14); 906const_str!(CLOSE_CURLY_ELSE_OPEN_CURLY_DOCUMENT, "} else {", 8); 907const_str!(EXCLAMATION_MARK_OPEN_PAREN_DOCUMENT, "!(", 2); 908const_str!(SPACE_DOUBLE_VERTICAL_BAR_SPACE_DOCUMENT, " || ", 4); 909const_str!(SPACE_DOUBLE_AMPERSAND_SPACE_DOCUMENT, " && ", 4); 910const_str!(LOOP_DOLLAR_DOCUMENT, "loop$", 5); 911const_str!(DIVIDE_INT_DOCUMENT, "divideInt", 9); 912const_str!(DIVIDE_FLOAT_DOCUMENT, "divideFloat", 9); 913const_str!( 914 GLOBAL_THIS_DOT_MATH_DOT_TRUNC_DOCUMENT, 915 "globalThis.Math.trunc", 916 21 917); 918const_str!(SPACE_MODULO_SPACE_DOCUMENT, " % ", 3); 919const_str!( 920 SPACE_EXCLAMATION_MARK_DOUBLE_EQUAL_SPACE_DOCUMENT, 921 " !== ", 922 5 923); 924const_str!(EXCLAMATION_MARK_DOUBLE_EQUAL_DOCUMENT, "!==", 3); 925const_str!(IS_EQUAL_DOCUMENT, "isEqual", 7); 926const_str!(EXCLAMATION_MARK_IS_EQUAL_DOCUMENT, "!isEqual", 8); 927const_str!(SPACE_LT_INT_SPACE_DOCUMENT, " < ", 3); 928const_str!(SPACE_LT_EQ_INT_SPACE_DOCUMENT, " <= ", 4); 929const_str!(SPACE_GT_INT_SPACE_DOCUMENT, " > ", 3); 930const_str!(SPACE_GT_EQ_INT_SPACE_DOCUMENT, " >= ", 4); 931const_str!(CAMEL_CASE_REMAINDER_INT_DOCUMENT, "remainderInt", 12); 932const_str!(PURE_JAVASCRIPT_COMMENT_DOCUMENT, "/* @__PURE__ */ ", 16); 933const_str!(LOWERCASE_ARG_DOCUMENT, "arg", 3); 934const_str!(PUB_TYPE_SPACE_DOCUMENT, "pub type ", 9); 935const_str!(PUB_FN_SPACE_DOCUMENT, "pub fn ", 7); 936const_str!(PUB_CONST_SPACE_DOCUMENT, "pub const ", 10); 937const_str!(PUB_OPAQUE_TYPE_SPACE_DOCUMENT, "pub opaque type ", 16); 938const_str!(FN_OPEN_PAREN_DOCUMENT, "fn(", 3); 939 940const_zero_width_str!(ZERO_WIDTH_CLOSED_SPAN_TAG_DOCUMENT, "</span>"); 941const_zero_width_str!(ZERO_WIDTH_CLOSED_A_TAG_DOCUMENT, "</a>"); 942const_zero_width_str!( 943 ZERO_WIDTH_OPEN_VARIABLE_COLOUR_SPAN, 944 r#"<span class="hljs-variable">"# 945); 946const_zero_width_str!( 947 ZERO_WIDTH_OPEN_TITLE_COLOUR_SPAN, 948 r#"<span class="hljs-title">"# 949); 950const_zero_width_str!( 951 ZERO_WIDTH_OPEN_KEYWORD_COLOUR_SPAN, 952 r#"<span class="hljs-keyword">"# 953); 954const_zero_width_str!( 955 ZERO_WIDTH_OPEN_COMMENT_COLOUR_SPAN, 956 r#"<span class="hljs-comment">"# 957); 958 959const_break!(EMPTY_BREAK_DOCUMENT, "", ""); 960const_break!(BREAKABLE_SPACE_DOCUMENT, "", " "); 961const_break!(COMMA_BREAK_DOCUMENT, ",", ", "); 962const_break!(TRAILING_COMMA_BREAK_DOCUMENT, ",", ""); 963const_break!(TRAILING_COMMA_OR_SPACE_BREAK_DOCUMENT, ",", " "); 964const_break!(OPEN_CURLY_BREAK_DOCUMENT, "{", "{ "); 965const_break!(OPEN_SQUARE_BREAK_DOCUMENT, "[", "["); 966const_break!(OPEN_PAREN_BREAK_DOCUMENT, "(", "("); 967const_break!(OPEN_TUPLE_BREAK_DOCUMENT, "#(", "#("); 968const_break!(OPEN_BIT_ARRAY_BREAK_DOCUMENT, "<<", "<<"); 969const_break!(CASE_BREAK_DOCUMENT, "case", "case "); 970const_break!(SPACE_DOUBLE_AMPERSAND_BREAK_DOCUMENT, " &&", " && "); 971 972/// A structure used to efficiently allocate documents that can then be pretty 973/// printed. 974pub struct DocumentArena<'string, 'doc> { 975 documents: Arena<PrintableDocument<'string, 'doc>>, 976} 977 978impl<'string, 'doc> std::fmt::Debug for DocumentArena<'string, 'doc> { 979 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 980 f.debug_struct("DocumentArena").finish() 981 } 982} 983 984impl<'string, 'doc> Default for DocumentArena<'string, 'doc> { 985 fn default() -> Self { 986 Self::new() 987 } 988} 989 990impl<'string, 'doc> DocumentArena<'string, 'doc> { 991 /// Creates a new empty arena with no documents allocated. 992 pub fn new() -> Self { 993 let documents = Arena::new(); 994 Self { documents } 995 } 996 997 /// Allocates a document that is rendered as a single line. 998 pub fn line(&'doc self) -> Document<'string, 'doc> { 999 self.lines(1) 1000 } 1001 1002 pub fn position_observer( 1003 &'doc self, 1004 observer: Rc<RefCell<dyn CursorPositionObserver>>, 1005 ) -> Document<'string, 'doc> { 1006 Document( 1007 self.documents 1008 .alloc(PrintableDocument::CursorPositionObserver { observer }), 1009 ) 1010 } 1011 1012 /// Renders a string of newlines, equal in length to the number provided. 1013 #[inline] 1014 pub fn lines(&'doc self, i: usize) -> Document<'string, 'doc> { 1015 Document(self.documents.alloc(PrintableDocument::Line(i))) 1016 } 1017 1018 /// A document after which the formatter can insert a newline. This determines 1019 /// where line breaks can occur, outside of hardcoded `Line`s. 1020 /// 1021 /// If the formatter determines that a group cannot fit on a single line, 1022 /// all breaks in the group will be rendered as broken. Otherwise, they 1023 /// will be rendered as unbroken. 1024 /// 1025 /// A broken `Break` renders the `broken` string, followed by a newline. 1026 /// An unbroken `Break` renders the `unbroken` string by itself. 1027 /// 1028 /// For example: 1029 /// ```rust:norun 1030 /// let document = docvec!["Hello", break_("", ", "), "world!"]; 1031 /// assert_eq!(document.to_pretty_string(20), "Hello, world!"); 1032 /// assert_eq!(document.to_pretty_string(10), "Hello\nworld!"); 1033 /// ``` 1034 /// 1035 pub fn break_( 1036 &'doc self, 1037 broken: &'string str, 1038 unbroken: &'string str, 1039 ) -> Document<'string, 'doc> { 1040 Document(self.documents.alloc(PrintableDocument::Break { 1041 broken, 1042 unbroken, 1043 kind: BreakKind::Strict, 1044 })) 1045 } 1046 1047 /// A document after which the formatter can insert a newline, similar to 1048 /// `break_()`. The difference is that when a group is rendered broken, all 1049 /// breaks are rendered broken. However, `flex_break` decides whether to 1050 /// break or not for every individual `flex_break`. 1051 /// 1052 /// For example: 1053 /// ```rust:norun 1054 /// let with_breaks = docvec!["Hello", break_("", ", "), "pretty", break_("", ", "), "printed!"]; 1055 /// assert_eq!(with_breaks.to_pretty_string(20), "Hello\npretty\nprinted!"); 1056 /// 1057 /// let with_flex_breaks = docvec!["Hello", flex_break("", ", "), "pretty", flex_break("", ", "), "printed!"]; 1058 /// assert_eq!(with_flex_breaks.to_pretty_string(20), "Hello, pretty\nprinted!"); 1059 /// ``` 1060 /// 1061 pub fn flex_break( 1062 &'doc self, 1063 broken: &'string str, 1064 unbroken: &'string str, 1065 ) -> Document<'string, 'doc> { 1066 Document(self.documents.alloc(PrintableDocument::Break { 1067 broken, 1068 unbroken, 1069 kind: BreakKind::Flex, 1070 })) 1071 } 1072 1073 /// A string that is not taken into account when determining line length. 1074 /// This is useful for additional formatting text which won't be rendered 1075 /// in the final output, such as ANSI codes or HTML elements. 1076 /// 1077 /// For example: 1078 /// ```rust:norun 1079 /// let document = docvec!["Hello", zero_width_string("This is a very long string"), break_("", ""), "world"]; 1080 /// assert_eq!(document.to_pretty_string(20), "HelloThis is a very long stringworld"); 1081 /// ``` 1082 /// 1083 pub fn zero_width_string(&'doc self, string: EcoString) -> Document<'string, 'doc> { 1084 Document( 1085 self.documents 1086 .alloc(PrintableDocument::ZeroWidthString { string }), 1087 ) 1088 } 1089 1090 /// Joins together an iterator of documents into a single document. 1091 /// All the documents are gonna be rendered next to each other with no 1092 /// spaces in between. 1093 pub fn concat( 1094 &'doc self, 1095 documents: impl IntoIterator<Item = Document<'string, 'doc>>, 1096 ) -> Document<'string, 'doc> { 1097 let mut documents = documents.into_iter().peekable(); 1098 let Some(mut previous) = documents.next() else { 1099 return EMPTY_DOCUMENT; 1100 }; 1101 1102 while let Some(second) = documents.next() { 1103 previous = if let Some(third) = documents.next() { 1104 if let Some(fourth) = documents.next() { 1105 docvec![self, previous, second, third, fourth] 1106 } else { 1107 docvec![self, previous, second, third] 1108 } 1109 } else { 1110 previous.append(self, second) 1111 }; 1112 } 1113 1114 previous 1115 } 1116 1117 /// Joins an iterator into a single document, interspersing each element with 1118 /// another document. This is useful for example in argument lists, where a 1119 /// list of arguments must all be separated with a comma. 1120 pub fn join( 1121 &'doc self, 1122 elements: impl IntoIterator<Item = Document<'string, 'doc>>, 1123 separator: Document<'string, 'doc>, 1124 ) -> Document<'string, 'doc> { 1125 let mut elements = elements.into_iter().peekable(); 1126 let Some(mut previous) = elements.next() else { 1127 return EMPTY_DOCUMENT; 1128 }; 1129 1130 for next in elements { 1131 previous = docvec![self, previous, separator, next]; 1132 } 1133 1134 previous 1135 } 1136 1137 /// Joins exactly three documents into one. 1138 /// This is a little optimisation over joining three documents using `join` 1139 /// or `.append`. 1140 pub fn join3( 1141 &'doc self, 1142 first: impl Documentable<'string, 'doc>, 1143 second: impl Documentable<'string, 'doc>, 1144 third: impl Documentable<'string, 'doc>, 1145 ) -> Document<'string, 'doc> { 1146 Document(self.documents.alloc(PrintableDocument::Join3( 1147 first.to_doc(self), 1148 second.to_doc(self), 1149 third.to_doc(self), 1150 ))) 1151 } 1152 1153 /// Joins exactly four documents into one. 1154 /// This is a little optimisation over joining three documents using `join` 1155 /// or `.append`. 1156 pub fn join4( 1157 &'doc self, 1158 first: impl Documentable<'string, 'doc>, 1159 second: impl Documentable<'string, 'doc>, 1160 third: impl Documentable<'string, 'doc>, 1161 fourth: impl Documentable<'string, 'doc>, 1162 ) -> Document<'string, 'doc> { 1163 Document(self.documents.alloc(PrintableDocument::Join4( 1164 first.to_doc(self), 1165 second.to_doc(self), 1166 third.to_doc(self), 1167 fourth.to_doc(self), 1168 ))) 1169 } 1170} 1171 1172fn format<'string, 'doc>( 1173 writer: &mut impl std::fmt::Write, 1174 limit: isize, 1175 mut docs: im::Vector<(isize, Mode, Document<'string, 'doc>)>, 1176) -> std::fmt::Result { 1177 let mut line: isize = 0; 1178 let mut width: isize = 0; 1179 // As long as there are documents to print we'll take each one by one and 1180 // output the corresponding string to the given writer. 1181 // 1182 // Each document in the `docs` queue also has an accompanying indentation 1183 // and mode: 1184 // - the indentation is used to keep track of the current indentation, 1185 // you might notice in [ref:format-nest] that it adds documents to the 1186 // queue increasing their current indentation. 1187 // - the mode is used to keep track of the state of the documents inside a 1188 // group. For example, if a group doesn't fit on a single line its 1189 // documents will be split into multiple lines and the mode set to 1190 // `Broken` to keep track of this. 1191 while let Some((indent, mode, document)) = docs.pop_front() { 1192 match document.0 { 1193 // When we run into a line we print the given number of newlines and 1194 // add the indentation required by the given document. 1195 PrintableDocument::Line(count) => { 1196 for _ in 0..*count { 1197 writer.write_str("\n")?; 1198 } 1199 line += *count as isize; 1200 for _ in 0..indent { 1201 writer.write_char(' ')?; 1202 } 1203 width = indent; 1204 } 1205 1206 // Flex breaks are NOT conditional to the mode: if the mode is 1207 // already `Unbroken`, then the break is left unbroken (like strict 1208 // breaks); any other mode is ignored. 1209 // A flexible break will only be split if the following documents 1210 // can't fit on the same line; otherwise, it is just displayed as an 1211 // unbroken `Break`. 1212 PrintableDocument::Break { 1213 broken, 1214 unbroken, 1215 kind: BreakKind::Flex, 1216 } => { 1217 let unbroken_width = width + unbroken.len() as isize; 1218 // Every time we need to check again if the remaining piece can 1219 // fit. If it does, the flexible break is not broken. 1220 if mode == Mode::Unbroken || fits(limit, unbroken_width, docs.clone()) { 1221 writer.write_str(unbroken)?; 1222 width = unbroken_width; 1223 } else { 1224 writer.write_str(broken)?; 1225 writer.write_char('\n')?; 1226 line += 1; 1227 for _ in 0..indent { 1228 writer.write_char(' ')?; 1229 } 1230 width = indent; 1231 } 1232 } 1233 1234 // Strict breaks are conditional to the mode. They differ from 1235 // flexible break because, if a group gets split - that is the mode 1236 // is `Broken` or `ForceBroken` - ALL of the breaks in that group 1237 // will be split. You can notice the difference with flexible breaks 1238 // because here we only check the mode and then take action; before 1239 // we would try and see if the remaining documents fit on a single 1240 // line before deciding if the (flexible) break can be split or not. 1241 PrintableDocument::Break { 1242 broken, 1243 unbroken, 1244 kind: BreakKind::Strict, 1245 } => match mode { 1246 // If the mode requires the break to be broken, then its broken 1247 // string is printed, then we start a newline and indent it 1248 // according to the current indentation level. 1249 Mode::Broken | Mode::ForcedBroken => { 1250 writer.write_str(broken)?; 1251 writer.write_char('\n')?; 1252 line += 1; 1253 for _ in 0..indent { 1254 writer.write_char(' ')?; 1255 } 1256 width = indent; 1257 } 1258 // If the mode doesn't require the break to be broken, then its 1259 // unbroken string is printed as if it were a normal string; 1260 // also updating the width of the current line. 1261 Mode::Unbroken | Mode::ForcedUnbroken => { 1262 writer.write_str(unbroken)?; 1263 width += unbroken.len() as isize 1264 } 1265 }, 1266 1267 // Strings are printed as they are and the current width is 1268 // increased accordingly. 1269 PrintableDocument::EcoString { string, graphemes } => { 1270 width += graphemes; 1271 writer.write_str(string)?; 1272 } 1273 1274 PrintableDocument::Str { string, graphemes } => { 1275 width += graphemes; 1276 writer.write_str(string)?; 1277 } 1278 1279 PrintableDocument::ZeroWidthString { string } => { 1280 // We write the string, but do not increment the length 1281 writer.write_str(string)?; 1282 } 1283 PrintableDocument::ZeroWidthStr { string } => { 1284 // We write the string, but do not increment the length 1285 writer.write_str(string)?; 1286 } 1287 1288 // If multiple documents need to be printed, then they are all 1289 // pushed to the front of the queue and will be printed one by one. 1290 PrintableDocument::Join(first, second) => { 1291 // Just like `fits`, the elements will be pushed _on the front_ 1292 // of the queue. In order to keep their original order they need 1293 // to be pushed in reverse order. 1294 docs.push_front((indent, mode, *second)); 1295 docs.push_front((indent, mode, *first)); 1296 } 1297 PrintableDocument::Join3(first, second, third) => { 1298 docs.push_front((indent, mode, *third)); 1299 docs.push_front((indent, mode, *second)); 1300 docs.push_front((indent, mode, *first)); 1301 } 1302 PrintableDocument::Join4(first, second, third, fourth) => { 1303 docs.push_front((indent, mode, *fourth)); 1304 docs.push_front((indent, mode, *third)); 1305 docs.push_front((indent, mode, *second)); 1306 docs.push_front((indent, mode, *first)); 1307 } 1308 1309 // A `Nest` document doesn't result in anything being printed, its 1310 // only effect is to increase the current nesting level for the 1311 // wrapped document [tag:format-nest]. 1312 PrintableDocument::Nest(i, nest_mode, condition, doc) => match (condition, mode) { 1313 // The nesting is only applied under two conditions: 1314 // - either the nesting condition is `Always`. 1315 // - or the condition is `IfBroken` and the group was actually 1316 // broken (that is, the current mode is `Broken`). 1317 (NestCondition::Always, _) | (NestCondition::IfBroken, Mode::Broken) => { 1318 let new_indent = match nest_mode { 1319 NestMode::Increase => indent + i, 1320 NestMode::Set => *i, 1321 }; 1322 docs.push_front((new_indent, mode, *doc)) 1323 } 1324 // If none of the above conditions is met, then the nesting is 1325 // not applied. 1326 _ => docs.push_front((indent, mode, *doc)), 1327 }, 1328 1329 PrintableDocument::Group(doc) => { 1330 // When we see a group we first try and see if it can fit on a 1331 // single line without breaking any break; that is why we use 1332 // the `Unbroken` mode here: we want to try to fit everything on 1333 // a single line. 1334 let group_docs = im::vector![(indent, Mode::Unbroken, *doc)]; 1335 if fits(limit, width, group_docs) { 1336 // If everything can stay on a single line we print the 1337 // wrapped document with the `Unbroken` mode, leaving all 1338 // the group's break as unbroken. 1339 docs.push_front((indent, Mode::Unbroken, *doc)); 1340 } else { 1341 // Otherwise, we need to break the group. We print the 1342 // wrapped document changing its mode to `Broken` so that 1343 // all its breaks will be split on newlines. 1344 docs.push_front((indent, Mode::Broken, *doc)); 1345 } 1346 } 1347 1348 // `ForceBroken` and `NextBreakFits` only change the way the `fit` 1349 // function works but do not actually change the formatting of a 1350 // document by themselves. That's why when we run into those we 1351 // just go on printing the wrapped document without altering the 1352 // current mode. 1353 PrintableDocument::ForceBroken(document) 1354 | PrintableDocument::NextBreakFits(document, _) => { 1355 docs.push_front((indent, mode, *document)); 1356 } 1357 1358 PrintableDocument::CursorPositionObserver { observer } => { 1359 // Notify the observer of the current cursor position 1360 observer.borrow_mut().observe_cursor_position(line, width); 1361 } 1362 1363 PrintableDocument::Empty => (), 1364 } 1365 } 1366 Ok(()) 1367} 1368 1369fn fits<'string, 'doc>( 1370 limit: isize, 1371 mut current_width: isize, 1372 mut docs: im::Vector<(isize, Mode, Document<'string, 'doc>)>, 1373) -> bool { 1374 // The `fits` function is going to take each document from the `docs` queue 1375 // and check if those can fit on a single line. In order to do so documents 1376 // are going to be pushed in front of this queue and have to be accompanied 1377 // by additional information: 1378 // - the document indentation, that can be increased by the `Nest` block. 1379 // - the current mode; this is needed to know if a group is being broken or 1380 // not and treat `Break`s differently as a consequence. You can see how 1381 // the behaviour changes in [ref:break-fit]. 1382 // 1383 // The loop might be broken earlier without checking all documents under one 1384 // of two conditions: 1385 // - the documents exceed the line `limit` and surely won't fit 1386 // [ref:document-unfit]. 1387 // - the documents are sure to fit the line - for example, if we meet a 1388 // broken `Break` [ref:break-fit] or a newline [ref:newline-fit]. 1389 loop { 1390 // [tag:document-unfit] If we've exceeded the maximum width allowed for 1391 // a line, it means that the document won't fit on a single line, we can 1392 // break the loop. 1393 if current_width > limit { 1394 return false; 1395 }; 1396 1397 // We start by checking the first document of the queue. If there's no 1398 // documents then we can safely say that it fits (if reached this point 1399 // it means that the limit wasn't exceeded). 1400 let (indent, mode, document) = match docs.pop_front() { 1401 Some(x) => x, 1402 None => return true, 1403 }; 1404 1405 match document.0 { 1406 // If a document is marked as `ForceBroken` we can immediately say 1407 // that it doesn't fit, so that every break is going to be 1408 // forcefully broken. 1409 PrintableDocument::ForceBroken(doc) => match mode { 1410 // If the mode is `ForcedBroken` it means that we have to ignore 1411 // this break [ref:forced-broken], so we go check the inner 1412 // document ignoring the effects of this one. 1413 Mode::ForcedBroken => docs.push_front((indent, mode, *doc)), 1414 Mode::Broken | Mode::Unbroken | Mode::ForcedUnbroken => return false, 1415 }, 1416 1417 // [tag:newline-fit] When we run into a line we know that the 1418 // document has a bit that fits in the current line; if it didn't 1419 // fit (that is, it exceeded the maximum allowed width) the loop 1420 // would have been broken by one of the earlier checks. 1421 PrintableDocument::Line(_) => return true, 1422 1423 // If the nesting level is increased we go on checking the wrapped 1424 // document and increase its indentation level based on the nesting 1425 // condition. 1426 PrintableDocument::Nest(i, nest_mode, condition, doc) => match condition { 1427 NestCondition::IfBroken => docs.push_front((indent, mode, *doc)), 1428 NestCondition::Always => { 1429 let new_indent = match nest_mode { 1430 NestMode::Increase => indent + i, 1431 NestMode::Set => *i, 1432 }; 1433 docs.push_front((new_indent, mode, *doc)) 1434 } 1435 }, 1436 1437 // As a general rule, a group fits if it can stay on a single line 1438 // without its breaks being broken down. 1439 PrintableDocument::Group(doc) => match mode { 1440 // If an outer group was broken, we still try to fit the inner 1441 // group on a single line, that's why for the inner document 1442 // we change the mode back to `Unbroken`. 1443 Mode::Broken => docs.push_front((indent, Mode::Unbroken, *doc)), 1444 // Any other mode is preserved as-is: if the mode is forced it 1445 // has to be left unchanged, and if the mode is already unbroken 1446 // there's no need to change it. 1447 Mode::Unbroken | Mode::ForcedBroken | Mode::ForcedUnbroken => { 1448 docs.push_front((indent, mode, *doc)) 1449 } 1450 }, 1451 1452 // When we run into a string we increase the current_width; looping 1453 // back we will check if we've exceeded the maximum allowed width. 1454 PrintableDocument::Str { graphemes, .. } 1455 | PrintableDocument::EcoString { graphemes, .. } => current_width += graphemes, 1456 1457 // Zero width strings do nothing: they do not contribute to line length 1458 PrintableDocument::ZeroWidthString { .. } 1459 | PrintableDocument::ZeroWidthStr { .. } 1460 | PrintableDocument::CursorPositionObserver { .. } => {} 1461 1462 // If we get to a break we need to first see if it has to be 1463 // rendered as its unbroken or broken string, depending on the mode. 1464 PrintableDocument::Break { unbroken, .. } => match mode { 1465 // [tag:break-fit] If the break has to be broken we're done! 1466 // We haven't exceeded the maximum length (otherwise the loop 1467 // iteration would have stopped with one of the earlier checks), 1468 // and - since it needs to be broken - we'll have to go on a new 1469 // line anyway. 1470 // This means that the document inspected so far will fit on a 1471 // single line, thus we return true. 1472 Mode::Broken | Mode::ForcedBroken => return true, 1473 // If the break is not broken then it will be rendered inline as 1474 // its unbroken string, so we treat it exactly as if it were a 1475 // normal string. 1476 Mode::Unbroken | Mode::ForcedUnbroken => current_width += unbroken.len() as isize, 1477 }, 1478 1479 // The `NextBreakFits` can alter the current mode to `ForcedBroken` 1480 // or `ForcedUnbroken` based on its enabled flag. 1481 PrintableDocument::NextBreakFits(doc, enabled) => match enabled { 1482 // [tag:disable-next-break] If it is disabled then we check the 1483 // wrapped document changing the mode to `ForcedUnbroken`. 1484 NextBreakFitsMode::Disabled => { 1485 docs.push_front((indent, Mode::ForcedUnbroken, *doc)) 1486 } 1487 NextBreakFitsMode::Enabled => match mode { 1488 // If we're in `ForcedUnbroken` mode it means that the check 1489 // was disabled by a document wrapping this one 1490 // [ref:disable-next-break]; that's why we do nothing and 1491 // check the wrapped document as if it were a normal one. 1492 Mode::ForcedUnbroken => docs.push_front((indent, mode, *doc)), 1493 // [tag:forced-broken] Any other mode is turned into 1494 // `ForcedBroken` so that when we run into a break, the 1495 // response to the question "Does the document fit?" will be 1496 // yes [ref:break-fit]. 1497 // This is why this is called `NextBreakFit` I think. 1498 Mode::Broken | Mode::Unbroken | Mode::ForcedBroken => { 1499 docs.push_front((indent, Mode::ForcedBroken, *doc)) 1500 } 1501 }, 1502 }, 1503 1504 // If there's a sequence of documents we will check each one, one 1505 // after the other to see if - as a whole - they can fit on a single 1506 // line. 1507 PrintableDocument::Join(first, second) => { 1508 // The array needs to be reversed to preserve the order of the 1509 // documents since each one is pushed _to the front_ of the 1510 // queue of documents to check. 1511 docs.push_front((indent, mode, *second)); 1512 docs.push_front((indent, mode, *first)); 1513 } 1514 PrintableDocument::Join3(first, second, third) => { 1515 docs.push_front((indent, mode, *third)); 1516 docs.push_front((indent, mode, *second)); 1517 docs.push_front((indent, mode, *first)); 1518 } 1519 PrintableDocument::Join4(first, second, third, fourth) => { 1520 docs.push_front((indent, mode, *fourth)); 1521 docs.push_front((indent, mode, *third)); 1522 docs.push_front((indent, mode, *second)); 1523 docs.push_front((indent, mode, *first)); 1524 } 1525 1526 PrintableDocument::Empty => (), 1527 } 1528 } 1529}