public void TryUpdateBalanceCount_CompleteCharacterLiteral_CountsCorrectly() { // Arrange var token = new CSharpSymbol("'('", CSharpSymbolType.CharacterLiteral); var count = 2; // Act var result = ImplicitExpressionEditHandler.TryUpdateBalanceCount(token, ref count); // Assert Assert.True(result); Assert.Equal(2, count); }
public void TryUpdateBalanceCount_InvalidParenthesisCharacterLiteral_ReturnsFalse() { // Arrange var token = new CSharpSymbol("')", CSharpSymbolType.CharacterLiteral); var count = 0; // Act var result = ImplicitExpressionEditHandler.TryUpdateBalanceCount(token, ref count); // Assert Assert.False(result); Assert.Equal(0, count); }
public void TryUpdateBalanceCount_SingleRightParenthesis_CountsCorrectly() { // Arrange var token = new CSharpSymbol(")", CSharpSymbolType.RightParenthesis); var count = 2; // Act var result = ImplicitExpressionEditHandler.TryUpdateBalanceCount(token, ref count); // Assert Assert.True(result); Assert.Equal(1, count); }
// Internal for testing internal static bool TryUpdateBalanceCount(CSharpSymbol token, ref int count) { var updatedCount = count; if (token.Type == CSharpSymbolType.LeftParenthesis) { updatedCount++; } else if (token.Type == CSharpSymbolType.RightParenthesis) { if (updatedCount == 0) { return(false); } updatedCount--; } else if (token.Type == CSharpSymbolType.StringLiteral) { var content = token.Content; if (content.Length > 0 && content[content.Length - 1] != '"') { // Incomplete string literal may have consumed some of our parenthesis and usually occurr during auto-completion of '"' => '""'. if (!TryUpdateCountFromContent(content, ref updatedCount)) { return(false); } } } else if (token.Type == CSharpSymbolType.CharacterLiteral) { var content = token.Content; if (content.Length > 0 && content[content.Length - 1] != '\'') { // Incomplete character literal may have consumed some of our parenthesis and usually occurr during auto-completion of "'" => "''". if (!TryUpdateCountFromContent(content, ref updatedCount)) { return(false); } } } if (updatedCount < 0) { return(false); } count = updatedCount; return(true); }
// Internal for testing internal static bool ContainsPosition(int position, CSharpSymbol currentToken) { var tokenStart = currentToken.Start.AbsoluteIndex; if (tokenStart == position) { // Token is exactly at the insertion point. return(true); } var tokenEnd = tokenStart + currentToken.Content.Length; if (tokenStart < position && tokenEnd > position) { // Insertion point falls in the middle of the current token. return(true); } return(false); }