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 loc = doc.Editor.Caret.Location; bool first = true; if (lastDocument != doc.ParsedDocument || loc != lastLocation) { if (QuickTaskStrip.EnableFancyFeatures) { var ext = doc.GetContent <CodeActionEditorExtension> (); validActions = ext != null ? ext.GetCurrentFixes () : null; } else { validActions = RefactoringService.GetValidActions (doc, loc).Result; } lastLocation = loc; lastDocument = doc.ParsedDocument; } if (validActions != null) { foreach (var fix_ in validActions) { var fix = fix_; if (first) { first = false; if (ciset.CommandInfos.Count > 0) ciset.CommandInfos.AddSeparator (); } ciset.CommandInfos.Add (fix.Title, new Action (() => fix.Run (doc, loc))); } } 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 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 IMethod) { IMethod method = item as IMethod; if (method.IsOverride) { ainfo.Add (GettextCatalog.GetString ("Go to _base"), new System.Action (new GotoBase ((IMethod)item).Run)); added = true; } } else 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)); } } if (added) ainfo.AddSeparator (); }
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 { canRename = false; } if (canRename) { ciset.CommandInfos.Add (IdeApp.CommandService.GetCommandInfo (MonoDevelop.Ide.Commands.EditCommands.Rename), new System.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 System.Action (new RefactoringOperationWrapper (refactoring, options).Operation)); } } var loc = doc.Editor.Caret.Location; bool first = true; if (lastDocument != doc.ParsedDocument || loc != lastLocation) { validActions = RefactoringService.GetValidActions (doc, loc).Result; lastLocation = loc; lastDocument = doc.ParsedDocument; } foreach (var fix_ in validActions) { var fix = fix_; if (first) { first = false; if (ciset.CommandInfos.Count > 0) ciset.CommandInfos.AddSeparator (); } ciset.CommandInfos.Add (fix.Title, new System.Action (() => fix.Run (doc, loc))); } if (ciset.CommandInfos.Count > 0) { ainfo.Add (ciset, null); added = true; } if (IdeApp.ProjectOperations.CanJumpToDeclaration (item)) { var type = item as ICSharpCode.NRefactory.TypeSystem.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 IEntity || item is ITypeParameter || item is IVariable) { ainfo.Add (IdeApp.CommandService.GetCommandInfo (RefactoryCommands.FindReferences), new System.Action (new FindRefs (item, false).Run)); if (doc.HasProject && ReferenceFinder.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 IMethod) { IMethod method = item as IMethod; if (method.IsOverride) { ainfo.Add (GettextCatalog.GetString ("Go to _base"), new System.Action (new GotoBase ((IMethod)item).Run)); added = true; } } else 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)); } // if (baseConstructor != null) { // Refactorer refactorer2 = new Refactorer (ctx, pinfo, baseConstructor.DeclaringType, baseConstructor, null); // ainfo.Add (GettextCatalog.GetString ("Go to _base"), new RefactoryOperation (refactorer2.GoToBase)); // } } if (resolveResult != null) { // List<string> namespaces = QuickFixHandler.GetResolveableNamespaces (options, out resolveDirect); // // if (item == null || namespaces.Count > 1) { // if (item == null) { // foreach (string ns in namespaces) { // // remove used namespaces for conflict resolving. // if (options.Document.CompilationUnit.IsNamespaceUsedAt (ns, options.ResolveResult.ResolvedExpression.Region.Start)) // continue; // CommandInfo info = resolveMenu.CommandInfos.Add ("using " + ns + ";", new RefactoryOperation (new ResolveNameOperation (ctx, doc, resolveResult, ns).AddImport)); // info.Icon = MonoDevelop.Ide.Gui.Stock.AddNamespace; // } // // remove all unused namespaces (for resolving conflicts) // namespaces.RemoveAll (ns => !doc.CompilationUnit.IsNamespaceUsedAt (ns, resolveResult.ResolvedExpression.Region.Start)); // } // // if (namespaces.Count > (item == null ? 0 : 1)) // ainfo.Add (resolveMenu, null); // } } // // if (cls.GetSourceProject () != null && includeModifyCommands && ((cls.ClassType == ClassType.Class) || (cls.ClassType == ClassType.Struct))) { // ciset.CommandInfos.Add (GettextCatalog.GetString ("_Encapsulate Fields..."), new RefactoryOperation (refactorer.EncapsulateField)); // ciset.CommandInfos.Add (GettextCatalog.GetString ("Override/Implement members..."), new RefactoryOperation (refactorer.OverrideOrImplementMembers)); // } // // ainfo.Add (IdeApp.CommandService.GetCommandInfo (RefactoryCommands.FindReferences), new RefactoryOperation (refactorer.FindReferences)); // // if (canRename && cls.ClassType == ClassType.Interface && eclass != null) { // // is now provided by the refactoring command infrastructure: //// ciset.CommandInfos.Add (GettextCatalog.GetString ("Implement Interface (explicit)"), new RefactoryOperation (refactorer.ImplementExplicitInterface)); //// ciset.CommandInfos.Add (GettextCatalog.GetString ("Implement Interface (implicit)"), new RefactoryOperation (refactorer.ImplementImplicitInterface)); // } else if (canRename && includeModifyCommands && cls.BaseType != null && cls.ClassType != ClassType.Interface && cls == eclass) { // // Class might have interfaces... offer to implement them // CommandInfoSet impset = new CommandInfoSet (); // CommandInfoSet expset = new CommandInfoSet (); // CommandInfoSet abstactset = new CommandInfoSet (); // bool ifaceAdded = false; // bool abstractAdded = false; // // foreach (IReturnType rt in cls.BaseTypes) { // IType iface = ctx.GetType (rt); // if (iface == null) // continue; // if (iface.ClassType == ClassType.Interface) { // Refactorer ifaceRefactorer = new Refactorer (ctx, pinfo, cls, iface, rt); // impset.CommandInfos.Add (ambience.GetString (rt, OutputFlags.IncludeGenerics), new RefactoryOperation (ifaceRefactorer.ImplementImplicitInterface)); // expset.CommandInfos.Add (ambience.GetString (rt, OutputFlags.IncludeGenerics), new RefactoryOperation (ifaceRefactorer.ImplementExplicitInterface)); // ifaceAdded = true; // } else if (ContainsAbstractMembers (iface)) { // Refactorer ifaceRefactorer = new Refactorer (ctx, pinfo, cls, iface, rt); // abstactset.CommandInfos.Add (ambience.GetString (rt, OutputFlags.IncludeGenerics), new RefactoryOperation (ifaceRefactorer.ImplementAbstractMembers)); // abstractAdded = true; // } // } // // if (ifaceAdded) { // impset.Text = GettextCatalog.GetString ("Implement Interface (implicit)"); // ciset.CommandInfos.Add (impset, null); // // expset.Text = GettextCatalog.GetString ("Implement Interface (explicit)"); // ciset.CommandInfos.Add (expset, null); // } // if (abstractAdded) { // abstactset.Text = GettextCatalog.GetString ("Implement abstract members"); // ciset.CommandInfos.Add (abstactset, null); // } // } // } // IMember eitem = resolveResult != null ? (resolveResult.CallingMember ?? resolveResult.CallingType) : null; // // string itemName = null; // if (item is IMember) // itemName = ((IMember)item).Name; // // if (item != null && eitem != null && (eitem.Equals (item) || (eitem.Name == itemName && !(eitem is IProperty) && !(eitem is IMethod)))) { // // If this occurs, then @item is either its own enclosing item, in // // which case, we don't want to show it twice, or it is the base-class // // version of @eitem, in which case we don't want to show the base-class // // @item, we'd rather show the item the user /actually/ requested, @eitem. // item = eitem; // eitem = null; // } // // IType eclass = null; // // if (item is IType) { // if (((IType)item).ClassType == ClassType.Interface) // eclass = FindEnclosingClass (ctx, editor.Name, line, column); else // eclass = (IType)item; // if (eitem is IMethod && ((IMethod)eitem).IsConstructor && eitem.DeclaringType.Equals (item)) { // item = eitem; // eitem = null; // } // } // // INode realItem = item; // if (item is InstantiatedType) // realItem = ((InstantiatedType)item).UninstantiatedType; // if (realItem is CompoundType) { // editor.GetLineColumnFromPosition (editor.CursorPosition, out line, out column); // ((CompoundType)realItem).SetMainPart (doc.FileName, line, column); // item = realItem; // } // // // // var unit = doc.CompilationUnit; // if (unit != null && unit.Usings != null && unit.Usings.Any (u => !u.IsFromNamespace && u.Region.Contains (line, column))) { // CommandInfoSet organizeUsingsMenu = new CommandInfoSet (); // organizeUsingsMenu.Text = GettextCatalog.GetString ("_Organize Usings"); // organizeUsingsMenu.CommandInfos.Add (IdeApp.CommandService.GetCommandInfo (RefactoryCommands.RemoveUnusedImports), new RefactoryOperation (delegate { // new RemoveUnusedImportsHandler ().Start (options); // })); // organizeUsingsMenu.CommandInfos.Add (IdeApp.CommandService.GetCommandInfo (MonoDevelop.Refactoring.RefactoryCommands.SortImports), new RefactoryOperation (delegate { // new SortImportsHandler ().Start (options); // })); // organizeUsingsMenu.CommandInfos.Add (IdeApp.CommandService.GetCommandInfo (MonoDevelop.Refactoring.RefactoryCommands.RemoveSortImports), new RefactoryOperation (delegate { // new RemoveSortImportsHandler ().Start (options); // })); // ainfo.Add (organizeUsingsMenu, null); // added = true; // } // // IUnresolvedFile pinfo = doc.CompilationUnit; // if (pinfo == null) // return; // // // Refactorer refactorer = new Refactorer (ctx, pinfo, eclass, realItem, null); // Ambience ambience = AmbienceService.GetAmbienceForFile (pinfo.FileName); // bool includeModifyCommands = this.IsModifiable (item); // // // // case: clicked on base in "constructor" - so pointing to the base constructor using argument count // // not 100% correct, but it's the fastest thing to do. // if (resolveResult is BaseResolveResult && eitem is IMethod && ((IMethod)eitem).IsConstructor) { // IType type = item as IType; // IMethod baseConstructor = null; // int idx1 = resolveResult.ResolvedExpression.Expression.IndexOf ('('); // int idx2 = resolveResult.ResolvedExpression.Expression.IndexOf (')'); // int paramCount = 0; // if (idx1 > 0 && idx2 > 0) { // if (idx2 - idx1 > 1) // paramCount++; // for (int i=idx1; i < idx2; i++) { // if (resolveResult.ResolvedExpression.Expression[i] == ',') // paramCount++; // } // } // foreach (IMethod m in type.Methods) { // if (m.IsConstructor && m.Parameters.Count == paramCount) // baseConstructor = m; // } // Refactorer refactorer2 = new Refactorer (ctx, pinfo, baseConstructor.DeclaringType, baseConstructor, null); // ainfo.Add (GettextCatalog.GetString ("Go to _base"), new RefactoryOperation (refactorer2.GoToBase)); // } // // else if (item is IField) { // if (includeModifyCommands) { // if (canRename) // ciset.CommandInfos.Add (GettextCatalog.GetString ("_Encapsulate Field..."), new RefactoryOperation (refactorer.EncapsulateField)); // } // } else if (added) ainfo.AddSeparator (); /* while (item != null) { CommandInfo ci; // case: clicked on base in "constructor" - so pointing to the base constructor using argument count // not 100% correct, but it's the fastest thing to do. if (resolveResult is BaseResolveResult && eitem is IMethod && ((IMethod)eitem).IsConstructor) { IType type = item as IType; IMethod baseConstructor = null; int idx1 = resolveResult.ResolvedExpression.Expression.IndexOf ('('); int idx2 = resolveResult.ResolvedExpression.Expression.IndexOf (')'); int paramCount = 0; if (idx1 > 0 && idx2 > 0) { if (idx2 - idx1 > 1) paramCount++; for (int i=idx1; i < idx2; i++) { if (resolveResult.ResolvedExpression.Expression[i] == ',') paramCount++; } } foreach (IMethod m in type.Methods) { if (m.IsConstructor && m.Parameters.Count == paramCount) baseConstructor = m; } if (baseConstructor != null && (ci = BuildRefactoryMenuForItem (ctx, doc.CompilationUnit, null, baseConstructor, true)) != null) { ainfo.Add (ci, null); added = true; } } // Add the selected item if ((ci = BuildRefactoryMenuForItem (ctx, doc.CompilationUnit, eclass, item, IsModifiable (item))) != null) { ainfo.Add (ci, null); added = true; } if (item is IParameter) { // Add the encompasing method for the previous item in the menu item = ((IParameter) item).DeclaringMember; if (item != null && (ci = BuildRefactoryMenuForItem (ctx, doc.CompilationUnit, null, item, true)) != null) { ainfo.Add (ci, null); added = true; } } if (item is IMember && !(eitem != null && eitem is IMember)) { // Add the encompasing class for the previous item in the menu item = ((IMember) item).DeclaringType; if (item != null && (ci = BuildRefactoryMenuForItem (ctx, doc.CompilationUnit, null, item, IsModifiable (item))) != null) { ainfo.Add (ci, null); added = true; } } item = eitem; eitem = null; eclass = null; } if (added) ainfo.AddSeparator ();*/ }
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 loc = doc.Editor.Caret.Location; bool first = true; if (lastDocument != doc.ParsedDocument || loc != lastLocation) { if (QuickTaskStrip.EnableFancyFeatures) { var ext = doc.GetContent <CodeActionEditorExtension> (); validActions = ext != null?ext.GetCurrentFixes() : null; } else { validActions = RefactoringService.GetValidActions(doc, loc).Result; } lastLocation = loc; lastDocument = doc.ParsedDocument; } if (validActions != null) { foreach (var fix_ in validActions) { var fix = fix_; if (first) { first = false; if (ciset.CommandInfos.Count > 0) { ciset.CommandInfos.AddSeparator(); } } ciset.CommandInfos.Add(fix.Title, new Action(() => fix.Run(doc, loc))); } } 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 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 IMethod) { IMethod method = item as IMethod; if (method.IsOverride) { ainfo.Add(GettextCatalog.GetString("Go to _base"), new System.Action(new GotoBase((IMethod)item).Run)); added = true; } } else 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)); } } if (added) { ainfo.AddSeparator(); } }