Example #1
0
        ToolStripMenuItem MakeItem(LocalResolveResult local, bool isDefinition)
        {
            ToolStripMenuItem item = MakeItemInternal(MemberNode.GetText(local.Field),
                                                      local.IsParameter ? ClassBrowserIconService.ParameterIndex : ClassBrowserIconService.LocalVariableIndex,
                                                      local.CallingClass.CompilationUnit,
                                                      isDefinition ? DomRegion.Empty : local.Field.Region);
            string treePath = "/SharpDevelop/ViewContent/DefaultTextEditor/Refactoring/";

            treePath += local.IsParameter ? "Parameter" : "LocalVariable";
            if (isDefinition)
            {
                treePath += "Definition";
            }
            MenuService.AddItemsToMenu(item.DropDown.Items, local, treePath);
            return(item);
        }
Example #2
0
        public ToolStripItem[] BuildSubmenu(Codon codon, object owner)
        {
            ToolStripMenuItem item;

            TextEditorControl textEditorControl = (TextEditorControl)owner;

            if (textEditorControl.FileName == null)
            {
                return(new ToolStripItem[0]);
            }
            List <ToolStripItem> resultItems = new List <ToolStripItem>();
            TextArea             textArea    = textEditorControl.ActiveTextAreaControl.TextArea;
            IDocument            doc         = textArea.Document;
            int caretLine = textArea.Caret.Line;

            // list of dotnet names that have definition bookmarks in this line
            List <string> definitions = new List <string>();

            // Include definitions (use the bookmarks which should already be present)
            foreach (Bookmark mark in doc.BookmarkManager.Marks)
            {
                if (mark != null && mark.LineNumber == caretLine)
                {
                    ClassMemberBookmark cmb = mark as ClassMemberBookmark;
                    ClassBookmark       cb  = mark as ClassBookmark;
                    IClass type             = null;
                    if (cmb != null)
                    {
                        definitions.Add(cmb.Member.DotNetName);
                        item = new ToolStripMenuItem(MemberNode.GetText(cmb.Member),
                                                     ClassBrowserIconService.ImageList.Images[cmb.IconIndex]);
                        MenuService.AddItemsToMenu(item.DropDown.Items, mark, ClassMemberBookmark.ContextMenuPath);
                        resultItems.Add(item);
                        type = cmb.Member.DeclaringType;
                    }
                    else if (cb != null)
                    {
                        type = cb.Class;
                    }
                    if (type != null)
                    {
                        definitions.Add(type.DotNetName);
                        item = new ToolStripMenuItem(type.Name, ClassBrowserIconService.ImageList.Images[ClassBrowserIconService.GetIcon(type)]);
                        MenuService.AddItemsToMenu(item.DropDown.Items,
                                                   cb ?? new ClassBookmark(textArea.Document, type),
                                                   ClassBookmark.ContextMenuPath);
                        resultItems.Add(item);
                    }
                }
            }

            // Include menu for member that has been clicked on
            IExpressionFinder expressionFinder = ParserService.GetExpressionFinder(textEditorControl.FileName);
            ExpressionResult  expressionResult;
            ResolveResult     rr;
            int insertIndex = resultItems.Count;                // Insert items at this position to get the outermost expression first, followed by the inner expressions (if any).

            expressionResult = FindFullExpressionAtCaret(textArea, expressionFinder);
repeatResolve:
            rr   = ResolveExpressionAtCaret(textArea, expressionResult);
            item = null;
            if (rr is MethodResolveResult)
            {
                item = MakeItem(definitions, ((MethodResolveResult)rr).GetMethodIfSingleOverload());
            }
            else if (rr is MemberResolveResult)
            {
                MemberResolveResult mrr = (MemberResolveResult)rr;
                item = MakeItem(definitions, mrr.ResolvedMember);
                if (RefactoringService.FixIndexerExpression(expressionFinder, ref expressionResult, mrr))
                {
                    if (item != null)
                    {
                        resultItems.Insert(insertIndex, item);
                    }
                    // Include menu for the underlying expression of the
                    // indexer expression as well.
                    goto repeatResolve;
                }
            }
            else if (rr is TypeResolveResult)
            {
                item = MakeItem(definitions, ((TypeResolveResult)rr).ResolvedClass);
            }
            else if (rr is LocalResolveResult)
            {
                item        = MakeItem((LocalResolveResult)rr, caretLine + 1 == ((LocalResolveResult)rr).Field.Region.BeginLine);
                insertIndex = 0;                        // Insert local variable menu item at the topmost position.
            }
            if (item != null)
            {
                resultItems.Insert(insertIndex, item);
            }

            // Include menu for current class and method
            IMember callingMember = null;

            if (rr != null)
            {
                callingMember = rr.CallingMember;
            }
            else
            {
                ParseInformation parseInfo = ParserService.GetParseInformation(textEditorControl.FileName);
                if (parseInfo != null)
                {
                    ICompilationUnit cu = parseInfo.MostRecentCompilationUnit;
                    if (cu != null)
                    {
                        IClass callingClass = cu.GetInnermostClass(caretLine + 1, textArea.Caret.Column + 1);
                        callingMember = GetCallingMember(callingClass, caretLine + 1, textArea.Caret.Column + 1);
                    }
                }
            }
            if (callingMember != null)
            {
                item = MakeItem(definitions, callingMember);
                if (item != null)
                {
                    item.Text = StringParser.Parse("${res:SharpDevelop.Refactoring.CurrentMethod}: ") + callingMember.Name;
                    resultItems.Add(item);
                }
            }

            if (resultItems.Count == 0)
            {
                return(new ToolStripItem[0]);
            }
            else
            {
                resultItems.Add(new MenuSeparator());
                return(resultItems.ToArray());
            }
        }