internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { Target.AppendCodeString(res, ast, format); format.Append( res, format.SpaceBeforeIndexBracket, " ", "", this.GetProceedingWhiteSpace(ast) ); res.Append('['); _index.AppendCodeString( res, ast, format, format.SpaceWithinIndexBrackets != null ? format.SpaceWithinIndexBrackets.Value ? " " : "" : null ); if (!this.IsMissingCloseGrouping(ast)) { format.Append( res, format.SpaceWithinIndexBrackets, " ", "", this.GetSecondWhiteSpace(ast) ); res.Append(']'); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { Expression left = _left; Expression right = _right; string op1, op2; if (Operator == PythonOperator.NotIn) { op1 = "not"; if (!this.IsIncompleteNode(ast)) { op2 = "in"; } else { op2 = null; } } else if (Operator == PythonOperator.IsNot) { op1 = "is"; op2 = "not"; } else if ((op1 = this.GetVerbatimImage(ast)) != null) { // operator image differs from the operator enum, for example <> is always NotEqual which is != // so we store the verbatim image and use it here. op2 = null; } else { op1 = Operator.ToCodeString(); op2 = null; } BinaryToCodeString(res, ast, format, this, _left, _right, op1, op2); }
internal override void AppendCodeStringStmt(StringBuilder res, JAst ast, CodeFormattingOptions format) { foreach(var preceeding in _preceeding) { preceeding.AppendCodeString(res, ast, format); } res.Append(this.GetVerbatimImage(ast) ?? "<error stmt>"); }
internal override void AppendCodeString(StringBuilder res, JAst ast, CodeFormattingOptions format) { if (_preceeding != null) { _preceeding.AppendCodeString(res, ast, format); } res.Append(_verbatimImage ?? "<error>"); }
internal override void AppendCodeString(StringBuilder res, JAst ast, CodeFormattingOptions format, string leadingWhiteSpace) { string kwOnlyText = this.GetExtraVerbatimText(ast); if (kwOnlyText != null) { if (leadingWhiteSpace != null) { res.Append(leadingWhiteSpace); res.Append(kwOnlyText.TrimStart()); leadingWhiteSpace = null; } else { res.Append(kwOnlyText); } } bool isAltForm = this.IsAltForm(ast); if (isAltForm) { res.Append(leadingWhiteSpace ?? this.GetProceedingWhiteSpace(ast)); res.Append('('); leadingWhiteSpace = null; } _error.AppendCodeString(res, ast, format, leadingWhiteSpace); if (this.DefaultValue != null) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append('='); this.DefaultValue.AppendCodeString(res, ast, format); } if (isAltForm && !this.IsMissingCloseGrouping(ast)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append(')'); } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { _left.AppendCodeString(res, ast, format); res.Append(this.GetProceedingWhiteSpace(ast)); res.Append(_op.ToCodeString()); res.Append('='); _right.AppendCodeString(res, ast, format); }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { if (this.IsAltForm(ast)) { this.AppendCodeString(res, ast, format, "", "", _item); } else { this.AppendCodeString(res, ast, format, "(", this.IsMissingCloseGrouping(ast) ? "" : ")", _item); } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("with"); var itemWhiteSpace = this.GetListWhiteSpace(ast); int whiteSpaceIndex = 0; for (int i = 0; i < _items.Length; i++) { var item = _items[i]; if (i != 0) { if (itemWhiteSpace != null) { res.Append(itemWhiteSpace[whiteSpaceIndex++]); } res.Append(','); } item.ContextManager.AppendCodeString(res, ast, format); if (item.Variable != null) { if (itemWhiteSpace != null) { res.Append(itemWhiteSpace[whiteSpaceIndex++]); } else { res.Append(' '); } res.Append("as"); item.Variable.AppendCodeString(res, ast, format); } } _body.AppendCodeString(res, ast, format); }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpaceDefaultNull(ast)); if (format.UseVerbatimImage) { res.Append(this.GetVerbatimImage(ast) ?? _name); } else { res.Append(_name); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { _target.AppendCodeString(res, ast, format); res.Append(this.GetProceedingWhiteSpaceDefaultNull(ast)); res.Append('.'); if (!this.IsIncompleteNode(ast)) { res.Append(this.GetSecondWhiteSpaceDefaultNull(ast)); res.Append(this.GetVerbatimImage(ast) ?? _name); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append('`'); _expression.AppendCodeString(res, ast, format); if (!this.IsMissingCloseGrouping(ast)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append('`'); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { res.Append(this.GetProceedingWhiteSpace(ast)); res.Append("for"); _lhs.AppendCodeString(res, ast, format); if (!this.IsIncompleteNode(ast)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append("in"); _list.AppendCodeString(res, ast, format); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { var whitespace = this.GetListWhiteSpace(ast); for (int i = 0; i < _dotCount; i++) { if (whitespace != null) { res.Append(whitespace[i]); } res.Append('.'); } base.AppendCodeString(res, ast, format); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("assert"); _test.AppendCodeString(res, ast, format); if (_message != null) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append(','); _message.AppendCodeString(res, ast, format); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { _target.AppendCodeString(res, ast, format); format.Append( res, format.SpaceBeforeCallParen, " ", "", this.GetProceedingWhiteSpaceDefaultNull(ast) ); res.Append('('); if (_args.Length == 0) { if (format.SpaceWithinEmptyCallArgumentList != null && format.SpaceWithinEmptyCallArgumentList.Value) { res.Append(' '); } } else { var listWhiteSpace = this.GetListWhiteSpace(ast); for (int i = 0; i < _args.Length; i++) { if (i > 0) { if (listWhiteSpace != null) { res.Append(listWhiteSpace[i - 1]); } res.Append(','); } else if (format.SpaceWithinCallParens != null) { _args[i].AppendCodeString(res, ast, format, format.SpaceWithinCallParens.Value ? " " : ""); continue; } _args[i].AppendCodeString(res, ast, format); } if (listWhiteSpace != null && listWhiteSpace.Length == _args.Length) { // trailing comma res.Append(listWhiteSpace[listWhiteSpace.Length - 1]); res.Append(","); } } if (!this.IsMissingCloseGrouping(ast)) { if (Args.Count != 0 || format.SpaceWithinEmptyCallArgumentList == null || !String.IsNullOrWhiteSpace(this.GetSecondWhiteSpaceDefaultNull(ast))) { format.Append( res, format.SpaceWithinCallParens, " ", "", this.GetSecondWhiteSpaceDefaultNull(ast) ); } res.Append(')'); } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("while"); _test.AppendCodeString(res, ast, format); _body.AppendCodeString(res, ast, format); if (_else != null) { format.ReflowComment(res, this.GetSecondWhiteSpaceDefaultNull(ast)); res.Append("else"); _else.AppendCodeString(res, ast, format); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("await"); if (!this.IsAltForm(ast)) { _expression.AppendCodeString(res, ast, format); var itemWhiteSpace = this.GetListWhiteSpace(ast); if (itemWhiteSpace != null) { res.Append(","); res.Append(itemWhiteSpace[0]); } } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { var namesWhiteSpace = this.GetNamesWhiteSpace(ast); if (namesWhiteSpace != null) { ListExpression.AppendItems(res, ast, format, "nonlocal", "", this, Names.Count, (i, sb) => { sb.Append(namesWhiteSpace[i]); Names[i].AppendCodeString(res, ast, format); }); } else { ListExpression.AppendItems(res, ast, format, "nonlocal", "", this, Names.Count, (i, sb) => Names[i].AppendCodeString(sb, ast, format)); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { _expression.AppendCodeString(res, ast, format); if (_annotation != null) { // For now, use same formatting as around an assignment if (format.SpacesAroundAssignmentOperator == null) { res.Append(this.GetSecondWhiteSpaceDefaultNull(ast) ?? ""); } else if (format.SpacesAroundAssignmentOperator == true) { res.Append(' '); } res.Append(':'); _annotation.AppendCodeString(res, ast, format); } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("print"); if (_dest != null) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append(">>"); _dest.AppendCodeString(res, ast, format); if (_expressions.Length > 0) { res.Append(this.GetThirdWhiteSpace(ast)); res.Append(','); } } ListExpression.AppendItems(res, ast, format, "", "", this, Expressions); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("return"); if (_expression != null) { int len = res.Length; _expression.AppendCodeString(res, ast, format); if (this.IsAltForm(ast)) { // remove the leading white space and insert a single space res.Remove(len, _expression.GetLeadingWhiteSpace(ast).Length); res.Insert(len, ' '); } } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { res.Append(this.GetProceedingWhiteSpace(ast)); res.Append("yield"); res.Append(this.GetSecondWhiteSpace(ast)); res.Append("from"); if (!this.IsAltForm(ast)) { Expression.AppendCodeString(res, ast, format); var itemWhiteSpace = this.GetListWhiteSpace(ast); if (itemWhiteSpace != null) { res.Append(","); res.Append(itemWhiteSpace[0]); } } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("exec"); _code.AppendCodeString(res, ast, format); if (_globals != null) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append("in"); _globals.AppendCodeString(res, ast, format); if (_locals != null) { res.Append(this.GetThirdWhiteSpace(ast)); res.Append(','); _locals.AppendCodeString(res, ast, format); } } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("for"); _left.AppendCodeString(res, ast, format); if (!this.IsIncompleteNode(ast)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append("in"); _list.AppendCodeString(res, ast, format); _body.AppendCodeString(res, ast, format); // colon is handled by suite statements... if (_else != null) { format.ReflowComment(res, this.GetThirdWhiteSpace(ast)); res.Append("else"); _else.AppendCodeString(res, ast, format); } } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { var whitespace = this.GetNamesWhiteSpace(ast); for (int i = 0, whitespaceIndex = 0; i < _names.Length; i++) { if (whitespace != null) { res.Append(whitespace[whitespaceIndex++]); } if (i != 0) { res.Append('.'); if (whitespace != null) { res.Append(whitespace[whitespaceIndex++]); } } _names[i].AppendCodeString(res, ast, format); } }
internal void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format, string start, string end, Expression item) { if (!String.IsNullOrEmpty(start)) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append(start); } item.AppendCodeString(res, ast, format); for (int i = 0; i < Iterators.Count; i++) { Iterators[i].AppendCodeString(res, ast, format); } if (!String.IsNullOrEmpty(end)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append(end); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { _trueExpr.AppendCodeString(res, ast, format); res.Append(this.GetProceedingWhiteSpace(ast)); res.Append("if"); if (!ast.HasVerbatim) { res.Append(' '); } _testExpr.AppendCodeString(res, ast, format); res.Append(this.GetSecondWhiteSpace(ast)); if (!this.IsIncompleteNode(ast)) { res.Append("else"); if (!ast.HasVerbatim) { res.Append(' '); } _falseExpr.AppendCodeString(res, ast, format); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { var verbatimPieces = this.GetVerbatimNames(ast); var verbatimComments = this.GetListWhiteSpace(ast); if (verbatimPieces != null) { // string+ / bytes+, such as "abc" "abc", which can spawn multiple lines, and // have comments in between the peices. for (int i = 0; i < verbatimPieces.Length; i++) { if (verbatimComments != null && i < verbatimComments.Length) { format.ReflowComment(res, verbatimComments[i]); } res.Append(verbatimPieces[i]); } } else { format.ReflowComment(res, this.GetProceedingWhiteSpaceDefaultNull(ast)); res.Append(this.GetExtraVerbatimText(ast) ?? GetConstantRepr(ast.LanguageVersion)); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { if (_name != null) { if (Name == "*" || Name == "**") { _name.AppendCodeString(res, ast, format); _expression.AppendCodeString(res, ast, format); } else { // keyword arg _name.AppendCodeString(res, ast, format); res.Append(this.GetProceedingWhiteSpace(ast)); res.Append('='); _expression.AppendCodeString(res, ast, format); } } else { _expression.AppendCodeString(res, ast, format); } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format, string leadingWhiteSpace) { string kwOnlyText = this.GetExtraVerbatimText(ast); if (kwOnlyText != null) { if (leadingWhiteSpace != null) { res.Append(leadingWhiteSpace); res.Append(kwOnlyText.TrimStart()); leadingWhiteSpace = null; } else { res.Append(kwOnlyText); } } res.Append(leadingWhiteSpace ?? this.GetProceedingWhiteSpace(ast)); res.Append('('); Tuple.AppendCodeString(res, ast, format); if (!this.IsMissingCloseGrouping(ast)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append(')'); } }
public async Task FormatDocument2() { var input = @"import sys import abc #ABC foo() foo() x = 34; y = x;z = y goo(y) x = 34; y = x;z = y"; var expected = @"import sys import abc #ABC foo() foo() x = 34 y = x z = y goo(y) x = 34 y = x z = y"; var options = new CodeFormattingOptions { BreakMultipleStatementsPerLine = true, NewLineFormat = NewLineKind.CarriageReturnLineFeed.GetString(), RemoveTrailingSemicolons = true, ReplaceMultipleImportsWithMultipleStatements = true, SpaceAroundAnnotationArrow = true, SpaceAroundDefaultValueEquals = false, SpaceBeforeCallParen = false, SpaceBeforeClassDeclarationParen = false, SpaceBeforeFunctionDeclarationParen = false, SpaceBeforeIndexBracket = false, SpaceWithinCallParens = false, SpaceWithinClassDeclarationParens = false, SpaceWithinEmptyBaseClassList = false, SpaceWithinEmptyCallArgumentList = false, SpaceWithinEmptyParameterList = false, SpaceWithinEmptyTupleExpression = false, SpaceWithinFunctionDeclarationParens = false, SpaceWithinIndexBrackets = false, SpacesAroundAssignmentOperator = true, SpacesAroundBinaryOperators = true, SpacesWithinEmptyListExpression = false, SpacesWithinListExpression = false, SpacesWithinParenthesisExpression = false, SpacesWithinParenthesisedTupleExpression = false, WrapComments = true, WrappingWidth = 80 }; await CodeFormattingTest(input, new Span(0, input.Length), expected, null, options, false); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { foreach (var preceeding in _preceeding) { preceeding.AppendCodeString(res, ast, format); } res.Append(this.GetVerbatimImage(ast) ?? "<error stmt>"); }
private static async Task CodeFormattingTest(string input, object selection, string expected, object expectedSelection, CodeFormattingOptions options, bool formatSelected = true) { var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7)); var editorTestToolset = new EditorTestToolset().WithPythonToolsService(); var services = editorTestToolset.GetPythonEditorServices(); editorTestToolset.GetService <IPythonToolsOptionsService>().ImportFrom(options); using (var analyzer = await VsProjectAnalyzer.CreateForTestsAsync(services, fact)) { var analysisStartedTask = EventTaskSources.VsProjectAnalyzer.AnalysisStarted.Create(analyzer); var buffer = editorTestToolset.CreatePythonTextBuffer(input, analyzer); var view = editorTestToolset.CreateTextView(buffer); await analysisStartedTask; var bi = services.GetBufferInfo(buffer); var entry = await analyzer.AnalyzeFileAsync(bi.Filename); Assert.AreEqual(entry, bi.TrySetAnalysisEntry(entry, null), "Failed to set analysis entry"); entry.GetOrCreateBufferParser(services).AddBuffer(buffer); if (formatSelected) { var selectionSpan = new SnapshotSpan( buffer.CurrentSnapshot, ExtractMethodTests.GetSelectionSpan(input, selection) ); await editorTestToolset.UIThread.InvokeTask(async() => { view.Selection.Select(selectionSpan, false); await EditFilter.GetOrCreate(services, view).FormatSelectionAsync(); }); } else { await editorTestToolset.UIThread.InvokeTask(async() => { await EditFilter.GetOrCreate(services, view).FormatDocumentAsync(); }); } Assert.AreEqual(expected, view.TextBuffer.CurrentSnapshot.GetText()); if (expectedSelection != null) { Assert.AreEqual( ExtractMethodTests.GetSelectionSpan(expected, expectedSelection), view.Selection.StreamSelectionSpan.SnapshotSpan.Span ); } } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) => ListExpression.AppendItems(res, ast, format, "{", this.IsMissingCloseGrouping(ast) ? "" : "}", this, Items);
internal override sealed void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { AppendCodeStringStmt(res, ast, format); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { // SuiteStatement comes in 3 forms: // 1. The body of a if/else/while/for/etc... where there's an opening colon // 2. A set of semi-colon separated items // 3. A top-level group of statements in a top-level PythonAst node. var itemWhiteSpace = this.GetListWhiteSpace(ast); var colonWhiteSpace = this.GetPreceedingWhiteSpaceDefaultNull(ast); if (this.IsAltForm(ast)) { // suite statement in top-level PythonAst, we have no colons or other delimiters foreach (var statement in _statements) { statement.AppendCodeString(res, ast, format); } } else if (itemWhiteSpace != null) { if (format.BreakMultipleStatementsPerLine) { var leadingWhiteSpace = ""; for (var i = 0; i < _statements.Length; i++) { if (i == 0) { var tmp = new StringBuilder(); _statements[i].AppendCodeString(tmp, ast, format); var stmt = tmp.ToString(); res.Append(stmt); // figure out the whitespace needed for the next statement based upon the current statement for (var curChar = 0; curChar < stmt.Length; curChar++) { if (!char.IsWhiteSpace(stmt[curChar])) { leadingWhiteSpace = format.GetNextLineProceedingText(stmt.Substring(0, curChar)); break; } } } else { _statements[i].AppendCodeString(res, ast, format, leadingWhiteSpace); } } } else { // form 2, semi-colon seperated list. for (var i = 0; i < _statements.Length; i++) { if (i > 0) { if (i - 1 < itemWhiteSpace.Length) { res.Append(itemWhiteSpace[i - 1]); } res.Append(';'); } _statements[i].AppendCodeString(res, ast, format); } } if (itemWhiteSpace != null && itemWhiteSpace.Length == _statements.Length && _statements.Length != 0) { // trailing semi-colon if (!format.RemoveTrailingSemicolons) { res.Append(itemWhiteSpace[itemWhiteSpace.Length - 1]); res.Append(";"); } } } else { // 3rd form, suite statement as the body of a class/function, we include the colon. if (colonWhiteSpace != null) { res.Append(colonWhiteSpace); } res.Append(':'); foreach (var statement in _statements) { statement.AppendCodeString(res, ast, format); } } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format, string leadingWhiteSpace) { var kwOnlyText = this.GetExtraVerbatimText(ast); if (kwOnlyText != null) { if (leadingWhiteSpace != null) { res.Append(leadingWhiteSpace); res.Append(kwOnlyText.TrimStart()); leadingWhiteSpace = null; } else { res.Append(kwOnlyText); } } var writeName = true; switch (Kind) { case ParameterKind.Dictionary: res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? string.Empty); leadingWhiteSpace = null; res.Append("**"); break; case ParameterKind.List: res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? string.Empty); leadingWhiteSpace = null; res.Append('*'); break; case ParameterKind.Normal: if (this.IsAltForm(ast)) { res.Append(leadingWhiteSpace ?? this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? string.Empty); leadingWhiteSpace = null; res.Append('('); AppendParameterName(res, ast, format, leadingWhiteSpace); if (!this.IsMissingCloseGrouping(ast)) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append(')'); } writeName = false; } break; case ParameterKind.KeywordOnly: break; default: throw new InvalidOperationException(); } if (writeName) { AppendParameterName(res, ast, format, leadingWhiteSpace); } if (Annotation != null) { res.Append(this.GetThirdWhiteSpaceDefaultNull(ast) ?? ""); res.Append(':'); Annotation.AppendCodeString(res, ast, format); } if (DefaultValue != null) { format.Append( res, format.SpaceAroundDefaultValueEquals, " ", string.Empty, NodeAttributes.GetWhiteSpace(this, ast, WhitespacePrecedingAssign) ); res.Append('='); if (format.SpaceAroundDefaultValueEquals != null) { DefaultValue.AppendCodeString(res, ast, format, format.SpaceAroundDefaultValueEquals.Value ? " " : string.Empty); } else { DefaultValue.AppendCodeString(res, ast, format); } } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) => BinaryExpression.BinaryToCodeString(res, ast, format, this, Left, Right, "and");
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) => Value?.AppendCodeString(res, ast, format);
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { _body.AppendCodeString(res, ast, format); res.Append(this.GetExtraVerbatimText(ast)); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { var decorateWhiteSpace = this.GetNamesWhiteSpace(ast); if (Decorators != null) { for (int i = 0, curWhiteSpace = 0; i < Decorators.Count; i++) { if (decorateWhiteSpace != null) { format.ReflowComment(res, decorateWhiteSpace[curWhiteSpace++]); } res.Append('@'); if (Decorators[i] != null) { Decorators[i].AppendCodeString(res, ast, format); if (decorateWhiteSpace != null) { format.ReflowComment(res, decorateWhiteSpace[curWhiteSpace++]); } else { res.Append(Environment.NewLine); } } } } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast)); res.Append("assert"); Test.AppendCodeString(res, ast, format); if (Message != null) { res.Append(this.GetSecondWhiteSpace(ast)); res.Append(','); Message.AppendCodeString(res, ast, format); } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast)); res.Append("continue"); }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { _target.AppendCodeString(res, ast, format); format.Append( res, format.SpaceBeforeCallParen, " ", "", this.GetPreceedingWhiteSpaceDefaultNull(ast) ); res.Append('('); if (_args.Length == 0) { if (format.SpaceWithinEmptyCallArgumentList != null && format.SpaceWithinEmptyCallArgumentList.Value) { res.Append(' '); } } else { var listWhiteSpace = format.SpaceBeforeComma == null?this.GetListWhiteSpace(ast) : null; var spaceAfterComma = format.SpaceAfterComma.HasValue ? (format.SpaceAfterComma.Value ? " " : "") : (string)null; for (int i = 0; i < _args.Length; i++) { if (i > 0) { if (format.SpaceBeforeComma == true) { res.Append(' '); } else if (listWhiteSpace != null) { res.Append(listWhiteSpace[i - 1]); } res.Append(','); } else if (format.SpaceWithinCallParens != null) { _args[i].AppendCodeString(res, ast, format, format.SpaceWithinCallParens.Value ? " " : ""); continue; } _args[i].AppendCodeString(res, ast, format, spaceAfterComma); } if (listWhiteSpace != null && listWhiteSpace.Length == _args.Length) { // trailing comma res.Append(listWhiteSpace[listWhiteSpace.Length - 1]); res.Append(","); } } if (!this.IsMissingCloseGrouping(ast)) { if (Args.Count != 0 || format.SpaceWithinEmptyCallArgumentList == null || !String.IsNullOrWhiteSpace(this.GetSecondWhiteSpaceDefaultNull(ast))) { format.Append( res, format.SpaceWithinCallParens, " ", "", this.GetSecondWhiteSpaceDefaultNull(ast) ); } res.Append(')'); } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { var lhs = this.GetListWhiteSpace(ast); for (int i = 0; i < Left.Count; i++) { if (lhs != null && i != 0) { format.Append( res, format.SpacesAroundAssignmentOperator, " ", "", lhs[i - 1] ); res.Append("="); } Left[i].AppendCodeString( res, ast, format, i != 0 && format.SpacesAroundAssignmentOperator != null ? format.SpacesAroundAssignmentOperator.Value ? " " : "" : null ); } if (lhs != null) { format.Append( res, format.SpacesAroundAssignmentOperator, " ", "", lhs[lhs.Length - 1] ); } res.Append("="); Right.AppendCodeString( res, ast, format, format.SpacesAroundAssignmentOperator != null ? format.SpacesAroundAssignmentOperator.Value ? " " : "" : null ); }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { _test.AppendCodeString(res, ast, format); _body.AppendCodeString(res, ast, format); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { if (Decorators != null) { Decorators.AppendCodeString(res, ast, format); } format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("class"); res.Append(this.GetSecondWhiteSpace(ast)); res.Append(this.GetVerbatimImage(ast) ?? Name); if (!this.IsAltForm(ast)) { format.Append( res, format.SpaceBeforeClassDeclarationParen, " ", "", this.GetThirdWhiteSpace(ast) ); res.Append('('); } if (Bases.Count != 0) { ListExpression.AppendItems( res, ast, format, "", "", this, this.Bases.Count, (i, sb) => { if (format.SpaceWithinClassDeclarationParens != null && i == 0) { // need to remove any leading whitespace which was preserved for // the 1st param, and then force the correct whitespace. Bases[i].AppendCodeString(sb, ast, format, format.SpaceWithinClassDeclarationParens.Value ? " " : ""); } else { Bases[i].AppendCodeString(sb, ast, format); } } ); } else if (!this.IsAltForm(ast)) { if (format.SpaceWithinEmptyBaseClassList != null && format.SpaceWithinEmptyBaseClassList.Value) { res.Append(' '); } } if (!this.IsAltForm(ast) && !this.IsMissingCloseGrouping(ast)) { if (Bases.Count != 0 || format.SpaceWithinEmptyBaseClassList == null || !String.IsNullOrWhiteSpace(this.GetFourthWhiteSpace(ast))) { format.Append( res, format.SpaceWithinClassDeclarationParens, " ", "", this.GetFourthWhiteSpace(ast) ); } res.Append(')'); } _body.AppendCodeString(res, ast, format); }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { // WithStatement expands us throw new InvalidOperationException(); }
public CodeFormatter(IServiceProvider serviceProvider, ITextView view, CodeFormattingOptions format) { _view = view; _format = format; _serviceProvider = serviceProvider; }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); if (IsAsync) { res.Append("async"); res.Append(this.GetSecondWhiteSpace(ast)); } res.Append("with"); var itemWhiteSpace = this.GetListWhiteSpace(ast); int whiteSpaceIndex = 0; for (int i = 0; i < _items.Length; i++) { var item = _items[i]; if (i != 0) { if (itemWhiteSpace != null) { res.Append(itemWhiteSpace[whiteSpaceIndex++]); } res.Append(','); } item.ContextManager.AppendCodeString(res, ast, format); if (item.Variable != null) { if (itemWhiteSpace != null) { res.Append(itemWhiteSpace[whiteSpaceIndex++]); } else { res.Append(' '); } res.Append("as"); item.Variable.AppendCodeString(res, ast, format); } } _body.AppendCodeString(res, ast, format); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { var itemWhiteSpace = this.GetListWhiteSpace(ast); for (int i = 0; i < _tests.Length; i++) { if (itemWhiteSpace != null) { format.ReflowComment(res, itemWhiteSpace[i]); } if (i == 0) { res.Append("if"); } else { res.Append("elif"); } _tests[i].AppendCodeString(res, ast, format); } if (_else != null) { format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast)); res.Append("else"); _else.AppendCodeString(res, ast, format); } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { var namesWhiteSpace = this.GetNamesWhiteSpace(ast); if (namesWhiteSpace != null) { ListExpression.AppendItems(res, ast, format, "global", "", this, Names.Count, (i, sb) => { sb.Append(namesWhiteSpace[i]); Names[i].AppendCodeString(res, ast, format); }); } else { ListExpression.AppendItems(res, ast, format, "global", "", this, Names.Count, (i, sb) => Names[i].AppendCodeString(sb, ast, format)); } }
internal abstract void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format);
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { res.Append(this.GetPreceedingWhiteSpaceDefaultNull(ast) ?? ""); res.Append(new string('*', StarCount)); Expression.AppendCodeString(res, ast, format); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { ListExpression.AppendItems(res, ast, format, "del", "", this, Expressions); }
private static void CodeFormattingTest(string input, object selection, string expected, object expectedSelection, CodeFormattingOptions options, bool selectResult = true) { var fact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(new Version(2, 7)); var serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider(); using (var analyzer = new VsProjectAnalyzer(serviceProvider, fact)) { var buffer = new MockTextBuffer(input, PythonCoreConstants.ContentType, "C:\\fob.py"); buffer.AddProperty(typeof(VsProjectAnalyzer), analyzer); var view = new MockTextView(buffer); analyzer.MonitorTextBufferAsync(buffer).Wait(); var selectionSpan = new SnapshotSpan( buffer.CurrentSnapshot, ExtractMethodTests.GetSelectionSpan(input, selection) ); view.Selection.Select(selectionSpan, false); analyzer.FormatCodeAsync( selectionSpan, view, options, selectResult ).Wait(); Assert.AreEqual(expected, view.TextBuffer.CurrentSnapshot.GetText()); if (expectedSelection != null) { Assert.AreEqual( ExtractMethodTests.GetSelectionSpan(expected, expectedSelection), view.Selection.StreamSelectionSpan.SnapshotSpan.Span ); } } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("return"); if (_expression != null) { int len = res.Length; _expression.AppendCodeString(res, ast, format); if (this.IsAltForm(ast)) { // remove the leading white space and insert a single space res.Remove(len, _expression.GetLeadingWhiteSpace(ast).Length); res.Insert(len, ' '); } } }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { var asNameWhiteSpace = this.GetNamesWhiteSpace(ast); if (format.ReplaceMultipleImportsWithMultipleStatements) { var proceeding = this.GetProceedingWhiteSpace(ast); var additionalProceeding = format.GetNextLineProceedingText(proceeding); for (int i = 0, asIndex = 0; i < _names.Length; i++) { if (i == 0) { format.ReflowComment(res, proceeding); } else { res.Append(additionalProceeding); } res.Append("import"); _names[i].AppendCodeString(res, ast, format); AppendAs(res, ast, format, asNameWhiteSpace, i, ref asIndex); } return; } else { format.ReflowComment(res, this.GetProceedingWhiteSpace(ast)); res.Append("import"); var itemWhiteSpace = this.GetListWhiteSpace(ast); for (int i = 0, asIndex = 0; i < _names.Length; i++) { if (i > 0 && itemWhiteSpace != null) { res.Append(itemWhiteSpace[i - 1]); res.Append(','); } _names[i].AppendCodeString(res, ast, format); AppendAs(res, ast, format, asNameWhiteSpace, i, ref asIndex); } } }
internal override void AppendCodeString(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast)); res.Append(Op.ToCodeString()); Expression.AppendCodeString(res, ast, format); }
internal override void AppendCodeStringStmt(StringBuilder res, PythonAst ast, CodeFormattingOptions format) { format.ReflowComment(res, this.GetPreceedingWhiteSpace(ast)); res.Append("while"); Test.AppendCodeString(res, ast, format); Body.AppendCodeString(res, ast, format); if (ElseStatement != null) { format.ReflowComment(res, this.GetSecondWhiteSpaceDefaultNull(ast)); res.Append("else"); ElseStatement.AppendCodeString(res, ast, format); } }