private void Format(IBraceCompletionSession session) { var buffer = session.SubjectBuffer; EditFilter.ApplyEdits( buffer, Formatter.GetEditsAfterEnter( buffer.CurrentSnapshot.GetText(), session.OpeningPoint.GetPosition(buffer.CurrentSnapshot), session.ClosingPoint.GetPosition(buffer.CurrentSnapshot), EditFilter.CreateFormattingOptions(session.TextView.Options, session.TextView.TextSnapshot) ) ); }
/// <summary> /// Format the content inserted by a code snippet /// </summary> public int FormatSpan(IVsTextLines pBuffer, TextSpan[] ts) { // Example of a snippet template (taken from forprops.snippet and use ... to represent // indent spaces): // // for (var $property$ in $object$) { // ....if ($object$.hasOwnProperty($property$)) { // ........$selected$$end$ // ....} // }; // // Example of code in pBuffer (the for loop is inserted by a forprops snippet): // // var object = { one: 1, two: 2 }; // for (var property in object) { // ....if (object.hasOwnProperty(property)) { // ........ // ....} // }; // // Result examples: // // (1) If indent size = 2: // // var object = { one: 1, two: 2 }; // for (var property in object) { // ..if (object.hasOwnProperty(property)) { // .... // ..} // }; // // (2) If indent size = 2 and OpenBracesOnNewLineForControl = true: // // var object = { one: 1, two: 2 }; // for (var property in object) // { // ..if (object.hasOwnProperty(property)) // ..{ // .... // ..} // }; // Algorithm: The idea is to use Formatting.Formatter to format the inserted content. // However, Formatting.Formatter does not format lines that only contain spaces. For // example, here is how Formatting.Formatter formats the code above: // // var object = { one: 1, two: 2 }; // for (var property in object) { // ..if (object.hasOwnProperty(property)) { // ........ // ..} // }; // // An additional step will be included to ensure such lines are formatted correctly. int baseIndentationLevel = GetViewIndentationLevelAtLine(ts[0].iStartLine); for (int lineNumber = ts[0].iStartLine; lineNumber <= ts[0].iEndLine; ++lineNumber) { string lineContent = _textView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNumber).GetText(); string indentationString = GetTemplateIndentationString(lineContent); if (indentationString != lineContent) // This means line contains some characters other than spaces { continue; } int newIndentationLevel = baseIndentationLevel + GetTemplateIndentationLevel(indentationString); string newIndentation = _textView.Options.IsConvertTabsToSpacesEnabled() ? new string(' ', newIndentationLevel *_textView.Options.GetIndentSize()) : new string('\t', newIndentationLevel); using (var edit = _textView.TextBuffer.CreateEdit()) { int indendationPos; pBuffer.GetPositionOfLineIndex(lineNumber, 0, out indendationPos); Span bufferIndentationSpan = new Span(indendationPos, indentationString.Length); edit.Replace(bufferIndentationSpan, newIndentation); edit.Apply(); } } // Now that we have handled empty lines, use Formatter to format the inserted content. int startPos = 0, endPos = 0; pBuffer.GetPositionOfLineIndex(ts[0].iStartLine, ts[0].iStartIndex, out startPos); pBuffer.GetPositionOfLineIndex(ts[0].iEndLine, ts[0].iEndIndex, out endPos); Formatting.FormattingOptions options = EditFilter.CreateFormattingOptions(_textView.Options, _textView.TextBuffer.CurrentSnapshot); string text = _textView.TextBuffer.CurrentSnapshot.GetText(); EditFilter.ApplyEdits(_textView.TextBuffer, Formatting.Formatter.GetEditsForRange(text, startPos, endPos, options)); return(VSConstants.S_OK); }