public override void Run() { RefactoringMenuContext context = (RefactoringMenuContext)Owner; LocalResolveResult local = (LocalResolveResult)context.ResolveResult; FindReferencesAndRenameHelper.RunFindReferences(local); }
void AddTopLevelItems(List <object> resultItems, ITextEditor textEditor, ExpressionResult expressionResult, List <string> definitions, bool addAsSubmenu) { // Insert items at this position to get the outermost expression first, followed by the inner expressions (if any). int insertIndex = resultItems.Count; ResolveResult rr = ResolveExpressionAtCaret(textEditor, expressionResult); MenuItem item = null; if (rr is MethodGroupResolveResult) { item = MakeItem(definitions, ((MethodGroupResolveResult)rr).GetMethodIfSingleOverload()); } else if (rr is MemberResolveResult) { MemberResolveResult mrr = (MemberResolveResult)rr; item = MakeItem(definitions, mrr.ResolvedMember); // Seems not to be needed, as AddItemForCurrentClassAndMethod works for indexer as well (martinkonicek) /*if (RefactoringService.FixIndexerExpression(expressionFinder, ref expressionResult, mrr)) { * if (item != null) { * // Insert this member * resultItems.Insert(insertIndex, item); * } * // Include menu for the underlying expression of the * // indexer expression as well. * AddTopLevelItems(textEditor, expressionResult, true); * }*/ } else if (rr is TypeResolveResult) { item = MakeItem(definitions, ((TypeResolveResult)rr).ResolvedClass); } else if (rr is LocalResolveResult) { bool isDefinition = textEditor.Caret.Line == ((LocalResolveResult)rr).VariableDefinitionRegion.BeginLine; ParseInformation pi = ParserService.GetParseInformation(textEditor.FileName); IProjectContent pc = null; if (pi != null) { pc = pi.CompilationUnit.ProjectContent; } RefactoringMenuContext context = new RefactoringMenuContext(textEditor, expressionResult, rr, isDefinition, pc, pi.CompilationUnit); item = MakeItem((LocalResolveResult)rr, context); insertIndex = 0; // Insert local variable menu item at the topmost position. } if (item != null) { if (addAsSubmenu) { resultItems.Insert(insertIndex, item); } else { foreach (object subItem in item.Items) { resultItems.Add(subItem); } item.Items.Clear(); // detach resultItems from old parent } } }
void AddCheckForNull(RefactoringMenuContext context) { string name = ((LocalResolveResult)context.ResolveResult).VariableName; AddCheck(context, "if (" + name + " == null)\n" + "throw new ArgumentNullException(\"" + name + "\");"); }
public override void Run() { RefactoringMenuContext context = (RefactoringMenuContext)Owner; LocalResolveResult local = (LocalResolveResult)context.ResolveResult; FindReferencesAndRenameHelper.ShowAsSearchResults( StringParser.Parse("${res:SharpDevelop.Refactoring.ReferencesTo}", new string[, ] { { "Name", local.VariableName } }), RefactoringService.FindReferences(local, null) ); }
ToolStripMenuItem MakeItem(LocalResolveResult local, RefactoringMenuContext context) { Debug.Assert(local == context.ResolveResult); ToolStripMenuItem item = MakeItemInternal(local.VariableName, local.IsParameter ? ClassBrowserIconService.ParameterIndex : ClassBrowserIconService.LocalVariableIndex, local.CallingClass.CompilationUnit, context.IsDefinition ? DomRegion.Empty : local.VariableDefinitionRegion); string treePath = "/SharpDevelop/ViewContent/DefaultTextEditor/Refactoring/"; treePath += local.IsParameter ? "Parameter" : "LocalVariable"; if (context.IsDefinition) { treePath += "Definition"; } MenuService.AddItemsToMenu(item.DropDown.Items, context, treePath); return(item); }
MenuItem MakeItem(LocalResolveResult local, RefactoringMenuContext context) { Debug.Assert(local == context.ResolveResult); MenuItem item = MakeItemWithGoToDefinition(local.VariableName, local.IsParameter ? ClassBrowserIconService.Parameter : ClassBrowserIconService.LocalVariable, local.CallingClass.CompilationUnit, context.IsDefinition ? DomRegion.Empty : local.VariableDefinitionRegion); string treePath = "/SharpDevelop/ViewContent/DefaultTextEditor/Refactoring/"; treePath += local.IsParameter ? "Parameter" : "LocalVariable"; if (context.IsDefinition) { treePath += "Definition"; } foreach (object obj in MenuService.CreateMenuItems(null, context, treePath)) { item.Items.Add(obj); } return(item); }
public MenuItem Create(RefactoringMenuContext context) { if (context.ExpressionResult.Context == ExpressionContext.Attribute) return null; if (!(context.ResolveResult is UnknownMethodResolveResult)) return null; if (context.ProjectContent == null) return null; UnknownMethodResolveResult rr = context.ResolveResult as UnknownMethodResolveResult; MenuItem item = new MenuItem() { Header = string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.ResolveExtensionMethod}"), rr.CallName), Icon = ClassBrowserIconService.GotoArrow.CreateImage() }; List<IClass> results = new List<IClass>(); SearchAllExtensionMethodsWithName(results, context.ProjectContent, rr.CallName); foreach (IProjectContent content in context.ProjectContent.ThreadSafeGetReferencedContents()) SearchAllExtensionMethodsWithName(results, content, rr.CallName); if (!results.Any()) return null; foreach (IClass c in results) { string newNamespace = c.Namespace; MenuItem subItem = new MenuItem(); subItem.Header = "using " + newNamespace; subItem.Icon = ClassBrowserIconService.Namespace.CreateImage(); item.Items.Add(subItem); subItem.Click += delegate { NamespaceRefactoringService.AddUsingDeclaration(context.CompilationUnit, context.Editor.Document, newNamespace, true); ParserService.BeginParse(context.Editor.FileName, context.Editor.Document); }; } return item; }
void AddCheck(RefactoringMenuContext context, string newCode) { var codeGen = context.ResolveResult.CallingClass.ProjectContent.Language.CodeGenerator; TextArea textArea = context.TextArea; IMember m = context.ResolveResult.CallingMember; TextLocation methodStart = FindMethodStart(textArea.Document, m.BodyRegion); if (methodStart.IsEmpty) return; textArea.Caret.Position = methodStart; textArea.SelectionManager.ClearSelection(); textArea.Document.UndoStack.StartUndoGroup(); try { foreach (string newCodeLine in newCode.Split('\n')) { new Return().Execute(textArea); textArea.InsertString(newCodeLine); } } finally { textArea.Document.UndoStack.EndUndoGroup(); } textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); textArea.Document.CommitUpdate(); }
public override void Run() { RefactoringMenuContext context = (RefactoringMenuContext)Owner; Run((LocalResolveResult)context.ResolveResult); }
public MenuItem Create(RefactoringMenuContext context) { // TODO : [Test] above method is in Default context? // if (context.ExpressionResult.Context != ExpressionContext.Attribute) // return null; if (!(context.ResolveResult is UnknownIdentifierResolveResult || context.ResolveResult is UnknownMethodResolveResult)) return null; List<IClass> results = new List<IClass>(); ParseInformation info = ParserService.GetParseInformation(context.Editor.FileName); if (info == null || info.CompilationUnit == null || info.CompilationUnit.ProjectContent == null) return null; ICompilationUnit unit = info.CompilationUnit; IProjectContent pc = info.CompilationUnit.ProjectContent; string name = null; if (context.ResolveResult is UnknownMethodResolveResult) { var rr = context.ResolveResult as UnknownMethodResolveResult; SearchAttributesWithName(results, pc, rr.CallName); foreach (IProjectContent content in pc.ThreadSafeGetReferencedContents()) SearchAttributesWithName(results, content, rr.CallName); name = rr.CallName; } if (context.ResolveResult is UnknownIdentifierResolveResult) { var rr = context.ResolveResult as UnknownIdentifierResolveResult; SearchAttributesWithName(results, pc, rr.Identifier); foreach (IProjectContent content in pc.ThreadSafeGetReferencedContents()) SearchAttributesWithName(results, content, rr.Identifier); name = rr.Identifier; } if (!results.Any()) return null; MenuItem item = new MenuItem() { Header = string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.ResolveAttribute}"), name), Icon = ClassBrowserIconService.GotoArrow.CreateImage() }; foreach (IClass c in results) { string newNamespace = c.Namespace; MenuItem subItem = new MenuItem(); subItem.Header = "using " + newNamespace; subItem.Icon = ClassBrowserIconService.Namespace.CreateImage(); item.Items.Add(subItem); subItem.Click += delegate { NamespaceRefactoringService.AddUsingDeclaration(unit, context.Editor.Document, newNamespace, true); ParserService.BeginParse(context.Editor.FileName, context.Editor.Document); }; } return item; }
void AddTopLevelItems(List<object> resultItems, ITextEditor textEditor, ExpressionResult expressionResult, List<string> definitions, bool addAsSubmenu) { // Insert items at this position to get the outermost expression first, followed by the inner expressions (if any). int insertIndex = resultItems.Count; ResolveResult rr = ResolveExpressionAtCaret(textEditor, expressionResult); MenuItem item = null; if (rr is MethodGroupResolveResult) { item = MakeItem(definitions, ((MethodGroupResolveResult)rr).GetMethodIfSingleOverload()); } else if (rr is MemberResolveResult) { MemberResolveResult mrr = (MemberResolveResult)rr; item = MakeItem(definitions, mrr.ResolvedMember); // Seems not to be needed, as AddItemForCurrentClassAndMethod works for indexer as well (martinkonicek) /*if (RefactoringService.FixIndexerExpression(expressionFinder, ref expressionResult, mrr)) { if (item != null) { // Insert this member resultItems.Insert(insertIndex, item); } // Include menu for the underlying expression of the // indexer expression as well. AddTopLevelItems(textEditor, expressionResult, true); }*/ } else if (rr is TypeResolveResult) { item = MakeItem(definitions, ((TypeResolveResult)rr).ResolvedClass); } else if (rr is LocalResolveResult) { bool isDefinition = textEditor.Caret.Line == ((LocalResolveResult)rr).VariableDefinitionRegion.BeginLine; ParseInformation pi = ParserService.GetParseInformation(textEditor.FileName); IProjectContent pc = null; if (pi != null) pc = pi.CompilationUnit.ProjectContent; RefactoringMenuContext context = new RefactoringMenuContext(textEditor, expressionResult, rr, isDefinition, pc, pi.CompilationUnit); item = MakeItem((LocalResolveResult)rr, context); insertIndex = 0; // Insert local variable menu item at the topmost position. } if (item != null) { if (addAsSubmenu) { resultItems.Insert(insertIndex, item); } else { foreach (object subItem in item.Items) { resultItems.Add(subItem); } } } }
MenuItem MakeItem(LocalResolveResult local, RefactoringMenuContext context) { Debug.Assert(local == context.ResolveResult); MenuItem item = MakeItemWithGoToDefinition(local.VariableName, local.IsParameter ? ClassBrowserIconService.Parameter : ClassBrowserIconService.LocalVariable, local.CallingClass.CompilationUnit, context.IsDefinition ? DomRegion.Empty : local.VariableDefinitionRegion); string treePath = "/SharpDevelop/ViewContent/DefaultTextEditor/Refactoring/"; treePath += local.IsParameter ? "Parameter" : "LocalVariable"; if (context.IsDefinition) treePath += "Definition"; foreach (object obj in MenuService.CreateMenuItems(null, context, treePath)) item.Items.Add(obj); return item; }
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) // we need to use .ToArray() because the bookmarks might change during enumeration: // building member/class submenus can cause reparsing the current file, which might change // the available bookmarks foreach (Bookmark mark in doc.BookmarkManager.Marks.ToArray()) { 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(ClassNode.GetText(type), 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); RefactoringMenuContext context = new RefactoringMenuContext { TextArea = textArea, ResolveResult = rr, ExpressionResult = expressionResult }; item = null; if (rr is MethodGroupResolveResult) { item = MakeItem(definitions, ((MethodGroupResolveResult)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) { context.IsDefinition = caretLine + 1 == ((LocalResolveResult)rr).VariableDefinitionRegion.BeginLine; item = MakeItem((LocalResolveResult)rr, context); insertIndex = 0; // Insert local variable menu item at the topmost position. } else if (rr is UnknownIdentifierResolveResult) { item = MakeItemForResolveError((UnknownIdentifierResolveResult)rr, expressionResult.Context, textArea); insertIndex = 0; // Insert menu item at the topmost position. } else if (rr is UnknownConstructorCallResolveResult) { item = MakeItemForResolveError((UnknownConstructorCallResolveResult)rr, expressionResult.Context, textArea); insertIndex = 0; // Insert menu item at the topmost position. } if (item != null) { resultItems.Insert(insertIndex, item); } // Include menu for current class and method ICompilationUnit cu = null; IMember callingMember = null; if (rr != null && rr.CallingMember != null) { callingMember = rr.CallingMember; } else { ParseInformation parseInfo = ParserService.GetParseInformation(textEditorControl.FileName); if (parseInfo != null) { 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()); } }
void AddRangeCheck(RefactoringMenuContext context) { string name = ((LocalResolveResult)context.ResolveResult).VariableName; AddCheck(context, "if (" + name + " < 0 || " + name + " > upper_bound)\n" + "throw new ArgumentOutOfRangeException(\"" + name + "\", " + name + ", \"Value must be between 0 and \" + upper_bound);"); }
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) // we need to use .ToArray() because the bookmarks might change during enumeration: // building member/class submenus can cause reparsing the current file, which might change // the available bookmarks foreach (Bookmark mark in doc.BookmarkManager.Marks.ToArray()) { 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(ClassNode.GetText(type), 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); RefactoringMenuContext context = new RefactoringMenuContext { TextArea = textArea, ResolveResult = rr, ExpressionResult = expressionResult }; item = null; if (rr is MethodGroupResolveResult) { item = MakeItem(definitions, ((MethodGroupResolveResult)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) { context.IsDefinition = caretLine + 1 == ((LocalResolveResult)rr).VariableDefinitionRegion.BeginLine; item = MakeItem((LocalResolveResult)rr, context); insertIndex = 0; // Insert local variable menu item at the topmost position. } else if (rr is UnknownIdentifierResolveResult) { item = MakeItemForResolveError((UnknownIdentifierResolveResult)rr, expressionResult.Context, textArea); insertIndex = 0; // Insert menu item at the topmost position. } else if (rr is UnknownConstructorCallResolveResult) { item = MakeItemForResolveError((UnknownConstructorCallResolveResult)rr, expressionResult.Context, textArea); insertIndex = 0; // Insert menu item at the topmost position. } if (item != null) { resultItems.Insert(insertIndex, item); } // Include menu for current class and method ICompilationUnit cu = null; IMember callingMember = null; if (rr != null && rr.CallingMember != null) { callingMember = rr.CallingMember; } else { ParseInformation parseInfo = ParserService.GetParseInformation(textEditorControl.FileName); if (parseInfo != null) { 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(); } }
ToolStripMenuItem MakeItem(LocalResolveResult local, RefactoringMenuContext context) { Debug.Assert(local == context.ResolveResult); ToolStripMenuItem item = MakeItemInternal(local.VariableName, local.IsParameter ? ClassBrowserIconService.ParameterIndex : ClassBrowserIconService.LocalVariableIndex, local.CallingClass.CompilationUnit, context.IsDefinition ? DomRegion.Empty : local.VariableDefinitionRegion); string treePath = "/SharpDevelop/ViewContent/DefaultTextEditor/Refactoring/"; treePath += local.IsParameter ? "Parameter" : "LocalVariable"; if (context.IsDefinition) treePath += "Definition"; MenuService.AddItemsToMenu(item.DropDown.Items, context, treePath); return item; }