Skip to content

Commit

Permalink
Merge pull request #1 from SFrank1966/SFrank1966-patch-1
Browse files Browse the repository at this point in the history
Handling of Tab /Shift-Tab on block comments
  • Loading branch information
SFrank1966 authored Mar 20, 2024
2 parents 22df632 + c4ff247 commit 4f3d18f
Showing 1 changed file with 52 additions and 23 deletions.
75 changes: 52 additions & 23 deletions ICSharpCode.AvalonEdit/Editing/EditingCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,29 @@ static void OnTab(object target, ExecutedRoutedEventArgs args)
if (textArea != null && textArea.Document != null) {
using (textArea.Document.RunUpdate()) {
if (textArea.Selection.IsMultiline) {
var segment = textArea.Selection.SurroundingSegment;
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) {
int offset = current.Offset;
if (textArea.ReadOnlySectionProvider.CanInsert(offset))
textArea.Document.Replace(offset, 0, textArea.Options.IndentationString, OffsetChangeMappingType.KeepAnchorBeforeInsertion);
if (current == end)
break;
current = current.NextLine;
if (!textArea.Selection.EnableVirtualSpace) {
var segment = textArea.Selection.SurroundingSegment;
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) {
int offset = current.Offset;
if (textArea.ReadOnlySectionProvider.CanInsert(offset))
textArea.Document.Replace(offset, 0, textArea.Options.IndentationString, OffsetChangeMappingType.KeepAnchorBeforeInsertion);
if (current == end)
break;
current = current.NextLine;
}
} else {
IEnumerable<SelectionSegment> segments = textArea.Selection.Segments;
foreach (var segment in segments.Reverse()) {
int offset = segment.StartOffset;
if (textArea.ReadOnlySectionProvider.CanInsert(offset))
textArea.Document.Replace(offset, 0, textArea.Options.IndentationString, OffsetChangeMappingType.KeepAnchorBeforeInsertion);
}
}
} else {
string indentationString = textArea.Options.GetIndentationString(textArea.Caret.Column);
Expand All @@ -225,17 +234,37 @@ static void OnTab(object target, ExecutedRoutedEventArgs args)

static void OnShiftTab(object target, ExecutedRoutedEventArgs args)
{
TransformSelectedLines(
delegate (TextArea textArea, DocumentLine line) {
int offset = line.Offset;
ISegment s = TextUtilities.GetSingleIndentationSegment(textArea.Document, offset, textArea.Options.IndentationSize);
if (s.Length > 0) {
s = textArea.GetDeletableSegments(s).FirstOrDefault();
if (s != null && s.Length > 0) {
textArea.Document.Remove(s.Offset, s.Length);
TextArea textArea = GetTextArea(target);
if (textArea != null && textArea.Document != null) {
if (textArea.Selection.IsEmpty || !textArea.Selection.EnableVirtualSpace) {
TransformSelectedLines(
delegate (TextArea textAreaIn, DocumentLine line) {
int offset = line.Offset;
ISegment s = TextUtilities.GetSingleIndentationSegment(textAreaIn.Document, offset, textAreaIn.Options.IndentationSize);
if (s.Length > 0) {
s = textAreaIn.GetDeletableSegments(s).FirstOrDefault();
if (s != null && s.Length > 0) {
textAreaIn.Document.Remove(s.Offset, s.Length);
}
}
}, target, args, DefaultSegmentType.CurrentLine);
} else {
using (textArea.Document.RunUpdate()) {
IEnumerable<SelectionSegment> segments = textArea.Selection.Segments;
foreach (var segment in segments.Reverse()) {
int offset = segment.StartOffset - 1;
ISegment s = TextUtilities.GetSingleIndentationSegment(textArea.Document, offset,
textArea.Options.IndentationSize);
if (s.Length > 0) {
s = textArea.GetDeletableSegments(s).FirstOrDefault();
if (s != null && s.Length > 0) {
textArea.Document.Remove(s.Offset, s.Length);
}
}
}
}
}, target, args, DefaultSegmentType.CurrentLine);
}
}
}
#endregion

Expand Down

0 comments on commit 4f3d18f

Please sign in to comment.