diff --git a/CodeiumVS/SuggestionUI/StringCompare.cs b/CodeiumVS/SuggestionUI/StringCompare.cs index 9d51b0b..7511ca2 100644 --- a/CodeiumVS/SuggestionUI/StringCompare.cs +++ b/CodeiumVS/SuggestionUI/StringCompare.cs @@ -75,7 +75,7 @@ public static int CheckSuggestion(String suggestion, String line, bool isTextIns int index = suggestion.IndexOf(line); int endPos = index + line.Length; - int firstLineBreak = suggestion.IndexOf('\n'); + int firstLineBreak = IndexOfNewLine(suggestion); if (index > -1 && (firstLineBreak == -1 || endPos < firstLineBreak)) { @@ -88,5 +88,22 @@ public static int CheckSuggestion(String suggestion, String line, bool isTextIns return res.Item1 >= endPoint ? res.Item2 : -1; } } -} + + private static readonly System.Text.RegularExpressions.Regex newLineMatcher = new System.Text.RegularExpressions.Regex(@"(?:\r\n|\n|\r)", + System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.Singleline); + + /// + /// Returns the index of the first new line in the string, or -1 if not found + /// + /// Checks for both \r\n and \n + public static int IndexOfNewLine(string text) + { + System.Text.RegularExpressions.Match newLineMatch = newLineMatcher.Match(text); + + if (newLineMatch.Success) + return newLineMatch.Index; + + return -1; + } + } } diff --git a/CodeiumVS/SuggestionUI/TextViewListener.cs b/CodeiumVS/SuggestionUI/TextViewListener.cs index 344b057..490bf99 100644 --- a/CodeiumVS/SuggestionUI/TextViewListener.cs +++ b/CodeiumVS/SuggestionUI/TextViewListener.cs @@ -157,9 +157,10 @@ List> ParseCompletion(IList comple String completionText = completionItems[i].completion.text; if (!String.IsNullOrEmpty(end)) { - int endNewline = end.IndexOf('\r'); - endNewline = endNewline <= -1 ? end.IndexOf('\n') : endNewline; - endNewline = endNewline <= -1 ? end.Length : endNewline; + int endNewline = StringCompare.IndexOfNewLine(end); + + if (endNewline <= -1) + endNewline = end.Length; completionText = completionText + end.Substring(0, endNewline); }