Skip to main content

lf_to_crlf_outside_strings

Function lf_to_crlf_outside_strings 

Source
pub fn lf_to_crlf_outside_strings(s: &str) -> String
Expand description

Convert every \n line terminator OUTSIDE string literals back to \r\n, leaving \n characters inside strings (and inside comments… see below) untouched.

The canonical form emitted by format_source is LF-only. Editors that round-trip Windows-authored files want to see CRLF echoed back on every line. This helper bridges the two by walking the canonical output with the shared SourceState state machine. The walker respects:

  • String literals: bytes pass through verbatim. The user’s original line endings inside a multi-line narration / note / document string are preserved.
  • Line comments (;, %, #!, #+): the comment’s terminating newline IS a real structural line terminator, so it gets converted to CRLF; bytes inside the comment region (which can include arbitrary characters, notably stray ") pass through without flipping the in-string state. #! and #+ open a comment at any column — the lexer’s SHEBANG / EMACS_DIRECTIVE regexes carry no line-start anchor, and the state machine matches that classification.

The helper lives in this module rather than the LSP crate because its correctness depends on the lexer’s STRING and comment rules. Keep it co-located with the formatter so a lexer change forces a co-evaluation here.