-
Notifications
You must be signed in to change notification settings - Fork 16
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
Fix splice list insertion #84
Changes from 8 commits
76df4d8
94c1d3b
905f77e
fe44d09
5f369b4
5978c20
f223d48
825b7cf
436a2b5
1b0c434
5e71dfb
9c735e5
66b25a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -112,7 +112,7 @@ SourceEdit _appendToFlowList( | |||||||
/// block list. | ||||||||
SourceEdit _appendToBlockList( | ||||||||
YamlEditor yamlEdit, YamlList list, YamlNode item) { | ||||||||
var formattedValue = _formatNewBlock(yamlEdit, list, item); | ||||||||
var (formattedValue, _) = _formatNewBlock(yamlEdit, list, item); | ||||||||
final yaml = yamlEdit.toString(); | ||||||||
var offset = list.span.end.offset; | ||||||||
|
||||||||
|
@@ -132,7 +132,8 @@ SourceEdit _appendToBlockList( | |||||||
} | ||||||||
|
||||||||
/// Formats [item] into a new node for block lists. | ||||||||
String _formatNewBlock(YamlEditor yamlEdit, YamlList list, YamlNode item) { | ||||||||
(String formatted, String indent) _formatNewBlock( | ||||||||
YamlEditor yamlEdit, YamlList list, YamlNode item) { | ||||||||
final yaml = yamlEdit.toString(); | ||||||||
final listIndentation = getListIndentation(yaml, list); | ||||||||
final newIndentation = listIndentation + getIndentation(yamlEdit); | ||||||||
|
@@ -142,9 +143,12 @@ String _formatNewBlock(YamlEditor yamlEdit, YamlList list, YamlNode item) { | |||||||
if (isCollection(item) && !isFlowYamlCollectionNode(item) && !isEmpty(item)) { | ||||||||
valueString = valueString.substring(newIndentation); | ||||||||
} | ||||||||
final indentedHyphen = '${' ' * listIndentation}- '; | ||||||||
|
||||||||
return '$indentedHyphen$valueString$lineEnding'; | ||||||||
// Pass back the indentation for use | ||||||||
final hyphenIndentation = ' ' * listIndentation; | ||||||||
final indentedHyphen = '$hyphenIndentation- '; | ||||||||
|
||||||||
return ('$indentedHyphen$valueString$lineEnding', hyphenIndentation); | ||||||||
} | ||||||||
|
||||||||
/// Formats [item] into a new node for flow lists. | ||||||||
|
@@ -172,14 +176,39 @@ SourceEdit _insertInBlockList( | |||||||
|
||||||||
if (index == list.length) return _appendToBlockList(yamlEdit, list, item); | ||||||||
|
||||||||
final formattedValue = _formatNewBlock(yamlEdit, list, item); | ||||||||
var (formattedValue, indent) = _formatNewBlock(yamlEdit, list, item); | ||||||||
|
||||||||
final currNode = list.nodes[index]; | ||||||||
final currNodeStart = currNode.span.start.offset; | ||||||||
final yaml = yamlEdit.toString(); | ||||||||
final start = yaml.lastIndexOf('\n', currNodeStart) + 1; | ||||||||
|
||||||||
return SourceEdit(start, 0, formattedValue); | ||||||||
var currSequenceOffset = yaml.lastIndexOf('-', currNodeStart - 1); | ||||||||
|
||||||||
final (isNested, offset) = _isNestedInBlockList(currSequenceOffset - 1, yaml); | ||||||||
|
||||||||
/// We have to get rid of the left indentation applied by default | ||||||||
if (isNested && index == 0) { | ||||||||
// Give the previous first element its indent | ||||||||
formattedValue = formattedValue.trimLeft() + indent; | ||||||||
} | ||||||||
|
||||||||
return SourceEdit(offset, 0, formattedValue); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So when we're inserting into a block list, we:
I think what you're doing is probably correct in both cases, it's just a bit weird. If I have a nested list:
and I'm prepending
Where as if I have:
and I prepend
So in the case with no nested lists, we're inserting a full new line. Where as, if we have nested block lists, we insert into the middle of the existing line. I guess this makes sense because in Just typing this out so I think I understand it, this seems solid. I'd suggest we either inline I guess technically we could in the second case have done:
That would be more consistent with the first case, and we wouldn't need to know if it's a nested list or not. But the diff is much better when we're inserting a full line, and that's also the common case after all, nested lists in block mode is fairly weird. So I totally understand why we need to know if it's inside a nested block list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😅 -
- value instead of - - value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I didn't even realize that:
was an option... can we add a few tests 🤣 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Added these in 5978c20, I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added additional docs |
||||||||
|
||||||||
/// Checks if the [YamlNode]'s list is directly nested within another list | ||||||||
(bool isNested, int offset) _isNestedInBlockList(int start, String yaml) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure it's a good idea to make this a function? are we going to use it elsewhere? I don't mind, but if we do, we probably should document it clearly. Like what is The documentation has to explain that And this method aims to find out if:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this will probably only be used inside But add documentation comments that explains what it does is also fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I extracted it to make |
||||||||
if (start < 0) return (false, 0); | ||||||||
|
||||||||
final newLineStart = yaml.lastIndexOf('\n', start); | ||||||||
final seqStart = yaml.lastIndexOf('-', start); | ||||||||
|
||||||||
/// Anytime our '-' is non-existent, use '\n' as source of truth. Also, if | ||||||||
/// the new line is closer | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this mean, when does this happen? Can it happen?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it does since we are scanning backwards. I think you covered it in the last comment. Basically if:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Should we update the comment or not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll make it clearer. |
||||||||
if (newLineStart >= seqStart) { | ||||||||
return (false, newLineStart + 1); | ||||||||
} | ||||||||
|
||||||||
return (true, seqStart + 2); // Inclusive of space | ||||||||
} | ||||||||
|
||||||||
/// Returns a [SourceEdit] describing the change to be made on [yamlEdit] to | ||||||||
|
jonasfj marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
SLICE LIST IN NESTED BLOCK LIST | ||
--- | ||
key: | ||
- foo: | ||
- - bar: | ||
- - - false | ||
- - - false | ||
- - - false | ||
--- | ||
- [splice, [key], 0, 0, ['pre-foo']] | ||
- [splice, [key, 1, 'foo', 0], 0, 1, ['test']] | ||
- [splice, [key, 2], 0, 0, ['test']] | ||
- [splice, [key], 4, 1, ['tail-foo']] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
key: | ||
- foo: | ||
- - bar: | ||
- - - false | ||
- - - false | ||
- - - false | ||
--- | ||
key: | ||
- pre-foo | ||
- foo: | ||
- - bar: | ||
- - - false | ||
- - - false | ||
- - - false | ||
--- | ||
key: | ||
- pre-foo | ||
- foo: | ||
- - test | ||
- - - false | ||
- - - false | ||
- - - false | ||
--- | ||
key: | ||
- pre-foo | ||
- foo: | ||
- - test | ||
- - test | ||
- - false | ||
- - - false | ||
- - - false | ||
--- | ||
key: | ||
- pre-foo | ||
- foo: | ||
- - test | ||
- - test | ||
- - false | ||
- - - false | ||
- tail-foo |
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.
Are you sure it wouldn't make more sense to return
listIndentation
? Instead of returning the actual string.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.
I'll investigate that. I did that for convenience. I didn't want to change so many things in the existing code.