public void TestIndentClassBody () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test a; }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.IndentClassBody = true; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test a; }", data.Document.Text); policy.IndentClassBody = false; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test a; }", data.Document.Text); }
public void TestNamespaceBraceStyle () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"namespace A { namespace B { class Test {} } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.NamespaceBraceStyle = BraceStyle.EndOfLine; policy.ClassBraceStyle = BraceStyle.DoNotChange; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"namespace A { namespace B { class Test {} } }", data.Document.Text); policy.NamespaceBraceStyle = BraceStyle.NextLineShifted; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"namespace A { namespace B { class Test {} } }", data.Document.Text); }
public void TestIfAlignment() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.AlignEmbeddedIfStatements = true; policy.ClassBraceStyle = BraceStyle.EndOfLine; var adapter = Test(policy, @"class Test { Test TestMethod () { if (a) if (b) { } } }", @"class Test { Test TestMethod () { if (a) if (b) { } } }"); policy.AlignEmbeddedIfStatements = false; Continue(policy, adapter, @"class Test { Test TestMethod () { if (a) if (b) { } } }"); }
public void TestForEachBraceForcementRemove() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.NextLine; policy.ForEachBraceForcement = BraceForcement.RemoveBraces; Test(policy, @"class Test { Test TestMethod () { foreach (var obj in col) { ; ; } foreach (var obj in col) { ; } } }", @"class Test { Test TestMethod () { foreach (var obj in col) { ; ; } foreach (var obj in col) ; } }"); }
public void TestForBraceForcementAdd() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.NextLine; policy.ForBraceForcement = BraceForcement.AddBraces; Test(policy, @"class Test { Test TestMethod () { for (;;) { } for (;;) ; } }", @"class Test { Test TestMethod () { for (;;) { } for (;;) { ; } } }"); }
public void TestUsingAlignment() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.AlignEmbeddedUsingStatements = true; policy.ClassBraceStyle = BraceStyle.EndOfLine; var adapter = Test(policy, @"class Test { Test TestMethod () { using (var p = new MyObj()) using (var o = new MyObj()) { } } }", @"class Test { Test TestMethod () { using (var p = new MyObj()) using (var o = new MyObj()) { } } }"); policy.AlignEmbeddedUsingStatements = false; Continue(policy, adapter, @"class Test { Test TestMethod () { using (var p = new MyObj()) using (var o = new MyObj()) { } } }"); }
public void TestUsingForcementDoNotChange() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.EndOfLine; policy.UsingBraceForcement = BraceForcement.DoNotChange; Test(policy, @"class Test { Test TestMethod () { using (var o = new MyObj()) { } using (var o = new MyObj()) ; } }", @"class Test { Test TestMethod () { using (var o = new MyObj()) { } using (var o = new MyObj()) ; } }"); }
public void TestFixedForcementAdd() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.EndOfLine; policy.FixedBraceForcement = BraceForcement.AddBraces; Test(policy, @"class Test { Test TestMethod () { fixed (object* obj = &obj) { } fixed (object* obj = &obj) ; } }", @"class Test { Test TestMethod () { fixed (object* obj = &obj) { } fixed (object* obj = &obj) { ; } } }"); }
public void TestPlaceWhileOnNewLine() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.PlaceWhileOnNewLine = true; Test(policy, @"class Test { Test TestMethod () { do { ; } while (true); } }", @"class Test { Test TestMethod () { do { ; } while (true); } }"); }
public void TestWithinMethodCallParenthesesSpace() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.SpaceWithinMethodCallParentheses = true; var result = GetResult(policy, @"class Test { void TestMe () { MethodCall(true); } }"); int i1 = result.Text.LastIndexOf("("); int i2 = result.Text.LastIndexOf(")") + ")".Length; Assert.AreEqual(@"( true )", result.GetTextAt(i1, i2 - i1)); policy.SpaceWithinMethodCallParentheses = false; result = GetResult(policy, @"class Test { void TestMe () { MethodCall( true ); } }"); i1 = result.Text.LastIndexOf("("); i2 = result.Text.LastIndexOf(")") + ")".Length; Assert.AreEqual(@"(true)", result.GetTextAt(i1, i2 - i1)); }
public void TestForeachBracketPlacement2() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMethod () { foreach (var obj in col) {;} } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.NextLineShifted2; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { Test TestMethod () { foreach (var obj in col) { ; } } }", data.Document.Text); }
public void TestIndentNamespaceBody() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @" namespace Test { class FooBar {} }" ; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.ClassBraceStyle = BraceStyle.DoNotChange; policy.NamespaceBraceStyle = BraceStyle.EndOfLine; policy.IndentNamespaceBody = true; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"namespace Test { class FooBar {} }", data.Document.Text); policy.IndentNamespaceBody = false; compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"namespace Test { class FooBar {} }", data.Document.Text); }
public async Task SaveSolutionAfterChangingCSharpFormattingPolicyForTheFirstTime() { string dir = Util.CreateTmpDir("FormattingPolicyChangedOnce"); var formattingPolicy = new CSharpFormattingPolicy(); var solution = new Solution(); solution.Policies.Set(formattingPolicy); bool expectedSetting = !formattingPolicy.IndentSwitchCaseSection; formattingPolicy.IndentSwitchCaseSection = expectedSetting; string fileName = Path.Combine(dir, "FormattingPolicyChangedOnce.sln"); await solution.SaveAsync(fileName, Util.GetMonitor()); var file = new SlnFile(); file.Read(fileName); var s = file.Sections.GetSection("MonoDevelopProperties", SlnSectionType.PreProcess); var missingItem = default(KeyValuePair <string, string>); Assert.AreEqual(expectedSetting.ToString(), s.Properties.SingleOrDefault(p => p.Key.Contains("IndentSwitchCaseSection")).Value); Assert.AreEqual(missingItem, s.Properties.SingleOrDefault(p => p.Key.Contains("IndentSwitchSection"))); Assert.AreEqual(missingItem, s.Properties.SingleOrDefault(p => p.Key.Contains("IndentBlock"))); Assert.AreEqual(missingItem, s.Properties.SingleOrDefault(p => p.Key.Contains("SpaceBeforeDot"))); Assert.AreEqual(missingItem, s.Properties.SingleOrDefault(p => p.Key.Contains("NewLineForElse"))); solution.Dispose(); }
public void TestIndentEnumBody() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"enum Test { A }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.IndentEnumBody = true; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"enum Test { A }", data.Document.Text); policy.IndentEnumBody = false; compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"enum Test { A }", data.Document.Text); }
public void TestIndentPropertyOneLineCase2() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMe { get { ; }set{;} } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.PropertyFormatting = PropertyFormatting.AllowOneLine; policy.AllowPropertyGetBlockInline = true; policy.AllowPropertySetBlockInline = true; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { Test TestMe { get { ; } set { ; } } }", data.Document.Text); }
public void TestBug670213() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test MyMethod() // Comment { } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.MethodBraceStyle = BraceStyle.EndOfLine; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { Test MyMethod () { // Comment } }", data.Document.Text); }
public void TestWithinCheckedExpressionParanthesesSpace() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.SpacesWithinCheckedExpressionParantheses = true; var result = GetResult(policy, @"class Test { void TestMe () { a = checked(a + b); } }"); int i1 = result.Text.LastIndexOf("("); int i2 = result.Text.LastIndexOf(")") + ")".Length; Assert.AreEqual(@"( a + b )", result.GetTextAt(i1, i2 - i1)); result = GetResult(policy, @"class Test { void TestMe () { a = unchecked(a + b); } }"); result = GetResult(policy, result.Text); i1 = result.Text.LastIndexOf("("); i2 = result.Text.LastIndexOf(")") + ")".Length; Assert.AreEqual(@"( a + b )", result.GetTextAt(i1, i2 - i1)); }
public void TestAfterMethodCallParameterComma() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.SpaceBeforeMethodCallParameterComma = false; policy.SpaceAfterMethodCallParameterComma = true; var result = GetResult(policy, @"class FooBar { public void Foo () { Test(a,b,c); } }"); int i1 = result.Text.LastIndexOf("("); int i2 = result.Text.LastIndexOf(")") + ")".Length; Assert.AreEqual(@"(a, b, c)", result.GetTextAt(i1, i2 - i1)); policy.SpaceAfterMethodCallParameterComma = false; result = GetResult(policy, result.Text); i1 = result.Text.LastIndexOf("("); i2 = result.Text.LastIndexOf(")") + ")".Length; Assert.AreEqual(@"(a,b,c)", result.GetTextAt(i1, i2 - i1)); }
public void TestBeforeMethodCallParenthesesSpace() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.SpaceBeforeMethodCallParentheses = true; var result = GetResult(policy, @"class Test { void TestMe () { MethodCall(); } }"); int i1 = result.Text.IndexOf("MethodCall"); int i2 = result.Text.IndexOf(";") + ";".Length; Assert.AreEqual(@"MethodCall ();", result.GetTextAt(i1, i2 - i1)); result = GetResult(policy, @"class Test { void TestMe () { MethodCall (); } }"); policy.SpaceBeforeMethodCallParentheses = false; result = GetResult(policy, result.Text); i1 = result.Text.IndexOf("MethodCall"); i2 = result.Text.IndexOf(";") + ";".Length; Assert.AreEqual(@"MethodCall();", result.GetTextAt(i1, i2 - i1)); }
public void TestLocalVariableDeclarationComma() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.SpaceBeforeLocalVariableDeclarationComma = true; policy.SpaceAfterLocalVariableDeclarationComma = true; var result = GetResult(policy, @"class Test { void TestMe () { int a = 5,b = 6,c; } }"); int i1 = result.Text.IndexOf("int"); int i2 = result.Text.IndexOf(";") + ";".Length; Assert.AreEqual(@"int a = 5 , b = 6 , c;", result.GetTextAt(i1, i2 - i1)); result = GetResult(policy, result.Text); policy.SpaceBeforeLocalVariableDeclarationComma = false; policy.SpaceAfterLocalVariableDeclarationComma = false; result = GetResult(policy, result.Text); i1 = result.Text.IndexOf("int"); i2 = result.Text.IndexOf(";") + ";".Length; Assert.AreEqual(@"int a = 5,b = 6,c;", result.GetTextAt(i1, i2 - i1)); }
public void TestFieldDeclarationComma() { CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.SpaceBeforeFieldDeclarationComma = false; policy.SpaceAfterFieldDeclarationComma = true; var result = GetResult(policy, @"class Test { int a,b,c; }"); int i1 = result.Text.LastIndexOf("int"); int i2 = result.Text.LastIndexOf(";") + ";".Length; Assert.AreEqual(@"int a, b, c;", result.GetTextAt(i1, i2 - i1)); policy.SpaceBeforeFieldDeclarationComma = true; result = GetResult(policy, result.Text); i1 = result.Text.LastIndexOf("int"); i2 = result.Text.LastIndexOf(";") + ";".Length; Assert.AreEqual(@"int a , b , c;", result.GetTextAt(i1, i2 - i1)); policy.SpaceBeforeFieldDeclarationComma = false; policy.SpaceAfterFieldDeclarationComma = false; result = GetResult(policy, result.Text); i1 = result.Text.LastIndexOf("int"); i2 = result.Text.LastIndexOf(";") + ";".Length; Assert.AreEqual(@"int a,b,c;", result.GetTextAt(i1, i2 - i1)); }
public void TestBlankLinesAfterUsings () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"using System; using System.Text; namespace Test { }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.BlankLinesAfterUsings = 2; var compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstIndentationVisitor (policy, data), null); Assert.AreEqual (@"using System; using System.Text; namespace Test { }", data.Document.Text); policy.BlankLinesAfterUsings = 0; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstIndentationVisitor (policy, data), null); Assert.AreEqual (@"using System; using System.Text; namespace Test { }", data.Document.Text); }
public void TestBlankLinesBeforeUsings() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"using System; using System.Text; namespace Test { }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.BlankLinesAfterUsings = 0; policy.BlankLinesBeforeUsings = 2; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstIndentationVisitor(policy, data), null); Assert.AreEqual(@" using System; using System.Text; namespace Test { }", data.Document.Text); policy.BlankLinesBeforeUsings = 0; compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstIndentationVisitor(policy, data), null); Assert.AreEqual(@"using System; using System.Text; namespace Test { }", data.Document.Text); }
public void TestPropertyIndentationCase2() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { public int Prop { get; set; } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { public int Prop { get; set; } }", data.Document.Text); }
public void TestTryCatchBracketPlacement() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMethod () { try { ; } catch (Exception e) { } finally { } } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.EndOfLine; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { Test TestMethod () { try { ; } catch (Exception e) { } finally { } } }", data.Document.Text); }
public void TestDoWhileIndentation() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMethod () { do { } while (true); } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.ClassBraceStyle = BraceStyle.EndOfLine; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { Test TestMethod () { do { } while (true); } }", data.Document.Text); }
public void TestFixedForcementAdd() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMethod () { fixed (object* obj = &obj) { } fixed (object* obj = &obj) ; } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.EndOfLine; policy.FixedBraceForcement = BraceForcement.AddBraces; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { Test TestMethod () { fixed (object* obj = &obj) { } fixed (object* obj = &obj) { ; } } }", data.Document.Text); }
public void TestUsingForcementDoNotChange() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMethod () { using (var o = new MyObj()) { } using (var o = new MyObj()) ; } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.EndOfLine; policy.UsingBraceForcement = BraceForcement.DoNotChange; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { Test TestMethod () { using (var o = new MyObj()) { } using (var o = new MyObj()) ; } }", data.Document.Text); }
public void TestSimpleIfElseComment() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { void TestMethod () { if (true) Call (); else Call (); } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.EndOfLine; policy.PlaceElseIfOnNewLine = false; // for simple statements it must be new line. var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { void TestMethod () { if (true) Call (); else Call (); } }", data.Document.Text); }
public void TestPlaceWhileOnNewLine() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMethod () { do { ; } while (true); } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.PlaceWhileOnNewLine = true; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { Test TestMethod () { do { ; } while (true); } }", data.Document.Text); }
public void TestIfElseIFForcementAdd() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { void TestMethod () { if (true) Call (); else if (false) Call2 (); } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.EndOfLine; policy.IfElseBraceForcement = BraceForcement.AddBraces; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { void TestMethod () { if (true) { Call (); } else if (false) { Call2 (); } } }", data.Document.Text); }
public void TestIfForcementWithComment() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { void TestMethod () { if (true) // TestComment Call (); } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.StatementBraceStyle = BraceStyle.EndOfLine; policy.IfElseBraceForcement = BraceForcement.AddBraces; CSharpParser parser = new CSharpParser(); var compilationUnit = parser.Parse(data); TestErrors(parser); compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null); Assert.AreEqual(@"class Test { void TestMethod () { if (true) { // TestComment Call (); } } }", data.Document.Text); }
public void TestBug677261() { TextEditorData data = new TextEditorData(); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test () { } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy(); policy.ConstructorBraceStyle = BraceStyle.EndOfLine; var compilationUnit = new CSharpParser().Parse(data); compilationUnit.AcceptVisitor(new AstIndentationVisitor(policy, data), null); Assert.AreEqual(@"class Test { Test () { } }", data.Document.Text); }
public static void AddProfile (CSharpFormattingPolicy profile) { profiles.Add (profile); if (!Directory.Exists (ProfilePath)) Directory.CreateDirectory (ProfilePath); string fileName = Path.Combine (ProfilePath, profile.Name + ".xml"); profile.Save (fileName); }
// Constructors public CSharpIndentEngine (CSharpFormattingPolicy policy) { if (policy == null) throw new ArgumentNullException ("policy"); this.policy = policy; stack = new IndentStack (this); linebuf = new StringBuilder (); Reset (); }
public CSharpFormattingPolicies() { // Load global settings GlobalOptions = new CSharpFormattingPolicy( SD.PropertyService.MainPropertiesContainer, new CSharpFormattingOptionsContainer() { DefaultText = StringParser.Parse("${res:CSharpBinding.Formatting.GlobalOptionReference}") }); GlobalOptions.FormattingPolicyUpdated += OnFormattingPolicyUpdated; GlobalOptions.Load(); }
public CSharpFormattingOptionPanel(CSharpFormattingPolicy persistenceHelper, bool allowPresets, bool overrideGlobalIndentation) { if (persistenceHelper == null) throw new ArgumentNullException("persistenceHelper"); this.formattingPolicy = persistenceHelper; this.isDirty = false; InitializeComponent(); formattingEditor.AllowPresets = allowPresets; formattingEditor.OverrideGlobalIndentation = overrideGlobalIndentation; }
public CSharpFormattingPolicyPanelWidget () { // ReSharper disable once DoNotCallOverridableMethodsInConstructor this.Build (); policy = new CSharpFormattingPolicy (); buttonEdit.Clicked += HandleButtonEditClicked; texteditor.Options = DefaultSourceEditorOptions.PlainEditor; texteditor.IsReadOnly = true; texteditor.MimeType = CSharpFormatter.MimeType; scrolledwindow1.AddWithViewport (texteditor); ShowAll (); }
public void TestClassIndentation () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @" class Test {}"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.ClassBraceStyle = BraceStyle.DoNotChange; var compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test {}", data.Document.Text); }
public void TestBug325187 () { CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.PlaceElseOnNewLine = true; TestStatementFormatting (policy, @"foreach (int i in myints) if (i == 6) Console.WriteLine (""Yeah""); else Console.WriteLine (""Bad indent"");", @"foreach (int i in myints) if (i == 6) Console.WriteLine (""Yeah""); else Console.WriteLine (""Bad indent"");"); }
public CSharpFormattingPolicyPanelWidget () { this.Build (); policy = new CSharpFormattingPolicy (); buttonEdit.Clicked += HandleButtonEditClicked; var options = MonoDevelop.SourceEditor.DefaultSourceEditorOptions.Instance; texteditor.Options.FontName = options.FontName; texteditor.Options.ColorScheme = options.ColorScheme; texteditor.Options.ShowFoldMargin = false; texteditor.Options.ShowIconMargin = false; texteditor.Options.ShowLineNumberMargin = false; texteditor.Document.ReadOnly = true; texteditor.Document.MimeType = CSharpFormatter.MimeType; scrolledwindow1.Child = texteditor; ShowAll (); }
public void TestFieldSpacesBeforeComma2 () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { int a , b, c; }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.ClassBraceStyle = BraceStyle.EndOfLine; policy.BeforeFieldDeclarationComma = true; policy.AfterFieldDeclarationComma = true; var compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstSpacingVisitor (policy, data), null); Assert.AreEqual (@"class Test { int a , b , c; }", data.Document.Text); }
public void TestBug415469 () { CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); TestStatementFormatting (policy, @"switch (condition) { case CONDITION1: return foo != null ? foo.Bar : null; case CONDITION2: string goo = foo != null ? foo.Bar : null; return ""Should be indented like this""; }", @"switch (condition) { case CONDITION1: return foo != null ? foo.Bar : null; case CONDITION2: string goo = foo != null ? foo.Bar : null; return ""Should be indented like this""; }"); }
public void TestIndentBlocks () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMethod () { { {} } } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.IndentBlocks = true; var compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test TestMethod () { { {} } } }", data.Document.Text); policy.IndentBlocks = false; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test TestMethod () { { {} } } }", data.Document.Text); policy.IndentBlocks = false; }
public void TestInvocationIndentation () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMethod () { this.TestMethod (); } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.ClassBraceStyle = BraceStyle.EndOfLine; var compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstFormattingVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test TestMethod () { this.TestMethod (); } }", data.Document.Text); }
public void TestAllowEventRemoveBlockInline () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { public event EventHandler Handler { add { ; } remove { ; } } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.AllowEventAddBlockInline = false; policy.AllowEventRemoveBlockInline = true; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Console.WriteLine (data.Document.Text); Assert.AreEqual (@"class Test { public event EventHandler Handler { add { ; } remove { ; } } }", data.Document.Text); }
public void TestPropertySetBraceStyle () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test A { get; set { ; } } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.PropertySetBraceStyle = BraceStyle.NextLine; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Console.WriteLine (data.Document.Text); Assert.AreEqual (@"class Test { Test A { get; set { ; } } }", data.Document.Text); }
public void TestAllowPropertySetBlockInline () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test A { get { return null; } set { ; } } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.PropertyBraceStyle = BraceStyle.DoNotChange; policy.AllowPropertyGetBlockInline = false; policy.AllowPropertySetBlockInline = true; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Console.WriteLine (data.Document.Text); Assert.AreEqual (@"class Test { Test A { get { return null; } set { ; } } }", data.Document.Text); policy.AllowPropertySetBlockInline = false; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test A { get { return null; } set { ; } } }", data.Document.Text); }
public override void Initialize () { base.Initialize (); dom = Document.Dom; textEditorData = Document.Editor; InitTracker (); IEnumerable<string> types = MonoDevelop.Ide.DesktopService.GetMimeTypeInheritanceChain (CSharpFormatter.MimeType); if (dom != null && dom.Project != null) policy = dom.Project.Policies.Get<CSharpFormattingPolicy> (types); UpdatePath (null, null); textEditorData.Caret.PositionChanged += UpdatePath; Document.DocumentParsed += delegate { UpdatePath (null, null); }; }
public CSharpTextEditorCompletion () { IEnumerable<string> types = MonoDevelop.Ide.DesktopService.GetMimeTypeInheritanceChain (CSharpFormatter.MimeType); policy = MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<CSharpFormattingPolicy> (types); }
public void TestNoIndentationInNamespaces () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"namespace A { class Test {} }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.NamespaceBraceStyle = BraceStyle.EndOfLine; policy.ClassBraceStyle = BraceStyle.DoNotChange; policy.IndentNamespaceBody = false; var compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstIndentationVisitor (policy, data), null); Assert.AreEqual (@"namespace A { class Test {} }", data.Document.Text); }
public void TestMethodIndentation () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { MyType TestMethod () {} }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.MethodBraceStyle = BraceStyle.DoNotChange; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { MyType TestMethod () {} }", data.Document.Text); }
public void TestPropertyAlignment () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMe { get; set; } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.PropertyFormatting = PropertyFormatting.AllowOneLine; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test TestMe { get; set; } }", data.Document.Text); policy.PropertyFormatting = PropertyFormatting.ForceNewLine; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test TestMe { get; set; } }", data.Document.Text); policy.PropertyFormatting = PropertyFormatting.ForceOneLine; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test TestMe { get; set; } }", data.Document.Text); }
public void TestIndentPropertyOneLineCase2 () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test TestMe { get { ; }set{;} } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.PropertyFormatting = PropertyFormatting.AllowOneLine; policy.AllowPropertyGetBlockInline = true; policy.AllowPropertySetBlockInline = true; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test TestMe { get { ; } set { ; } } }", data.Document.Text); }
public void TestIndentMethodBodyOperatorCase () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { static Test operator+(Test left, Test right) { ; ; } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.IndentMethodBody = true; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { static Test operator+(Test left, Test right) { ; ; } }", data.Document.Text); policy.IndentMethodBody = false; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { static Test operator+(Test left, Test right) { ; ; } }", data.Document.Text); }
public void TestClassIndentationInNamespacesCase2 () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"using System; namespace MonoDevelop.CSharp.Formatting { public class FormattingProfileService { public FormattingProfileService () { } } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.NamespaceBraceStyle = BraceStyle.NextLine; policy.ClassBraceStyle = BraceStyle.NextLine; policy.ConstructorBraceStyle = BraceStyle.NextLine; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"using System; namespace MonoDevelop.CSharp.Formatting { public class FormattingProfileService { public FormattingProfileService () { } } }", data.Document.Text); }
public void TestStructBraceStyle () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"struct Test {}"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.StructBraceStyle = BraceStyle.NextLine; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"struct Test { }", data.Document.Text); }
public void TestPropertyIndentationCase2 () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { public int Prop { get; set; } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { public int Prop { get; set; } }", data.Document.Text); }
public void TestIndentPropertyBodyIndexerCase () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { Test this[int a] { get { return null; } set { ; } } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.IndentPropertyBody = true; CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test this[int a] { get { return null; } set { ; } } }", data.Document.Text); policy.IndentPropertyBody = false; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { Test this[int a] { get { return null; } set { ; } } }", data.Document.Text); }
public void TestIndentEventBody () { TextEditorData data = new TextEditorData (); data.Document.FileName = "a.cs"; data.Document.Text = @"class Test { public event EventHandler TestMe { add { ; } remove { ; } } }"; CSharpFormattingPolicy policy = new CSharpFormattingPolicy (); policy.IndentEventBody = true; var compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { public event EventHandler TestMe { add { ; } remove { ; } } }", data.Document.Text); policy.IndentEventBody = false; compilationUnit = new CSharpParser ().Parse (data); compilationUnit.AcceptVisitor (new AstIndentationVisitor (policy, data), null); Assert.AreEqual (@"class Test { public event EventHandler TestMe { add { ; } remove { ; } } }", data.Document.Text); }