public static IList<Edit> GetEdits(SyntaxTree syntaxTree, TextSpan textSpan, FormattingOptions options) { // Format. var formattingVisitor = new FormattingVisitor(syntaxTree, textSpan, options); formattingVisitor.Visit(syntaxTree.Root); return formattingVisitor.Edits.Values; }
public static IList<Edit> GetEditsAfterKeystroke(SyntaxTree syntaxTree, int position, char character, FormattingOptions options) { var location = syntaxTree.MapRootFilePosition(position); // Find span of block / statement terminated by the character just typed. var token = syntaxTree.Root.FindTokenOnLeft(location); if (token.Text != character.ToString()) return new List<Edit>(); // Get span of node containing this token. var span = token.Parent.GetTextSpanRoot(); if (span == TextSpan.None) return new List<Edit>(); return GetEdits(syntaxTree, span, options); }
public void BraceOnNewLineWithInterveningSingleLineComment() { const string before = @" struct S // This is a comment. { float4 position; float3 normal; };"; var options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForTypes = false } }; AssertFormat(before, @" struct S // This is a comment. { float4 position; float3 normal; };", options: options); options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForTypes = true } }; AssertFormat(before, @" struct S // This is a comment. { float4 position; float3 normal; };", options: options); }
public void BraceOnNewLineForTypes() { const string before = @" struct S { float4 position; float3 normal; }; class C { float4 position; float3 normal; };"; var options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForTypes = false } }; AssertFormat(before, @" struct S { float4 position; float3 normal; }; class C { float4 position; float3 normal; };", options: options); options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForTypes = true } }; AssertFormat(before, @" struct S { float4 position; float3 normal; }; class C { float4 position; float3 normal; };", options: options); }
public static IList <Edit> GetEditsAfterKeystroke(SyntaxTree syntaxTree, int position, char character, FormattingOptions options) { var location = syntaxTree.MapRootFilePosition(position); // Find span of block / statement terminated by the character just typed. var token = syntaxTree.Root.FindTokenOnLeft(location); if (token.Text != character.ToString()) { return(new List <Edit>()); } // Get span of node containing this token. var span = token.Parent.GetTextSpanRoot(); if (span == TextSpan.None) { return(new List <Edit>()); } return(GetEdits(syntaxTree, span, options)); }
public void FunctionCallSpacing() { const string before = @" void Foo() { Bar(1); } void Bar(int x) { Foo(); float4 v = float4 ( 1, 2, 3, 4); }"; var options = new FormattingOptions { Spacing = new SpacingOptions { FunctionCallInsertSpaceAfterFunctionName = false, FunctionCallInsertSpaceWithinArgumentListParentheses = false, FunctionCallInsertSpaceWithinEmptyArgumentListParentheses = false } }; AssertFormat(before, @" void Foo() { Bar(1); } void Bar(int x) { Foo(); float4 v = float4(1, 2, 3, 4); }", options: options); options = new FormattingOptions { Spacing = new SpacingOptions { FunctionCallInsertSpaceAfterFunctionName = true, FunctionCallInsertSpaceWithinArgumentListParentheses = true, FunctionCallInsertSpaceWithinEmptyArgumentListParentheses = true } }; AssertFormat(before, @" void Foo() { Bar ( 1 ); } void Bar(int x) { Foo ( ); float4 v = float4 ( 1, 2, 3, 4 ); }", options: options); }
public void BraceOnNewLineForArrayInitializerBraces() { const string before = @" int arrayVariable[2] = { 1 , 2 } ; float3 wave [ 2 ] = { { 1.0, 1.0, 0.5 }, { 2.0, 0.5, 1.3 } };"; var options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForArrayInitializers = false } }; AssertFormat(before, @" int arrayVariable[2] = { 1, 2 }; float3 wave[2] = { { 1.0, 1.0, 0.5 }, { 2.0, 0.5, 1.3 } };", options: options); options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForArrayInitializers = true } }; AssertFormat(before, @" int arrayVariable[2] = { 1, 2 }; float3 wave[2] = { { 1.0, 1.0, 0.5 }, { 2.0, 0.5, 1.3 } };", options: options); }
public void BraceOnNewLineForControlBlocks() { const string before = @" float4 main() { for (int i = 0; i < 10; i++) { } }"; var options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForControlBlocks = false } }; AssertFormat(before, @" float4 main() { for (int i = 0; i < 10; i++) { } }", options: options); options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForControlBlocks = true } }; AssertFormat(before, @" float4 main() { for (int i = 0; i < 10; i++) { } }", options: options); }
public void BinaryOperatorSpacing() { const string before = @" void main(int x, int y) { int z = x * (x * y); }"; var options = new FormattingOptions { Spacing = new SpacingOptions { BinaryOperatorSpaces = BinaryOperatorSpaces.InsertSpaces } }; AssertFormat(before, @" void main(int x, int y) { int z = x * (x * y); }", options: options); options = new FormattingOptions { Spacing = new SpacingOptions { BinaryOperatorSpaces = BinaryOperatorSpaces.RemoveSpaces } }; AssertFormat(before, @" void main(int x, int y) { int z = x*(x*y); }", options: options); }
public void CommaDotSpacing() { const string before = @" void main() { float4 c = myTex. Sample(mySampler, coords); } "; var options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpaceBeforeComma = false, InsertSpaceAfterComma = false, InsertSpaceBeforeDot = false, InsertSpaceAfterDot = false, } }; AssertFormat(before, @" void main() { float4 c = myTex.Sample(mySampler,coords); } ", options: options); options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpaceBeforeComma = true, InsertSpaceAfterComma = true, InsertSpaceBeforeDot = true, InsertSpaceAfterDot = true } }; AssertFormat(before, @" void main() { float4 c = myTex . Sample(mySampler , coords); } ", options: options); }
public void BaseTypeSpacing() { const string before = @" interface iBaseLight { float3 IlluminateAmbient ( float3 vNormal ) ; } ; class cAmbientLight : iBaseLight // Comment { float3 IlluminateAmbient(float3 vNormal); }; class cHemiAmbientLight : cAmbientLight { float3 IlluminateAmbient(float3 vNormal); }; "; var options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpaceBeforeColonForBaseOrInterfaceInTypeDeclaration = false, InsertSpaceAfterColonForBaseOrInterfaceInTypeDeclaration = false } }; AssertFormat(before, @" interface iBaseLight { float3 IlluminateAmbient(float3 vNormal); }; class cAmbientLight:iBaseLight // Comment { float3 IlluminateAmbient(float3 vNormal); }; class cHemiAmbientLight:cAmbientLight { float3 IlluminateAmbient(float3 vNormal); }; ", options: options); options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpaceBeforeColonForBaseOrInterfaceInTypeDeclaration = true, InsertSpaceAfterColonForBaseOrInterfaceInTypeDeclaration = true } }; AssertFormat(before, @" interface iBaseLight { float3 IlluminateAmbient(float3 vNormal); }; class cAmbientLight : iBaseLight // Comment { float3 IlluminateAmbient(float3 vNormal); }; class cHemiAmbientLight : cAmbientLight { float3 IlluminateAmbient(float3 vNormal); }; ", options: options); }
public void BracketsSpacing() { const string before = @" int x[] = { 0, 1 }; int y[2] = { 2, 3 }; void main() { int z = x [ 0 ]; }"; var options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpacesWithinSquareBrackets = false, InsertSpaceWithinEmptySquareBrackets = false, InsertSpaceBeforeOpenSquareBracket = false, InsertSpacesWithinArrayInitializerBraces = false } }; AssertFormat(before, @" int x[] = {0, 1}; int y[2] = {2, 3}; void main() { int z = x[0]; }", options: options); options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpacesWithinSquareBrackets = true, InsertSpaceWithinEmptySquareBrackets = true, InsertSpaceBeforeOpenSquareBracket = true, InsertSpacesWithinArrayInitializerBraces = true } }; AssertFormat(before, @" int x [ ] = { 0, 1 }; int y [ 2 ] = { 2, 3 }; void main() { int z = x [ 0 ]; }", options: options); }
public void SemanticRegisterPackOffsetSpacing() { const string before = @" struct VertexInput { float3 position : POSITION : register(b1); float3 normal : NORMAL; }; cbuffer cbPerObject : register(b0 ) { float4 g_vObjectColor : packoffset(c0); }; "; var options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpacesWithinParenthesesOfRegisterOrPackOffsetQualifiers = false, InsertSpaceBeforeColonForSemanticOrRegisterOrPackOffset = false, InsertSpaceAfterColonForSemanticOrRegisterOrPackOffset = false } }; AssertFormat(before, @" struct VertexInput { float3 position:POSITION:register(b1); float3 normal:NORMAL; }; cbuffer cbPerObject:register(b0) { float4 g_vObjectColor:packoffset(c0); }; ", options: options); options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpacesWithinParenthesesOfRegisterOrPackOffsetQualifiers = true, InsertSpaceBeforeColonForSemanticOrRegisterOrPackOffset = true, InsertSpaceAfterColonForSemanticOrRegisterOrPackOffset = true } }; AssertFormat(before, @" struct VertexInput { float3 position : POSITION : register( b1 ); float3 normal : NORMAL; }; cbuffer cbPerObject : register( b0 ) { float4 g_vObjectColor : packoffset( c0 ); }; ", options: options); }
public void TypeCastSpacing() { const string before = @" void main() { int y = (int) x; }"; var options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpaceAfterCast = false, InsertSpacesWithinParenthesesOfTypeCasts = false } }; AssertFormat(before, @" void main() { int y = (int)x; }", options: options); options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpaceAfterCast = true, InsertSpacesWithinParenthesesOfTypeCasts = true } }; AssertFormat(before, @" void main() { int y = ( int ) x; }", options: options); }
public void BraceOnNewLineForTechniquesAndPasses() { const string before = @" technique T { pass P { // A comment VertexShader = compile vs_2_0 VS ( ) ; PixelShader = compile ps_2_0 PS(); } }"; var options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForTechniquesAndPasses = false } }; AssertFormat(before, @" technique T { pass P { // A comment VertexShader = compile vs_2_0 VS(); PixelShader = compile ps_2_0 PS(); } }", options: options); options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForTechniquesAndPasses = true } }; AssertFormat(before, @" technique T { pass P { // A comment VertexShader = compile vs_2_0 VS(); PixelShader = compile ps_2_0 PS(); } }", options: options); }
public void BraceOnNewLineForFunctions() { const string before = @" float4 main() { return float4(1, 0, 0, 1); }"; var options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForFunctions = false } }; AssertFormat(before, @" float4 main() { return float4(1, 0, 0, 1); }", options: options); options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForFunctions = true } }; AssertFormat(before, @" float4 main() { return float4(1, 0, 0, 1); }", options: options); }
private static void AssertFormat(string unformattedText, string expectedNewText, TextSpan? textSpan = null, FormattingOptions options = null, Dictionary<string, string> includes = null, bool allowSyntaxErrors = false) { Func<string, SyntaxTree> parse = code => SyntaxFactory.ParseSyntaxTree(SourceText.From(code), fileSystem: new InMemoryFileSystem(includes ?? new Dictionary<string, string>())); // Arrange. var syntaxTree = parse(unformattedText); if (textSpan == null) textSpan = new TextSpan(syntaxTree.Text, 0, unformattedText.Length); if (options == null) options = new FormattingOptions(); // Act. var edits = Formatter.GetEdits(syntaxTree, textSpan.Value, options); var formattedCode = Formatter.ApplyEdits(unformattedText, edits); // Assert. if (!allowSyntaxErrors) ShaderTestUtility.CheckForParseErrors(parse(formattedCode)); Assert.That(formattedCode, Is.EqualTo(expectedNewText)); }
public void BraceOnNewLineForStateBlocks() { const string before = @" DepthStencilState DepthDisabling { DepthEnable = FALSE; DepthWriteMask = ZERO; };"; var options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForStateBlocks = false } }; AssertFormat(before, @" DepthStencilState DepthDisabling { DepthEnable = FALSE; DepthWriteMask = ZERO; };", options: options); options = new FormattingOptions { NewLines = { PlaceOpenBraceOnNewLineForStateBlocks = true } }; AssertFormat(before, @" DepthStencilState DepthDisabling { DepthEnable = FALSE; DepthWriteMask = ZERO; };", options: options); }
public void IndentBlockContents() { const string before = @" int Method() { int x; int y; }"; var options = new FormattingOptions { Indentation = { IndentBlockContents = false } }; AssertFormat(before, @" int Method() { int x; int y; }", options: options); options = new FormattingOptions { Indentation = { IndentBlockContents = true } }; AssertFormat(before, @" int Method() { int x; int y; }", options: options); }
public void ElseOnNewLine() { const string before = @" void main() { if (false) { } else { } }"; var options = new FormattingOptions { NewLines = { PlaceElseOnNewLine = false } }; AssertFormat(before, @" void main() { if (false) { } else { } }", options: options); options = new FormattingOptions { NewLines = { PlaceElseOnNewLine = true } }; AssertFormat(before, @" void main() { if (false) { } else { } }", options: options); }
public void IndentBraces() { const string before = @" int Method() { int x; int y; }"; var options = new FormattingOptions { Indentation = { IndentOpenAndCloseBraces = false } }; AssertFormat(before, @" int Method() { int x; int y; }", options: options); options = new FormattingOptions { Indentation = { IndentOpenAndCloseBraces = true } }; AssertFormat(before, @" int Method() { int x; int y; }", options: options); }
public void ControlFlowStatements() { const string before = @" void main() { for ( int i = 0 ; i < x ; i ++) { } while (true) { } do { } while (true); switch (1) { } if (true) { } else if (false) { } else { } }"; var options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpaceAfterKeywordsInControlFlowStatements = false, InsertSpacesWithinParenthesesOfControlFlowStatements = false } }; AssertFormat(before, @" void main() { for(int i = 0; i < x; i++) { } while(true) { } do { } while(true); switch(1) { } if(true) { } else if(false) { } else { } }", options: options); options = new FormattingOptions { Spacing = new SpacingOptions { InsertSpaceAfterKeywordsInControlFlowStatements = true, InsertSpacesWithinParenthesesOfControlFlowStatements = true } }; AssertFormat(before, @" void main() { for ( int i = 0; i < x; i++ ) { } while ( true ) { } do { } while ( true ); switch ( 1 ) { } if ( true ) { } else if ( false ) { } else { } }", options: options); }
public void IndentSwitchCase() { const string before = @" void main(int foo) { switch ( foo ) { case 0 : break ; default : break ; } }"; var options = new FormattingOptions { Indentation = { IndentCaseContents = false, IndentCaseLabels = false } }; AssertFormat(before, @" void main(int foo) { switch (foo) { case 0: break; default: break; } }", options: options); options = new FormattingOptions { Indentation = { IndentCaseContents = true, IndentCaseLabels = true } }; AssertFormat(before, @" void main(int foo) { switch (foo) { case 0: break; default: break; } }", options: options); }
public void IndentPreprocessorDirectives() { const string before = @" void main(int foo) { if (true) { #if TEXTURES float4 color = ReadTexture(); #endif } }"; var options = new FormattingOptions { Indentation = { PreprocessorDirectivePosition = PreprocessorDirectivePosition.LeaveIndented } }; AssertFormat(before, @" void main(int foo) { if (true) { #if TEXTURES float4 color = ReadTexture(); #endif } }", options: options); options = new FormattingOptions { Indentation = { PreprocessorDirectivePosition = PreprocessorDirectivePosition.MoveToLeftmostColumn } }; AssertFormat(before, @" void main(int foo) { if (true) { #if TEXTURES float4 color = ReadTexture(); #endif } }", options: options); options = new FormattingOptions { Indentation = { PreprocessorDirectivePosition = PreprocessorDirectivePosition.OneIndentToLeft } }; AssertFormat(before, @" void main(int foo) { if (true) { #if TEXTURES float4 color = ReadTexture(); #endif } }", options: options); }
public static IList <Edit> GetEdits(SyntaxTree syntaxTree, TextSpan textSpan, FormattingOptions options) { // Format. var formattingVisitor = new FormattingVisitor(syntaxTree, textSpan, options); formattingVisitor.Visit(syntaxTree.Root); return(formattingVisitor.Edits.Values); }