public static Token GetTokenForTermInsertion(this TolerantTokenizer tokenizer, int caret) { tokenizer.Parse(); var tokens = tokenizer.Tokens; var token = tokens.FirstOrDefault(_ => _.OverlapsCaret(caret)) ?? tokens.LastOrDefault(_ => _.IsLeftToCaret(caret)); if (token == null) { return(tokenOnEmptyInput(tokens, caret: 0)); } var current = token; while (true) { if (!current.IsPhrase && string.IsNullOrEmpty(current.NextTokenField)) { return(tokenOnEmptyInput(tokens, current.Position + current.Value.Length)); } current = current.Next; if (current == null) { return(tokenOnEmptyInput(tokens, caret: 0)); } } }
public static Token GetEditedToken(string query, int caret) { var tokenizer = new TolerantTokenizer(query); tokenizer.Parse(); var tokens = tokenizer.Tokens; var overllapingToken = tokens.FirstOrDefault(_ => _.OverlapsCaret(caret)); if (overllapingToken != null) { return(overllapingToken); } var leftToken = tokens.LastOrDefault(_ => _.IsLeftToCaret(caret)); var rightToken = tokens.FirstOrDefault(_ => _.IsRightToCaret(caret)); if (leftToken?.IsConnectedToCaret(caret) != true && rightToken?.IsConnectedToCaret(caret) == true) { return(rightToken); } if (leftToken?.Type.IsAny(TokenType.Modifier) == true && leftToken.TouchesCaret(caret)) { return(new Token(caret, string.Empty, TokenType.ModifierValue, leftToken.ParentField)); } if (leftToken?.TouchesCaret(caret) == true) { if (leftToken.Type.IsAny(TokenType.Field | TokenType.FieldValue | TokenType.Modifier)) { return(leftToken); } if (leftToken.Type.IsAny(TokenType.Boolean) && leftToken.Value.Length > 1) { return(leftToken); } return(tokenOnEmptyInput(tokens, caret, leftToken.NextTokenField)); } return(tokenOnEmptyInput(tokens, caret, leftToken?.NextTokenField)); }
public static Token GetTokenForArbitraryInsertion(this TolerantTokenizer tokenizer, int caret) { tokenizer.Parse(); var tokens = tokenizer.Tokens; var overlappingToken = tokens.FirstOrDefault(_ => _.OverlapsCaret(caret)); if (overlappingToken != null) { return(tokenOnEmptyInput(tokens, overlappingToken.Position + overlappingToken.Value.Length)); } var leftToken = tokens.LastOrDefault(_ => _.IsLeftToCaret(caret)); if (leftToken == null) { return(tokenOnEmptyInput(tokens, caret: 0)); } return(tokenOnEmptyInput(tokens, caret)); }