public void AreNeighbors_ShouldProduceCorrectResult(string firstSpan, string secondSpan, bool expectedResult) { var first = TextSpan.Parse(firstSpan); var second = TextSpan.Parse(secondSpan); TextSpan.AreNeighbors(first, second).Should().Be(expectedResult); }
/// <summary> /// Returnes nodes whose span contains the specified offset from least specific to the most specific. /// </summary> /// <param name="syntax">The program node</param> /// <param name="offset">The offset</param> private static List <SyntaxBase> FindNodesMatchingOffset(ProgramSyntax syntax, int offset) { var nodes = new List <SyntaxBase>(); syntax.TryFindMostSpecificNodeInclusive(offset, current => { // callback is invoked only if node span contains the offset // in inclusive mode, 2 nodes can be returned if cursor is between end of one node and beginning of another // we will pick the node to the left as the winner if (nodes.Any() == false || TextSpan.AreNeighbors(nodes.Last(), current) == false) { nodes.Add(current); } // don't filter out the nodes return(true); }); return(nodes); }
/// <summary> /// Returns nodes whose span contains the specified offset from least specific to the most specific. /// </summary> /// <param name="syntax">The program node</param> /// <param name="offset">The offset</param> public static List <SyntaxBase> FindNodesMatchingOffset(ProgramSyntax syntax, int offset) { var nodes = new List <SyntaxBase>(); syntax.TryFindMostSpecificNodeInclusive(offset, current => { // callback is invoked only if node span contains the offset // in inclusive mode, 2 nodes can be returned if cursor is between end of one node and beginning of another // we will pick the node to the left as the winner if it's not a newline. Otherwise, pick the right one instead. if (!nodes.Any()) { nodes.Add(current); } else { var lastNode = nodes[^ 1]; if (TextSpan.AreNeighbors(lastNode, current) && lastNode is Token { Type: TokenType.NewLine })