public void TestProjectionUpdate ()
		{
			var editor = TextEditorFactory.CreateNewEditor ();
			editor.Text = "1234567890";

			var projectedDocument = TextEditorFactory.CreateNewDocument (
				new StringTextSource ("__12__34__56__78__90"),
				"a"
			);

			var segments = new List<ProjectedSegment> ();
			for (int i = 0; i < 5; i++) {
				segments.Add (new ProjectedSegment (i * 2, 2 + i * 4, 2));
			}
			var projection = new Projection.Projection (projectedDocument, segments);
			var tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			tww.ViewContent = content;

			var originalContext = new Document (tww);
			var projectedEditor = projection.CreateProjectedEditor (originalContext);
			editor.SetOrUpdateProjections (originalContext, new [] { projection }, TypeSystem.DisabledProjectionFeatures.All);
			editor.InsertText (1, "foo");
			Assert.AreEqual ("__1foo2__34__56__78__90", projectedEditor.Text);

			Assert.AreEqual (2, projection.ProjectedSegments.ElementAt (0).ProjectedOffset);
			Assert.AreEqual (2 + "foo".Length, projection.ProjectedSegments.ElementAt (0).Length);
			for (int i = 1; i < 5; i++) {
				Assert.AreEqual (2 + i * 4 + "foo".Length, projection.ProjectedSegments.ElementAt (i).ProjectedOffset);
				Assert.AreEqual (2, projection.ProjectedSegments.ElementAt (i).Length);
			}
		}
        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);
        }
		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 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;
		}
		public static IDocumentIndentEngine CreateEngine (string text)
		{
			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 tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			tww.ViewContent = content;
			content.ContentName = "/a.json";
			content.Data.MimeType = "application/json";

			content.Data.Text = sb.ToString ();
			var doc = new MonoDevelop.Ide.Gui.Document (tww);

			var csi = new JSonIndentEngine (content.Data, doc);
			var result = new CacheIndentEngine (csi);
			result.Update (content.Data, offset);
			return result;
		}
		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;
		}
        internal static IParameterDataProvider CreateProvider(string text)
        {
            string parsedText;
            string editorText;
            int    cursorPosition = text.IndexOf('$');
            int    endPos         = text.IndexOf('$', cursorPosition + 1);

            if (endPos == -1)
            {
                parsedText = editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1);
            }
            else
            {
                parsedText     = text.Substring(0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring(endPos + 1);
                editorText     = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring(endPos + 1);
                cursorPosition = endPos - 1;
            }

            TestWorkbenchWindow tww     = new TestWorkbenchWindow();
            TestViewContent     sev     = new TestViewContent();
            DotNetProject       project = new DotNetAssemblyProject("C#");

            project.FileName = GetTempFile(".csproj");

            string file = GetTempFile(".cs");

            project.AddFile(file);

            ProjectDomService.Load(project);
//			ProjectDom dom = ProjectDomService.GetProjectDom (project);
            ProjectDomService.Parse(project, file, null, delegate { return(parsedText); });
            ProjectDomService.Parse(project, file, null, delegate { return(parsedText); });

            sev.Project        = project;
            sev.ContentName    = file;
            sev.Text           = editorText;
            sev.CursorPosition = cursorPosition;
            tww.ViewContent    = sev;
            Document doc = new Document(tww);

            doc.ParsedDocument = new NRefactoryParser().Parse(null, sev.ContentName, parsedText);
            CSharpTextEditorCompletion textEditorCompletion = new CSharpTextEditorCompletion(doc);

            CodeCompletionContext ctx = new CodeCompletionContext();

            ctx.TriggerOffset = sev.CursorPosition;
            int line, column;

            sev.GetLineColumnFromPosition(sev.CursorPosition, out line, out column);
            ctx.TriggerLine       = line;
            ctx.TriggerLineOffset = column;

            IParameterDataProvider result = textEditorCompletion.HandleParameterCompletion(ctx, editorText[cursorPosition - 1]);

            ProjectDomService.Unload(project);
            return(result);
        }
		static CompletionDataList CreateProvider (string text, bool isCtrlSpace)
		{
			string parsedText;
			string editorText;
			int cursorPosition = text.IndexOf ('$');
			int endPos = text.IndexOf ('$', cursorPosition + 1);
			if (endPos == -1)
				parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1);
			else {
				parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1);
				editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1);
				cursorPosition = endPos - 1; 
			}
			
			TestWorkbenchWindow tww = new TestWorkbenchWindow ();
			TestViewContent sev = new TestViewContent ();
			DotNetProject project = new DotNetAssemblyProject ("C#");
			project.FileName = GetTempFile (".csproj");
			
			string file = GetTempFile (".cs");
			project.AddFile (file);
			
			ProjectDomService.Load (project);
			ProjectDom dom = ProjectDomService.GetProjectDom (project);
			dom.ForceUpdate (true);
			ProjectDomService.Parse (project, file, null, delegate { return parsedText; });
			ProjectDomService.Parse (project, file, null, delegate { return parsedText; });
			
			sev.Project = project;
			sev.ContentName = file;
			sev.Text = editorText;
			sev.CursorPosition = cursorPosition;
			tww.ViewContent = sev;
			Document doc = new Document (tww);
			doc.ParsedDocument = new NRefactoryParser ().Parse (null, sev.ContentName, parsedText);
			foreach (var e in doc.ParsedDocument.Errors)
				Console.WriteLine (e);
			CSharpTextEditorCompletion textEditorCompletion = new CSharpTextEditorCompletion (doc);
			
			int triggerWordLength = 1;
			CodeCompletionContext ctx = new CodeCompletionContext ();
			ctx.TriggerOffset = sev.CursorPosition;
			int line, column;
			sev.GetLineColumnFromPosition (sev.CursorPosition, out line, out column);
			ctx.TriggerLine = line;
			ctx.TriggerLineOffset = column;
			
			if (isCtrlSpace)
				return textEditorCompletion.CodeCompletionCommand (ctx) as CompletionDataList;
			else
				return textEditorCompletion.HandleCodeCompletion (ctx, editorText[cursorPosition - 1] , ref triggerWordLength) as CompletionDataList;
		}
        static async Task <UnitTestTextEditorExtension> Setup(string input)
        {
            var tww     = new TestWorkbenchWindow();
            var content = new TestViewContent();

            tww.ViewContent       = content;
            content.ContentName   = "/a.cs";
            content.Data.MimeType = "text/x-csharp";
            MonoDevelop.AnalysisCore.AnalysisOptions.EnableUnitTestEditorIntegration.Set(true);
            var doc = new Document(tww);

            var text   = @"namespace NUnit.Framework {
	public class TestFixtureAttribute : System.Attribute {} 
	public class TestAttribute : System.Attribute {} 
} namespace TestNs { " + 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 project = MonoDevelop.Ide.Services.ProjectService.CreateDotNetProject("C#");

            project.Name     = "test";
            project.FileName = "test.csproj";
            project.Files.Add(new ProjectFile("/a.cs", BuildAction.Compile));

            var solution = new 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 UnitTestTextEditorExtension();

            compExt.Initialize(doc.Editor, doc);
            content.Contents.Add(compExt);
            await doc.UpdateParseDocument();

            TypeSystemService.Unload(solution);
            return(compExt);
        }
		internal static IParameterDataProvider CreateProvider (string text)
		{
			string parsedText;
			string editorText;
			int cursorPosition = text.IndexOf ('$');
			int endPos = text.IndexOf ('$', cursorPosition + 1);
			if (endPos == -1)
				parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1);
			else {
				parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1);
				editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1);
				cursorPosition = endPos - 1; 
			}
			
			TestWorkbenchWindow tww = new TestWorkbenchWindow ();
			TestViewContent sev = new TestViewContent ();
			DotNetProject project = new DotNetAssemblyProject ("C#");
			project.FileName = GetTempFile (".csproj");
			
			string file = GetTempFile (".cs");
			project.AddFile (file);
			
			ProjectDomService.Load (project);
//			ProjectDom dom = ProjectDomService.GetProjectDom (project);
			ProjectDomService.Parse (project, file, delegate { return parsedText; });
			ProjectDomService.Parse (project, file, delegate { return parsedText; });
			
			sev.Project = project;
			sev.ContentName = file;
			sev.Text = editorText;
			sev.CursorPosition = cursorPosition;
			tww.ViewContent = sev;
			Document doc = new Document (tww);
			doc.ParsedDocument = new NRefactoryParser ().Parse (null, sev.ContentName, parsedText);
			CSharpTextEditorCompletion textEditorCompletion = new CSharpTextEditorCompletion (doc);
			
			CodeCompletionContext ctx = new CodeCompletionContext ();
			ctx.TriggerOffset = sev.CursorPosition;
			int line, column;
			sev.GetLineColumnFromPosition (sev.CursorPosition, out line, out column);
			ctx.TriggerLine = line;
			ctx.TriggerLineOffset = column - 1;
			
			IParameterDataProvider result = textEditorCompletion.HandleParameterCompletion (ctx, editorText[cursorPosition - 1]);
			ProjectDomService.Unload (project);
			return result;
		}
		async Task<Document> CreateDocument (string input)
		{
			var text = input;
			int endPos = text.IndexOf ('$');
			if (endPos >= 0)
				text = text.Substring (0, endPos) + text.Substring (endPos + 1);

			var project = Services.ProjectService.CreateDotNetProject ("C#");
			project.Name = "test";
			project.References.Add (MonoDevelop.Projects.ProjectReference.CreateAssemblyReference ("mscorlib"));
			project.References.Add (MonoDevelop.Projects.ProjectReference.CreateAssemblyReference ("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"));
			project.References.Add (MonoDevelop.Projects.ProjectReference.CreateAssemblyReference ("System.Core"));

			project.FileName = "test.csproj";
			project.Files.Add (new ProjectFile ("/a.cs", BuildAction.Compile)); 

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

			var tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			tww.ViewContent = content;
			content.ContentName = "/a.cs";
			content.Data.MimeType = "text/x-csharp";
			content.Project = project;


			content.Text = text;
			content.CursorPosition = Math.Max (0, endPos);
			var doc = new Document (tww);
			doc.SetProject (project);

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

			await doc.UpdateParseDocument ();

			return doc;
		}
		static async Task<UnitTestTextEditorExtension> Setup (string input)
		{
			var tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			tww.ViewContent = content;
			content.ContentName = "/a.cs";
			content.Data.MimeType = "text/x-csharp";
			MonoDevelop.AnalysisCore.AnalysisOptions.EnableUnitTestEditorIntegration.Set (true);
			var doc = new Document (tww);

			var text = @"namespace NUnit.Framework {
	public class TestFixtureAttribute : System.Attribute {} 
	public class TestAttribute : System.Attribute {} 
} namespace TestNs { " + 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 project = MonoDevelop.Ide.Services.ProjectService.CreateDotNetProject ("C#");
			project.Name = "test";
			project.FileName = "test.csproj";
			project.Files.Add (new ProjectFile ("/a.cs", BuildAction.Compile)); 

			var solution = new 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 UnitTestTextEditorExtension ();
			compExt.Initialize (doc.Editor, doc);
			content.Contents.Add (compExt);
			await doc.UpdateParseDocument ();
			TypeSystemService.Unload (solution);
			return compExt;
		}
		async Task<RazorCSharpParsedDocument> Parse (string text, bool isPreprocessed)
		{
			var project = Services.ProjectService.CreateDotNetProject ("C#", "AspNetApp");

			project.FileName = UnitTests.TestBase.GetTempFile (".csproj");
			string file = UnitTests.TestBase.GetTempFile (".cshtml");
			ProjectFile projectFile = project.AddFile (file);
			if (isPreprocessed)
				projectFile.Generator = "RazorTemplatePreprocessor";

			var sev = new TestViewContent ();
			sev.Project = project;
			sev.ContentName = file;
			sev.Text = text;

			var tww = new TestWorkbenchWindow ();
			tww.ViewContent = sev;

			var doc = new TestDocument (tww);
			doc.Editor.FileName = sev.ContentName;
			doc.UpdateProject (project);

			solution = new MonoDevelop.Projects.Solution ();
			solution.DefaultSolutionFolder.AddItem (project);
			solution.AddConfiguration ("", true);
			await TypeSystemServiceTestExtensions.LoadSolution (solution);

			var parser = new RazorTestingParser {
				Doc = doc
			};
			var options = new ParseOptions {
				Project = project,
				FileName = file,
				Content = new StringTextSource (text)
			};
			return (RazorCSharpParsedDocument)parser.Parse (options, default(CancellationToken)).Result;
		}
		static async Task<Tuple<CSharpCompletionTextEditorExtension,TestViewContent>> Setup (string input)
		{
			TestWorkbenchWindow tww = new TestWorkbenchWindow ();
			TestViewContent content = new TestViewContent ();
			tww.ViewContent = content;
			content.ContentName = "/a.cs";
			content.Data.MimeType = "text/x-csharp";

			var doc = new MonoDevelop.Ide.Gui.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 project = Services.ProjectService.CreateProject ("C#");
			project.Name = "test";
			project.FileName = "test.csproj";
			project.Files.Add (new ProjectFile (content.ContentName, BuildAction.Compile)); 
			project.Policies.Set (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);

			await doc.UpdateParseDocument ();
			TypeSystemService.Unload (solution);
			return Tuple.Create (compExt, content);
		}
		static void TestInsertionPoints (string text)
		{
			
			TestWorkbenchWindow tww = new TestWorkbenchWindow ();
			TestViewContent sev = new TestViewContent ();
			DotNetProject project = new DotNetAssemblyProject ("C#");
			project.FileName = GetTempFile (".csproj");
			
			string file = GetTempFile (".cs");
			project.AddFile (file);
			sev.Project = project;
			sev.ContentName = file;
			tww.ViewContent = sev;
			var doc = new MonoDevelop.Ide.Gui.Document (tww);
			var data = doc.Editor;
			List<InsertionPoint> loc = new List<InsertionPoint> ();
			for (int i = 0; i < text.Length; i++) {
				char ch = text[i];
				if (ch == '@') {
					i++;
					ch = text[i];
					NewLineInsertion insertBefore = NewLineInsertion.None;
					NewLineInsertion insertAfter  = NewLineInsertion.None;
					
					switch (ch) {
					case 'n':
						break;
					case 'd':
						insertAfter = NewLineInsertion.Eol;
						break;
					case 'D':
						insertAfter = NewLineInsertion.BlankLine;
						break;
					case 'u':
						insertBefore = NewLineInsertion.Eol;
						break;
					case 'U':
						insertBefore = NewLineInsertion.BlankLine;
						break;
					case 's':
						insertBefore = insertAfter = NewLineInsertion.Eol;
						break;
					case 'S':
						insertBefore = insertAfter = NewLineInsertion.BlankLine;
						break;
						
					case 't':
						insertBefore = NewLineInsertion.Eol;
						insertAfter = NewLineInsertion.BlankLine;
						break;
					case 'T':
						insertBefore = NewLineInsertion.None;
						insertAfter = NewLineInsertion.BlankLine;
						break;
					case 'v':
						insertBefore = NewLineInsertion.BlankLine;
						insertAfter = NewLineInsertion.Eol;
						break;
					case 'V':
						insertBefore = NewLineInsertion.None;
						insertAfter = NewLineInsertion.Eol;
						break;
					default:
						Assert.Fail ("unknown insertion point:" + ch);
						break;
					}
					loc.Add (new InsertionPoint (data.Document.OffsetToLocation (data.Document.Length), insertBefore, insertAfter));
				} else {
					data.Insert (data.Document.Length, ch.ToString ());
				}
			}
			
			
			doc.ParsedDocument =  new NRefactoryParser ().Parse (null, "a.cs", data.Document.Text);
			
			var foundPoints = HelperMethods.GetInsertionPoints (doc, doc.ParsedDocument.CompilationUnit.Types[0]);
			Assert.AreEqual (loc.Count, foundPoints.Count, "point count doesn't match");
			for (int i = 0; i < loc.Count; i++) {
				Assert.AreEqual (loc[i].Location, foundPoints[i].Location, "point " + i + " doesn't match");
				Assert.AreEqual (loc[i].LineAfter, foundPoints[i].LineAfter, "point " + i + " ShouldInsertNewLineAfter doesn't match");
				Assert.AreEqual (loc[i].LineBefore, foundPoints[i].LineBefore, "point " + i + " ShouldInsertNewLineBefore doesn't match");
			}
		}
		static void TestInsertionPoints (string text)
		{
			
			TestWorkbenchWindow tww = new TestWorkbenchWindow ();
			TestViewContent sev = new TestViewContent ();
			var project = new UnknownProject ();
			project.FileName = "test.csproj";
			
			TypeSystemService.LoadProject (project);

			sev.Project = project;
			tww.ViewContent = sev;
			var doc = new MonoDevelop.Ide.Gui.Document (tww);
			var data = doc.Editor;
			List<InsertionPoint> loc = new List<InsertionPoint> ();
			for (int i = 0; i < text.Length; i++) {
				char ch = text [i];
				if (ch == '@') {
					i++;
					ch = text [i];
					NewLineInsertion insertBefore = NewLineInsertion.None;
					NewLineInsertion insertAfter = NewLineInsertion.None;
					
					switch (ch) {
					case 'n':
						break;
					case 'd':
						insertAfter = NewLineInsertion.Eol;
						break;
					case 'D':
						insertAfter = NewLineInsertion.BlankLine;
						break;
					case 'u':
						insertBefore = NewLineInsertion.Eol;
						break;
					case 'U':
						insertBefore = NewLineInsertion.BlankLine;
						break;
					case 's':
						insertBefore = insertAfter = NewLineInsertion.Eol;
						break;
					case 'S':
						insertBefore = insertAfter = NewLineInsertion.BlankLine;
						break;
						
					case 't':
						insertBefore = NewLineInsertion.Eol;
						insertAfter = NewLineInsertion.BlankLine;
						break;
					case 'T':
						insertBefore = NewLineInsertion.None;
						insertAfter = NewLineInsertion.BlankLine;
						break;
					case 'v':
						insertBefore = NewLineInsertion.BlankLine;
						insertAfter = NewLineInsertion.Eol;
						break;
					case 'V':
						insertBefore = NewLineInsertion.None;
						insertAfter = NewLineInsertion.Eol;
						break;
					default:
						Assert.Fail ("unknown insertion point:" + ch);
						break;
					}
					loc.Add (new InsertionPoint (data.Document.OffsetToLocation (data.Document.TextLength), insertBefore, insertAfter));
				} else {
					data.Insert (data.Document.TextLength, ch.ToString ());
				}
			}
			
			var parsedFile = TypeSystemService.ParseFile (project, "program.cs", "text/x-csharp", data.Document.Text);

			var foundPoints = CodeGenerationService.GetInsertionPoints (doc.Editor, parsedFile, parsedFile.TopLevelTypeDefinitions.First ());
			Assert.AreEqual (loc.Count, foundPoints.Count, "point count doesn't match");
			for (int i = 0; i < loc.Count; i++) {
				Assert.AreEqual (loc[i].Location, foundPoints[i].Location, "point " + i + " doesn't match");
				Assert.AreEqual (loc[i].LineAfter, foundPoints[i].LineAfter, "point " + i + " ShouldInsertNewLineAfter doesn't match");
				Assert.AreEqual (loc[i].LineBefore, foundPoints[i].LineBefore, "point " + i + " ShouldInsertNewLineBefore doesn't match");
			}
		}
        public void TestBug17766()
        {
            var data = Create (@"
            class Foo
            {
            $void Bar ()
            {
            }
            }
            ");
            var engine = new CSharpTextEditorIndentation ();

            var tww = new TestWorkbenchWindow ();
            var content = new TestViewContent (data);
            tww.ViewContent = content;
            content.ContentName = "a.cs";
            engine.Initialize (new Document (tww));
            MiscActions.RemoveTab (data);
            engine.KeyPress (Gdk.Key.Tab, '\t', Gdk.ModifierType.ShiftMask);
            CheckOutput (data, @"
            class Foo
            {
            $void Bar ()
            {
            }
            }
            ", engine);
        }
		static async Task<EditorInfo> CreateEditor (string text, bool isInCSharpContext)
		{
			string parsedText, editorText;
			int cursorPosition = text.IndexOf ('$');
			int endPos = text.IndexOf ('$', cursorPosition + 1);
			if (endPos == -1)
				parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1);
			else {
				parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1);
				editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1);
				cursorPosition = endPos - 1;
			}

			var project = Services.ProjectService.CreateProject ("C#", "AspNetApp");

			project.FileName = UnitTests.TestBase.GetTempFile (".csproj");
			string file = UnitTests.TestBase.GetTempFile (extension);
			project.AddFile (file);

			var sev = new TestViewContent ();
			sev.Project = project;
			sev.ContentName = file;
			sev.Text = editorText;
			sev.CursorPosition = cursorPosition;

			var tww = new TestWorkbenchWindow ();
			tww.ViewContent = sev;

			var doc = new TestDocument (tww);
			doc.Editor.FileName = sev.ContentName;
			doc.UpdateProject (project);

			solution = new MonoDevelop.Projects.Solution ();
			solution.DefaultSolutionFolder.AddItem (project);
			solution.AddConfiguration ("", true);
			await TypeSystemServiceTestExtensions.LoadSolution (solution);

			var parser = new RazorTestingParser {
				Doc = doc
			};
			var options = new ParseOptions {
				Project = project,
				FileName = sev.ContentName,
				Content = new StringTextSource (parsedText)
			};
			var parsedDoc = (RazorCSharpParsedDocument)parser.Parse (options, default(CancellationToken)).Result;
			doc.HiddenParsedDocument = parsedDoc;

			var editorExtension = new RazorCSharpEditorExtension (doc, parsedDoc as RazorCSharpParsedDocument, isInCSharpContext);
			return new EditorInfo {
				Extension = editorExtension,
				EditorText = editorText,
				View = sev
			};
		}
		static WebFormsTestingEditorExtension CreateEditor (string text, string extension, out string editorText, out TestViewContent sev)
		{
			string parsedText;
			int cursorPosition = text.IndexOf ('$');
			int endPos = text.IndexOf ('$', cursorPosition + 1);
			if (endPos == -1)
				parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1);
			else {
				parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1);
				editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1);
				cursorPosition = endPos - 1;
			}

			var project = new AspNetAppProject ("C#");
			project.References.Add (new ProjectReference (ReferenceType.Package, "System"));
			project.References.Add (new ProjectReference (ReferenceType.Package, "System.Web"));
			project.FileName = UnitTests.TestBase.GetTempFile (".csproj");
			string file = UnitTests.TestBase.GetTempFile (extension);
			project.AddFile (file);

			var pcw = TypeSystemService.LoadProject (project);
			TypeSystemService.ForceUpdate (pcw);
			pcw.ReconnectAssemblyReferences ();

			sev = new TestViewContent ();
			sev.Project = project;
			sev.ContentName = file;
			sev.Text = editorText;
			sev.CursorPosition = cursorPosition;

			var tww = new TestWorkbenchWindow ();
			tww.ViewContent = sev;

			var doc = new TestDocument (tww);
			doc.Editor.Document.FileName = sev.ContentName;
			var parser = new WebFormsParser ();
			var parsedDoc = (WebFormsParsedDocument) parser.Parse (false, sev.ContentName, new StringReader (parsedText), project);
			doc.HiddenParsedDocument = parsedDoc;

			return new WebFormsTestingEditorExtension (doc);
		}
		static async Task TestInsertionPoints (string text)
		{
			var tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			tww.ViewContent = content;
			content.ContentName = "/a.cs";
			content.Data.MimeType = "text/x-csharp";
			MonoDevelop.AnalysisCore.AnalysisOptions.EnableUnitTestEditorIntegration.Set (true);
			var doc = new MonoDevelop.Ide.Gui.Document (tww);

			var data = doc.Editor;
			List<InsertionPoint> loc = new List<InsertionPoint> ();
			for (int i = 0; i < text.Length; i++) {
				char ch = text [i];
				if (ch == '@') {
					i++;
					ch = text [i];
					NewLineInsertion insertBefore = NewLineInsertion.None;
					NewLineInsertion insertAfter = NewLineInsertion.None;

					switch (ch) {
					case 'n':
						break;
					case 'd':
						insertAfter = NewLineInsertion.Eol;
						break;
					case 'D':
						insertAfter = NewLineInsertion.BlankLine;
						break;
					case 'u':
						insertBefore = NewLineInsertion.Eol;
						break;
					case 'U':
						insertBefore = NewLineInsertion.BlankLine;
						break;
					case 's':
						insertBefore = insertAfter = NewLineInsertion.Eol;
						break;
					case 'S':
						insertBefore = insertAfter = NewLineInsertion.BlankLine;
						break;

					case 't':
						insertBefore = NewLineInsertion.Eol;
						insertAfter = NewLineInsertion.BlankLine;
						break;
					case 'T':
						insertBefore = NewLineInsertion.None;
						insertAfter = NewLineInsertion.BlankLine;
						break;
					case 'v':
						insertBefore = NewLineInsertion.BlankLine;
						insertAfter = NewLineInsertion.Eol;
						break;
					case 'V':
						insertBefore = NewLineInsertion.None;
						insertAfter = NewLineInsertion.Eol;
						break;
					default:
						Assert.Fail ("unknown insertion point:" + ch);
						break;
					}
					var vv = data.OffsetToLocation (data.Length);
					loc.Add (new InsertionPoint (new DocumentLocation (vv.Line, vv.Column), insertBefore, insertAfter));
				} else {
					data.InsertText (data.Length, ch.ToString ());
				}
			}


			var project = Services.ProjectService.CreateProject ("C#");
			project.Name = "test";
			project.FileName = "test.csproj";
			project.Files.Add (new ProjectFile ("/a.cs", BuildAction.Compile)); 

			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 parsedFile = await doc.UpdateParseDocument ();
			var model = parsedFile.GetAst<SemanticModel> ();
			var sym = model?.GetEnclosingSymbol (data.Text.IndexOf ('{'));
			var type = sym as INamedTypeSymbol ?? sym?.ContainingType;
			if (type != null) {
				var foundPoints = InsertionPointService.GetInsertionPoints (doc.Editor, parsedFile, type, type.Locations.First ());
				Assert.AreEqual (loc.Count, foundPoints.Count, "point count doesn't match");
				for (int i = 0; i < loc.Count; i++) {
					Assert.AreEqual (loc [i].Location, foundPoints [i].Location, "point " + i + " doesn't match");
					Assert.AreEqual (loc [i].LineAfter, foundPoints [i].LineAfter, "point " + i + " ShouldInsertNewLineAfter doesn't match");
					Assert.AreEqual (loc [i].LineBefore, foundPoints [i].LineBefore, "point " + i + " ShouldInsertNewLineBefore doesn't match");
				}
			}

			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;
        }
		internal static RefactoringOptions CreateRefactoringOptions (string text)
		{
			int cursorPosition = -1;
			int endPos = text.IndexOf ('$');
			if (endPos >= 0) {
				cursorPosition = endPos;
				text = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1);
			}
			
			int selectionStart = -1;
			int selectionEnd   = -1;
			int idx = text.IndexOf ("<-");
			if (idx >= 0) {
				selectionStart = idx;
				text = text.Substring (0, idx) + text.Substring (idx + 2);
				selectionEnd = idx = text.IndexOf ("->");
				
				text = text.Substring (0, idx) + text.Substring (idx + 2);
				if (cursorPosition < 0)
					cursorPosition = selectionEnd - 1;
			}
			
			TestWorkbenchWindow tww = new TestWorkbenchWindow ();
			TestViewContent sev = new TestViewContent ();
	//		return new RefactoringOptions ();
			
			DotNetProject project = new DotNetAssemblyProject ("C#");
			Solution solution = new Solution ();
			solution.RootFolder.Items.Add (project);
			project.FileName = GetTempFile (".csproj");
			string file = GetTempFile (".cs");
			project.AddFile (file);
			string parsedText = text;
			string editorText = text;
			ProjectDomService.Load (project);
			ProjectDom dom = ProjectDomService.GetProjectDom (project);
			dom.ForceUpdate (true);
			ProjectDomService.Parse (project, file, delegate { return parsedText; });
			ProjectDomService.Parse (project, file, delegate { return parsedText; });
			
			sev.Project = project;
			sev.ContentName = file;
			sev.Text = editorText;
			sev.CursorPosition = cursorPosition;
			
			tww.ViewContent = sev;
			var doc = new MonoDevelop.Ide.Gui.Document (tww);
			doc.Editor.Document.MimeType = "text/x-csharp";
			doc.Editor.Document.FileName = file;
			doc.ParsedDocument = new McsParser ().Parse (null, sev.ContentName, parsedText);
			foreach (var e in doc.ParsedDocument.Errors)
				Console.WriteLine (e);
			if (cursorPosition >= 0)
				doc.Editor.Caret.Offset = cursorPosition;
			if (selectionStart >= 0) 
				doc.Editor.SetSelection (selectionStart, selectionEnd);
			
			NRefactoryResolver resolver = new NRefactoryResolver (dom, 
			                                                      doc.ParsedDocument.CompilationUnit, 
			                                                      sev.Data, 
			                                                      file);
			
			ExpressionResult expressionResult;
			if (selectionStart >= 0) {
				expressionResult = new ExpressionResult (editorText.Substring (selectionStart, selectionEnd - selectionStart).Trim ());
				endPos = selectionEnd;
			} else {
				expressionResult = new NewCSharpExpressionFinder (dom).FindFullExpression (doc.Editor, cursorPosition + 1);
			}
			ResolveResult resolveResult = endPos >= 0 ? resolver.Resolve (expressionResult, new DomLocation (doc.Editor.Caret.Line, doc.Editor.Caret.Column)) : null;
			
			RefactoringOptions result = new RefactoringOptions {
				Document = doc,
				Dom = dom,
				ResolveResult = resolveResult,
				SelectedItem = null
			};
			if (resolveResult is MemberResolveResult)
				result.SelectedItem = ((MemberResolveResult)resolveResult).ResolvedMember;
			if (resolveResult is LocalVariableResolveResult)
				result.SelectedItem = ((LocalVariableResolveResult)resolveResult).LocalVariable;
			if (resolveResult is ParameterResolveResult)
				result.SelectedItem = ((ParameterResolveResult)resolveResult).Parameter;
			result.TestFileProvider = new FileProvider (result);
			return result;
		}
		static WebFormsTestingEditorExtension CreateEditor (string text, string extension, out string editorText, out TestViewContent sev)
		{
			string parsedText;
			int cursorPosition = text.IndexOf ('$');
			int endPos = text.IndexOf ('$', cursorPosition + 1);
			if (endPos == -1)
				parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1);
			else {
				parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1);
				editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1);
				cursorPosition = endPos - 1;
			}

			var project = Services.ProjectService.CreateDotNetProject ("C#");
			project.References.Add (ProjectReference.CreateAssemblyReference ("System"));
			project.References.Add (ProjectReference.CreateAssemblyReference ("System.Web"));
			project.FileName = UnitTests.TestBase.GetTempFile (".csproj");
			string file = UnitTests.TestBase.GetTempFile (extension);
			project.AddFile (file);

			sev = new TestViewContent ();
			sev.Project = project;
			sev.ContentName = file;
			sev.Text = editorText;
			sev.CursorPosition = cursorPosition;

			var tww = new TestWorkbenchWindow ();
			tww.ViewContent = sev;

			var doc = new TestDocument (tww);
			doc.Editor.FileName = sev.ContentName;
			var parser = new WebFormsParser ();
			var options = new ParseOptions {
				Project = project,
				FileName = sev.ContentName,
				Content = new StringTextSource (parsedText)
			};
			var parsedDoc = (WebFormsParsedDocument)parser.Parse (options, default(CancellationToken)).Result;
			doc.HiddenParsedDocument = parsedDoc;

			return new WebFormsTestingEditorExtension (doc);
		}
		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);
			}
		}
		public void TestProjectionHighlighting ()
		{
			var editor = TextEditorFactory.CreateNewEditor ();
			var options = new CustomEditorOptions (editor.Options);
			options.ColorScheme = "Tango";
			editor.Options = options;
			editor.Text = "1234567890";

			var projectedDocument = TextEditorFactory.CreateNewDocument (
				new StringTextSource ("__12__34__56__78__90"),
				"a"
			);

			var segments = new List<ProjectedSegment> ();
			for (int i = 0; i < 5; i++) {
				segments.Add (new ProjectedSegment (i * 2, 2 + i * 4, 2));
			}
			var projection = new Projection.Projection (projectedDocument, segments);
			var tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			tww.ViewContent = content;

			var originalContext = new Document (tww);
			var projectedEditor = projection.CreateProjectedEditor (originalContext);
			projectedEditor.SemanticHighlighting = new TestSemanticHighlighting (projectedEditor, originalContext);
			editor.SetOrUpdateProjections (originalContext, new [] { projection }, TypeSystem.DisabledProjectionFeatures.None);

			var markup = editor.GetPangoMarkup (0, editor.Length);
			var color = "#75507B";
			Assert.AreEqual ("<span foreground=\"" + color + "\">1</span><span foreground=\"#222222\">234</span><span foreground=\"" + color + "\">5</span><span foreground=\"#222222\">678</span><span foreground=\"" + color + "\">9</span><span foreground=\"#222222\">0</span>", markup);
		}
		static DefaultCommandTextEditorExtension GetExtension (TextEditor editor)
		{
			var ext = new DefaultCommandTextEditorExtension ();
			var tww = new TestWorkbenchWindow { ViewContent = new TestViewContent () };
			ext.Initialize (editor, new TestDocument (tww)); 
			return ext;
		}
		static RazorTestingEditorExtension CreateEditor (string text, bool isInCSharpContext, out string editorText,
			out TestViewContent sev)
		{
			string parsedText;
			int cursorPosition = text.IndexOf ('$');
			int endPos = text.IndexOf ('$', cursorPosition + 1);
			if (endPos == -1)
				parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1);
			else {
				parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1);
				editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1);
				cursorPosition = endPos - 1;
			}

			var project = new AspMvc3Project ("C#");
			project.FileName = UnitTests.TestBase.GetTempFile (".csproj");
			string file = UnitTests.TestBase.GetTempFile (extension);
			project.AddFile (file);

			var pcw = TypeSystemService.LoadProject (project);
			TypeSystemService.ForceUpdate (pcw);
			pcw.ReconnectAssemblyReferences ();

			sev = new TestViewContent ();
			sev.Project = project;
			sev.ContentName = file;
			sev.Text = editorText;
			sev.CursorPosition = cursorPosition;

			var tww = new TestWorkbenchWindow ();
			tww.ViewContent = sev;

			var doc = new Document (tww);
			var parser = new RazorTestingParser ()	{
				Doc = doc
			};
			var parsedDoc = parser.Parse (false, sev.ContentName, new StringReader (parsedText), project);

			return new RazorTestingEditorExtension (doc, parsedDoc as RazorCSharpParsedDocument, isInCSharpContext);
		}
		public void TestProjectionCompletion ()
		{
			var editor = TextEditorFactory.CreateNewEditor ();
			var options = new CustomEditorOptions (editor.Options);
			options.ColorScheme = "Tango";
			editor.Options = options;
			editor.Text = "12345678901234567890";

			var projectedDocument = TextEditorFactory.CreateNewDocument (
				new StringTextSource ("__12__34__56__78__90"),
				"a"
			);

			var segments = new List<ProjectedSegment> ();
			for (int i = 0; i < 5; i++) {
				segments.Add (new ProjectedSegment (i * 2, 2 + i * 4, 2));
			}
			var projection = new Projection.Projection (projectedDocument, segments);
			var tww = new TestWorkbenchWindow ();
			var content = new TestViewContent ();
			tww.ViewContent = content;

			var originalContext = new Document (tww);
			var projectedEditor = projection.CreateProjectedEditor (originalContext);
			TestCompletionExtension orignalExtension;
			editor.SetExtensionChain (originalContext, new [] { orignalExtension = new TestCompletionExtension (editor) });
			TestCompletionExtension projectedExtension;
			projectedEditor.SetExtensionChain (originalContext, new [] { projectedExtension = new TestCompletionExtension (editor) });

			orignalExtension.CompletionWidget = new EmptyCompletionWidget (editor);
			projectedExtension.CompletionWidget = new EmptyCompletionWidget (projectedEditor);

			editor.SetOrUpdateProjections (originalContext, new [] { projection }, TypeSystem.DisabledProjectionFeatures.None);
			editor.CaretOffset = 1;

			var service = new CommandManager ();
			service.LoadCommands ("/MonoDevelop/Ide/Commands");
			service.DispatchCommand (TextEditorCommands.ShowCompletionWindow, null, editor.CommandRouter);
			Assert.IsFalse (orignalExtension.CompletionRun);
			Assert.IsTrue (projectedExtension.CompletionRun);

			editor.CaretOffset = 15;

			service.DispatchCommand (TextEditorCommands.ShowCompletionWindow, null, editor.CommandRouter);
			Assert.IsTrue (orignalExtension.CompletionRun);
		}