public static void FormatClosingBrace(ITextSnapshot Snapshot, ITextSnapshotLine line) { Span s = new Span(line.Start, line.Length - 1); if (string.IsNullOrWhiteSpace(Snapshot.GetText(s))) { SnapshotSpan pair; if (BraceMatchingTagger.FindMatchingOpenChar(line.End, '{', '}', Snapshot.LineCount, out pair, true)) { var previousLine = Snapshot.GetLineFromPosition(pair.Span.Start); int previousLineNum = previousLine.LineNumber; string previousLineText = previousLine.GetText(); int indentLevel = LineIndenter.GetIndentLevel(Snapshot, previousLineText); Snapshot.TextBuffer.Replace(s, new string(' ', indentLevel)); } } }
void TextBuffer_ChangedHighPriority(object sender, Microsoft.VisualStudio.Text.TextContentChangedEventArgs e) { foreach (var change in e.Changes) { if (change.NewText.EndsWith("{")) { var line = e.After.GetLineFromPosition(change.NewPosition); int linenum = line.LineNumber; if (linenum > 0) { int?ind = lineindenter.GetDesiredIndentation(line); int indentLevel; if (ind == null) { int previousLine = LineIndenter.GetPreviousNonWhitespaceLine(e.After, linenum); string previousLineText = e.After.GetLineFromLineNumber(previousLine).GetText(); indentLevel = LineIndenter.GetIndentLevel(e.After, previousLineText); } else { indentLevel = (int)ind; } Span s = new Span(line.Start, line.Length - 1); if (string.IsNullOrWhiteSpace(e.After.GetText(s))) { e.After.TextBuffer.Replace(s, new string(' ', indentLevel)); } } } else if (change.NewText.EndsWith("}")) { var line = e.After.GetLineFromPosition(change.NewPosition); FormatClosingBrace(e.After, line); } } }
public ISmartIndent CreateSmartIndent(ITextView textView) { textView.TextBuffer.ChangedHighPriority += new EventHandler <Microsoft.VisualStudio.Text.TextContentChangedEventArgs>(TextBuffer_ChangedHighPriority); return(lineindenter ?? (lineindenter = new LineIndenter(textView))); }