-
-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add setOrigRanges() to YAML.parseCST() output #31
Conversation
I have no opinion on it. 😅
I think it's fine either way since it's just a matter of
👍
I actually won't use this functionality so I'm not sure, the original start/end is not suitable for my use case (e.g., |
src/cst/parse.js
Outdated
for (let i = 1; i < cr.length; ++i) cr[i] -= i | ||
let crOffset = 0 | ||
for (let i = 0; i < documents.length; ++i) { | ||
documents[i].setOrigRanges(cr, crOffset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
crOffset = documents[i].setOrigRanges(cr, crOffset)
? If so, we should add a test for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. And yes, this should indeed be tested.
No, all ranges should get Docs indeed will need to be updated for this change. And it'd probably be better to completely drop the |
This (hopefully) fixes #20 by adding a method
setOrigRanges()
to the array object returned byYAML.parseCST()
. That method would setorigStart
andorigEnd
indices for all of the range objects contained within the CST, which in turn would point to the start and end of the source before the CR characters got stripped out.@ikatyang, comment from you would be appreciated here esp. regarding the specifics of the API, as you would have an actual use case for this. In particular:
setOrigRanges()
implementation will return a boolean indicating whether it did in fact add the ranges, to allow for a fast return for inputs that did not contain a\r\n
string. Does this make its usage more difficult, as it's then not certain thatorigStart
andorigEnd
are always set after calling?end
is pointing at the\n
character that in the source was preceded by a\r
, the correspondingorigEnd
will then point at the\r
character to maintain the same semantic meaning as before. This has particular significance forvalueRange
values, as this makes sure that a slice of the source using the range does not get a somewhat surprising\r
suffix iforigEnd
is left pointing at the\n
.Range
class now has a couple of utility methods,apply(src)
andapplyOrig(src)
, for slicing the range contents from the source. Should these be published and documented? Atm justapplyOrig
is used, and even then in just one test case.I did try a couple of more automated methods of handling all of this, e.g. mutating the range
start
andend
values automatically after parsing to match what's noworigX
, but that messes up the value parsing and actually breaks YAML spec a bit. I also considered callingsetOrigRanges()
internally before returning fromparseCST()
, but that would make the presence or non-presence oforigX
values unclear, as well as adding a useless tree traversal for use cases that don't need the indices into the original source.