public static bool ResolveAt(Document doc, out ResolveResult resolveResult, out AstNode node, CancellationToken token = default(CancellationToken))
        {
            var parsedDocument = doc.ParsedDocument;

            resolveResult = null;
            node          = null;
            if (parsedDocument == null)
            {
                return(false);
            }
            var unit       = parsedDocument.GetAst <SyntaxTree> ();
            var parsedFile = parsedDocument.ParsedFile as CSharpUnresolvedFile;

            if (unit == null || parsedFile == null)
            {
                return(false);
            }
            try {
                var location = RefactoringService.GetCorrectResolveLocation(doc, doc.Editor.Caret.Location);
                resolveResult = ResolveAtLocation.Resolve(doc.Compilation, parsedFile, unit, location, out node, token);
                if (resolveResult == null || node is Statement)
                {
                    return(false);
                }
            } catch (OperationCanceledException) {
                return(false);
            } catch (Exception e) {
                Console.WriteLine("Got resolver exception:" + e);
                return(false);
            }
            return(true);
        }
        public static bool ResolveAt(Document doc, out ResolveResult resolveResult, out AstNode node, CancellationToken token = default(CancellationToken))
        {
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }
            var editor = doc.Editor;

            if (editor == null || editor.MimeType != "text/x-csharp")
            {
                node          = null;
                resolveResult = null;
                return(false);
            }
            if (!InternalResolveAt(doc, out resolveResult, out node))
            {
                var location = RefactoringService.GetCorrectResolveLocation(doc, editor.Caret.Location);
                resolveResult = GetHeuristicResult(doc, location, ref node);
                if (resolveResult == null)
                {
                    return(false);
                }
            }
            var oce = node as ObjectCreateExpression;

            if (oce != null)
            {
                node = oce.Type;
            }
            return(true);
        }
        public virtual void Run(RefactoringOptions options)
        {
            List <Change>    changes = PerformChanges(options, null);
            IProgressMonitor monitor = IdeApp.Workbench.ProgressMonitors.GetBackgroundProgressMonitor(Name, null);

            RefactoringService.AcceptChanges(monitor, options.Dom, changes);
        }
        public static List <PossibleNamespace> GetPossibleNamespaces(Document doc, AstNode node, ref ResolveResult resolveResult)
        {
            if (doc == null)
            {
                throw new ArgumentNullException("doc");
            }
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            var location = RefactoringService.GetCorrectResolveLocation(doc, doc.Editor.Caret.Location);

            if (resolveResult == null || resolveResult.Type.FullName == "System.Void")
            {
                resolveResult = GetHeuristicResult(doc, location, ref node) ?? resolveResult;
            }
            var foundNamespaces = GetPossibleNamespaces(doc, node, resolveResult, location);

            if (!(resolveResult is AmbiguousTypeResolveResult))
            {
                var usedNamespaces = RefactoringOptions.GetUsedNamespaces(doc, location);
                foundNamespaces = foundNamespaces.Where(n => !usedNamespaces.Contains(n.Namespace));
            }
            var result = new List <PossibleNamespace> ();

            foreach (var ns in foundNamespaces)
            {
                if (result.Any(n => n.Namespace == ns.Namespace))
                {
                    continue;
                }
                result.Add(ns);
            }
            return(result);
        }
        public static List <string> GetResolveableNamespaces(RefactoringOptions options, out bool resolveDirect)
        {
            IReturnType            returnType  = null;
            INRefactoryASTProvider astProvider = RefactoringService.GetASTProvider(DesktopService.GetMimeTypeForUri(options.Document.FileName));

            if (options.ResolveResult != null && options.ResolveResult.ResolvedExpression != null)
            {
                if (astProvider != null)
                {
                    returnType = astProvider.ParseTypeReference(options.ResolveResult.ResolvedExpression.Expression).ConvertToReturnType();
                }
                if (returnType == null)
                {
                    returnType = DomReturnType.GetSharedReturnType(options.ResolveResult.ResolvedExpression.Expression);
                }
            }

            List <string> namespaces;

            if (options.ResolveResult is UnresolvedMemberResolveResult)
            {
                namespaces = new List <string> ();
                UnresolvedMemberResolveResult unresolvedMemberResolveResult = options.ResolveResult as UnresolvedMemberResolveResult;
                IType type = unresolvedMemberResolveResult.TargetResolveResult != null?options.Dom.GetType(unresolvedMemberResolveResult.TargetResolveResult.ResolvedType) : null;

                if (type != null)
                {
                    List <IType> allExtTypes = DomType.GetAccessibleExtensionTypes(options.Dom, null);
                    foreach (ExtensionMethod method in type.GetExtensionMethods(allExtTypes, unresolvedMemberResolveResult.MemberName))
                    {
                        string ns = method.OriginalMethod.DeclaringType.Namespace;
                        if (!namespaces.Contains(ns) && !options.Document.CompilationUnit.Usings.Any(u => u.Namespaces.Contains(ns)))
                        {
                            namespaces.Add(ns);
                        }
                    }
                }
                resolveDirect = false;
            }
            else
            {
                namespaces    = new List <string> (options.Dom.ResolvePossibleNamespaces(returnType));
                resolveDirect = true;
            }
            for (int i = 0; i < namespaces.Count; i++)
            {
                for (int j = i + 1; j < namespaces.Count; j++)
                {
                    if (namespaces[j] == namespaces[i])
                    {
                        namespaces.RemoveAt(j);
                        j--;
                    }
                }
            }
            return(namespaces);
        }
Exemple #6
0
        public RefactoringPreviewDialog(ProjectDom ctx, List <Change> changes)
        {
            this.Build();
            this.changes          = changes;
            treeviewPreview.Model = store;

            TreeViewColumn column = new TreeViewColumn();

            // pixbuf column
            var pixbufCellRenderer = new CellRendererPixbuf();

            column.PackStart(pixbufCellRenderer, false);
            column.SetAttributes(pixbufCellRenderer, "pixbuf", pixbufColumn);
            column.AddAttribute(pixbufCellRenderer, "visible", statusVisibleColumn);

            // text column
            CellRendererText cellRendererText = new CellRendererText();

            column.PackStart(cellRendererText, false);
            column.SetAttributes(cellRendererText, "text", textColumn);
            column.AddAttribute(cellRendererText, "visible", statusVisibleColumn);

            // location column
            CellRendererText cellRendererText2 = new CellRendererText();

            column.PackStart(cellRendererText2, false);
            column.SetCellDataFunc(cellRendererText2, new TreeCellDataFunc(SetLocationTextData));

            CellRendererDiff cellRendererDiff = new CellRendererDiff();

            column.PackStart(cellRendererDiff, true);
            column.SetCellDataFunc(cellRendererDiff, new TreeCellDataFunc(SetDiffCellData));

            treeviewPreview.AppendColumn(column);
            treeviewPreview.HeadersVisible = false;

            buttonCancel.Clicked += delegate {
                Destroy();
            };

            buttonOk.Clicked += delegate {
                IProgressMonitor monitor = IdeApp.Workbench.ProgressMonitors.GetBackgroundProgressMonitor(this.Title, null);
                RefactoringService.AcceptChanges(monitor, ctx, changes);

                Destroy();
            };

            FillChanges();
            Resize(IdeApp.Workbench.ActiveDocument.ActiveView.Control.Allocation.Width,
                   IdeApp.Workbench.ActiveDocument.ActiveView.Control.Allocation.Height);
        }
Exemple #7
0
        public RefactoringPreviewDialog(IList <Change> changes)
        {
            this.Build();
            this.changes                 = changes;
            treeviewPreview.Model        = store;
            treeviewPreview.SearchColumn = -1;             // disable the interactive search

            TreeViewColumn column = new TreeViewColumn();

            // pixbuf column
            var pixbufCellRenderer = new CellRendererImage();

            column.PackStart(pixbufCellRenderer, false);
            column.SetAttributes(pixbufCellRenderer, "image", pixbufColumn);
            column.AddAttribute(pixbufCellRenderer, "visible", statusVisibleColumn);

            // text column
            CellRendererText cellRendererText = new CellRendererText();

            column.PackStart(cellRendererText, false);
            column.SetAttributes(cellRendererText, "text", textColumn);
            column.AddAttribute(cellRendererText, "visible", statusVisibleColumn);

            // location column
            CellRendererText cellRendererText2 = new CellRendererText();

            column.PackStart(cellRendererText2, false);
            column.SetCellDataFunc(cellRendererText2, new TreeCellDataFunc(SetLocationTextData));

            CellRendererDiff cellRendererDiff = new CellRendererDiff();

            column.PackStart(cellRendererDiff, true);
            column.SetCellDataFunc(cellRendererDiff, new TreeCellDataFunc(SetDiffCellData));

            treeviewPreview.AppendColumn(column);
            treeviewPreview.HeadersVisible = false;

            buttonCancel.Clicked += delegate {
                Destroy();
            };

            buttonOk.Clicked += delegate {
                ProgressMonitor monitor = IdeApp.Workbench.ProgressMonitors.GetBackgroundProgressMonitor(this.Title, null);
                RefactoringService.AcceptChanges(monitor, changes);

                Destroy();
            };

            FillChanges();
        }
        public static HashSet <PossibleNamespace> GetPossibleNamespaces(Document doc, AstNode node, ref ResolveResult resolveResult)
        {
            var location = RefactoringService.GetCorrectResolveLocation(doc, doc.Editor.Caret.Location);

            if (resolveResult == null || resolveResult.Type.FullName == "System.Void")
            {
                resolveResult = GetHeuristicResult(doc, location, ref node) ?? resolveResult;
            }
            var foundNamespaces = GetPossibleNamespaces(doc, node, resolveResult, location);

            if (!(resolveResult is AmbiguousTypeResolveResult))
            {
                var usedNamespaces = RefactoringOptions.GetUsedNamespaces(doc, location);
                foundNamespaces = foundNamespaces.Where(n => !usedNamespaces.Contains(n.Namespace));
            }

            return(new HashSet <PossibleNamespace> (foundNamespaces));
        }
Exemple #9
0
        public static bool ResolveAt(Document doc, out ResolveResult resolveResult, out AstNode node, CancellationToken token = default(CancellationToken))
        {
            if (!InternalResolveAt(doc, out resolveResult, out node))
            {
                var location = RefactoringService.GetCorrectResolveLocation(doc, doc.Editor.Caret.Location);
                resolveResult = GetHeuristicResult(doc, location, ref node);
                if (resolveResult == null)
                {
                    return(false);
                }
            }
            var oce = node as ObjectCreateExpression;

            if (oce != null)
            {
                node = oce.Type;
            }
            return(true);
        }
Exemple #10
0
 public INRefactoryASTProvider GetASTProvider()
 {
     return(RefactoringService.GetASTProvider(MimeType));
 }
        protected override void Update(CommandArrayInfo ainfo)
        {
            var doc = IdeApp.Workbench.ActiveDocument;

            if (doc == null || doc.FileName == FilePath.Null || doc.ParsedDocument == null)
            {
                return;
            }

            ResolveResult resolveResult;
            AstNode       node;

            if (!ResolveAt(doc, out resolveResult, out node))
            {
                var location = RefactoringService.GetCorrectResolveLocation(doc, doc.Editor.Caret.Location);
                resolveResult = GetHeuristicResult(doc, location, ref node);
                if (resolveResult == null)
                {
                    return;
                }
            }
            var resolveMenu = new CommandInfoSet();

            resolveMenu.Text = GettextCatalog.GetString("Resolve");

            var possibleNamespaces = GetPossibleNamespaces(doc, node, ref resolveResult);

            bool addUsing = !(resolveResult is AmbiguousTypeResolveResult);

            if (addUsing)
            {
                foreach (var t in possibleNamespaces.Where(tp => tp.IsAccessibleWithGlobalUsing))
                {
                    string ns        = t.Namespace;
                    var    reference = t.Reference;
                    var    info      = resolveMenu.CommandInfos.Add(
                        t.GetImportText(),
                        new System.Action(new AddImport(doc, resolveResult, ns, reference, true, node).Run)
                        );
                    info.Icon = MonoDevelop.Ide.Gui.Stock.AddNamespace;
                }
            }

            bool resolveDirect = !(resolveResult is UnknownMemberResolveResult);

            if (resolveDirect)
            {
                if (resolveMenu.CommandInfos.Count > 0)
                {
                    resolveMenu.CommandInfos.AddSeparator();
                }
                if (node is ObjectCreateExpression)
                {
                    node = ((ObjectCreateExpression)node).Type;
                }
                foreach (var t in possibleNamespaces)
                {
                    string ns        = t.Namespace;
                    var    reference = t.Reference;
                    resolveMenu.CommandInfos.Add(t.GetInsertNamespaceText(doc.Editor.GetTextBetween(node.StartLocation, node.EndLocation)), new System.Action(new AddImport(doc, resolveResult, ns, reference, false, node).Run));
                }
            }

            if (resolveMenu.CommandInfos.Count > 0)
            {
                ainfo.Insert(0, resolveMenu);
            }
        }
        protected override void Update(CommandArrayInfo ainfo)
        {
            var doc = IdeApp.Workbench.ActiveDocument;

            if (doc == null || doc.FileName == FilePath.Null)
            {
                return;
            }

            var parsedDocument = doc.ParsedDocument;

            if (parsedDocument == null || parsedDocument.IsInvalid)
            {
                return;
            }

            ResolveResult resolveResult;
            object        item  = GetItem(doc, out resolveResult);
            bool          added = false;

            var options = new RefactoringOptions(doc)
            {
                ResolveResult = resolveResult,
                SelectedItem  = item
            };

            var ciset = new CommandInfoSet();

            ciset.Text = GettextCatalog.GetString("Refactor");

            bool canRename;

            if (item is IVariable || item is IParameter)
            {
                canRename = true;
            }
            else if (item is ITypeDefinition)
            {
                canRename = !((ITypeDefinition)item).Region.IsEmpty;
            }
            else if (item is IType)
            {
                canRename = ((IType)item).Kind == TypeKind.TypeParameter;
            }
            else if (item is IMember)
            {
                canRename = !((IMember)item).Region.IsEmpty;
            }
            else if (item is INamespace)
            {
                canRename = true;
            }
            else
            {
                canRename = false;
            }
            if (canRename)
            {
                ciset.CommandInfos.Add(IdeApp.CommandService.GetCommandInfo(MonoDevelop.Ide.Commands.EditCommands.Rename), new Action(delegate {
                    new MonoDevelop.Refactoring.Rename.RenameHandler().Start(null);
                }));
                added = true;
            }

            foreach (var refactoring in RefactoringService.Refactorings)
            {
                if (refactoring.IsValid(options))
                {
                    CommandInfo info = new CommandInfo(refactoring.GetMenuDescription(options));
                    info.AccelKey = refactoring.AccelKey;
                    ciset.CommandInfos.Add(info, new Action(new RefactoringOperationWrapper(refactoring, options).Operation));
                }
            }
            var refactoringInfo = doc.Annotation <RefactoringDocumentInfo> ();

            if (refactoringInfo == null)
            {
                refactoringInfo = new RefactoringDocumentInfo();
                doc.AddAnnotation(refactoringInfo);
            }
            var  loc   = doc.Editor.Caret.Location;
            bool first = true;

            if (refactoringInfo.lastDocument != doc.ParsedDocument || loc != lastLocation)
            {
                if (QuickTaskStrip.EnableFancyFeatures)
                {
                    var ext = doc.GetContent <CodeActionEditorExtension> ();
                    refactoringInfo.validActions = ext != null?ext.GetCurrentFixes() : null;
                }
                else
                {
                    refactoringInfo.validActions = RefactoringService.GetValidActions(doc, loc).Result;
                }

                lastLocation = loc;
                refactoringInfo.lastDocument = doc.ParsedDocument;
            }
            if (refactoringInfo.validActions != null && refactoringInfo.lastDocument != null && refactoringInfo.lastDocument.CreateRefactoringContext != null)
            {
                var context = refactoringInfo.lastDocument.CreateRefactoringContext(doc, CancellationToken.None);

                foreach (var fix_ in refactoringInfo.validActions.OrderByDescending(i => Tuple.Create(CodeActionEditorExtension.IsAnalysisOrErrorFix(i), (int)i.Severity, CodeActionEditorExtension.GetUsage(i.IdString))))
                {
                    if (CodeActionEditorExtension.IsAnalysisOrErrorFix(fix_))
                    {
                        continue;
                    }
                    var fix = fix_;
                    if (first)
                    {
                        first = false;
                        if (ciset.CommandInfos.Count > 0)
                        {
                            ciset.CommandInfos.AddSeparator();
                        }
                    }

                    ciset.CommandInfos.Add(fix.Title, new Action(() => RefactoringService.ApplyFix(fix, context)));
                }
            }

            if (ciset.CommandInfos.Count > 0)
            {
                ainfo.Add(ciset, null);
                added = true;
            }

            if (IdeApp.ProjectOperations.CanJumpToDeclaration(item))
            {
                var type = item as IType;
                if (type != null && type.GetDefinition().Parts.Count > 1)
                {
                    var declSet = new CommandInfoSet();
                    declSet.Text = GettextCatalog.GetString("_Go to Declaration");
                    var ct = type.GetDefinition();
                    foreach (var part in ct.Parts)
                    {
                        declSet.CommandInfos.Add(string.Format(GettextCatalog.GetString("{0}, Line {1}"), FormatFileName(part.Region.FileName), part.Region.BeginLine), new System.Action(new JumpTo(part).Run));
                    }
                    ainfo.Add(declSet);
                }
                else
                {
                    ainfo.Add(IdeApp.CommandService.GetCommandInfo(RefactoryCommands.GotoDeclaration), new System.Action(new JumpTo(item).Run));
                }
                added = true;
            }

            if (item is IMember)
            {
                var member = (IMember)item;
                if (member.IsOverride || member.ImplementedInterfaceMembers.Any())
                {
                    ainfo.Add(GettextCatalog.GetString("Go to _Base Symbol"), new System.Action(new GotoBase(member).Run));
                    added = true;
                }
            }

            if (!(item is IMethod && ((IMethod)item).SymbolKind == SymbolKind.Operator) && (item is IEntity || item is ITypeParameter || item is IVariable || item is INamespace))
            {
                ainfo.Add(IdeApp.CommandService.GetCommandInfo(RefactoryCommands.FindReferences), new System.Action(new FindRefs(item, false).Run));
                if (doc.HasProject && HasOverloads(doc.Project.ParentSolution, item))
                {
                    ainfo.Add(IdeApp.CommandService.GetCommandInfo(RefactoryCommands.FindAllReferences), new System.Action(new FindRefs(item, true).Run));
                }
                added = true;
            }

            if (item is IMember)
            {
                var member = (IMember)item;
                if (member.IsVirtual || member.IsAbstract || member.DeclaringType.Kind == TypeKind.Interface)
                {
                    var handler = new FindDerivedSymbolsHandler(doc, member);
                    if (handler.IsValid)
                    {
                        ainfo.Add(GettextCatalog.GetString("Find Derived Symbols"), new System.Action(handler.Run));
                        added = true;
                    }
                }
            }
            if (item is IMember)
            {
                var member = (IMember)item;
                if (member.SymbolKind == SymbolKind.Method || member.SymbolKind == SymbolKind.Indexer)
                {
                    var findMemberOverloadsHandler = new FindMemberOverloadsHandler(doc, member);
                    if (findMemberOverloadsHandler.IsValid)
                    {
                        ainfo.Add(GettextCatalog.GetString("Find Member Overloads"), new System.Action(findMemberOverloadsHandler.Run));
                        added = true;
                    }
                }
            }

            if (item is ITypeDefinition)
            {
                ITypeDefinition cls = (ITypeDefinition)item;
                foreach (var bc in cls.DirectBaseTypes)
                {
                    if (bc != null && bc.GetDefinition() != null && bc.GetDefinition().Kind != TypeKind.Interface /* TODO: && IdeApp.ProjectOperations.CanJumpToDeclaration (bc)*/)
                    {
                        ainfo.Add(GettextCatalog.GetString("Go to _Base"), new System.Action(new GotoBase((ITypeDefinition)item).Run));
                        break;
                    }
                }
                if ((cls.Kind == TypeKind.Class && !cls.IsSealed) || cls.Kind == TypeKind.Interface)
                {
                    ainfo.Add(cls.Kind != TypeKind.Interface ? GettextCatalog.GetString("Find _derived classes") : GettextCatalog.GetString("Find _implementor classes"), new System.Action(new FindDerivedClasses(cls).Run));
                }
                ainfo.Add(GettextCatalog.GetString("Find Extension Methods"), new System.Action(new FindExtensionMethodHandler(doc, cls).Run));
                added = true;
            }

            if (added)
            {
                ainfo.AddSeparator();
            }
        }