private JSONParseItem GetItemBeforePosition(int pos, JSONComplexItem parentItem) { JSONParseItem item = null; JSONParseItemList children = parentItem.Children; int start = 0; if (children.Any()) { start = children[0].Start; } if (start < pos) { int i = FindInsertIndex(children, pos, beforeExisting: true) - 1; if (i >= 0) { item = children[i]; if (item is JSONComplexItem complexItem) { // Recurse to find the deepest item item = GetItemBeforePosition(pos, complexItem); } } } return(item); }
public void JSONHelpers_GetItemBeforePosition_EmptyFile() { JSONComplexItem complexItem = JSONParser.Parse(string.Empty); JSONParseItem item = JsonHelpers.GetItemBeforePosition(0, complexItem); Assert.IsNull(item); }
[DataRow(170, 5)] // The last index public void JSONHelpers_FindInsertIndex_InvalidJson(int position, int expectedIndex) { JSONComplexItem item = JSONParser.Parse(_invalidJsonText).Children[0] as JSONComplexItem; JSONParseItemList items = item.Children; int actualIndex = JsonHelpers.FindInsertIndex(items, position); Assert.AreEqual(expectedIndex, actualIndex); }
[DataRow(170, "}")] // The last token item position public void JSONHelpers_GetItemBeforePosition_InvalidJson(int position, string expectedText) { JSONComplexItem complexItem = JSONParser.Parse(_invalidJsonText); JSONParseItem item = JsonHelpers.GetItemBeforePosition(position, complexItem); Assert.IsTrue(item is JSONTokenItem); Assert.AreEqual(expectedText, item.Text); }
private JSONParseItem GetNextSibling(JSONArrayElement arrayElement) { JSONComplexItem parent = arrayElement.Parent; return(parent != null?GetNextChild(arrayElement, parent.Children) : null); }