Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve block indentation behavior for C# #119

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ICSharpCode.AvalonEdit/Editing/TextArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,9 @@ internal void ReplaceSelectionWithText(string newText)
if (this.Document == null)
throw ThrowUtil.NoDocumentAssigned();
selection.ReplaceSelectionWithText(newText);

DocumentLine line = this.Document.GetLineByNumber(this.Caret.Line);
this.IndentationStrategy.OnLineChanged(this.Document, line, newText);
}

internal ISegment[] GetDeletableSegments(ISegment segment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,15 @@ public override void IndentLines(TextDocument document, int beginLine, int endLi
{
Indent(new TextDocumentAccessor(document, beginLine, endLine), true);
}

/// <inheritdoc cref="IIndentationStrategy.OnLineChanged"/>
public override void OnLineChanged(TextDocument document, DocumentLine line, string newText)
{
// Reformat line when blocks are created
if (newText == "{" || newText == "}")
{
IndentLine(document, line);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@ public virtual void IndentLine(TextDocument document, DocumentLine line)
public virtual void IndentLines(TextDocument document, int beginLine, int endLine)
{
}

/// <inheritdoc/>
public virtual void OnLineChanged(TextDocument document, DocumentLine line, string newText)
{
}
}
}
5 changes: 5 additions & 0 deletions ICSharpCode.AvalonEdit/Indentation/IIndentationStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,10 @@ public interface IIndentationStrategy
/// Reindents a set of lines.
/// </summary>
void IndentLines(TextDocument document, int beginLine, int endLine);

/// <summary>
/// Allows the indentation strategy to act based upon the user entering new text.
/// </summary>
void OnLineChanged(TextDocument document, DocumentLine line, string newText);
}
}