The tests all run, and the code is much better. It s time for a break. Let s see what we ve accomplished. We had these two methods :
public void InsertParagraphTag() {
if ( lines.Count == 0 ) {
lines.Add( "<P></P>" );
selectionStart = 3;
return;
}
lines.InsertRange(LineContainingCursor()+1, NewParagraph());
selectionStart = NewSelectionStart(LineContainingCursor() + 2, "<P>");
}
public void InsertSectionTags() {
if ( lines.Count == 0 ) {
lines.Add( "<sect1><title></title>" );
lines.Add( "</sect1>");
selectionStart = 14;
return;
}
lines.InsertRange(LineContainingCursor()+1, NewSection());
selectionStart = NewSelectionStart(LineContainingCursor() + 1,
"<sect1><title>");
}
And now we have these:
public void InsertParagraphTag() {
int cursorLine = LineContainingCursor();
lines.InsertRange(cursorLine+1, NewParagraph());
selectionStart = NewSelectionStart(cursorLine + 1, "<P>");
}
public void InsertSectionTags() {
int cursorLine = LineContainingCursor();
lines.InsertRange(cursorLine+1, NewSection());
selectionStart = NewSelectionStart(cursorLine + 1, "<sect1><title>");
}
Much better. Of course, we can see that these two methods are almost exactly the same, so there s duplication to remove. We ll catch that next time.