From 46060649fb481bbc725e9c67bd3abca48b54eaa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kon=C3=AD=C4=8Dek?= Date: Sun, 8 Aug 2010 22:54:11 +0000 Subject: [PATCH] Moved "Add check for null" and "And range check" from context menu to context actions. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@6389 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/SharpRefactoring.addin | 6 +- .../Project/SharpRefactoring.csproj | 4 +- .../Project/Src/ContextActions/Extensions.cs | 37 ++++++ .../Project/Src/ContextActions/ParamCheck.cs | 49 ++++++++ .../Src/ContextActions/ParamCheckForNull.cs | 34 ++++++ .../Src/ContextActions/ParamRangeCheck.cs | 35 ++++++ .../ParameterCheckRefactoringMenuBuilder.cs | 115 ------------------ 7 files changed, 160 insertions(+), 120 deletions(-) create mode 100644 src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamCheck.cs create mode 100644 src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamCheckForNull.cs create mode 100644 src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamRangeCheck.cs delete mode 100644 src/AddIns/Misc/SharpRefactoring/Project/Src/MenuItemFactories/ParameterCheckRefactoringMenuBuilder.cs diff --git a/src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.addin b/src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.addin index 75e3e4f3b4..69583ca030 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.addin +++ b/src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.addin @@ -11,10 +11,6 @@ - - - - + + diff --git a/src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.csproj b/src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.csproj index 731feb0200..93f1bfc928 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.csproj +++ b/src/AddIns/Misc/SharpRefactoring/Project/SharpRefactoring.csproj @@ -80,6 +80,9 @@ + + + @@ -111,7 +114,6 @@ - diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/Extensions.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/Extensions.cs index f7e9a026de..4c1d3c96ff 100644 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/Extensions.cs +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/Extensions.cs @@ -86,5 +86,42 @@ namespace SharpRefactoring.ContextActions int lineEndOffset = line.Offset + line.Length; document.Remove(offset, lineEndOffset - offset); } + + /// + /// C# only. + /// + public static void AddCodeToMethodStart(IMember m, ITextEditor textArea, string newCode) + { + int methodStart = FindMethodStartOffset(textArea.Document, m.BodyRegion); + if (methodStart < 0) + return; + textArea.Select(methodStart, 0); + using (textArea.Document.OpenUndoGroup()) { + int startLine = textArea.Caret.Line; + foreach (string newCodeLine in newCode.Split('\n')) { + textArea.Document.Insert(textArea.Caret.Offset, + DocumentUtilitites.GetLineTerminator(textArea.Document, textArea.Caret.Line) + newCodeLine); + } + int endLine = textArea.Caret.Line; + textArea.Language.FormattingStrategy.IndentLines(textArea, startLine, endLine); + } + } + + /// + /// C# only. + /// + public static int FindMethodStartOffset(IDocument document, DomRegion bodyRegion) + { + if (bodyRegion.IsEmpty) + return -1; + int offset = document.PositionToOffset(bodyRegion.BeginLine, bodyRegion.BeginColumn); + while (offset < document.TextLength) { + if (document.GetCharAt(offset) == '{') { + return offset + 1; + } + offset++; + } + return -1; + } } } diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamCheck.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamCheck.cs new file mode 100644 index 0000000000..d6469b9712 --- /dev/null +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamCheck.cs @@ -0,0 +1,49 @@ +// +// +// +// +// $Revision: $ +// +using System; +using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.SharpDevelop.Refactoring; + +namespace SharpRefactoring.ContextActions +{ + /// + /// Description of ParamCheck. + /// + public abstract class ParamCheck : ContextAction + { + public LocalResolveResult GetParameterAtCaret(ResolveResult symbol) + { + LocalResolveResult param = symbol as LocalResolveResult; + if (param == null || param.CallingClass == null || param.ResolvedType == null) + return null; + if (param.CallingClass.ProjectContent.Language != LanguageProperties.CSharp) + return null; + if (!param.IsParameter) + return null; + return param; + } + + public override bool IsAvailable(EditorContext context) + { + var paramAtCaret = GetParameterAtCaret(context.CurrentSymbol); + if (paramAtCaret == null) + return false; + + return IsAvailable(paramAtCaret.ResolvedType); + } + + public override void Execute(EditorContext context) + { + var paramAtCaret = GetParameterAtCaret(context.CurrentSymbol); + Extensions.AddCodeToMethodStart(paramAtCaret.CallingMember, context.Editor, GetCodeToInsert(paramAtCaret.VariableName)); + } + + public abstract bool IsAvailable(IReturnType parameterType); + + public abstract string GetCodeToInsert(string parameterName); + } +} diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamCheckForNull.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamCheckForNull.cs new file mode 100644 index 0000000000..148713ef6c --- /dev/null +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamCheckForNull.cs @@ -0,0 +1,34 @@ +// +// +// +// +// $Revision: $ +// +using System; +using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.SharpDevelop.Refactoring; + +namespace SharpRefactoring.ContextActions +{ + /// + /// Description of ParamCheckForNull. + /// + public class ParamCheckForNull : ParamCheck + { + public override string Title { + get { return "Add check for null"; } + } + + public override bool IsAvailable(IReturnType parameterType) + { + var parameterTypeClass = parameterType.GetUnderlyingClass(); + return (parameterTypeClass == null || parameterTypeClass.ClassType != ClassType.Enum && parameterTypeClass.ClassType != ClassType.Struct); + } + + public override string GetCodeToInsert(string parameterName) + { + return "if (" + parameterName + " == null)\n" + + "throw new ArgumentNullException(\"" + parameterName + "\");"; + } + } +} diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamRangeCheck.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamRangeCheck.cs new file mode 100644 index 0000000000..3f62977c32 --- /dev/null +++ b/src/AddIns/Misc/SharpRefactoring/Project/Src/ContextActions/ParamRangeCheck.cs @@ -0,0 +1,35 @@ +// +// +// +// +// $Revision: $ +// +using System; +using System.Runtime.Remoting.Contexts; +using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.SharpDevelop.Refactoring; + +namespace SharpRefactoring.ContextActions +{ + /// + /// Description of ParamRangeCheck. + /// + public class ParamRangeCheck : ParamCheck + { + public override string Title { + get { return "Add range check"; } + } + + public override bool IsAvailable(IReturnType parameterType) + { + IClass parameterTypeClass = parameterType.GetUnderlyingClass(); + return (parameterTypeClass != null && parameterTypeClass.FullyQualifiedName == "System.Int32"); + } + + public override string GetCodeToInsert(string parameterName) + { + return "if (" + parameterName + " < 0 || " + parameterName + " > upper_bound)\n" + + "throw new ArgumentOutOfRangeException(\"" + parameterName + "\", " + parameterName + ", \"Value must be between 0 and \" + upper_bound);"; + } + } +} diff --git a/src/AddIns/Misc/SharpRefactoring/Project/Src/MenuItemFactories/ParameterCheckRefactoringMenuBuilder.cs b/src/AddIns/Misc/SharpRefactoring/Project/Src/MenuItemFactories/ParameterCheckRefactoringMenuBuilder.cs deleted file mode 100644 index d7d59382bf..0000000000 --- a/src/AddIns/Misc/SharpRefactoring/Project/Src/MenuItemFactories/ParameterCheckRefactoringMenuBuilder.cs +++ /dev/null @@ -1,115 +0,0 @@ -// -// -// -// -// $Revision$ -// - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Windows.Forms; - -using ICSharpCode.Core; -using ICSharpCode.Core.Presentation; -using ICSharpCode.Core.WinForms; -using ICSharpCode.SharpDevelop; -using ICSharpCode.SharpDevelop.Dom; -using ICSharpCode.SharpDevelop.Editor; -using ICSharpCode.SharpDevelop.Refactoring; - -namespace SharpRefactoring -{ - /// - /// Provides "Add check for null" and "Add range check" commands in - /// context menu of parameter declarations. - /// - public class ParameterCheckRefactoringMenuBuilder : ISubmenuBuilder, IMenuItemBuilder - { - public ICollection BuildItems(Codon codon, object owner) - { - return BuildSubmenu(codon, owner).TranslateToWpf(); - } - - public ToolStripItem[] BuildSubmenu(Codon codon, object owner) - { - List resultItems = new List(); - RefactoringMenuContext context = (RefactoringMenuContext)owner; - LocalResolveResult lrr = context.ResolveResult as LocalResolveResult; - if (lrr == null || lrr.CallingClass == null || lrr.ResolvedType == null) - return resultItems.ToArray(); - LanguageProperties language = lrr.CallingClass.ProjectContent.Language; - if (language != LanguageProperties.CSharp) - return resultItems.ToArray(); - - IClass parameterTypeClass = lrr.ResolvedType.GetUnderlyingClass(); - if (parameterTypeClass == null || parameterTypeClass.ClassType != ClassType.Enum && parameterTypeClass.ClassType != ClassType.Struct) { - // the parameter is a reference type - resultItems.Add(MakeItem("Add check for null", delegate { AddCheckForNull(context); })); - } - if (parameterTypeClass != null) { - if (parameterTypeClass.FullyQualifiedName == "System.Int32") { - resultItems.Add(MakeItem("Add range check", delegate { AddRangeCheck(context); })); - } - } - return resultItems.ToArray(); - } - - ToolStripMenuItem MakeItem(string title, EventHandler onClick) - { - ToolStripMenuItem menuItem = new ToolStripMenuItem(StringParser.Parse(title)); - menuItem.Click += onClick; - return menuItem; - } - - void AddCheck(RefactoringMenuContext context, string newCode) - { - var codeGen = context.ResolveResult.CallingClass.ProjectContent.Language.CodeGenerator; - ITextEditor textArea = context.Editor; - IMember m = context.ResolveResult.CallingMember; - int methodStart = FindMethodStartOffset(textArea.Document, m.BodyRegion); - if (methodStart < 0) - return; - textArea.Select(methodStart, 0); - using (textArea.Document.OpenUndoGroup()) { - int startLine = textArea.Caret.Line; - foreach (string newCodeLine in newCode.Split('\n')) { - textArea.Document.Insert(textArea.Caret.Offset, - DocumentUtilitites.GetLineTerminator(textArea.Document, textArea.Caret.Line) + newCodeLine); - } - int endLine = textArea.Caret.Line; - textArea.Language.FormattingStrategy.IndentLines(textArea, startLine, endLine); - } - } - - void AddCheckForNull(RefactoringMenuContext context) - { - string name = ((LocalResolveResult)context.ResolveResult).VariableName; - AddCheck(context, - "if (" + name + " == null)\n" + - "throw new ArgumentNullException(\"" + name + "\");"); - } - - 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);"); - } - - static int FindMethodStartOffset(IDocument document, DomRegion bodyRegion) - { - if (bodyRegion.IsEmpty) - return -1; - int offset = document.PositionToOffset(bodyRegion.BeginLine, bodyRegion.BeginColumn); - while (offset < document.TextLength) { - if (document.GetCharAt(offset) == '{') { - return offset + 1; - } - offset++; - } - return -1; - } - } -}