protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
		{
			expectedOutput = NormalizeNewlines(expectedOutput);

			IDocument doc = GetResult(policy, input, mode, options);
			if (expectedOutput != doc.Text) {
				Console.WriteLine ("expected:");
				Console.WriteLine (expectedOutput);
				Console.WriteLine ("got:");
				Console.WriteLine (doc.Text);
				for (int i = 0; i < expectedOutput.Length && i < doc.TextLength; i++) {
					if (expectedOutput [i] != doc.GetCharAt(i)) {
						Console.WriteLine (
							"i:"+i+" differ:"+ 
							expectedOutput[i].ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t") +
							" !=" + 
							doc.GetCharAt(i).ToString ().Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t")
						);
						Console.WriteLine(">"+expectedOutput.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t"));
						Console.WriteLine(">"+doc.Text.Substring (i).Replace ("\n", "\\n").Replace ("\r", "\\r").Replace ("\t", "\\t"));
						break;
					}
				}
			}
			Assert.AreEqual (expectedOutput, doc.Text);
			return doc;
		}
		public static CacheIndentEngine CreateEngine(string text, CSharpFormattingOptions formatOptions = null, TextEditorOptions options = null)
		{
			if (formatOptions == null) {
				formatOptions = FormattingOptionsFactory.CreateMono();
				formatOptions.AlignToFirstIndexerArgument = formatOptions.AlignToFirstMethodCallArgument = true;
			}
			
			var sb = new StringBuilder();
			int offset = 0;
			for (int i = 0; i < text.Length; i++) {
				var ch = text [i];
				if (ch == '$') {
					offset = i;
					continue;
				}
				sb.Append(ch);
			}
			
			var document = new ReadOnlyDocument(sb.ToString());
			options = options ?? new TextEditorOptions { EolMarker = "\n" };
			
			var result = new CacheIndentEngine(new CSharpIndentEngine(document, options, formatOptions));
			result.Update(offset);
			return result;
		}
Example #3
0
		public void TestNamespaceBraceStyle ()
		{
			CSharpFormattingOptions policy = new CSharpFormattingOptions ();
			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 {}
		}
	}");
		}
		public MDRefactoringScript (MDRefactoringContext context, CSharpFormattingOptions formattingOptions) : base(context.TextEditor.Document, formattingOptions, context.TextEditor.CreateNRefactoryTextEditorOptions ())
		{
			this.context = context;
			undoGroup  = this.context.TextEditor.OpenUndoGroup ();
			this.startVersion = this.context.TextEditor.Version;

		}
		public void TestClassIndentationInNamespacesCase2 ()
		{
			CSharpFormattingOptions policy = new CSharpFormattingOptions ();
			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 ()
		{
		}
	}
}");
		}
Example #6
0
		public static void RandomTests(string filePath, int count, CSharpFormattingOptions policy = null, TextEditorOptions options = null)
		{
			if (File.Exists(filePath))
			{
				var code = File.ReadAllText(filePath);
				var document = new ReadOnlyDocument(code);
				policy = policy ?? FormattingOptionsFactory.CreateMono();
				options = options ?? new TextEditorOptions { IndentBlankLines = false };

				var engine = new CacheIndentEngine(new CSharpIndentEngine(document, options, policy) { EnableCustomIndentLevels = true });
				Random rnd = new Random();

				for (int i = 0; i < count; i++) {
					int offset = rnd.Next(document.TextLength);
					engine.Update(offset);
					if (engine.CurrentIndent.Length == 0)
						continue;
				}

			}
			else
			{
				Assert.Fail("File " + filePath + " doesn't exist.");
			}
		}
        public void TestBlankLinesBeforeFirstDeclaration()
        {
            CSharpFormattingOptions policy = new CSharpFormattingOptions ();
            policy.BlankLinesBeforeFirstDeclaration = 2;

            var adapter = Test (policy, @"namespace Test
            {
            class Test
            {
            }
            }",
            @"namespace Test
            {

            class Test
            {
            }
            }");

            policy.BlankLinesBeforeFirstDeclaration = 0;
            Continue (policy, adapter,
            @"namespace Test
            {
            class Test
            {
            }
            }");
        }
		public void TestBlankLinesBeforeUsings ()
		{
			CSharpFormattingOptions policy = new CSharpFormattingOptions ();
			policy.BlankLinesAfterUsings = 0;
			policy.BlankLinesBeforeUsings = 2;
			
			var adapter = Test (policy, @"using System;
using System.Text;
namespace Test
{
}",
@"

using System;
using System.Text;
namespace Test
{
}", FormattingMode.Intrusive);
			
			policy.BlankLinesBeforeUsings = 0;
			Continue (policy, adapter, 
@"using System;
using System.Text;
namespace Test
{
}", FormattingMode.Intrusive);
		}
Example #9
0
		/// <summary>
		/// Formats the specified part of the document.
		/// </summary>
		public static void Format(ITextEditor editor, int offset, int length, CSharpFormattingOptions options)
		{
			var formatter = new CSharpFormatter(options, editor.ToEditorOptions());
			formatter.AddFormattingRegion(new DomRegion(editor.Document.GetLocation(offset), editor.Document.GetLocation(offset + length)));
			var changes = formatter.AnalyzeFormatting(editor.Document, SyntaxTree.Parse(editor.Document));
			changes.ApplyChanges(offset, length);
		}
Example #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ICSharpCode.NRefactory.CSharp.CSharpFormatter"/> class.
 /// </summary>
 /// <param name="policy">The formatting policy to use.</param>
 /// <param name="document">The text document to work upon.</param>
 /// <param name="options">The text editor options (optional). Default is: TextEditorOptions.Default</param>
 public CSharpFormatter(CSharpFormattingOptions policy, TextEditorOptions options = null)
 {
     if (policy == null)
         throw new ArgumentNullException("policy");
     this.policy = policy;
     this.options = options ?? TextEditorOptions.Default;
 }
		public void TestIndentBlocks ()
		{
			CSharpFormattingOptions policy = new CSharpFormattingOptions ();
			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 ()
	{
		{
		{}
		}
	}
}");
		}
Example #12
0
		public EditorScript(ITextEditor editor, SDRefactoringContext context, CSharpFormattingOptions formattingOptions)
			: base(editor.Document, formattingOptions, context.TextEditorOptions)
		{
			this.editor = editor;
			this.context = context;
			this.textSegmentCollection = new TextSegmentCollection<TextSegment>((TextDocument)editor.Document);
		}
		/// <summary>
		///     Creates a new TextPasteIndentEngine instance.
		/// </summary>
		/// <param name="decoratedEngine">
		///     An instance of <see cref="IStateMachineIndentEngine"/> to which the
		///     logic for indentation will be delegated.
		/// </param>
		/// <param name="textEditorOptions">
		///    Text editor options for indentation.
		/// </param>
		/// <param name="formattingOptions">
		///     C# formatting options.
		/// </param>
		public TextPasteIndentEngine(IStateMachineIndentEngine decoratedEngine, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions)
		{
			this.engine = decoratedEngine;
			this.textEditorOptions = textEditorOptions;
			this.formattingOptions = formattingOptions;

			this.engine.EnableCustomIndentLevels = false;
		}
 public static IDictionary<AstNode, ISegment> WriteNode(StringWriter writer, AstNode node, CSharpFormattingOptions policy, ICSharpCode.AvalonEdit.TextEditorOptions options)
 {
     var formatter = new SegmentTrackingOutputFormatter(writer);
     formatter.IndentationString = options.IndentationString;
     var visitor = new CSharpOutputVisitor(formatter, policy);
     node.AcceptVisitor(visitor);
     return formatter.Segments;
 }
 public CSharpIndentEngine(IDocument document, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions)
 {
     this.document = document;
     this.options = formattingOptions;
     this.textEditorOptions = textEditorOptions;
     this.indent = new Indent(textEditorOptions);
     this.thisLineindent = new Indent(textEditorOptions);
 }
 /// <summary>
 /// Formats the specified part of the document.
 /// </summary>
 public static void Format(IDocument document, int offset, int length, CSharpFormattingOptions options)
 {
     var syntaxTree = new CSharpParser().Parse(document);
     var fv = new AstFormattingVisitor(options, document);
     fv.FormattingRegion = new DomRegion(document.GetLocation(offset), document.GetLocation(offset + length));
     syntaxTree.AcceptVisitor(fv);
     fv.ApplyChanges(offset, length);
 }
		protected static IDocument GetResult(CSharpFormattingOptions policy, string input, FormattingMode mode = FormattingMode.Intrusive, TextEditorOptions options = null)
		{
			StringBuilderDocument document;
			var changes = GetChanges(policy, input, out document, mode, options);
			
			changes.ApplyChanges();
			return document;
		}
		void AssertOutput(string expected, Expression expr, CSharpFormattingOptions policy = null)
		{
			if (policy == null)
				policy = new CSharpFormattingOptions();;
			StringWriter w = new StringWriter();
			w.NewLine = "\n";
			expr.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w) { IndentationString = "\t" }, policy), null);
			Assert.AreEqual(expected.Replace("\r", ""), w.ToString());
		}
		public void TestClassIndentation ()
		{
			CSharpFormattingOptions policy = new CSharpFormattingOptions ();
			policy.ClassBraceStyle = BraceStyle.DoNotChange;
			
			Test (policy,
@"			class Test {}",
@"class Test {}");
		}
Example #20
0
 public PatchTask(ITaskInterface taskInterface, string baseDir, string srcDir, string patchDir,
         CSharpFormattingOptions format = null)
     : base(taskInterface)
 {
     this.baseDir = baseDir;
     this.srcDir = srcDir;
     this.patchDir = patchDir;
     this.format = format;
 }
Example #21
0
		void AssertOutput(string expected, AstNode node, CSharpFormattingOptions policy = null)
		{
			if (policy == null)
				policy = FormattingOptionsFactory.CreateMono();
			StringWriter w = new StringWriter();
			w.NewLine = "\n";
			node.AcceptVisitor(new CSharpOutputVisitor(new TextWriterOutputFormatter(w) { IndentationString = "$" }, policy));
			Assert.AreEqual(expected.Replace("\r", ""), w.ToString());
		}
 protected static IDocument Test(CSharpFormattingOptions policy, string input, string expectedOutput)
 {
     IDocument doc = GetResult(policy, input);
     if (expectedOutput != doc.Text) {
         Console.WriteLine (doc.Text);
     }
     Assert.AreEqual (expectedOutput, doc.Text);
     return doc;
 }
Example #23
0
	    public PatchTask(ITaskInterface taskInterface, string baseDir, string srcDir, string patchDir,
            ProgramSetting<DateTime> cutoff, CSharpFormattingOptions format = null) : base(taskInterface)
		{
			this.baseDir = baseDir;
			this.srcDir = srcDir;
			this.patchDir = patchDir;
			this.format = format;
            this.cutoff = cutoff;
		}
		private CSharpFormattingOptionsContainer(CSharpFormattingOptionsContainer parent, HashSet<string> activeOptions)
		{
			this.parent = parent;
			if (parent != null) {
				parent.PropertyChanged += HandlePropertyChanged;
			}
			this.activeOptions = activeOptions;
			Reset();
			cachedOptions = CreateCachedOptions();
		}
Example #25
0
		public void TestClassBraceStlye ()
		{
			CSharpFormattingOptions policy = new CSharpFormattingOptions ();
			policy.ClassBraceStyle = BraceStyle.EndOfLine;
			
			Test (policy,
@"class Test {}",
@"class Test {
}");
		}
Example #26
0
		public DiffTask(ITaskInterface taskInterface, string baseDir, string srcDir, string patchDir, int stepNumber,
			CSharpFormattingOptions format = null)
			: base(taskInterface)
		{
			this.baseDir = baseDir;
			this.srcDir = srcDir;
			this.patchDir = patchDir;
			this.format = format;
			this.stepNumber = stepNumber;
		}
Example #27
0
        //public static CSharpFormattingOptions tModLoaderFormat = FormattingOptionsFactory.CreateAllman();

        public static string FormatCode(string text, CSharpFormattingOptions options, CancellationToken ct) {
            var formatter = new CSharpFormatter(options) { FormattingMode = FormattingMode.Intrusive };
            text = text.Replace("\r\n\r\n", "\r\n");

            var doc = new StringBuilderDocument(text);
            var syntaxTree = SyntaxTree.Parse(doc, doc.FileName, null, ct);
            formatter.AnalyzeFormatting(doc, syntaxTree, ct).ApplyChanges();

            return doc.Text;
        }
Example #28
0
		protected static IDocument Test (CSharpFormattingOptions policy, string input, string expectedOutput, FormattingMode mode = FormattingMode.Intrusive)
		{
			expectedOutput = NormalizeNewlines(expectedOutput);
			IDocument doc = GetResult(policy, input, mode);
			if (expectedOutput != doc.Text) {
				Console.WriteLine (doc.Text);
			}
			Assert.AreEqual (expectedOutput, doc.Text);
			return doc;
		}
        protected static IDocument GetResult(CSharpFormattingOptions policy, string input)
        {
            var adapter = new ReadOnlyDocument (input);
            var visitor = new AstFormattingVisitor (policy, adapter, factory);

            var compilationUnit = new CSharpParser ().Parse (new StringReader (input), "test.cs");
            compilationUnit.AcceptVisitor (visitor, null);

            return new ReadOnlyDocument (ApplyChanges (input, visitor.Changes));
        }
Example #30
0
		public CSharpOutputVisitor (IOutputFormatter formatter, CSharpFormattingOptions formattingPolicy)
		{
			if (formatter == null) {
				throw new ArgumentNullException ("formatter");
			}
			if (formattingPolicy == null) {
				throw new ArgumentNullException ("formattingPolicy");
			}
			this.formatter = formatter;
			this.policy = formattingPolicy;
		}
Example #31
0
 public override string ToString(CSharpFormattingOptions formattingOptions)
 {
     return(TokenRole.Tokens [(int)(this.flags >> AstNodeFlagsUsedBits)]);
 }
Example #32
0
 public override string ToString(CSharpFormattingOptions formattingOptions)
 {
     return("[" + new string(',', this.Dimensions - 1) + "]");
 }
        /// <summary>
        ///     Creates a new TextPasteIndentEngine instance.
        /// </summary>
        /// <param name="decoratedEngine">
        ///     An instance of <see cref="IStateMachineIndentEngine"/> to which the
        ///     logic for indentation will be delegated.
        /// </param>
        /// <param name="textEditorOptions">
        ///    Text editor options for indentation.
        /// </param>
        /// <param name="formattingOptions">
        ///     C# formatting options.
        /// </param>
        public TextPasteIndentEngine(IStateMachineIndentEngine decoratedEngine, TextEditorOptions textEditorOptions, CSharpFormattingOptions formattingOptions)
        {
            this.engine            = decoratedEngine;
            this.textEditorOptions = textEditorOptions;
            this.formattingOptions = formattingOptions;

            this.engine.EnableCustomIndentLevels = false;
        }
Example #34
0
 public override string ToString(CSharpFormattingOptions formattingOptions)
 {
     return(GetModifierName(Modifier));
 }
Example #35
0
 public ConstructFixer(CSharpFormattingOptions options, TextEditorOptions textEditorOptions)
 {
     this.options           = options;
     this.textEditorOptions = textEditorOptions;
 }
Example #36
0
 public string GetText(CSharpFormattingOptions formattingOptions = null)
 {
     return(ToString(formattingOptions));
 }
 public AstAmbience(ICSharpCode.NRefactory.CSharp.CSharpFormattingOptions options)
 {
     this.options = options;
 }