Exemple #1
0
        protected ResolveResult Resolve(string code)
        {
            CompilationUnit cu = new CSharpParser().Parse(new StringReader(code.Replace("$", "")));

            TextLocation[] dollars = FindDollarSigns(code).ToArray();
            Assert.AreEqual(2, dollars.Length, "Expected 2 dollar signs marking start+end of desired node");

            SetUp();

            CSharpParsedFile         parsedFile     = new CSharpParsedFile("test.cs", resolver.CurrentUsingScope);
            TypeSystemConvertVisitor convertVisitor = new TypeSystemConvertVisitor(parsedFile, resolver.CurrentUsingScope, null);

            cu.AcceptVisitor(convertVisitor, null);
            project.UpdateProjectContent(null, convertVisitor.ParsedFile);

            FindNodeVisitor fnv = new FindNodeVisitor(dollars[0], dollars[1]);

            cu.AcceptVisitor(fnv, null);
            Assert.IsNotNull(fnv.ResultNode, "Did not find DOM node at the specified location");

            Debug.WriteLine(new string('=', 70));
            Debug.WriteLine("Starting new resolver for " + fnv.ResultNode);

            var           navigator = new NodeListResolveVisitorNavigator(new[] { fnv.ResultNode });
            ResolveResult rr;

            using (var context = this.context.Synchronize()) {
                ResolveVisitor rv = new ResolveVisitor(new CSharpResolver(context), convertVisitor.ParsedFile, navigator);
                rv.Scan(cu);
                rr = rv.GetResolveResult(fnv.ResultNode);
            }
            Assert.IsNotNull(rr, "ResolveResult is null - did something go wrong while navigating to the target node?");
            Debug.WriteLine("ResolveResult is " + rr);
            return(rr);
        }
Exemple #2
0
        void Run(Config config)
        {
            if (config.ShowHelp)
            {
                Console.WriteLine("Netjs compiler, Copyright 2014 Frank A. Krueger");
                Console.WriteLine("netjs [options] assembly-file");
                Console.WriteLine("   -help                Lists all compiler options (short: -?)");
                return;
            }

            if (string.IsNullOrEmpty(config.MainAssembly))
            {
                throw new Exception("No assembly specified.");
            }

            var asmPath = Path.GetFullPath(config.MainAssembly);

            Step("Loading " + asmPath);
            string sourceCode;

            if (asmPath.EndsWith(".cs"))
            {
                sourceCode = File.ReadAllText(asmPath);
            }
            else
            {
                sourceCode = decompileAssembly(asmPath);
                File.WriteAllText("temp.cs", sourceCode);
            }
            asmDir = Path.GetDirectoryName(asmPath);
            var outPath = Path.ChangeExtension(asmPath, ".ts");

            var syntaxTree  = new CSharpParser().Parse(sourceCode, "temp.cs");
            var compilation = createCompilation(asmPath, syntaxTree);

            /*{
             *  var resolver = new CSharpAstResolver(compilation, syntaxTree);
             *  var expr = syntaxTree.Descendants.OfType<Expression>().Where(e => (e.ToString()) == "(text + \"\\0\")").First();
             *  Console.WriteLine(resolver.Resolve(expr).Type.FullName);
             * }*/
            Step("Translating C# to TypeScript");
            CsToTs.Run(compilation, syntaxTree);

            Step("Writing to " + outPath);
            using (var outputWriter = new StreamWriter(outPath))
            {
                syntaxTree.AcceptVisitor(new ICSharpCode.NRefactory.CSharp.InsertParenthesesVisitor {
                    InsertParenthesesForReadability = true
                });
                syntaxTree.AcceptVisitor(new FromILSharp.TSOutputVisitor(outputWriter, FormattingOptionsFactory.CreateAllman()));
            }

            Step("Done");
        }
Exemple #3
0
        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;

            var compilationUnit = new CSharpParser().Parse(data);

            compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null);

            Assert.AreEqual(@"class Test
{
	Test A {
		get;
		set
		{
			;
		}
	}
}", data.Document.Text);
        }
Exemple #4
0
        protected ResolveResult Resolve(string code)
        {
            CompilationUnit cu = new CSharpParser().Parse(new StringReader(code.Replace("$", "")), "code.cs");

            TextLocation[] dollars = FindDollarSigns(code).ToArray();
            Assert.AreEqual(2, dollars.Length, "Expected 2 dollar signs marking start+end of desired node");

            SetUp();

            CSharpParsedFile parsedFile = cu.ToTypeSystem();

            project     = project.UpdateProjectContent(null, parsedFile);
            compilation = project.CreateCompilation();

            FindNodeVisitor fnv = new FindNodeVisitor(dollars[0], dollars[1]);

            cu.AcceptVisitor(fnv, null);
            Assert.IsNotNull(fnv.ResultNode, "Did not find DOM node at the specified location");

            Debug.WriteLine(new string('=', 70));
            Debug.WriteLine("Starting new resolver for " + fnv.ResultNode);

            CSharpAstResolver resolver = new CSharpAstResolver(compilation, cu, parsedFile);
            ResolveResult     rr       = resolver.Resolve(fnv.ResultNode);

            Assert.IsNotNull(rr, "ResolveResult is null - did something go wrong while navigating to the target node?");
            Debug.WriteLine("ResolveResult is " + rr);
            return(rr);
        }
        public void TestConstantVariableDeclarationIndentation()
        {
            TextEditorData data = new TextEditorData();

            data.Document.FileName = "a.cs";
            data.Document.Text     = @"class Test {
	Test TestMethod ()
	{
const int a = 5;
	}
}";

            CSharpFormattingPolicy policy = new CSharpFormattingPolicy();

            policy.ClassBraceStyle = BraceStyle.EndOfLine;
            var compilationUnit = new CSharpParser().Parse(data);

            compilationUnit.AcceptVisitor(new AstIndentationVisitor(policy, data), null);
            Console.WriteLine(data.Document.Text);
            Assert.AreEqual(@"class Test {
	Test TestMethod ()
	{
		const int a = 5;
	}
}", data.Document.Text);
        }
        public void TestUsingBracketPlacement()
        {
            TextEditorData data = new TextEditorData();

            data.Document.FileName = "a.cs";
            data.Document.Text     = @"class Test
{
	Test TestMethod ()
	{
		using (var e = new E())
		{
			;
		}
	}
}";

            CSharpFormattingPolicy policy = new CSharpFormattingPolicy();

            policy.StatementBraceStyle = BraceStyle.EndOfLine;
            var compilationUnit = new CSharpParser().Parse(data);

            compilationUnit.AcceptVisitor(new AstIndentationVisitor(policy, data), null);
            Assert.AreEqual(@"class Test
{
	Test TestMethod ()
	{
		using (var e = new E()) {
			;
		}
	}
}", data.Document.Text);
        }
Exemple #7
0
        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;

            var compilationUnit = new CSharpParser().Parse(data);

            compilationUnit.AcceptVisitor(new AstFormattingVisitor(policy, data), null);

            Assert.AreEqual(@"class Test
{
	public event EventHandler Handler {
		add {
			;
		}
		remove { ; }
	}
}", data.Document.Text);
        }
Exemple #8
0
        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 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);
        }
Exemple #10
0
        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);
        }
Exemple #11
0
        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);
        }
Exemple #12
0
        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);
        }
Exemple #13
0
        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);
        }
Exemple #14
0
        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);
        }
Exemple #15
0
        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);
        }
Exemple #16
0
        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);
        }
Exemple #17
0
        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);
        }
Exemple #18
0
        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);
        }
Exemple #19
0
        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);
        }
Exemple #20
0
        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);
        }
Exemple #22
0
        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);
        }
Exemple #23
0
        public void TestCollectNodes()
        {
            var cu = new CSharpParser().Parse("using System;\nclass Test {\n  int field; // comment\n}");
            CollectNodesVisitor v = new CollectNodesVisitor();

            cu.AcceptVisitor(v);
            Assert.AreEqual(cu.DescendantsAndSelf.ToList(), v.nodes);
        }
Exemple #24
0
        public override ValueReference Evaluate(EvaluationContext ctx, string expression, object expectedType)
        {
            expression = expression.Trim();

            if (expression.Length > 0 && expression[0] == '?')
            {
                expression = expression.Substring(1).TrimStart();
            }

            if (expression.Length > 3 && expression.StartsWith("var", StringComparison.Ordinal) && char.IsWhiteSpace(expression[3]))
            {
                expression = expression.Substring(4).TrimStart();
                string variable = null;

                for (int n = 0; n < expression.Length; n++)
                {
                    if (!char.IsLetterOrDigit(expression[n]) && expression[n] != '_')
                    {
                        variable = expression.Substring(0, n);
                        if (!expression.Substring(n).TrimStart().StartsWith("=", StringComparison.Ordinal))
                        {
                            variable = null;
                        }
                        break;
                    }

                    if (n == expression.Length - 1)
                    {
                        variable   = expression;
                        expression = null;
                        break;
                    }
                }

                if (!string.IsNullOrEmpty(variable))
                {
                    userVariables[variable] = new UserVariableReference(ctx, variable);
                }

                if (expression == null)
                {
                    return(null);
                }
            }

            expression = ReplaceExceptionTag(expression, ctx.Options.CurrentExceptionTag);

            var expr = new CSharpParser().ParseExpression(expression);

            if (expr == null)
            {
                throw new EvaluatorException("Could not parse expression '{0}'", expression);
            }

            var evaluator = new NRefactoryExpressionEvaluatorVisitor(ctx, expression, expectedType, userVariables);

            return(expr.AcceptVisitor(evaluator));
        }
        static void TestStatementFormatting(CSharpFormattingPolicy policy, string input, string expectedOutput)
        {
            TextEditorData data = new TextEditorData();

            data.Document.FileName = "a.cs";
            data.Document.Text     =
                @"class Test
{
	MyType TestMethod ()
	{
		"         + input + @"
	}
}";
            var compilationUnit = new CSharpParser().Parse(data);
            AstSpacingVisitor domSpacingVisitor = new AstSpacingVisitor(policy, data);

            domSpacingVisitor.AutoAcceptChanges = false;
            compilationUnit.AcceptVisitor(domSpacingVisitor, null);

            AstIndentationVisitor domIndentationVisitor = new AstIndentationVisitor(policy, data);

            domIndentationVisitor.AutoAcceptChanges = false;
            compilationUnit.AcceptVisitor(domIndentationVisitor, null);

            List <Change> changes = new List <Change> ();

            changes.AddRange(domSpacingVisitor.Changes);
            changes.AddRange(domIndentationVisitor.Changes);
            RefactoringService.AcceptChanges(null, null, changes);

            for (int i = 1; i <= data.Document.LineCount; i++)
            {
                LineSegment line = data.Document.GetLine(i);
                if (line.EditableLength < 2)
                {
                    continue;
                }
                data.Remove(line.Offset, 2);
            }
            string text = data.Document.GetTextBetween(data.Document.GetLine(5).Offset,
                                                       data.Document.GetLine(data.Document.LineCount - 1).Offset).Trim();

            Console.WriteLine(text);
            Assert.AreEqual(expectedOutput, text);
        }
Exemple #26
0
        /// <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);
        }
Exemple #27
0
        public void TestBlankLinesBetweenMembers()
        {
            TextEditorData data = new TextEditorData();

            data.Document.FileName = "a.cs";
            data.Document.Text     = @"class Test
{
	void AMethod ()
	{
	}
	void BMethod ()
	{
	}
	void CMethod ()
	{
	}
}";

            CSharpFormattingPolicy policy = new CSharpFormattingPolicy();

            policy.BlankLinesBetweenMembers = 1;
            var compilationUnit = new CSharpParser().Parse(data);

            compilationUnit.AcceptVisitor(new AstIndentationVisitor(policy, data), null);
            Console.WriteLine(data.Text);
            Assert.AreEqual(@"class Test
{
	void AMethod ()
	{
	}

	void BMethod ()
	{
	}

	void CMethod ()
	{
	}
}", data.Document.Text);

            policy.BlankLinesBetweenMembers = 0;
            compilationUnit = new CSharpParser().Parse(data);
            compilationUnit.AcceptVisitor(new AstIndentationVisitor(policy, data), null);
            Assert.AreEqual(@"class Test
{
	void AMethod ()
	{
	}
	void BMethod ()
	{
	}
	void CMethod ()
	{
	}
}", data.Document.Text);
        }
        protected static void Continue(CSharpFormattingOptions policy, ITextEditorAdapter adapter, string expectedOutput)
        {
            var visitior = new AstFormattingVisitor(policy, adapter);

            var compilationUnit = new CSharpParser().Parse(new StringReader(adapter.Text));

            compilationUnit.AcceptVisitor(visitior, null);
            ((TextEditorTestAdapter)adapter).ApplyChanges(visitior.Changes);
            Assert.AreEqual(expectedOutput, adapter.Text);
        }
Exemple #29
0
        public void TestBlankLinesBetweenTypes()
        {
            TextEditorData data = new TextEditorData();

            data.Document.FileName = "a.cs";
            data.Document.Text     = @"namespace Test
{
	class Test1
	{
	}
	class Test2
	{
	}
	class Test3
	{
	}
}";

            CSharpFormattingPolicy policy = new CSharpFormattingPolicy();

            policy.BlankLinesBetweenTypes = 1;
            var compilationUnit = new CSharpParser().Parse(data);

            compilationUnit.AcceptVisitor(new AstIndentationVisitor(policy, data), null);
            Assert.AreEqual(@"namespace Test
{
	class Test1
	{
	}

	class Test2
	{
	}

	class Test3
	{
	}
}", data.Document.Text);

            policy.BlankLinesBetweenTypes = 0;
            compilationUnit = new CSharpParser().Parse(data);
            compilationUnit.AcceptVisitor(new AstIndentationVisitor(policy, data), null);
            Assert.AreEqual(@"namespace Test
{
	class Test1
	{
	}
	class Test2
	{
	}
	class Test3
	{
	}
}", data.Document.Text);
        }
            void ScanPreProcessorElseIf(ref int i)
            {
                DocumentLine line      = doc.GetLineByOffset(i);
                int          length    = line.Offset + line.Length - i;
                string       parameter = doc.GetTextAt(i + 5, length - 5);
                AstNode      expr      = new CSharpParser().ParseExpression(parameter);
                bool         result;

                if (expr != null && !expr.IsNull)
                {
                    var visitResult = expr.AcceptVisitor(new ConditinalExpressionEvaluator(doc, Defines), null);
                    result = visitResult != null ? (bool)visitResult : false;
                }
                else
                {
                    result = false;
                }

                IfBlockSpan containingIf = null;

                if (result)
                {
                    bool previousResult = false;
                    foreach (Span span in spanStack)
                    {
                        if (span is IfBlockSpan)
                        {
                            containingIf   = (IfBlockSpan)span;
                            previousResult = ((IfBlockSpan)span).IsValid;
                            break;
                        }
                        if (span is ElseIfBlockSpan)
                        {
                            previousResult |= ((ElseIfBlockSpan)span).IsValid;
                        }
                    }

                    result = !previousResult;
                }

                var elseIfBlockSpan = new ElseIfBlockSpan(result);

                if (containingIf != null)
                {
                    elseIfBlockSpan.Disabled = containingIf.Disabled;
                }

                FoundSpanBegin(elseIfBlockSpan, i, 0);

                // put pre processor eol span on stack, so that '#elif' gets the correct highlight
                var preprocessorSpan = CreatePreprocessorSpan();

                FoundSpanBegin(preprocessorSpan, i, 0);
            }
		public void TestLoadAllTests ()
		{
			int found = 0;
			int missing = 0;
			int exceptions = 0;
			int i = 0;
			foreach (var file in Directory.EnumerateFiles ("/Users/mike/work/mono/mcs/tests", "*.cs")) {
				if (i++ > 2)
					break;
				if (i <= 2)
					continue;
				var text = File.ReadAllText (file, Encoding.Default);
				try {
					var unit = new CSharpParser ().Parse (text, file);
					
					var cnt = GetContent (text, unit);
					
					var visitor = new TestLocVisitor ();
					unit.AcceptVisitor (visitor);
					foreach (var loc in visitor.output) {
						var provider = CreateProvider (text, unit, cnt.Item2, cnt.Item1, loc.Item1);
						if (provider.Find (loc.Item2) != null) {
							found++;
						} else {
							missing++;
						}
					}
				} catch (Exception e) {
					Console.WriteLine ("Exception in:" + file  + "/" + e);
					exceptions++;
				}
			}
			Console.WriteLine ("Found:" + found);
			Console.WriteLine ("Missing:" + missing);
			Console.WriteLine ("Exceptions:" + exceptions);
			if (missing > 0)
				Assert.Fail ();
		}