Browse Source

Fixed http://community.sharpdevelop.net/forums/t/11909.aspx (selection is quirky when indenting/unindenting).

pull/12/head
Daniel Grunwald 15 years ago
parent
commit
e866b3fdf2
  1. 34
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/EditingCommandHandler.cs

34
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/EditingCommandHandler.cs

@ -110,8 +110,12 @@ namespace ICSharpCode.AvalonEdit.Editing
start = end = null; start = end = null;
} }
} else { } else {
start = textArea.Document.GetLineByOffset(textArea.Selection.SurroundingSegment.Offset); ISegment segment = textArea.Selection.SurroundingSegment;
end = textArea.Document.GetLineByOffset(textArea.Selection.SurroundingSegment.EndOffset); start = textArea.Document.GetLineByOffset(segment.Offset);
end = textArea.Document.GetLineByOffset(segment.EndOffset);
// don't include the last line if no characters on it are selected
if (start != end && end.Offset == segment.EndOffset)
end = end.PreviousLine;
} }
if (start != null) { if (start != null) {
transformLine(textArea, start); transformLine(textArea, start);
@ -178,16 +182,32 @@ namespace ICSharpCode.AvalonEdit.Editing
if (textArea != null && textArea.Document != null) { if (textArea != null && textArea.Document != null) {
using (textArea.Document.RunUpdate()) { using (textArea.Document.RunUpdate()) {
if (textArea.Selection.IsMultiline(textArea.Document)) { if (textArea.Selection.IsMultiline(textArea.Document)) {
DocumentLine start = textArea.Document.GetLineByOffset(textArea.Selection.SurroundingSegment.Offset); var segment = textArea.Selection.SurroundingSegment;
DocumentLine end = textArea.Document.GetLineByOffset(textArea.Selection.SurroundingSegment.EndOffset); int oldSelectionStart = segment.Offset;
int oldCaretPos = textArea.Caret.Offset;
DocumentLine start = textArea.Document.GetLineByOffset(segment.Offset);
DocumentLine end = textArea.Document.GetLineByOffset(segment.EndOffset);
// don't include the last line if no characters on it are selected
if (start != end && end.Offset == segment.EndOffset)
end = end.PreviousLine;
DocumentLine current = start;
while (true) { while (true) {
int offset = start.Offset; int offset = current.Offset;
if (textArea.ReadOnlySectionProvider.CanInsert(offset)) if (textArea.ReadOnlySectionProvider.CanInsert(offset))
textArea.Document.Insert(offset, textArea.Options.IndentationString); textArea.Document.Insert(offset, textArea.Options.IndentationString);
if (start == end) if (current == end)
break; break;
start = start.NextLine; current = current.NextLine;
}
// Fix for http://community.sharpdevelop.net/forums/t/11909.aspx:
// The insertion at the first line will move the start of selection, so we change the selection
// to keep the old start offset if it started at the beginning of the line.
if (textArea.Selection.Segments.Count() == 1 && oldSelectionStart == start.Offset) {
textArea.Selection = new SimpleSelection(oldSelectionStart, textArea.Selection.SurroundingSegment.EndOffset);
} }
// do the same to the caret:
if (oldCaretPos == start.Offset)
textArea.Caret.Offset = oldCaretPos;
} else { } else {
string indentationString = textArea.Options.GetIndentationString(textArea.Caret.Column); string indentationString = textArea.Options.GetIndentationString(textArea.Caret.Column);
textArea.ReplaceSelectionWithText(indentationString); textArea.ReplaceSelectionWithText(indentationString);

Loading…
Cancel
Save