public async Task TestBug58473() { var text = @"$"; int endPos = text.IndexOf('$'); if (endPos >= 0) { text = text.Substring(0, endPos) + text.Substring(endPos + 1); } var project = Ide.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)); var 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 MonoDevelop.Ide.Gui.Document(tww); doc.SetProject(project); var compExt = new CSharpCompletionTextEditorExtension(); compExt.Initialize(doc.Editor, doc); content.Contents.Add(compExt); await doc.UpdateParseDocument(); var ctx = new CodeCompletionContext(); var tmp = IdeApp.Preferences.EnableAutoCodeCompletion; IdeApp.Preferences.EnableAutoCodeCompletion.Set(false); var list = await compExt.HandleCodeCompletionAsync(ctx, CompletionTriggerInfo.CodeCompletionCommand); Assert.IsNotNull(list); IdeApp.Preferences.EnableAutoCodeCompletion.Set(tmp); project.Dispose(); }
public override void InsertCompletionText(CompletionListWindow window, ref KeyActions ka, Gdk.Key closeChar, char keyChar, Gdk.ModifierType modifier) { Initialize(); using (var undo = doc.Editor.OpenUndoGroup()) { string text = insertNamespace ? type.Namespace + "." + type.Name : type.Name; if (text != GetCurrentWord(window)) { if (window.WasShiftPressed && generateUsing) { text = type.Namespace + "." + text; } window.CompletionWidget.SetCompletionText(window.CodeCompletionContext, GetCurrentWord(window), text); } if (!window.WasShiftPressed && generateUsing) { var generator = CodeGenerator.CreateGenerator(doc); if (generator != null) { generator.AddGlobalNamespaceImport(doc, type.Namespace); // reparse doc.UpdateParseDocument(); } } } ka |= KeyActions.Ignore; }
public async Task TestBug60365() { var 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 = "@c$"; 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.Projects.Services.ProjectService.CreateProject("C#"); project.Name = "test"; project.FileName = "test.csproj"; project.Files.Add(new ProjectFile(content.ContentName, 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 ext = new CSharpCompletionTextEditorExtension(); ext.Initialize(doc.Editor, doc); var listWindow = new CompletionListWindow(); var widget = new TestCompletionWidget(ext.Editor, ext.DocumentContext); listWindow.CompletionWidget = widget; listWindow.CodeCompletionContext = widget.CurrentCodeCompletionContext; var list = await ext.HandleCodeCompletionAsync(widget.CurrentCodeCompletionContext, new CompletionTriggerInfo (CompletionTriggerReason.CharTyped, 'c')); var ka = KeyActions.Complete; list.First(d => d.CompletionText == "class").InsertCompletionText(listWindow, ref ka, KeyDescriptor.Tab); Assert.AreEqual("@class", content.Text); content.Contents.Add(ext); await doc.UpdateParseDocument(); TypeSystemService.Unload(solution); }
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 async Task <List <SearchResult> > GatherReferences(string input, Func <MonoDevelop.Projects.Project, Task <IEnumerable <SearchResult> > > findRefsCallback) { 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; content.Text = text; 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); await doc.UpdateParseDocument(); try { return((await findRefsCallback(project)).ToList()); } finally { TypeSystemService.Unload(solution); } }
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 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 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); }
private static async Task CheckAutoBracket(string mid, string expected) { var prefix = @" class FooBar { public static void Main (string[] args) { Console.WriteLine (" ; var suffix = ");\n\t}\n}\n"; var text = prefix + mid + "@" + suffix; int endPos = text.IndexOf('@'); if (endPos >= 0) { text = text.Substring(0, endPos) + text.Substring(endPos + 1); } var project = Ide.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)); var 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 MonoDevelop.Ide.Gui.Document(tww); doc.SetProject(project); var compExt = new CSharpCompletionTextEditorExtension(); compExt.Initialize(doc.Editor, doc); content.Contents.Add(compExt); await doc.UpdateParseDocument(); var ctx = new CodeCompletionContext(); var handler = new CSharpAutoInsertBracketHandler(); char ch = mid [mid.Length - 1]; handler.Handle(doc.Editor, doc, Ide.Editor.Extension.KeyDescriptor.FromGtk((Gdk.Key)ch, ch, Gdk.ModifierType.None)); var newText = doc.Editor.GetTextAt(prefix.Length, doc.Editor.Length - prefix.Length - suffix.Length); Assert.AreEqual(expected, newText); project.Dispose(); }