Skip to content

Conversation

@yjp20
Copy link

@yjp20 yjp20 commented Feb 5, 2026

[wip]

I believe this fixes #283 by adding a keepIndent option to toString.

I temporarily enabled this keepIndent + keepSourceToken on by default and ran all the tests and it seems to work.

Alternatively, indent could be an optional property on the AST, which is maybe better in theory but also leads to some odd situations. Another alternative to this implementation might be something more general like keepSourceFormatting where in addition to formatting changes, we would attempt to keep more formatting e.g. in multiline flow text we wouldn't re-fold text if sourceToken exists.

This would be really helpful for us at Stainless, as we apply "transforms" to a yaml file, but also need to textually diff the original yaml file against the transformed file to show changes to our users. Spurius diffs make this process really difficult to inspect.

And while I'm at it just want to say thank you for your work on this library, much of my work at Stainles would just not be possible without it. Diffing, transforming, adding comments, etc. to yaml files are a core part of what we do, and we use this library for things like just parsing YAML, analyzing source in our language server, showing great diffs in our transforms product, and so, so many more things.

@yjp20 yjp20 marked this pull request as draft February 5, 2026 02:26
@yjp20 yjp20 force-pushed the youngjin/keep-indent branch 3 times, most recently from 269c70d to 2edefe2 Compare February 5, 2026 03:29
@yjp20 yjp20 marked this pull request as ready for review February 5, 2026 03:30
@yjp20 yjp20 force-pushed the youngjin/keep-indent branch from 2edefe2 to f46ef18 Compare February 6, 2026 18:50
@eemeli
Copy link
Owner

eemeli commented Feb 7, 2026

I rather like this solution; I hadn't thought of focusing on and only solving it for the indentation of a pair's value, but that is indeed the place where it's most relevant.

My only concerns here are that I'd like to avoid the need for the keepIndent option, and to make it possible to customize the indentation of a YAML doc or node not originally created by parsing.

So (pretty much thinking out loud here) maybe we want to add an optional indent?: number property on Pair and fill that out during parsing when the value is indented?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array-indent not maintained between parseDocument/toString

2 participants