Skip to content
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

Comment causes issue with ScalarStyle.FOLDED #1935

Open
jonasfj opened this issue Mar 2, 2023 · 8 comments
Open

Comment causes issue with ScalarStyle.FOLDED #1935

jonasfj opened this issue Mar 2, 2023 · 8 comments
Labels
good first issue A good starting issue for contributors (issues with this label will appear in /contribute) package:yaml_edit type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@jonasfj
Copy link
Member

jonasfj commented Mar 2, 2023

Example

import 'package:yaml/yaml.dart';
import 'package:yaml_edit/yaml_edit.dart';

void main() {
  final doc = YamlEditor('''
- 0
- 1 # comment
- 2
''');

  final node = wrapAsYamlNode(
    'hello',
    scalarStyle: ScalarStyle.FOLDED,
  );
  doc.update([1], node);

  print(doc);
}

We should have a test case covering this. And ideally more test cases covering similar cases.

And we should fix this bug. Maybe we need to move the comment to the next line.
Or maybe we need to drop the comment in cases like this.

@jonasfj jonasfj added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) good first issue A good starting issue for contributors (issues with this label will appear in /contribute) labels Mar 2, 2023
@MikiPAUL
Copy link
Contributor

MikiPAUL commented Mar 4, 2023

This issue occurs even with ScalarStyle.LITERAL

@MikiPAUL
Copy link
Contributor

MikiPAUL commented Mar 4, 2023

Hi @jonasfj, This issue involves checking if the value that was passed in the update function is of scalar style (FOLDED or LITERAL) and the node to get updated with docs contains a comment, then removing the comment and proceeding with a normal update. Will this solve the issue?

@jonasfj
Copy link
Member Author

jonasfj commented Mar 6, 2023

Any other ways we can solve this?

Do the same thing happen if I try to insert a map or list in block mode?

Is it possible to allow to comment to remain before/after the text?

@MikiPAUL
Copy link
Contributor

MikiPAUL commented Mar 9, 2023

- 1
- >- #comment
   hello
- 2

We could allow comments to remain before the text. To do so, we have to insert comments in replacement string and increase the length to cover the comment. Similar approaches can be used with map and list in block mode.
https://github.com/dart-lang/yaml_edit/blob/c9e82f0cc8b6609493ae891afd4f4901d68afa2c/lib/src/source_edit.dart#L129-L131

@jonasfj
Copy link
Member Author

jonasfj commented Mar 9, 2023

Allowing the comment before will probably backfire in cases like:

- 1
- 2 # line1
    # line2
    # line3
- 3

@jonasfj
Copy link
Member Author

jonasfj commented Mar 9, 2023

This could work:

- 1
- >
  new text with 2 space indentation
 # comment with one space indentation
- 3

but it also fail if the comment is like:

- 1
- 2 # This
      # is
        # probabluy
          # not
            # sane
- 3

Then we'll be forced to mess with the whitespacing in some way.


Perhaps:

- 1 
#comment
- >-
   hello
- 2

is the most reasonable thing, in the degenerate case that becomes:

- 1
# This
# is
# probabluy
# not
# sane
- >
  hello
- 3

or we could do:

key1: 1
key2: >
  hello
# This
# is
# probabluy
# not
# sane
key3: 3

Just always move the comment out to have zero indentation, so it never messes with anything. We might mangle it a bit, if it contains ascii art that depends on the indentation. But that seems acceptable.

This might be better because we don't modify any of the key encodings.


Options I think are possibly good:

  • (A) Remove comments when switching from flow-mode to strings in block mode.
  • (B) Move the comment to the start of the line, like \n#<comment>.

I think both (A) and (B) are reasonable.

I'm not sure (B) isn't so bad -- even if the mutation gets a bit large.

But I have to ask, if maybe removing the comment (option A), isn't actually the right choice.

Imagine:

keyA: valA
keyB:
  sub1key: value1
  # comment that should probably be removed, if we replace `keyB` with a string
  sub2key: value2
keyC: valC

becomes:

keyA: valA
keyB: >
  String that replaced the object with sub1key and sub2key
keyC: valC

It would be surprising if the comment embedded inside the object that was replaced is preserved. So similarly, in a case like:

keyA: valA
keyB: 3 # best number ever!
keyC: valC

becomes

keyA: valA
keyB: 42
keyC: valC

Well, we could argue that the comment # best number ever! was part of the value replaced, thus, it might be very reasonable to remove it.


Sorry, for the long trail of thoughts, I'm starting to conclude that comments after a value that is replaced, should be removed. At-least it should be okay to do when switching from flow-mode to block-mode for strings.

What are the scenarios in which we want to do this?

I imagine it'll also be relevant if we do:

keyA: valA
keyB: {subkey: value} # comment
keyC: valC

into:

keyA: valA
keyB: >
  hello world
keyC: valC

@MikiPAUL
Copy link
Contributor

Yeah, It perfectly makes sense to remove the comments after a value that is replaced. Should we do the same with null content also?

- 0
- # comment
- 2

@jonasfj
Copy link
Member Author

jonasfj commented Apr 18, 2023

yeah, we probably just should remove the comment in all such cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue A good starting issue for contributors (issues with this label will appear in /contribute) package:yaml_edit type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

3 participants