Beispiel #1
0
        DotNetProject FindBestDefaultProject(Projects.Solution solution = null)
        {
            // The best candidate to be selected as default project for this document is the startup project.
            // If the startup project is not an owner, pick any project that is not disabled in the current configuration.
            DotNetProject best = null;

            if (solution == null)
            {
                solution = IdeApp.ProjectOperations.CurrentSelectedSolution;
            }
            foreach (var p in ownerProjects)
            {
                if (p.ParentSolution != solution)
                {
                    continue;
                }
                var solConf = p.ParentSolution.GetConfiguration(IdeApp.Workspace.ActiveConfiguration);
                if (solConf == null || !solConf.BuildEnabledForItem(p))
                {
                    continue;
                }
                if (p == p.ParentSolution.StartupItem)
                {
                    return(p);
                }
                if (best == null)
                {
                    best = p;
                }
            }
            return(best ?? ownerProjects.FirstOrDefault(pr => pr.ParentSolution == solution) ?? ownerProjects.FirstOrDefault());
        }
Beispiel #2
0
        static async Task <Tuple <CodeActionEditorExtension, Projects.Solution> > GatherFixesNoDispose <T> (string input, Action <ResultsEditorExtension, TaskCompletionSource <T> > callback, params int[] caretLocations)
        {
            await Ide.Composition.CompositionManager.InitializeAsync();

            TestWorkbenchWindow tww     = new TestWorkbenchWindow();
            TestViewContent     content = new TestViewContent();

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

            var doc = new Ide.Gui.Document(tww);

            var text = input;

            content.Text = text;

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

            project.Name     = "test";
            project.FileName = "test.csproj";
            project.Files.Add(new ProjectFile(content.ContentName, BuildAction.Compile));
            var solution = new Projects.Solution();

            solution.AddConfiguration("", true);
            solution.DefaultSolutionFolder.AddItem(project);
            content.Project = project;
            doc.SetProject(project);

            using (var monitor = new ProgressMonitor())
                await TypeSystemService.Load(solution, monitor);

            var resultsExt = new ResultsEditorExtension();

            resultsExt.Initialize(doc.Editor, doc);
            content.Contents.Add(resultsExt);

            var compExt = new CodeActionEditorExtension();

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

            var tcs = new TaskCompletionSource <T> ();

            var cts = new CancellationTokenSource();

            cts.CancelAfter(60 * 1000);
            cts.Token.Register(() => tcs.TrySetCanceled());

            resultsExt.TasksUpdated += delegate {
                callback(resultsExt, tcs);
            };

            await doc.UpdateParseDocument();

            await Task.Run(() => tcs.Task);

            return(Tuple.Create(compExt, solution));
        }
Beispiel #3
0
        public async Task FixesAreReportedByExtension()
        {
            var expected = new ExpectedCodeFixes {
                CodeFixData = new CodeActionData [] {
                    new CodeActionData {
                        Message = "Add accessibility modifiers"
                    },
                },
                CodeRefactoringData = new CodeActionData [] {
                    new CodeActionData {
                        Message = "To public"
                    },
                    new CodeActionData {
                        Message = "Generate overrides..."
                    },
                    new CodeActionData {
                        Message = "Generate constructor 'MyClass()'"
                    },
                    new CodeActionData {
                        Message = "Rename file to MyClass.cs"
                    },
                    new CodeActionData {
                        Message = "Rename type to a"
                    },
                }
            };

            Projects.Solution sol = null;
            var old = IdeApp.Preferences.EnableSourceAnalysis;

            try {
                IdeApp.Preferences.EnableSourceAnalysis.Value = true;

                int expectedUpdates           = 1;
                ImmutableArray <QuickTask> qt = ImmutableArray <QuickTask> .Empty;
                var tuple = await GatherFixesNoDispose <bool> (OneFromEach, (resultExt, tcs) => {
                    if (--expectedUpdates == 0)
                    {
                        qt = resultExt.QuickTasks;
                        tcs.SetResult(true);
                    }
                });

                var ext = tuple.Item1;
                sol = tuple.Item2;

                Assert.AreEqual(1, qt.Length);

                ext.Editor.CaretOffset = qt[0].Location;

                var fixes = await ext.GetCurrentFixesAsync(CancellationToken.None);

                AssertCodeFixes(fixes, expected);
            } finally {
                IdeApp.Preferences.EnableSourceAnalysis.Value = old;
                TypeSystemService.Unload(sol);
            }
        }
Beispiel #4
0
        public async Task FixesAreReportedForCompilerErrors()
        {
            var expected = new ExpectedCodeFixes {
                CodeFixData = new CodeActionData [] {
                    new CodeActionData {
                        Message = "Implement interface"
                    },
                    new CodeActionData {
                        Message = "Implement interface with Dispose pattern"
                    },
                    new CodeActionData {
                        Message = "Implement interface explicitly"
                    },
                    new CodeActionData {
                        Message = "Implement interface explicitly with Dispose pattern"
                    },
                },
                CodeRefactoringData = new CodeActionData[] {
                    new CodeActionData {
                        Message = "To public"
                    },
                },
            };

            Projects.Solution sol = null;
            var old = IdeApp.Preferences.EnableSourceAnalysis;

            try {
                IdeApp.Preferences.EnableSourceAnalysis.Value = true;

                int expectedUpdates        = 2;
                ImmutableArray <Result> qt = ImmutableArray <Result> .Empty;
                var tuple = await GatherFixesNoDispose <bool> (IDisposableImplement, (resultExt, tcs) => {
                    if (--expectedUpdates == 0)
                    {
                        qt = resultExt.GetResults().ToImmutableArray();
                        tcs.SetResult(true);
                    }
                });

                var ext = tuple.Item1;
                sol = tuple.Item2;

                Assert.AreEqual(2, qt.Length);

                var diag = qt.OfType <DiagnosticResult> ().Single(x => x.Diagnostic.Id == "CS0535");
                ext.Editor.CaretOffset = diag.Region.Start;

                var fixes = await ext.GetCurrentFixesAsync(CancellationToken.None);

                AssertCodeFixes(fixes, expected);
            } finally {
                IdeApp.Preferences.EnableSourceAnalysis.Value = old;
                TypeSystemService.Unload(sol);
            }
        }
        public async Task DiagnosticEnableSourceAnalysisChanged()
        {
            var old = IdeApp.Preferences.EnableSourceAnalysis;

            Projects.Solution sol = null;
            try {
                int expectedUpdates = 5;
                IdeApp.Preferences.EnableSourceAnalysis.Value = true;

                var tuple = await GatherDiagnosticsNoDispose <bool> (OneFromEach, (ext, tcs) => {
                    --expectedUpdates;
                    if (expectedUpdates == 4)
                    {
                        Assert.AreEqual(2, ext.QuickTasks.Length);
                        for (int i = 0; i < ext.QuickTasks.Length; ++i)
                        {
                            AssertExpectedDiagnostic(OneFromEachDiagnostics [i], ext.QuickTasks [i]);
                        }
                    }

                    if (expectedUpdates == 1)
                    {
                        IdeApp.Preferences.EnableSourceAnalysis.Value = false;

                        Assert.AreEqual(5, ext.QuickTasks.Length);
                        for (int i = 0; i < ext.QuickTasks.Length; ++i)
                        {
                            AssertExpectedDiagnostic(OneFromEachDiagnostics [i], ext.QuickTasks [i]);
                        }
                    }

                    if (expectedUpdates == 0)
                    {
                        Assert.AreEqual(0, ext.QuickTasks.Length);
                        tcs.SetResult(true);
                    }
                });

                sol = tuple.Item2;
            } finally {
                IdeApp.Preferences.EnableSourceAnalysis.Value = old;
                if (sol != null)
                {
                    TypeSystemService.Unload(sol);
                }
            }
        }