Skip to content

Commit

Permalink
Fix for github issue #101 (#133)
Browse files Browse the repository at this point in the history
Codeium was not checking for both \r and \n when working out what else should be appended to the end of the line. It now does that.
  • Loading branch information
Zallist authored Oct 30, 2024
1 parent 72bb1e9 commit 0351f35
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
21 changes: 19 additions & 2 deletions CodeiumVS/SuggestionUI/StringCompare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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);

/// <summary>
/// Returns the index of the first new line in the string, or -1 if not found
/// </summary>
/// <remarks>Checks for both \r\n and \n</remarks>
public static int IndexOfNewLine(string text)
{
System.Text.RegularExpressions.Match newLineMatch = newLineMatcher.Match(text);

if (newLineMatch.Success)
return newLineMatch.Index;

return -1;
}
}
}
7 changes: 4 additions & 3 deletions CodeiumVS/SuggestionUI/TextViewListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ List<Tuple<String, String>> ParseCompletion(IList<Packets.CompletionItem> 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);
}
Expand Down

0 comments on commit 0351f35

Please sign in to comment.