提交 05e4c24c 编写于 作者: A Allison Chou

Refactoring

上级 5b95097c
......@@ -63,47 +63,33 @@ public bool ExecuteCommandWorker(ReturnKeyCommandArgs args)
}
var caret = textView.GetCaretPoint(subjectBuffer);
if (caret != null)
if (caret == null)
{
// First, we need to verify that we are only working with string literals.
// Otherwise, let the editor handle all carets.
for (var spanIndex = 0; spanIndex < spans.Count; spanIndex++)
return false;
}
// First, we need to verify that we are only working with string literals.
// Otherwise, let the editor handle all carets.
foreach (var span in spans)
{
var spanStart = span.Start;
var line = subjectBuffer.CurrentSnapshot.GetLineFromPosition(span.Start);
if (!LineContainsQuote(line, span.Start))
{
var spanStart = spans[spanIndex].Start;
var line = subjectBuffer.CurrentSnapshot.GetLineFromPosition(spanStart);
if (!LineContainsQuote(line, spanStart))
{
return false;
}
return false;
}
}
// We now go through the verified string literals and split each of them.
for (var spanIndex = 0; spanIndex < spans.Count; spanIndex++)
// We now go through the verified string literals and split each of them.
foreach (var span in spans.Reverse())
{
if (!SplitString(textView, subjectBuffer, span.Start))
{
var spanStart = spans[spanIndex].Start;
// Multi-caret case
if (spanIndex > 0)
{
caret = textView.GetCaretPoint(subjectBuffer);
if (caret == null)
{
return false;
}
// We change the span's starting point based on how much space was added from the last split.
var addedSpan = caret.Value.Subtract(spans[spanIndex - 1].Start);
spanStart = new SnapshotPoint(caret.Value.Snapshot, addedSpan.Position + spanStart.Position);
}
if (!SplitString(textView, subjectBuffer, spanStart))
{
return false;
}
return false;
}
return true;
}
return false;
return true;
}
private bool SplitString(ITextView textView, ITextBuffer subjectBuffer, SnapshotPoint caret)
......
......@@ -68,7 +68,7 @@ public class SplitStringLiteralCommandHandlerTests
out var expectedOutput, out ImmutableArray<TextSpan> expectedSpans);
Assert.Equal(expectedOutput, view.TextBuffer.CurrentSnapshot.AsText().ToString());
Assert.Equal(expectedSpans.Last().Start, view.Caret.Position.BufferPosition.Position);
Assert.Equal(expectedSpans.First().Start, view.Caret.Position.BufferPosition.Position);
if (verifyUndo)
{
......@@ -78,7 +78,7 @@ public class SplitStringLiteralCommandHandlerTests
var currentSnapshot = document.GetTextBuffer().CurrentSnapshot;
Assert.Equal(originalSnapshot.GetText(), currentSnapshot.GetText());
Assert.Equal(originalSelections.Last().Start, view.Caret.Position.BufferPosition.Position);
Assert.Equal(originalSelections.First().Start, view.Caret.Position.BufferPosition.Position);
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册