本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.CSharpFormattingPolicy类的典型用法代码示例。如果您正苦于以下问题:C# CSharpFormattingPolicy类的具体用法?C# CSharpFormattingPolicy怎么用?C# CSharpFormattingPolicy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CSharpFormattingPolicy类属于ICSharpCode.NRefactory.CSharp命名空间,在下文中一共展示了CSharpFormattingPolicy类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: FormatText
public string FormatText (CSharpFormattingPolicy policy, TextStylePolicy textPolicy, string mimeType, string input, int startOffset, int endOffset)
{
var data = new TextEditorData ();
data.Document.SuppressHighlightUpdate = true;
data.Document.MimeType = mimeType;
data.Document.FileName = "toformat.cs";
if (textPolicy != null) {
data.Options.TabsToSpaces = textPolicy.TabsToSpaces;
data.Options.TabSize = textPolicy.TabWidth;
data.Options.IndentationSize = textPolicy.IndentWidth;
data.Options.IndentStyle = textPolicy.RemoveTrailingWhitespace ? IndentStyle.Virtual : IndentStyle.Smart;
}
data.Text = input;
// System.Console.WriteLine ("-----");
// System.Console.WriteLine (data.Text.Replace (" ", ".").Replace ("\t", "->"));
// System.Console.WriteLine ("-----");
var parser = new CSharpParser ();
var compilationUnit = parser.Parse (data);
bool hadErrors = parser.HasErrors;
if (hadErrors) {
// foreach (var e in parser.ErrorReportPrinter.Errors)
// Console.WriteLine (e.Message);
return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset));
}
var originalVersion = data.Document.Version;
var textEditorOptions = data.CreateNRefactoryTextEditorOptions ();
var formattingVisitor = new ICSharpCode.NRefactory.CSharp.CSharpFormatter (
policy.CreateOptions (),
textEditorOptions
) {
FormattingMode = FormattingMode.Intrusive
};
var changes = formattingVisitor.AnalyzeFormatting (data.Document, compilationUnit);
try {
changes.ApplyChanges (startOffset, endOffset - startOffset);
} catch (Exception e) {
LoggingService.LogError ("Error in code formatter", e);
return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset));
}
// check if the formatter has produced errors
parser = new CSharpParser ();
parser.Parse (data);
if (parser.HasErrors) {
LoggingService.LogError ("C# formatter produced source code errors. See console for output.");
return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset));
}
var currentVersion = data.Document.Version;
string result = data.GetTextBetween (startOffset, originalVersion.MoveOffsetTo (currentVersion, endOffset, ICSharpCode.NRefactory.Editor.AnchorMovementType.Default));
data.Dispose ();
return result;
}
开发者ID:segaman,项目名称:monodevelop,代码行数:60,代码来源:CSharpFormatter.cs
示例2: TestNamespaceBraceStyle
public void TestNamespaceBraceStyle ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.NamespaceBraceStyle = BraceStyle.EndOfLine;
policy.ClassBraceStyle = BraceStyle.DoNotChange;
var adapter = Test (policy, @"namespace A
{
namespace B {
class Test {}
}
}",
@"namespace A {
namespace B {
class Test {}
}
}");
policy.NamespaceBraceStyle = BraceStyle.NextLineShifted;
Continue (policy, adapter,
@"namespace A
{
namespace B
{
class Test {}
}
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:28,代码来源:TestBraceStlye.cs
示例3: TestBlankLinesBeforeUsings
public void TestBlankLinesBeforeUsings ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.BlankLinesAfterUsings = 0;
policy.BlankLinesBeforeUsings = 2;
var adapter = Test (policy, @"using System;
using System.Text;
namespace Test
{
}",
@"
using System;
using System.Text;
namespace Test
{
}");
policy.BlankLinesBeforeUsings = 0;
Continue (policy, adapter,
@"using System;
using System.Text;
namespace Test
{
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:27,代码来源:TestBlankLineFormatting.cs
示例4: TestClassIndentationInNamespacesCase2
public void TestClassIndentationInNamespacesCase2 ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.NamespaceBraceStyle = BraceStyle.NextLine;
policy.ClassBraceStyle = BraceStyle.NextLine;
policy.ConstructorBraceStyle = BraceStyle.NextLine;
Test (policy,
@"using System;
namespace MonoDevelop.CSharp.Formatting {
public class FormattingProfileService {
public FormattingProfileService () {
}
}
}",
@"using System;
namespace MonoDevelop.CSharp.Formatting
{
public class FormattingProfileService
{
public FormattingProfileService ()
{
}
}
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:28,代码来源:TestTypeLevelIndentation.cs
示例5: TestIndentBlocks
public void TestIndentBlocks ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentBlocks = true;
var adapter = Test (policy,
@"class Test {
Test TestMethod ()
{
{
{}
}
}
}",
@"class Test
{
Test TestMethod ()
{
{
{}
}
}
}");
policy.IndentBlocks = false;
Continue (policy, adapter, @"class Test
{
Test TestMethod ()
{
{
{}
}
}
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:34,代码来源:TestStatementIndentation.cs
示例6: TestClassIndentation
public void TestClassIndentation ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.DoNotChange;
Test (policy,
@" class Test {}",
@"class Test {}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:9,代码来源:TestTypeLevelIndentation.cs
示例7: TestClassBraceStlye
public void TestClassBraceStlye ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.EndOfLine;
Test (policy,
@"class Test {}",
@"class Test {
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:10,代码来源:TestBraceStlye.cs
示例8: TestStructBraceStyle
public void TestStructBraceStyle ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.StructBraceStyle = BraceStyle.NextLine;
Test (policy,
@"struct Test {}",
@"struct Test
{
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:11,代码来源:TestBraceStlye.cs
示例9: TestClassIndentationInNamespaces
public void TestClassIndentationInNamespaces ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.NamespaceBraceStyle = BraceStyle.EndOfLine;
policy.ClassBraceStyle = BraceStyle.DoNotChange;
Test (policy,
@"namespace A { class Test {} }",
@"namespace A {
class Test {}
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:12,代码来源:TestTypeLevelIndentation.cs
示例10: TestFixedFieldSpacesBeforeComma
public void TestFixedFieldSpacesBeforeComma ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.EndOfLine;
policy.SpaceAfterFieldDeclarationComma = true;
policy.SpaceBeforeFieldDeclarationComma = true;
Test (policy, @"class Test {
fixed int a[10] , b[10], c[10];
}",
@"class Test {
fixed int a[10] , b[10] , c[10];
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:14,代码来源:TestSpacingVisitor.cs
示例11: GenerateCode
public void GenerateCode(ITextOutput output, Predicate<IAstTransform> transformAbortCondition)
{
TransformationPipeline.RunTransformationsUntil(astCompileUnit, transformAbortCondition, context);
astCompileUnit.AcceptVisitor(new InsertParenthesesVisitor { InsertParenthesesForReadability = true }, null);
var outputFormatter = new TextOutputFormatter(output);
var formattingPolicy = new CSharpFormattingPolicy();
// disable whitespace in front of parentheses:
formattingPolicy.BeforeMethodCallParentheses = false;
formattingPolicy.BeforeMethodDeclarationParentheses = false;
formattingPolicy.BeforeConstructorDeclarationParentheses = false;
formattingPolicy.BeforeDelegateDeclarationParentheses = false;
astCompileUnit.AcceptVisitor(new OutputVisitor(outputFormatter, formattingPolicy), null);
}
开发者ID:eldersantos,项目名称:ILSpy,代码行数:14,代码来源:AstBuilder.cs
示例12: TestFieldSpacesBeforeComma1
public void TestFieldSpacesBeforeComma1 ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.EndOfLine;
policy.SpaceBeforeFieldDeclarationComma = false;
policy.SpaceAfterFieldDeclarationComma = false;
Test (policy, @"class Test {
int a , b, c;
}",
@"class Test {
int a,b,c;
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:14,代码来源:TestSpacingVisitor.cs
示例13: TestBug325187
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"");");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:17,代码来源:TestFormattingBugs.cs
示例14: TestBug415469
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"";
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:19,代码来源:TestFormattingBugs.cs
示例15: TestInvocationIndentation
public void TestInvocationIndentation ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.EndOfLine;
Test (policy,
@"class Test {
Test TestMethod ()
{
this.TestMethod ();
}
}",
@"class Test {
Test TestMethod ()
{
this.TestMethod ();
}
}");
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:19,代码来源:TestStatementIndentation.cs
示例16: SetUp
public void SetUp()
{
policy = new CSharpFormattingPolicy();
}
开发者ID:richardschneider,项目名称:ILSpy,代码行数:4,代码来源:InsertParenthesesVisitorTests.cs
示例17: TestSpacesInLambdaExpression
public void TestSpacesInLambdaExpression ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.SpacesWithinWhileParentheses = true;
var result = GetResult (policy, @"class Test {
void TestMe ()
{
var v = x=>x!=null;
}
}");
int i1 = result.Text.IndexOf ("x");
int i2 = result.Text.LastIndexOf ("null") + "null".Length;
Assert.AreEqual (@"x => x != null", result.GetTextAt (i1, i2 - i1));
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:15,代码来源:TestSpacingVisitor.cs
示例18: TestAfterMethodDeclarationParameterComma
public void TestAfterMethodDeclarationParameterComma ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.SpaceBeforeMethodDeclarationParameterComma = false;
policy.SpaceAfterMethodDeclarationParameterComma = true;
var result = GetResult (policy, @"class Test {
public void Foo (int a,int b,int c) {}
}");
int i1 = result.Text.LastIndexOf ("(");
int i2 = result.Text.LastIndexOf (")") + ")".Length;
Assert.AreEqual (@"(int a, int b, int c)", result.GetTextAt (i1, i2 - i1));
policy.SpaceAfterMethodDeclarationParameterComma = false;
result = GetResult (policy, result.Text);
i1 = result.Text.LastIndexOf ("(");
i2 = result.Text.LastIndexOf (")") + ")".Length;
Assert.AreEqual (@"(int a,int b,int c)", result.GetTextAt (i1, i2 - i1));
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:19,代码来源:TestSpacingVisitor.cs
示例19: TestFieldDeclarationComma
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));
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:26,代码来源:TestSpacingVisitor.cs
示例20: TestAfterNewParameterComma
public void TestAfterNewParameterComma ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.SpaceAfterNewParameterComma = true;
var result = GetResult (policy, @"class Test {
void TestMe ()
{
new Test (1,2);
}
}");
int i1 = result.Text.LastIndexOf ("new");
int i2 = result.Text.LastIndexOf (";") + ";".Length;
Assert.AreEqual (@"new Test (1, 2);", result.GetTextAt (i1, i2 - i1));
}
开发者ID:tanujmathur,项目名称:ILSpy,代码行数:15,代码来源:TestSpacingVisitor.cs
注:本文中的ICSharpCode.NRefactory.CSharp.CSharpFormattingPolicy类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论