-
Notifications
You must be signed in to change notification settings - Fork 42
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
[Feature] Comment Preservation Proposal #42
Comments
Made several edits to first post for clarification and some simplification... hope this makes sense and would help for defining the expected behavior. |
What a nice notification to receive a WFH friday morning :) This is a great suggestion, and a lot more fleshed out than the few words I posted. I have a few points that I'd like to bring up:
That turned into more than I initially thoughts. Anyways, I'm looking forward to hearing what you have to say 😄 |
And yes, the detection could be flawed, that said, should be able to serialize/deserialze the comments relatively easily... modifying comments programatically, a bit harder. For me the biggest issue is being able to load, edit a value, and save with comments, so my config files wouldn't lose instructional comments. |
I see a lot of value in this, even if there are some edge cases that are strange. Compared to having all comments removed, having some level of comment preservation (even if lossy), would be hugely valuable! I can see the appeal of trying to make it lossless in both directions, but that doesn't seem to be a goal of this project in the first place as it doesn't maintain whitespaces between parse & stringify either. So, is there any appetite for doing this work? I am fairly confident that @tracker1's suggestions around comment preservation are sound, and if you were to have any concern about backwards compat, I'd encourage adding |
(I have a stringify solution discussion toml-lang/toml#854, which includes comment:)
|
@LongTengDao I do like the appearance, for the manual access formatting... that said, that type of property declaration would wind up in the object model, and parse/stringify would get noisy going to/from JSON... Perhaps... const obj = TOML.parse(...);
TOML.commentBefore(obj, "dotted.key", "This is before the dotted key/value pair"); As a convenience access method... where the third parameter if set, sets the value... if it's only the first two, it gets the value... and to clear/remove a comment the third parameter is an empty string. |
Alternatively... const comment = TOML.commentBefore`${obj}.dotted.key`(); //get
TOML.commentBefore`${obj}.dotted.key`("new value..."); // set
TOML.commentBefore`${obj}.dotted.key`(""); // clear Maybe just |
In addition to ideas from #22 I wanted to make some specific suggestions about comments support.
preserveComments
when not truthy will exclude the___COMMENTS___
property altogetherenumerableComments
(See Object.defineProperty.) would allow for the___COMMENTS___
property to be enumerable. This would allow for preservation viaJSON.stringify
, default would befalse
.toml.stringify
would explicitly check for values and include them if non-whitespace comments are present.The property for comments would literally be
result.___COMMENTS___
; (three underscores before and after)The format for comments would be:
result,___COMMENTS___.${entry}.BEFORE
- entries and documentresult,___COMMENTS___.${entry}.INLINE
- only for entriesresult,___COMMENTS___.${entry}.AFTER
The entry key for the document is
result.__COMMENTS__.___DOCUMENT___
What would be returned, should be the same object structure as before with an optionally enumerable (false by default)
___COMMENTS___
property, three underscores, all caps.As mentioned in @scriptcoded 's comment Multiple comments together should be merged with
\n
.Edge cases... For multiple interspersed comments/lines, the first block with a blank line after, and no blank after the node will belong to the previous node's AFTER, all additional nodes will be part of the next node's BEFORE, The document nodes will be before/after as such.
Becomes:
NOTE: spaces after
#
preserved, no whitespace lines preserved except for the case of\n \n
in between comment blocks, but any whitespace on the original, otherwise blank lines are removed.With two
\n\n
together the output blank line stays blank... if there was a block between, even if no whitespace, should parse with\n \n
space between the\n
and will output#
with no space on the blank line, even if the original had one. This may not align with the original input, but is probably the best option for dealing with this edge case.Comment sections beginning with
\n
mean there are blank lines at the top of said block, similarly for the end, any dangling\n
indicate blank lines.becomes
block\n
whitespace on blank line removed, but dangling\n
is blank line.\n\n comment
becomes:
empty line(s) at start.
Carriage returns (
\r
) are dropped.The middle line (
#
) with whitespace, will result instart\n \n more
where the extra whitespace is dropped to a single space, similar for otherwise blank lines being replaced with nothing, but having extra\n
The text was updated successfully, but these errors were encountered: