Fix TabCompleteBehavior problems due to differences between .NET and JS

- CaretIndex can not be less than 0
- Substring function requires the length of the slice