in src/Editor/Text/Impl/EditorOperations/EditorOperations.cs [4008:4069]
private static bool DeleteHorizontalWhitespace(ITextEdit textEdit, ICollection<Span> largeSpansToDelete, ICollection<Span> singleSpansToDelete, ICollection<Span> singleTabsToReplace)
{
ITextSnapshot snapshot = textEdit.Snapshot;
Func<int, bool> isLeadingWhitespace = (p) => snapshot.GetLineFromPosition(p).Start == p;
Func<int, bool> isTrailingWhitespace = (p) => snapshot.GetLineFromPosition(p).End == p;
if (largeSpansToDelete.Count == 0)
{
// If there were no spans of more than one space or tab,
// then delete all spans of just a single space or tab
foreach (var span in singleSpansToDelete)
{
if (!textEdit.Delete(span))
return false;
}
}
else
{
// If there was at least one span of more than one space or
// tab then replace all of those spans with spaces
foreach (var span in largeSpansToDelete)
{
// if it is leading or trailing whitespace, just delete it
if (isLeadingWhitespace(span.Start) || isTrailingWhitespace(span.End))
{
if (!textEdit.Delete(span))
return false;
}
// if there's space adjacent to the beginning/end of the selection
else if (IsSpaceCharacter(snapshot[span.Start - 1]) || IsSpaceCharacter(snapshot[span.End]))
{
if (!textEdit.Delete(span))
return false;
}
else if (!textEdit.Replace(span, " "))
return false;
}
// Replace single tabs with a single space, but
// skip leading / trailing tabs since they will be deleted below by the singleSpansToDelete loop.
foreach (var span in singleTabsToReplace)
{
if (!isLeadingWhitespace(span.Start) && !isTrailingWhitespace(span.End))
{
if (!textEdit.Replace(span, " "))
return false;
}
}
// Delete leading or trailing whitespace in singleSpansToDelete
foreach (var span in singleSpansToDelete)
{
if (isLeadingWhitespace(span.Start) || isTrailingWhitespace(span.End))
{
if (!textEdit.Delete(span))
return false;
}
}
}
return true;
}