static Document Setup (string input)
		{
			var tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();

			var project = new DotNetAssemblyProject ("C#");
			project.Name = "test";
			project.References.Add (new ProjectReference (ReferenceType.Package, "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
			project.References.Add (new ProjectReference (ReferenceType.Package, "System.Core"));

			project.FileName = "test.csproj";

			TypeSystemService.LoadProject (project);
			TypeSystemService.GetProjectContentWrapper (project).ReconnectAssemblyReferences (); 
			content.Project = project;

			tww.ViewContent = content;
			content.ContentName = "a.cs";
			content.GetTextEditorData ().Document.MimeType = "text/x-csharp";
			var doc = new Document (tww);

			var text = input;
			int endPos = text.IndexOf ('$');
			if (endPos >= 0)
				text = text.Substring (0, endPos) + text.Substring (endPos + 1);

			content.Text = text;
			content.CursorPosition = Math.Max (0, endPos);

			var compExt = new CSharpCompletionTextEditorExtension ();
			compExt.Initialize (doc);
			content.Contents.Add (compExt);
			doc.UpdateParseDocument ();
			return doc;
		}
		static Document Setup (string input)
		{
			TestWorkbenchWindow tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			tww.ViewContent = content;
			content.ContentName = "a.cs";
			content.GetTextEditorData ().Document.MimeType = "text/x-csharp";

			Document doc = new Document (tww);

			var text = input;
			int endPos = text.IndexOf ('$');
			if (endPos >= 0)
				text = text.Substring (0, endPos) + text.Substring (endPos + 1);

			content.Text = text;
			content.CursorPosition = System.Math.Max (0, endPos);

			var compExt = new CSharpCompletionTextEditorExtension ();
			compExt.Initialize (doc);
			content.Contents.Add (compExt);

			doc.UpdateParseDocument ();
			return doc;
		}
		static CSharpTextEditorIndentation Setup (string input, out TestViewContent content)
		{
			TestWorkbenchWindow tww = new TestWorkbenchWindow ();
			content = new TestViewContent ();
			content.Data.Options.IndentStyle = IndentStyle.Auto;
			tww.ViewContent = content;
			content.ContentName = "a.cs";
			content.GetTextEditorData ().Document.MimeType = "text/x-csharp";

			Document doc = new Document (tww);

			var text = input;
			int endPos = text.IndexOf ('$');
			if (endPos >= 0)
				text = text.Substring (0, endPos) + text.Substring (endPos + 1);

			content.Text = text;
			content.CursorPosition = System.Math.Max (0, endPos);


			var compExt = new CSharpCompletionTextEditorExtension ();
			compExt.Initialize (doc);
			content.Contents.Add (compExt);
			
			var ext = new CSharpTextEditorIndentation ();
			CSharpTextEditorIndentation.OnTheFlyFormatting = true;
			ext.Initialize (doc);
			content.Contents.Add (ext);
			
			doc.UpdateParseDocument ();
			return ext;
		}
        static UnitTestTextEditorExtension Setup(string input, out TestViewContent content)
        {
            TestWorkbenchWindow tww = new TestWorkbenchWindow();

            content             = new TestViewContent();
            tww.ViewContent     = content;
            content.ContentName = "a.cs";
            content.GetTextEditorData().Document.MimeType = "text/x-csharp";

            Document doc = new Document(tww);

            var text   = @"namespace NUnit.Framework {
	using System;
	class TestFixtureAttribute : Attribute {} 
	class TestAttribute : Attribute {} 
} namespace Test { " + input + "}";
            int endPos = text.IndexOf('$');

            if (endPos >= 0)
            {
                text = text.Substring(0, endPos) + text.Substring(endPos + 1);
            }

            content.Text           = text;
            content.CursorPosition = System.Math.Max(0, endPos);


            var compExt = new UnitTestTextEditorExtension();

            compExt.Initialize(doc);
            content.Contents.Add(compExt);

            doc.UpdateParseDocument();
            return(compExt);
        }
Beispiel #5
0
        void RunTest(string test, LocalVariable localVariable)
        {
            StringBuilder      testText = new StringBuilder();
            List <DomLocation> expectedReferences = new List <DomLocation> ();
            DomLocation        memberLocation = DomLocation.Empty;
            int line = 1, col = 1;

            foreach (char ch in test)
            {
                switch (ch)
                {
                case '$':
                    memberLocation = new DomLocation(line, col);
                    break;

                case '@':
                    expectedReferences.Add(new DomLocation(line, col));
                    break;

                default:
                    col++;
                    if (ch == '\n')
                    {
                        col = 1;
                        line++;
                    }
                    testText.Append(ch);
                    break;
                }
            }
            DotNetProject project = new DotNetAssemblyProject("C#");

            project.FileName = "/tmp/a.csproj";

            SimpleProjectDom dom  = new SimpleProjectDom();

            dom.Project = project;
            ProjectDomService.RegisterDom(dom, "Project:" + project.FileName);

            ParsedDocument parsedDocument = parser.Parse(null, "a.cs", testText.ToString());

            dom.Add(parsedDocument.CompilationUnit);

            TestViewContent testViewContent = new TestViewContent();

            testViewContent.Name = "a.cs";
            testViewContent.Text = testText.ToString();
            //	RefactorerContext ctx = new RefactorerContext (dom, new DumbTextFileProvider(testViewContent), null);
            NRefactoryResolver resolver = new NRefactoryResolver(dom,
                                                                 parsedDocument.CompilationUnit,
                                                                 testViewContent.Data,
                                                                 "a.cs");
            SearchMemberVisitor smv = new SearchMemberVisitor(memberLocation.Line);

            if (localVariable != null)
            {
                ((LocalVariable)localVariable).DeclaringMember = parsedDocument.CompilationUnit.GetMemberAt(expectedReferences[0]);
                smv.FoundMember = localVariable;
            }
            else
            {
                smv.Visit(parsedDocument.CompilationUnit, null);
                if (smv.FoundMember == null)
                {
                    ResolveResult resolveResult = resolver.ResolveIdentifier("a", memberLocation);
                    if (resolveResult is LocalVariableResolveResult)
                    {
                        smv.FoundMember = ((LocalVariableResolveResult)resolveResult).LocalVariable;
                    }
                }
            }

            Assert.IsNotNull(smv.FoundMember, "Member to search not found.");
            if (smv.FoundMember is IType)
            {
                smv.FoundMember = dom.GetType(((IType)smv.FoundMember).FullName,
                                              ((IType)smv.FoundMember).TypeParameters.Count,
                                              true);
            }
            FindMemberAstVisitor astVisitor = new FindMemberAstVisitor(testViewContent.GetTextEditorData().Document, resolver, smv.FoundMember);

            astVisitor.RunVisitor();

            int           i = 0, j = 0;
            StringBuilder errorText = new StringBuilder();
            Document      doc       = new Document();

            doc.Text = testViewContent.Text;
            while (i < expectedReferences.Count && j < astVisitor.FoundReferences.Count)
            {
                if (expectedReferences[i].Line != astVisitor.FoundReferences[j].Line || expectedReferences[i].Column != astVisitor.FoundReferences[j].Column)
                {
                    if (expectedReferences[i].Line < astVisitor.FoundReferences[j].Line)
                    {
                        errorText.Append("Reference at  line " + expectedReferences[i].Line + " not found.");
                        errorText.AppendLine();
                        errorText.Append(doc.GetTextAt(doc.GetLine(expectedReferences[i].Line)).Replace('\t', ' '));
                        errorText.AppendLine();
                        errorText.Append(new string (' ', expectedReferences[i].Column)); errorText.Append('^');
                        errorText.AppendLine();
                        i++;
                        continue;
                    }
                    if (expectedReferences[i].Line > astVisitor.FoundReferences[j].Line)
                    {
                        errorText.Append("Found unexpected Reference at line " + astVisitor.FoundReferences[j].Line);
                        errorText.AppendLine();
                        errorText.Append(doc.GetTextAt(doc.GetLine(astVisitor.FoundReferences[j].Line)).Replace('\t', ' '));
                        errorText.AppendLine();
                        errorText.Append(new string (' ', astVisitor.FoundReferences[j].Column)); errorText.Append('^');
                        errorText.AppendLine();
                        j++;
                        continue;
                    }

                    errorText.Append("Column mismatch at line " + astVisitor.FoundReferences[j].Line + " was: " + astVisitor.FoundReferences[j].Column + " should be:" + expectedReferences[i].Column);
                    errorText.AppendLine();
                    errorText.Append(doc.GetTextAt(doc.GetLine(astVisitor.FoundReferences[j].Line)).Replace('\t', ' '));
                    errorText.Append(new string (' ', expectedReferences[i].Column)); errorText.Append('^');
                    errorText.AppendLine();
                    errorText.Append(new string (' ', astVisitor.FoundReferences[j].Column)); errorText.Append('^');
                    errorText.AppendLine();
                }
                i++; j++;
            }
            while (i < expectedReferences.Count)
            {
                errorText.Append("Reference at  line " + expectedReferences[i].Line + " not found.");
                errorText.AppendLine();
                errorText.Append(doc.GetTextAt(doc.GetLine(expectedReferences[i].Line)).Replace('\t', ' '));
                errorText.AppendLine();
                errorText.Append(new string (' ', expectedReferences[j].Column)); errorText.Append('^');
                errorText.AppendLine();
                i++;
            }
            while (j < astVisitor.FoundReferences.Count)
            {
                errorText.Append("Found unexpected Reference at line " + astVisitor.FoundReferences[j].Line);
                errorText.AppendLine();
                errorText.Append(doc.GetTextAt(doc.GetLine(astVisitor.FoundReferences[j].Line)).Replace('\t', ' '));
                errorText.AppendLine();
                errorText.Append(new string (' ', astVisitor.FoundReferences[i].Column)); errorText.Append('^');
                errorText.AppendLine();
                j++;
            }
            if (errorText.Length > 0)
            {
                Assert.Fail("Member to find:" + smv.FoundMember + Environment.NewLine + errorText.ToString() + Environment.NewLine + "found : " + astVisitor.FoundReferences.Count + " expected:" + expectedReferences.Count);
            }
        }
		static async Task Simulate (string input, Action<TestViewContent, CSharpTextEditorIndentation> act)
		{
			TestWorkbenchWindow tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			content.Data.Options = new CustomEditorOptions {
				IndentStyle = IndentStyle.Auto
			};

			tww.ViewContent = content;
			content.ContentName = "/a.cs";
			content.Data.MimeType = "text/x-csharp";

			var doc = new Document (tww);

			var sb = new StringBuilder ();
			int cursorPosition = 0, selectionStart = -1, selectionEnd = -1;

			for (int i = 0; i < input.Length; i++) {
				var ch = input [i];
				switch (ch) {
				case '$':
					cursorPosition = sb.Length;
					break;
				case '<':
					if (i + 1 < input.Length) {
						if (input [i + 1] == '-') {
							selectionStart = sb.Length;
							i++;
							break;
						}
					}
					goto default;
				case '-':
					if (i + 1 < input.Length) {
						var next = input [i + 1];
						if (next == '>') {
							selectionEnd = sb.Length;
							i++;
							break;
						}
					}
					goto default;
				default:
					sb.Append (ch);
					break;
				}
			}
			content.Text = sb.ToString ();
			content.CursorPosition = cursorPosition;

			var project = Services.ProjectService.CreateProject ("C#");
			project.Name = "test";
			project.FileName = "test.csproj";
			project.Files.Add (new ProjectFile (content.ContentName, BuildAction.Compile)); 
			project.Policies.Set (Projects.Policies.PolicyService.InvariantPolicies.Get<CSharpFormattingPolicy> (), CSharpFormatter.MimeType);

			var solution = new MonoDevelop.Projects.Solution ();
			solution.AddConfiguration ("", true); 
			solution.DefaultSolutionFolder.AddItem (project);
			using (var monitor = new ProgressMonitor ())
				await TypeSystemService.Load (solution, monitor);
			content.Project = project;
			doc.SetProject (project);

			var compExt = new CSharpCompletionTextEditorExtension ();
			compExt.Initialize (doc.Editor, doc);
			content.Contents.Add (compExt);
			
			var ext = new CSharpTextEditorIndentation ();
			CSharpTextEditorIndentation.OnTheFlyFormatting = true;
			ext.Initialize (doc.Editor, doc);
			content.Contents.Add (ext);
			
			await doc.UpdateParseDocument ();
			if (selectionStart >= 0 && selectionEnd >= 0)
				content.GetTextEditorData ().SetSelection (selectionStart, selectionEnd);
			try {
				act (content, ext);
			} finally {
				TypeSystemService.Unload (solution);
			}
		}
        static CSharpTextEditorIndentation Setup(string input, out TestViewContent content)
        {
            TestWorkbenchWindow tww = new TestWorkbenchWindow ();
            content = new TestViewContent ();
            content.Data.Options.IndentStyle = IndentStyle.Auto;
            tww.ViewContent = content;
            content.ContentName = "a.cs";
            content.GetTextEditorData ().Document.MimeType = "text/x-csharp";

            Document doc = new Document (tww);

            var sb = new StringBuilder ();
            int cursorPosition = 0, selectionStart = -1, selectionEnd = -1;

            for (int i = 0; i < input.Length; i++) {
                var ch = input [i];
                switch (ch) {
                case '$':
                    cursorPosition = sb.Length;
                    break;
                case '<':
                    if (i + 1 < input.Length) {
                        if (input [i + 1] == '-') {
                            selectionStart = sb.Length;
                            i++;
                            break;
                        }
                    }
                    goto default;
                case '-':
                    if (i + 1 < input.Length) {
                        var next = input [i + 1];
                        if (next == '>') {
                            selectionEnd = sb.Length;
                            i++;
                            break;
                        }
                    }
                    goto default;
                default:
                    sb.Append (ch);
                    break;
                }
            }
            content.Text = sb.ToString ();
            content.CursorPosition = cursorPosition;

            var compExt = new CSharpCompletionTextEditorExtension ();
            compExt.Initialize (doc);
            content.Contents.Add (compExt);

            var ext = new CSharpTextEditorIndentation ();
            CSharpTextEditorIndentation.OnTheFlyFormatting = true;
            ext.Initialize (doc);
            content.Contents.Add (ext);

            doc.UpdateParseDocument ();
            if (selectionStart >= 0 && selectionEnd >= 0)
                content.GetTextEditorData ().SetSelection (selectionStart, selectionEnd);
            return ext;
        }