private static void FormatDocumentTest(string input, string expected, FormattingOptions options = null) { string pathToFile = "fob.py"; var buffer = new MockTextBuffer(input, pathToFile, "Node.js"); var edits = Formatter.GetEditsForDocument( buffer.CurrentSnapshot.GetText(), options ?? new FormattingOptions() ); EditFilter.ApplyEdits(buffer, edits); Assert.AreEqual(expected, buffer.CurrentSnapshot.GetText()); }
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); }
private static bool ApplyEdits(string filename, dynamic testCase, MockTextBuffer buffer) { foreach (var operation in testCase["operations"]) { Edit[] edits = null; switch ((string)operation["operation"]) { case "Document": edits = Formatter.GetEditsForDocument( buffer.CurrentSnapshot.GetText(), Options ); break; case "CloseBrace": edits = Formatter.GetEditsAfterKeystroke( buffer.CurrentSnapshot.GetText(), (int)operation["point"]["position"], '}', Options ); break; case "Enter": var line = buffer.CurrentSnapshot.GetLineFromPosition((int)operation["point"]["position"]); if (line.LineNumber > 0) { edits = Formatter.GetEditsAfterEnter( buffer.CurrentSnapshot.GetText(), buffer.CurrentSnapshot.GetLineFromLineNumber(line.LineNumber - 1).Start, line.End, Options ); } break; case "Semicolon": edits = Formatter.GetEditsAfterKeystroke( buffer.CurrentSnapshot.GetText(), (int)operation["point"]["position"], ';', Options ); break; case "Paste": edits = Formatter.GetEditsForRange( buffer.CurrentSnapshot.GetText(), (int)operation["span"]["start"], (int)operation["span"]["start"] + (int)operation["span"]["length"], Options ); break; case "Selection": edits = Formatter.GetEditsForRange( buffer.CurrentSnapshot.GetText(), (int)operation["span"]["start"], (int)operation["span"]["start"] + (int)operation["span"]["length"], Options ); break; default: Console.WriteLine("Skipping unsupported test case " + operation["operation"]); return(false); } EditFilter.ApplyEdits(buffer, edits); } return(true); }