Ejemplo n.º 1
0
        void PopupQuickFixMenu(Gdk.EventButton evt)
        {
            Gtk.Menu menu = new Gtk.Menu();

            Dictionary <Gtk.MenuItem, ContextAction> fixTable = new Dictionary <Gtk.MenuItem, ContextAction> ();
            int mnemonic = 1;

            foreach (ContextAction fix in fixes)
            {
                var escapedLabel = fix.GetMenuText(document, loc).Replace("_", "__");
                var label        = (mnemonic <= 10)
                                                ? "_" + (mnemonic++ % 10).ToString() + " " + escapedLabel
                                                : "  " + escapedLabel;
                Gtk.MenuItem menuItem = new Gtk.MenuItem(label);
                fixTable [menuItem] = fix;
                menuItem.Activated += delegate(object sender, EventArgs e) {
                    // ensure that the Ast is recent.
                    document.UpdateParseDocument();
                    var runFix = fixTable [(Gtk.MenuItem)sender];
                    runFix.Run(document, loc);

                    document.Editor.Document.CommitUpdateAll();
                    menu.Destroy();
                };
                menu.Add(menuItem);
            }
            menu.ShowAll();
            menu.SelectFirst(true);
            menuPushed      = true;
            menu.Destroyed += delegate {
                menuPushed = false;
                QueueDraw();
            };
            GtkWorkarounds.ShowContextMenu(menu, this, evt, Allocation);
        }
Ejemplo n.º 2
0
        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 async static Task Run(MonoDevelop.Ide.Gui.Document doc)
        {
            if (!doc.Editor.IsSomethingSelected)
            {
                return;
            }
            var ad = doc.AnalysisDocument;

            if (ad == null || !await IsValid(doc))
            {
                return;
            }
            try {
                var selectionRange = doc.Editor.SelectionRange;
                var token          = default(CancellationToken);
                var selection      = new CSharpSelectionValidator(await SemanticDocument.CreateAsync(ad, token).ConfigureAwait(false), new TextSpan(selectionRange.Offset, selectionRange.Length), doc.GetOptionSet());
                var result         = await selection.GetValidSelectionAsync(token).ConfigureAwait(false);

                if (!result.ContainsValidContext)
                {
                    return;
                }
                var extractor        = new CSharpMethodExtractor((CSharpSelectionResult)result);
                var extractionResult = await extractor.ExtractMethodAsync(token).ConfigureAwait(false);

                var changes = await extractionResult.Document.GetTextChangesAsync(ad, token);

                using (var undo = doc.Editor.OpenUndoGroup()) {
                    foreach (var change in changes.OrderByDescending(ts => ts.Span.Start))
                    {
                        doc.Editor.ReplaceText(change.Span.Start, change.Span.Length, change.NewText);
                    }
                    // hack to remove the redundant private modifier.
                    if (doc.Editor.GetTextAt(extractionResult.MethodDeclarationNode.SpanStart, "private ".Length) == "private ")
                    {
                        doc.Editor.RemoveText(extractionResult.MethodDeclarationNode.SpanStart, "private ".Length);
                    }
                }
                await doc.UpdateParseDocument();

                var info = RefactoringSymbolInfo.GetSymbolInfoAsync(doc, extractionResult.InvocationNameToken.Span.Start).Result;
                var sym  = info.DeclaredSymbol ?? info.Symbol;
                if (sym != null)
                {
                    await new MonoDevelop.Refactoring.Rename.RenameRefactoring().Rename(sym);
                }
            }
            catch (Exception e) {
                LoggingService.LogError("Error while extracting method", e);
            }
        }
Ejemplo n.º 4
0
        public void PopupQuickFixMenu()
        {
            Gtk.Menu menu = new Gtk.Menu();

            Dictionary <Gtk.MenuItem, ContextAction> fixTable = new Dictionary <Gtk.MenuItem, ContextAction> ();
            int mnemonic = 1;

            foreach (ContextAction fix in fixes)
            {
                var escapedLabel = fix.GetMenuText(document, loc).Replace("_", "__");
                var label        = (mnemonic <= 10)
                                                ? "_" + (mnemonic++ % 10).ToString() + " " + escapedLabel
                                                : "  " + escapedLabel;
                Gtk.MenuItem menuItem = new Gtk.MenuItem(label);
                fixTable [menuItem] = fix;
                menuItem.Activated += delegate(object sender, EventArgs e) {
                    // ensure that the Ast is recent.
                    document.UpdateParseDocument();
                    var runFix = fixTable [(Gtk.MenuItem)sender];
                    runFix.Run(document, loc);

                    document.Editor.Document.CommitUpdateAll();
                    menu.Destroy();
                };
                menu.Add(menuItem);
            }
            menu.ShowAll();
            int dx, dy;

            this.ParentWindow.GetOrigin(out dx, out dy);
            dx += ((TextEditorContainer.EditorContainerChild)(this.document.Editor.Parent.Parent as TextEditorContainer) [this]).X;
            dy += ((TextEditorContainer.EditorContainerChild)(this.document.Editor.Parent.Parent as TextEditorContainer) [this]).Y - (int)document.Editor.VAdjustment.Value;

            menu.Popup(null, null, delegate(Gtk.Menu menu2, out int x, out int y, out bool pushIn) {
                x          = dx;
                y          = dy + Allocation.Height;
                pushIn     = false;
                menuPushed = true;
                QueueDraw();
            }, 0, Gtk.Global.CurrentEventTime);
            menu.SelectFirst(true);
            menu.Destroyed += delegate {
                menuPushed = false;
                QueueDraw();
            };
        }
Ejemplo n.º 5
0
        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);
            }
        }
 public static void FormatText(this AstNode node, MonoDevelop.Ide.Gui.Document doc)
 {
     doc.UpdateParseDocument();
     MonoDevelop.PlayScript.Formatting.OnTheFlyFormatter.Format(doc, node.StartLocation);
 }
 public static void FormatText(this AstNode node, MonoDevelop.Ide.Gui.Document doc)
 {
     doc.UpdateParseDocument();
     MonoDevelop.CSharp.Formatting.OnTheFlyFormatter.Format(doc, doc.Dom, new DomLocation(node.StartLocation.Line, node.StartLocation.Column));
 }