本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.CSharpFormattingPolicy类的典型用法代码示例。如果您正苦于以下问题:C# CSharpFormattingPolicy类的具体用法?C# CSharpFormattingPolicy怎么用?C# CSharpFormattingPolicy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CSharpFormattingPolicy类属于ICSharpCode.NRefactory.CSharp命名空间,在下文中一共展示了CSharpFormattingPolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
示例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 {}
}
}");
}
示例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
{
}");
}
示例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 ()
{
}
}
}");
}
示例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 ()
{
{
{}
}
}
}");
}
示例6: TestClassIndentation
public void TestClassIndentation ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.DoNotChange;
Test (policy,
@" class Test {}",
@"class Test {}");
}
示例7: TestClassBraceStlye
public void TestClassBraceStlye ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.ClassBraceStyle = BraceStyle.EndOfLine;
Test (policy,
@"class Test {}",
@"class Test {
}");
}
示例8: TestStructBraceStyle
public void TestStructBraceStyle ()
{
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.StructBraceStyle = BraceStyle.NextLine;
Test (policy,
@"struct Test {}",
@"struct Test
{
}");
}
示例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 {}
}");
}
示例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];
}");
}
示例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);
}
示例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;
}");
}
示例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"");");
}
示例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"";
}");
}
示例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 ();
}
}");
}