From e1e7877a4918c9716885bdb8c9ba7cfb311bec01 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 27 Dec 2005 20:31:55 +0000 Subject: [PATCH] Fixed BOO-628 - Alt+Ins code generation only works for C# and VB. Alt+Ins code generators now use ICSharpCode.SharpDevelop.Refactoring.CodeGenerator. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@946 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- AddIns/ICSharpCode.SharpDevelop.addin | 33 ++- .../Src/CodeCompletion/ConvertVisitor.cs | 4 +- .../Project/ConvertVisitorStatements.cs | 20 +- .../Test/StatementTests.cs | 18 ++ .../RefactoringService/CodeGenerator.cs | 86 ++++++- .../Commands/ClassMemberMenuBuilder.cs | 9 +- .../AbstractClassImplementorCodeGenerator.cs | 34 ++- .../AbstractFieldCodeGenerator.cs | 8 +- .../AbstractPropertyCodeGenerator.cs | 139 ---------- .../Commands/CodeGenerators/CodeGenerator.cs | 125 ++------- .../ConstructorCodeGenerator.cs | 77 ++---- .../CodeGenerators/EqualsCodeGenerator.cs | 141 +++++------ .../GetterAndSetterCodeGenerator.cs | 16 +- .../CodeGenerators/GetterCodeGenerator.cs | 16 +- .../InterfaceImplementorCodeGenerator.cs | 239 ++---------------- .../OnXXXMethodsCodeGenerator.cs | 50 +--- .../OverrideMethodsCodeGenerator.cs | 119 +++------ .../OverridePropertiesCodeGenerator.cs | 133 ++-------- .../CodeGenerators/SetterCodeGenerator.cs | 15 +- .../CodeGenerators/ToStringCodeGenerator.cs | 110 +++----- .../Commands/GenerateCodeCommand.cs | 39 ++- .../OverrideCompletionDataProvider.cs | 53 +--- .../ResourceService/ResourceService.cs | 21 +- 23 files changed, 433 insertions(+), 1072 deletions(-) diff --git a/AddIns/ICSharpCode.SharpDevelop.addin b/AddIns/ICSharpCode.SharpDevelop.addin index e424db17c9..414f6b31d2 100644 --- a/AddIns/ICSharpCode.SharpDevelop.addin +++ b/AddIns/ICSharpCode.SharpDevelop.addin @@ -421,9 +421,9 @@ class = "ICSharpCode.SharpDevelop.Project.Commands.RenameEntryEvent"/> + icon = "Icons.16x16.PropertiesIcon" + label = "${res:XML.MainMenu.FormatMenu.ShowProperties}" + class = "ICSharpCode.SharpDevelop.Project.Commands.ShowPropertiesForNode"/> @@ -1934,6 +1934,31 @@ + class = "ICSharpCode.SharpDevelop.Project.DefaultDotNetNodeBuilder"/> + + + + + + + + + + + + + + diff --git a/src/AddIns/BackendBindings/Boo/BooBinding/Project/Src/CodeCompletion/ConvertVisitor.cs b/src/AddIns/BackendBindings/Boo/BooBinding/Project/Src/CodeCompletion/ConvertVisitor.cs index 790e0fb7f6..bd40841eb9 100644 --- a/src/AddIns/BackendBindings/Boo/BooBinding/Project/Src/CodeCompletion/ConvertVisitor.cs +++ b/src/AddIns/BackendBindings/Boo/BooBinding/Project/Src/CodeCompletion/ConvertVisitor.cs @@ -61,9 +61,9 @@ namespace Grunwald.BooBinding.CodeCompletion if (m.IsStatic) r |= ModifierEnum.Static; if (m is AST.Field) { - if (m.IsFinal) r |= ModifierEnum.Readonly; + if (m.IsFinal) r |= ModifierEnum.Readonly; } else { - if (!m.IsFinal) r |= ModifierEnum.Virtual; + if (m.IsFinal) r |= ModifierEnum.Sealed; } if (m.IsAbstract) r |= ModifierEnum.Abstract; if (m.IsOverride) r |= ModifierEnum.Override; diff --git a/src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Project/ConvertVisitorStatements.cs b/src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Project/ConvertVisitorStatements.cs index 0b0b75e280..31dce363f0 100644 --- a/src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Project/ConvertVisitorStatements.cs +++ b/src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Project/ConvertVisitorStatements.cs @@ -278,18 +278,20 @@ namespace NRefactoryToBooConverter public object Visit(AddHandlerStatement addHandlerStatement, object data) { - return new B.BinaryExpression(GetLexicalInfo(addHandlerStatement), - B.BinaryOperatorType.InPlaceAddition, - ConvertExpression(addHandlerStatement.EventExpression), - ConvertExpression(addHandlerStatement.HandlerExpression)); + B.Expression expr = new B.BinaryExpression(GetLexicalInfo(addHandlerStatement), + B.BinaryOperatorType.InPlaceAddition, + ConvertExpression(addHandlerStatement.EventExpression), + ConvertExpression(addHandlerStatement.HandlerExpression)); + return new B.ExpressionStatement(expr); } public object Visit(RemoveHandlerStatement removeHandlerStatement, object data) { - return new B.BinaryExpression(GetLexicalInfo(removeHandlerStatement), - B.BinaryOperatorType.InPlaceSubtraction, - ConvertExpression(removeHandlerStatement.EventExpression), - ConvertExpression(removeHandlerStatement.HandlerExpression)); + B.Expression expr = new B.BinaryExpression(GetLexicalInfo(removeHandlerStatement), + B.BinaryOperatorType.InPlaceSubtraction, + ConvertExpression(removeHandlerStatement.EventExpression), + ConvertExpression(removeHandlerStatement.HandlerExpression)); + return new B.ExpressionStatement(expr); } public object Visit(RaiseEventStatement raiseEventStatement, object data) @@ -297,7 +299,7 @@ namespace NRefactoryToBooConverter B.MethodInvocationExpression mie = new B.MethodInvocationExpression(GetLexicalInfo(raiseEventStatement)); mie.Target = new B.ReferenceExpression(raiseEventStatement.EventName); ConvertExpressions(raiseEventStatement.Arguments, mie.Arguments); - return mie; + return new B.ExpressionStatement(mie); } public object Visit(EraseStatement eraseStatement, object data) diff --git a/src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Test/StatementTests.cs b/src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Test/StatementTests.cs index d49e8cdb36..f7a1a65086 100644 --- a/src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Test/StatementTests.cs +++ b/src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Test/StatementTests.cs @@ -258,5 +258,23 @@ namespace NRefactoryToBooConverter.Tests { TestStatementVB("Dim A As Integer()\nB = A(5)", "A as (System.Int32)\nB = A[5]"); } + + [Test] + public void VBRaiseEvent() + { + TestStatementVB("RaiseEvent Test(4)", "Test(4)"); + } + + [Test] + public void VBAddEventHandler() + { + TestStatementVB("AddHandler someObject.Test, AddressOf HandlerMethod", "someObject.Test += HandlerMethod"); + } + + [Test] + public void VBAddRemoveHandler() + { + TestStatementVB("RemoveHandler someObject.Test, AddressOf HandlerMethod", "someObject.Test -= HandlerMethod"); + } } } diff --git a/src/Main/Base/Project/Src/Services/RefactoringService/CodeGenerator.cs b/src/Main/Base/Project/Src/Services/RefactoringService/CodeGenerator.cs index 676b7bfadd..fe4d9e2eaf 100644 --- a/src/Main/Base/Project/Src/Services/RefactoringService/CodeGenerator.cs +++ b/src/Main/Base/Project/Src/Services/RefactoringService/CodeGenerator.cs @@ -273,7 +273,17 @@ namespace ICSharpCode.SharpDevelop.Refactoring return Char.ToUpper(fieldName[0]) + fieldName.Substring(1); } - public virtual void CreateProperty(IField field, IDocument document, bool createGetter, bool createSetter) + public virtual string GetParameterName(string fieldName) + { + if (fieldName.StartsWith("_") && fieldName.Length > 1) + return Char.ToLower(fieldName[1]) + fieldName.Substring(2); + else if (fieldName.StartsWith("m_") && fieldName.Length > 2) + return Char.ToLower(fieldName[2]) + fieldName.Substring(3); + else + return Char.ToLower(fieldName[0]) + fieldName.Substring(1); + } + + public virtual PropertyDeclaration CreateProperty(IField field, bool createGetter, bool createSetter) { string name = GetPropertyName(field.Name); PropertyDeclaration property = new PropertyDeclaration(name, @@ -293,8 +303,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring } property.Modifier = Modifier.Public | (property.Modifier & Modifier.Static); - - InsertCodeAfter(field, document, property); + return property; } #endregion @@ -319,7 +328,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring #endregion #region Generate OnEventMethod - public virtual void CreateOnEventMethod(IEvent e, IDocument document) + public virtual MethodDeclaration CreateOnEventMethod(IEvent e) { TypeReference type = ConvertType(e.ReturnType, new ClassFinder(e)); if (type.Type.EndsWith("Handler")) @@ -348,7 +357,7 @@ namespace ICSharpCode.SharpDevelop.Refactoring method.Body = new BlockStatement(); method.Body.AddChild(new RaiseEventStatement(e.Name, arguments)); - InsertCodeAfter(e, document, method); + return method; } #endregion @@ -361,12 +370,21 @@ namespace ICSharpCode.SharpDevelop.Refactoring return member.DeclaringType.FullyQualifiedName; } - public virtual void ImplementInterface(IReturnType interf, IDocument document, bool explicitImpl, ModifierEnum implModifier, IClass targetClass) + public void ImplementInterface(IReturnType interf, IDocument document, bool explicitImpl, ModifierEnum implModifier, IClass targetClass) + { + List nodes = new List(); + InsertCodeInClass(targetClass, document, nodes.ToArray()); + } + + /// + /// Adds the methods implementing the to the list + /// . + /// + public virtual void ImplementInterface(IList nodes, IReturnType interf, bool explicitImpl, ModifierEnum implModifier, IClass targetClass) { ClassFinder context = new ClassFinder(targetClass, targetClass.Region.BeginLine + 1, 0); TypeReference interfaceReference = ConvertType(interf, context); Modifier modifier = ConvertModifier(implModifier); - List nodes = new List(); List targetClassEvents = targetClass.DefaultReturnType.GetEvents(); foreach (IEvent e in interf.GetEvents()) { if (targetClassEvents.Find(delegate(IEvent te) { return e.Name == te.Name; }) == null) { @@ -410,7 +428,59 @@ namespace ICSharpCode.SharpDevelop.Refactoring } } } - InsertCodeInClass(targetClass, document, nodes.ToArray()); + } + #endregion + + #region Override member + public virtual AttributedNode GetOverridingMethod(IMember baseMember, ClassFinder targetContext) + { + AttributedNode node = ConvertMember(baseMember, targetContext); + node.Modifier &= ~(Modifier.Virtual | Modifier.Abstract); + node.Modifier |= Modifier.Override; + + MethodDeclaration method = node as MethodDeclaration; + if (method != null) { + method.Body.Children.Clear(); + if (method.TypeReference.SystemType == "System.Void") { + method.Body.AddChild(new StatementExpression(CreateForwardingMethodCall(method))); + } else { + method.Body.AddChild(new ReturnStatement(CreateForwardingMethodCall(method))); + } + } + PropertyDeclaration property = node as PropertyDeclaration; + if (property != null) { + Expression field = new FieldReferenceExpression(new BaseReferenceExpression(), + property.Name); + if (!property.GetRegion.Block.IsNull) { + property.GetRegion.Block.Children.Clear(); + property.GetRegion.Block.AddChild(new ReturnStatement(field)); + } + if (!property.SetRegion.Block.IsNull) { + property.SetRegion.Block.Children.Clear(); + Expression expr = new AssignmentExpression(field, + AssignmentOperatorType.Assign, + new IdentifierExpression("value")); + property.SetRegion.Block.AddChild(new StatementExpression(expr)); + } + } + return node; + } + + static InvocationExpression CreateForwardingMethodCall(MethodDeclaration method) + { + Expression methodName = new FieldReferenceExpression(new BaseReferenceExpression(), + method.Name); + InvocationExpression ie = new InvocationExpression(methodName, null); + foreach (ParameterDeclarationExpression param in method.Parameters) { + Expression expr = new IdentifierExpression(param.ParameterName); + if (param.ParamModifier == ParamModifier.Ref) { + expr = new DirectionExpression(FieldDirection.Ref, expr); + } else if (param.ParamModifier == ParamModifier.Out) { + expr = new DirectionExpression(FieldDirection.Out, expr); + } + ie.Arguments.Add(expr); + } + return ie; } #endregion } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/ClassMemberMenuBuilder.cs b/src/Main/Base/Project/Src/TextEditor/Commands/ClassMemberMenuBuilder.cs index 4b6805fb06..4273ee13d1 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/ClassMemberMenuBuilder.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/ClassMemberMenuBuilder.cs @@ -133,7 +133,9 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands IField member = (IField)item.Tag; TextEditorControl textEditor = FindReferencesAndRenameHelper.JumpBehindDefinition(member); - member.DeclaringType.ProjectContent.Language.CodeGenerator.CreateProperty(member, textEditor.Document, true, includeSetter); + CodeGenerator codeGen = member.DeclaringType.ProjectContent.Language.CodeGenerator; + codeGen.InsertCodeAfter(member, textEditor.Document, + codeGen.CreateProperty(member, true, includeSetter)); ParserService.ParseCurrentViewContent(); } @@ -151,7 +153,8 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands MenuCommand item = (MenuCommand)sender; IEvent member = (IEvent)item.Tag; TextEditorControl textEditor = FindReferencesAndRenameHelper.JumpBehindDefinition(member); - member.DeclaringType.ProjectContent.Language.CodeGenerator.CreateOnEventMethod(member, textEditor.Document); + CodeGenerator codeGen = member.DeclaringType.ProjectContent.Language.CodeGenerator; + codeGen.InsertCodeAfter(member, textEditor.Document, codeGen.CreateOnEventMethod(member)); ParserService.ParseCurrentViewContent(); } @@ -184,7 +187,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands if (member is IField) { IProperty property = FindProperty((IField)member); if (property != null) { - string newPropertyName = AbstractPropertyCodeGenerator.GetPropertyName(newName); + string newPropertyName = member.DeclaringType.ProjectContent.Language.CodeGenerator.GetPropertyName(newName); if (newPropertyName != newName && newPropertyName != property.Name) { if (MessageService.AskQuestionFormatted("${res:SharpDevelop.Refactoring.Rename}", "${res:SharpDevelop.Refactoring.RenameFieldAndProperty}", property.FullyQualifiedName, newPropertyName)) { list = RefactoringService.FindReferences(property, null); diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractClassImplementorCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractClassImplementorCodeGenerator.cs index 20c87a866f..81c5b74695 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractClassImplementorCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractClassImplementorCodeGenerator.cs @@ -7,9 +7,11 @@ using System; using System.Collections; -using ICSharpCode.TextEditor; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.SharpDevelop.Refactoring; using ICSharpCode.Core; namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands @@ -22,20 +24,40 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { + public override string Hint { get { return "Choose abstract class to override"; } } - public AbstractClassImplementorCodeGenerator(IClass currentClass) : base(currentClass) + public override void GenerateCode(List nodes, IList items) { - base.useOverrideKeyword = true; - base.implementOnlyAbstractMembers = true; + foreach (IProperty property in currentClass.DefaultReturnType.GetProperties()) { + if (property.IsAbstract) { + AttributedNode node = CodeGenerator.ConvertMember(property, classFinderContext); + node.Modifier &= ~Modifier.Abstract; + node.Modifier |= Modifier.Override; + nodes.Add(node); + } + } + foreach (IMethod method in currentClass.DefaultReturnType.GetMethods()) { + if (method.IsAbstract) { + AttributedNode node = CodeGenerator.ConvertMember(method, classFinderContext); + node.Modifier &= ~Modifier.Abstract; + node.Modifier |= Modifier.Override; + nodes.Add(node); + } + } + } + + protected override void InitContent() + { + if (currentClass.ClassType != ICSharpCode.SharpDevelop.Dom.ClassType.Class) + return; for (int i = 0; i < currentClass.BaseTypes.Count; i++) { IReturnType baseType = currentClass.GetBaseType(i); IClass baseClass = (baseType != null) ? baseType.GetUnderlyingClass() : null; - if (baseClass != null && baseClass.ClassType == ClassType.Class && baseClass.IsAbstract) { + if (baseClass != null && baseClass.ClassType == ICSharpCode.SharpDevelop.Dom.ClassType.Class && baseClass.IsAbstract) { Content.Add(new ClassWrapper(baseType)); } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractFieldCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractFieldCodeGenerator.cs index 765597c96c..fca94bb9c7 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractFieldCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractFieldCodeGenerator.cs @@ -6,14 +6,18 @@ // using System; +using System.Collections; +using System.Collections.Generic; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; +using ICSharpCode.TextEditor; +using ICSharpCode.NRefactory.Parser.AST; namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands { - public abstract class AbstractFieldCodeGenerator : OldCodeGeneratorBase + public abstract class AbstractFieldCodeGenerator : CodeGeneratorBase { - public AbstractFieldCodeGenerator(IClass currentClass) : base(currentClass) + protected override void InitContent() { foreach (IField field in currentClass.Fields) { Content.Add(new FieldWrapper(field)); diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractPropertyCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractPropertyCodeGenerator.cs index f57afa4eb6..2accd80a8b 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractPropertyCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/AbstractPropertyCodeGenerator.cs @@ -16,149 +16,10 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands { public abstract class AbstractPropertyCodeGenerator : AbstractFieldCodeGenerator { - public AbstractPropertyCodeGenerator(IClass currentClass) : base(currentClass) - { - } - public override int ImageIndex { get { return ClassBrowserIconService.PropertyIndex; } } - - public static string GetPropertyName(string fieldName) - { - if (fieldName.StartsWith("_") && fieldName.Length > 1) - return Char.ToUpper(fieldName[1]) + fieldName.Substring(2); - else if (fieldName.StartsWith("m_") && fieldName.Length > 2) - return Char.ToUpper(fieldName[2]) + fieldName.Substring(3); - else - return Char.ToUpper(fieldName[0]) + fieldName.Substring(1); - } - - bool beginWithNewLine; - - /// - /// Gets/Sets if the CodeGenerator should insert a blank line in front of the - /// first property. - /// - public bool BeginWithNewLine { - get { - return beginWithNewLine; - } - set { - beginWithNewLine = value; - } - } - - protected override void StartGeneration(IList items, string fileExtension) - { - for (int i = 0; i < items.Count; ++i) { - if ((i > 0 || BeginWithNewLine) && BlankLinesBetweenMembers) { - Return(); - IndentLine(); - } - FieldWrapper fw = (FieldWrapper)items[i]; - if (fileExtension == ".vb") { - editActionHandler.InsertString("Public " + (fw.Field.IsStatic ? "Shared " : "") + "Property " + GetPropertyName(fw.Field.Name) + " As " + vba.Convert(fw.Field.ReturnType)); - } else { - editActionHandler.InsertString("public " + (fw.Field.IsStatic ? "static " : "") + csa.Convert(fw.Field.ReturnType) + " " + GetPropertyName(fw.Field.Name)); - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {"); - } else { - Return(); - editActionHandler.InsertString("{"); - } - ++numOps; - } - ++numOps; - - - Return(); - - GeneratePropertyBody(editActionHandler, fw, fileExtension); - - if (fileExtension == ".vb") { - editActionHandler.InsertString("End Property"); - } else { - editActionHandler.InsertChar('}'); - } - ++numOps; - - Return(); - IndentLine(); - } - } - - protected void GenerateGetter(TextArea editActionHandler, FieldWrapper fw, string fileExtension) - { - if (fileExtension == ".vb") { - editActionHandler.InsertString("Get"); - } else { - editActionHandler.InsertString("get"); - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {"); - } else { - Return(); - editActionHandler.InsertString("{"); - } - ++numOps; - - } - ++numOps; - Return(); - Indent(); - if (fileExtension == ".vb") { - editActionHandler.InsertString("Return " + fw.Field.Name); - } else { - editActionHandler.InsertString("return " + fw.Field.Name+ ";"); - } - ++numOps; - Return(); - if (fileExtension == ".vb") { - editActionHandler.InsertString("End Get"); - } else { - editActionHandler.InsertString("}"); - } - ++numOps; - Return(); - } - - protected void GenerateSetter(TextArea editActionHandler, FieldWrapper fw, string fileExtension) - { - if (fileExtension == ".vb") { - editActionHandler.InsertString("Set"); - } else { - editActionHandler.InsertString("set"); - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {"); - } else { - Return(); - editActionHandler.InsertString("{"); - } - ++numOps; - - } - ++numOps; - Return(); - Indent(); - if (fileExtension == ".vb") { - editActionHandler.InsertString(fw.Field.Name+ " = Value"); - } else { - editActionHandler.InsertString(fw.Field.Name+ " = value;"); - } - ++numOps; - Return(); - - if (fileExtension == ".vb") { - editActionHandler.InsertString("End Set"); - } else { - editActionHandler.InsertString("}"); - } - ++numOps; - Return(); - } - - protected abstract void GeneratePropertyBody(TextArea editActionHandler, FieldWrapper fw, string fileExtension); } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/CodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/CodeGenerator.cs index f9e380f998..b25a9fac69 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/CodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/CodeGenerator.cs @@ -8,50 +8,43 @@ using System; using System.IO; using System.Collections; +using System.Collections.Generic; -using ICSharpCode.TextEditor.Document; -using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor; -using ICSharpCode.SharpDevelop.Gui; -using ICSharpCode.TextEditor.Actions; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.TextEditor; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; +using ICSharpCode.SharpDevelop.Refactoring; namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands { - public abstract class OldCodeGeneratorBase + public abstract class CodeGeneratorBase { ArrayList content = new ArrayList(); - protected int numOps = 0; - protected IAmbience csa; - protected IAmbience vba; - protected IClass currentClass = null; - protected TextArea editActionHandler; + protected IClass currentClass; + protected ICSharpCode.SharpDevelop.Refactoring.CodeGenerator codeGen; + protected ClassFinder classFinderContext; - public OldCodeGeneratorBase(IClass currentClass) + public void Initialize(IClass currentClass) { this.currentClass = currentClass; - try { - csa = (IAmbience)AddInTree.BuildItem("/SharpDevelop/Workbench/Ambiences/C#", this); - csa.ConversionFlags = ConversionFlags.ShowAccessibility | ConversionFlags.ShowModifiers | ConversionFlags.QualifiedNamesOnlyForReturnTypes | ConversionFlags.ShowReturnType | ConversionFlags.ShowParameterNames; - } catch (TreePathNotFoundException) { - LoggingService.Warn("CSharpAmbience not found -- is the C# backend binding loaded???"); - } - - try { - vba = (IAmbience)AddInTree.BuildItem("/SharpDevelop/Workbench/Ambiences/VBNet", this); - vba.ConversionFlags = ConversionFlags.ShowAccessibility | ConversionFlags.ShowModifiers | ConversionFlags.QualifiedNamesOnlyForReturnTypes | ConversionFlags.ShowReturnType | ConversionFlags.ShowParameterNames; - } catch (TreePathNotFoundException) { - LoggingService.Warn("VBNet ambience not found -- is the VB.NET backend binding loaded???"); - } + this.codeGen = currentClass.ProjectContent.Language.CodeGenerator; + this.classFinderContext = new ClassFinder(currentClass, currentClass.Region.BeginLine + 1, 0); + this.InitContent(); + } + + protected virtual void InitContent() + { } public abstract string CategoryName { get; } - public abstract string Hint { - get; + public virtual string Hint { + get { + return "no hint"; + } } public abstract int ImageIndex { @@ -70,83 +63,19 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public static bool BlankLinesBetweenMembers { - get { - return AmbienceService.CodeGenerationProperties.Get("BlankLinesBetweenMembers", true); - } - } - - public static bool StartCodeBlockInSameLine { - get { - return AmbienceService.CodeGenerationProperties.Get("StartBlockOnSameLine", true); - } - } - - public void GenerateCode(TextArea editActionHandler, IList items) - { - numOps = 0; - this.editActionHandler = editActionHandler; - editActionHandler.BeginUpdate(); - - - bool save1 = editActionHandler.TextEditorProperties.AutoInsertCurlyBracket; - IndentStyle save2 = editActionHandler.TextEditorProperties.IndentStyle; - bool save3 = PropertyService.Get("VBBinding.TextEditor.EnableEndConstructs", true); - PropertyService.Set("VBBinding.TextEditor.EnableEndConstructs", false); - editActionHandler.TextEditorProperties.AutoInsertCurlyBracket = false; - editActionHandler.TextEditorProperties.IndentStyle = IndentStyle.Smart; - - string extension = Path.GetExtension(editActionHandler.MotherTextEditorControl.FileName).ToLower(); - StartGeneration(items, extension); - - if (numOps > 0) { - editActionHandler.Document.UndoStack.UndoLast(numOps); - } - // restore old property settings - editActionHandler.TextEditorProperties.AutoInsertCurlyBracket = save1; - editActionHandler.TextEditorProperties.IndentStyle = save2; - PropertyService.Set("VBBinding.TextEditor.EnableEndConstructs", save3); - editActionHandler.EndUpdate(); - - editActionHandler.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); - editActionHandler.Document.CommitUpdate(); - } - - protected abstract void StartGeneration(IList items, string fileExtension); - - protected void Indent() + protected TypeReference ConvertType(IReturnType type) { - - Properties p = ((Properties)PropertyService.Get("ICSharpCode.TextEditor.Document.Document.DefaultDocumentAggregatorProperties", new Properties())); - - bool tabsToSpaces = p.Get("TabsToSpaces", false); - - int tabSize = p.Get("TabIndent", 4); - int indentSize = p.Get("IndentationSize", 4); - - if (tabsToSpaces) { - editActionHandler.InsertString(new String(' ', indentSize)); - } else { - editActionHandler.InsertString(new String('\t', indentSize / tabSize)); - int trailingSpaces = indentSize % tabSize; - if (trailingSpaces > 0) { - editActionHandler.InsertString(new String(' ', trailingSpaces)); - ++numOps; - } - } - ++numOps; + return CodeGenerator.ConvertType(type, classFinderContext); } - protected void Return() + public virtual void GenerateCode(TextArea textArea, IList items) { - IndentLine(); - new Return().Execute(editActionHandler); - ++numOps; + List nodes = new List(); + GenerateCode(nodes, items); + codeGen.InsertCodeInClass(currentClass, textArea.Document, nodes.ToArray()); + ParserService.ParseCurrentViewContent(); } - protected void IndentLine() - { - editActionHandler.Document.FormattingStrategy.IndentLine(editActionHandler, editActionHandler.Caret.Line); - } + public abstract void GenerateCode(List nodes, IList items); } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/ConstructorCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/ConstructorCodeGenerator.cs index 71fd63d7f6..dcfb63a6fc 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/ConstructorCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/ConstructorCodeGenerator.cs @@ -7,6 +7,8 @@ using System; using System.Collections; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; @@ -21,7 +23,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { + public override string Hint { get { return "Choose fields to initialize by constructor"; } @@ -29,71 +31,24 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands public override int ImageIndex { get { - return ClassBrowserIconService.MethodIndex; } } - public ConstructorCodeGenerator(IClass currentClass) : base(currentClass) + public override void GenerateCode(List nodes, IList items) { - } - - protected override void StartGeneration(IList items, string fileExtension) - { - if (fileExtension == ".vb") { - editActionHandler.InsertString("Public Sub New("); - } else { - editActionHandler.InsertString("public " + currentClass.Name + "("); - } - ++numOps; - - for (int i = 0; i < items.Count; ++i) { - FieldWrapper fw = (FieldWrapper)items[i]; - if (fileExtension == ".vb") { - editActionHandler.InsertString(fw.Field.Name + " As " + vba.Convert(fw.Field.ReturnType)); - } else { - editActionHandler.InsertString(csa.Convert(fw.Field.ReturnType) + " " + fw.Field.Name); - } - ++numOps; - if (i + 1 < items.Count) { - editActionHandler.InsertString(", "); - ++numOps; - } - } - - editActionHandler.InsertChar(')');++numOps; - Return(); - if (fileExtension == ".vb") { - editActionHandler.InsertString("MyBase.New"); - } else { - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {"); - } else { - Return(); - editActionHandler.InsertString("{"); - } - } - ++numOps; - Return(); - for (int i = 0; i < items.Count; ++i) { - Indent(); - FieldWrapper fw = (FieldWrapper)items[i]; - if (fileExtension == ".vb") { - editActionHandler.InsertString("Me." + fw.Field.Name + " = " + fw.Field.Name); - } else { - editActionHandler.InsertString("this." + fw.Field.Name + " = " + fw.Field.Name + ";"); - } - ++numOps; - Return(); - } - if (fileExtension == ".vb") { - editActionHandler.InsertString("End Sub"); - } else { - editActionHandler.InsertChar('}'); - } - ++numOps; - Return(); - IndentLine(); + ConstructorDeclaration ctor = new ConstructorDeclaration(currentClass.Name, Modifier.Public, null, null); + ctor.Body = new BlockStatement(); + foreach (FieldWrapper w in items) { + string parameterName = codeGen.GetParameterName(w.Field.Name); + ctor.Parameters.Add(new ParameterDeclarationExpression(ConvertType(w.Field.ReturnType), + parameterName)); + Expression left = new FieldReferenceExpression(new ThisReferenceExpression(), w.Field.Name); + Expression right = new IdentifierExpression(parameterName); + Expression expr = new AssignmentExpression(left, AssignmentOperatorType.Assign, right); + ctor.Body.AddChild(new StatementExpression(expr)); + } + nodes.Add(ctor); } } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/EqualsCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/EqualsCodeGenerator.cs index 2827d92e6a..2e7bb11770 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/EqualsCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/EqualsCodeGenerator.cs @@ -7,17 +7,15 @@ using System; using System.Collections; -using ICSharpCode.TextEditor; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands { - /// - /// Description of EqualsCodeGenerator. - /// - public class EqualsCodeGenerator : OldCodeGeneratorBase + public class EqualsCodeGenerator : CodeGeneratorBase { public override string CategoryName { get { @@ -25,10 +23,64 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { - get { - return "no hint"; + public override void GenerateCode(List nodes, IList items) + { + TypeReference intReference = new TypeReference("System.Int32"); + MethodDeclaration method = new MethodDeclaration("GetHashCode", Modifier.Public | Modifier.Override, intReference, null, null); + Expression expr = CallGetHashCode(new IdentifierExpression(currentClass.Fields[0].Name)); + for (int i = 1; i < currentClass.Fields.Count; i++) { + IdentifierExpression identifier = new IdentifierExpression(currentClass.Fields[i].Name); + expr = new BinaryOperatorExpression(expr, BinaryOperatorType.ExclusiveOr, + CallGetHashCode(identifier)); } + method.Body = new BlockStatement(); + method.Body.AddChild(new ReturnStatement(expr)); + nodes.Add(method); + + TypeReference boolReference = new TypeReference("System.Boolean"); + TypeReference objectReference = new TypeReference("System.Object"); + + method = new MethodDeclaration("Equals", Modifier.Public | Modifier.Override, boolReference, null, null); + method.Parameters.Add(new ParameterDeclarationExpression(objectReference, "obj")); + method.Body = new BlockStatement(); + + TypeReference currentType = ConvertType(currentClass.DefaultReturnType); + expr = new TypeOfIsExpression(new IdentifierExpression("obj"), currentType); + expr = new ParenthesizedExpression(expr); + expr = new UnaryOperatorExpression(expr, UnaryOperatorType.Not); + method.Body.AddChild(new IfElseStatement(expr, new ReturnStatement(new PrimitiveExpression(false, "false")))); + + expr = new BinaryOperatorExpression(new ThisReferenceExpression(), + BinaryOperatorType.Equality, + new IdentifierExpression("obj")); + method.Body.AddChild(new IfElseStatement(expr, new ReturnStatement(new PrimitiveExpression(true, "true")))); + + VariableDeclaration var = new VariableDeclaration("my" + currentClass.Name, + new CastExpression(currentType, new IdentifierExpression("obj")), + currentType); + method.Body.AddChild(new LocalVariableDeclaration(var)); + + expr = TestEquality(var.Name, currentClass.Fields[0]); + for (int i = 1; i < currentClass.Fields.Count; i++) { + expr = new BinaryOperatorExpression(expr, BinaryOperatorType.LogicalAnd, + TestEquality(var.Name, currentClass.Fields[i])); + } + + method.Body.AddChild(new ReturnStatement(expr)); + + nodes.Add(method); + } + + static InvocationExpression CallGetHashCode(Expression expr) + { + return new InvocationExpression(new FieldReferenceExpression(expr, "GetHashCode")); + } + + static Expression TestEquality(string other, IField field) + { + return new BinaryOperatorExpression(new FieldReferenceExpression(new ThisReferenceExpression(), field.Name), + BinaryOperatorType.Equality, + new FieldReferenceExpression(new IdentifierExpression(other), field.Name)); } public override bool IsActive { @@ -38,81 +90,8 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } public override int ImageIndex { get { - return ClassBrowserIconService.MethodIndex; } } - - public EqualsCodeGenerator(IClass currentClass) : base(currentClass) - { - } - - protected override void StartGeneration(IList items, string fileExtension) - { - editActionHandler.InsertString("public override bool Equals(object obj)");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {");++numOps; - } else { - Return(); - editActionHandler.InsertString("{");++numOps; - } - Return(); - Indent(); - editActionHandler.InsertString("if (!(obj is " + currentClass.Name + ")) return false;");++numOps; - Return(); - Indent(); - editActionHandler.InsertString("if (this == obj) return true;");++numOps; - Return(); - string className = "my" + currentClass.Name; - editActionHandler.InsertString(currentClass.Name + " " + className + " = (" + currentClass.Name + ")obj;");++numOps; - Return(); - - foreach (IField field in currentClass.Fields) { - Indent(); - IClass cName = field.ReturnType.GetUnderlyingClass(); - if (cName == null || cName.ClassType == ClassType.Struct || cName.ClassType == ClassType.Enum) { - editActionHandler.InsertString("if (" + field.Name + " != " + className + "." + field.Name + ") return false;");++numOps; - } else { - editActionHandler.InsertString("if (" + field.Name + " != null ? "+ field.Name + ".Equals(" + className + "." + field.Name + "): " + className + "." + field.Name + " != null) return false;");++numOps; - } - Return(); - } - - Return(); - Indent(); - editActionHandler.InsertString("return true");++numOps; - Return(); - editActionHandler.InsertString("}");++numOps; - Return(); - Return(); - editActionHandler.InsertString("public virtual int GetHashCode()");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {");++numOps; - } else { - Return(); - editActionHandler.InsertString("{");++numOps; - } - Return(); - Indent(); - editActionHandler.InsertString("return ");++numOps; - for (int i = 0; i < currentClass.Fields.Count; ++i) { - IField field = currentClass.Fields[i]; - IClass cName = field.ReturnType.GetUnderlyingClass(); - if (cName == null || cName.ClassType == ClassType.Struct || cName.ClassType == ClassType.Enum) { - editActionHandler.InsertString(field.Name + ".GetHashCode()");++numOps; - } else { - editActionHandler.InsertString("(" + field.Name + " != null ? " + field.Name + ".GetHashCode() : 0)");++numOps; - } - if (i + 1 < currentClass.Fields.Count) { - editActionHandler.InsertString(" ^ "); - } else { - editActionHandler.InsertString(";"); - } - ++numOps; - } - Return(); - editActionHandler.InsertString("}");++numOps; - Return(); - } } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/GetterAndSetterCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/GetterAndSetterCodeGenerator.cs index cdfa6eb584..710372779a 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/GetterAndSetterCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/GetterAndSetterCodeGenerator.cs @@ -7,7 +7,8 @@ using System; using System.Collections; -using ICSharpCode.TextEditor; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; @@ -22,20 +23,17 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { + public override string Hint { get { return "Choose fields to generate getters and setters"; } } - public GetterAndSetterCodeGenerator(IClass currentClass) : base(currentClass) + public override void GenerateCode(List nodes, IList items) { - } - - protected override void GeneratePropertyBody(TextArea editActionHandler, FieldWrapper fw, string fileExtension) - { - GenerateGetter(editActionHandler, fw, fileExtension); - GenerateSetter(editActionHandler, fw, fileExtension); + foreach (FieldWrapper w in items) { + nodes.Add(codeGen.CreateProperty(w.Field, true, true)); + } } } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/GetterCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/GetterCodeGenerator.cs index 0ff568ae13..737d1ebcfd 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/GetterCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/GetterCodeGenerator.cs @@ -7,7 +7,8 @@ using System; using System.Collections; -using ICSharpCode.TextEditor; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; @@ -22,20 +23,17 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { + public override string Hint { get { return "Choose fields to generate getters"; } } - public GetterCodeGenerator(IClass currentClass) : base(currentClass) + public override void GenerateCode(List nodes, IList items) { - } - - protected override void GeneratePropertyBody(TextArea editActionHandler, FieldWrapper fw, string fileExtension) - { - GenerateGetter(editActionHandler, fw, fileExtension); + foreach (FieldWrapper w in items) { + nodes.Add(codeGen.CreateProperty(w.Field, true, false)); + } } } - } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/InterfaceImplementorCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/InterfaceImplementorCodeGenerator.cs index 9535ce3225..d0594e9bd2 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/InterfaceImplementorCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/InterfaceImplementorCodeGenerator.cs @@ -7,14 +7,15 @@ using System; using System.Collections; -using ICSharpCode.TextEditor; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands { - public abstract class InterfaceOrAbstractClassCodeGenerator : OldCodeGeneratorBase + public abstract class InterfaceOrAbstractClassCodeGenerator : CodeGeneratorBase { public override int ImageIndex { get { @@ -22,10 +23,6 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public InterfaceOrAbstractClassCodeGenerator(IClass currentClass) : base(currentClass) - { - } - protected class ClassWrapper { IReturnType c; @@ -46,221 +43,6 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands return ambience.Convert(c); } } - - protected override void StartGeneration(IList items, string fileExtension) - { - for (int i = 0; i < items.Count; ++i) { - ClassWrapper cw = (ClassWrapper)items[i]; - GenerateInterface(cw.ClassType, fileExtension); - } - } - - protected bool useOverrideKeyword = false; - protected bool implementOnlyAbstractMembers = false; - - void GenerateInterface(IReturnType intf, string fileExtension) - { - Return(); - Return(); - editActionHandler.InsertString("#region " + intf.FullyQualifiedName + " interface implementation\n\t\t");++numOps; - - foreach (IProperty property in intf.GetProperties()) { - if (implementOnlyAbstractMembers && !property.IsAbstract) { - continue; - } - string returnType = (fileExtension == ".vb" ? vba : csa).Convert(property.ReturnType); - if (property.IsProtected) { - if (fileExtension == ".vb") { - editActionHandler.InsertString("Protected "); - } else { - editActionHandler.InsertString("protected "); - } - ++numOps; - } else { - if (fileExtension == ".vb") { - editActionHandler.InsertString("Public "); - } else { - editActionHandler.InsertString("public "); - } - ++numOps; - } - - if (fileExtension == ".vb") { - if (useOverrideKeyword) { - editActionHandler.InsertString("Overrides ");++numOps; - } - if (!property.CanSet) { - editActionHandler.InsertString("ReadOnly ");++numOps; - } else if (!property.CanGet) { - editActionHandler.InsertString("WriteOnly ");++numOps; - } - editActionHandler.InsertString("Property " + property.Name + " As " + returnType + "\n"); - } else { - if (useOverrideKeyword) { - editActionHandler.InsertString("override ");++numOps; - } - editActionHandler.InsertString(returnType + " " + property.Name); - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {");++numOps; - } else { - Return(); - editActionHandler.InsertString("{");++numOps; - } - Return(); - } - ++numOps; - if (property.CanGet) { - if (fileExtension == ".vb") { - editActionHandler.InsertString("\tGet");++numOps; - Return(); - editActionHandler.InsertString("\t\tReturn " + GetReturnValue(returnType));++numOps; - Return(); - editActionHandler.InsertString("\tEnd Get");++numOps; - Return(); - } else { - editActionHandler.InsertString("\tget");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {");++numOps; - } else { - Return(); - editActionHandler.InsertString("{");++numOps; - } - - Return(); - editActionHandler.InsertString("\t\treturn " + GetReturnValue(returnType) +";");++numOps; - Return(); - editActionHandler.InsertString("\t}");++numOps; - Return(); - } - } - - if (property.CanSet) { - if (fileExtension == ".vb") { - editActionHandler.InsertString("\tSet");++numOps; - Return(); - editActionHandler.InsertString("Throw New NotImplementedException()");++numOps; - Return(); - editActionHandler.InsertString("\tEnd Set");++numOps; - Return(); - } else { - editActionHandler.InsertString("\tset");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {");++numOps; - } else { - Return(); - editActionHandler.InsertString("{");++numOps; - } - Return(); - editActionHandler.InsertString("throw new NotImplementedException();");++numOps; - Return(); - editActionHandler.InsertString("\t}");++numOps; - Return(); - } - } - - if (fileExtension == ".vb") { - editActionHandler.InsertString("End Property");++numOps; - } else { - editActionHandler.InsertChar('}');++numOps; - } - - Return(); - Return(); - IndentLine(); - } - - foreach (IMethod method in intf.GetMethods()) { - string parameters = String.Empty; - string returnType = (fileExtension == ".vb" ? vba : csa).Convert(method.ReturnType); - if (implementOnlyAbstractMembers && !method.IsAbstract) { - continue; - } - for (int j = 0; j < method.Parameters.Count; ++j) { - parameters += (fileExtension == ".vb" ? vba : csa).Convert(method.Parameters[j]); - if (j + 1 < method.Parameters.Count) { - parameters += ", "; - } - } - if (method.IsProtected) { - if (fileExtension == ".vb") { - editActionHandler.InsertString("Protected "); - } else { - editActionHandler.InsertString("protected "); - } - } else { - if (fileExtension == ".vb") { - editActionHandler.InsertString("Public "); - } else { - editActionHandler.InsertString("public "); - } - } - bool isSub = returnType == "void"; - if (fileExtension == ".vb") { - if (useOverrideKeyword) { - editActionHandler.InsertString("Overrides ");++numOps; - } - editActionHandler.InsertString((isSub ? "Sub " : "Function ") + method.Name + "(" + parameters + ") As " + returnType);++numOps; - Return(); - } else { - if (useOverrideKeyword) { - editActionHandler.InsertString("override ");++numOps; - } - editActionHandler.InsertString(returnType + " " + method.Name + "(" + parameters + ")");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {");++numOps; - } else { - Return(); - editActionHandler.InsertString("{");++numOps; - } - Return(); - } - - if (fileExtension == ".vb") { - editActionHandler.InsertString("Throw New NotImplementedException()");++numOps; - } else { - editActionHandler.InsertString("throw new NotImplementedException();");++numOps; - } - Return(); - - if (fileExtension == ".vb") { - editActionHandler.InsertString("End " + (isSub ? "Sub" : "Function")); - } else { - editActionHandler.InsertChar('}');++numOps; - } - Return(); - Return(); - IndentLine(); - } - - Return(); - editActionHandler.InsertString("#endregion");++numOps; - Return(); - } - - string GetReturnValue(string returnType) - { - switch (returnType) { - case "string": - return "String.Empty"; - case "char": - return "'\\0'"; - case "bool": - return "false"; - case "int": - case "long": - case "short": - case "byte": - case "uint": - case "ulong": - case "ushort": - case "double": - case "float": - case "decimal": - return "0"; - default: - return "null"; - } - } } public class InterfaceImplementorCodeGenerator : InterfaceOrAbstractClassCodeGenerator @@ -271,18 +53,27 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { + public override string Hint { get { return "Choose interfaces to implement"; } } - public InterfaceImplementorCodeGenerator(IClass currentClass) : base(currentClass) + public override void GenerateCode(List nodes, IList items) + { + foreach (ClassWrapper w in items) { + codeGen.ImplementInterface(nodes, w.ClassType, + !currentClass.ProjectContent.Language.SupportsImplicitInterfaceImplementation, + ModifierEnum.Public, currentClass); + } + } + + protected override void InitContent() { for (int i = 0; i < currentClass.BaseTypes.Count; i++) { IReturnType baseType = currentClass.GetBaseType(i); IClass baseClass = (baseType != null) ? baseType.GetUnderlyingClass() : null; - if (baseClass != null && baseClass.ClassType == ClassType.Interface) { + if (baseClass != null && baseClass.ClassType == ICSharpCode.SharpDevelop.Dom.ClassType.Interface) { Content.Add(new ClassWrapper(baseType)); } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OnXXXMethodsCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OnXXXMethodsCodeGenerator.cs index 03030e26f0..92896b9a95 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OnXXXMethodsCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OnXXXMethodsCodeGenerator.cs @@ -7,14 +7,15 @@ using System; using System.Collections; -using ICSharpCode.TextEditor; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands { - public class OnXXXMethodsCodeGenerator : OldCodeGeneratorBase + public class OnXXXMethodsCodeGenerator : CodeGeneratorBase { public override string CategoryName { get { @@ -22,7 +23,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { + public override string Hint { get { return "Choose events to generate OnXXX methods"; } @@ -30,56 +31,21 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands public override int ImageIndex { get { - return ClassBrowserIconService.EventIndex; } } - public OnXXXMethodsCodeGenerator(IClass currentClass) : base(currentClass) + protected override void InitContent() { foreach (IEvent evt in currentClass.Events) { Content.Add(new EventWrapper(evt)); } } - protected override void StartGeneration(IList items, string fileExtension) + public override void GenerateCode(List nodes, IList items) { - for (int i = 0; i < items.Count; ++i) { - EventWrapper ew = (EventWrapper)items[i]; - string eventArgsName = String.Empty; - if (ew.Event.ReturnType.FullyQualifiedName.EndsWith("Handler")) { - eventArgsName = ew.Event.ReturnType.FullyQualifiedName.Substring(0, ew.Event.ReturnType.FullyQualifiedName.Length - "Handler".Length); - } else { - eventArgsName = ew.Event.ReturnType.FullyQualifiedName; - } - eventArgsName += "Args"; - - editActionHandler.InsertString("protected " + (ew.Event.IsStatic ? "static" : "virtual") + " void On" + ew.Event.Name + "(" + eventArgsName + " e)");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {");++numOps; - } else { - Return(); - editActionHandler.InsertString("{");++numOps; - } - Return(); - Indent(); - editActionHandler.InsertString("if (" + ew.Event.Name + " != null)");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {");++numOps; - } else { - Return(); - editActionHandler.InsertString("{");++numOps; - } - - Return(); - Indent(); - editActionHandler.InsertString(ew.Event.Name + "(this, e);");++numOps; - Return(); - editActionHandler.InsertChar('}');++numOps; - Return(); - editActionHandler.InsertChar('}');++numOps; - Return(); - IndentLine(); + foreach (EventWrapper ev in items) { + nodes.Add(codeGen.CreateOnEventMethod(ev.Event)); } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OverrideMethodsCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OverrideMethodsCodeGenerator.cs index a62ecc66e7..10f1eb6e4f 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OverrideMethodsCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OverrideMethodsCodeGenerator.cs @@ -7,14 +7,15 @@ using System; using System.Collections; -using ICSharpCode.TextEditor; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands { - public class OverrideMethodsCodeGenerator : OldCodeGeneratorBase + public class OverrideMethodsCodeGenerator : CodeGeneratorBase { public override string CategoryName { get { @@ -22,7 +23,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { + public override string Hint { get { return "Choose methods to override"; } @@ -30,18 +31,20 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands public override int ImageIndex { get { - return ClassBrowserIconService.MethodIndex; } } - public OverrideMethodsCodeGenerator(IClass currentClass) : base(currentClass) + protected override void InitContent() { foreach (IClass c in currentClass.ClassInheritanceTree) { if (c.FullyQualifiedName != currentClass.FullyQualifiedName) { foreach (IMethod method in c.Methods) { if (!method.IsPrivate && (method.IsAbstract || method.IsVirtual || method.IsOverride)) { - Content.Add(new MethodWrapper(method)); + MethodWrapper newWrapper = new MethodWrapper(method); + if (!Content.Contains(newWrapper)) { + Content.Add(newWrapper); + } } } } @@ -49,82 +52,11 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands Content.Sort(); } - protected override void StartGeneration(IList items, string fileExtension) + public override void GenerateCode(List nodes, IList items) { -// bool moveToMethod = sf.SelectedItems.Count == 1; -// int caretPos = 0; - for (int i = 0; i < items.Count; ++i) { - MethodWrapper mw = (MethodWrapper)items[i]; - - string parameters = String.Empty; - string paramList = String.Empty; - string returnType = (fileExtension == ".vb" ? vba : csa).Convert(mw.Method.ReturnType); - - for (int j = 0; j < mw.Method.Parameters.Count; ++j) { - paramList += mw.Method.Parameters[j].Name; - parameters += (fileExtension == ".vb" ? vba : csa).Convert(mw.Method.Parameters[j]); - if (j + 1 < mw.Method.Parameters.Count) { - parameters += ", "; - paramList += ", "; - } - } - if (fileExtension == ".vb"){ - editActionHandler.InsertString(vba.Convert(mw.Method.Modifiers) + "Overrides ");++numOps; - if (mw.Method.ReturnType.FullyQualifiedName != "System.Void") { - editActionHandler.InsertString("Function ");++numOps; - } else { - editActionHandler.InsertString("Sub ");++numOps; - } - editActionHandler.InsertString(mw.Method.Name + "(" + parameters + ")");++numOps; - if (mw.Method.ReturnType.FullyQualifiedName != "System.Void") { - editActionHandler.InsertString(" As " + returnType);++numOps; - } - } else { - editActionHandler.InsertString(csa.Convert(mw.Method.Modifiers) + "override " + returnType + " " + mw.Method.Name + "(" + parameters + ")");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {"); - } else { - Return(); - editActionHandler.InsertString("{"); - } - ++numOps; - } - - - Return(); - - if(fileExtension == ".vb") { - if (mw.Method.ReturnType.FullyQualifiedName != "System.Void") { - editActionHandler.InsertString("Return MyBase." + mw.Method.Name + "(" + paramList + ")");++numOps; - Return(); - editActionHandler.InsertString("End Function"); - } else { - editActionHandler.InsertString("MyBase." + mw.Method.Name + "(" + paramList + ")");++numOps; - Return(); - editActionHandler.InsertString("End Sub"); - } - } else { - if (mw.Method.ReturnType.FullyQualifiedName != "System.Void") { - string str = "return base." + mw.Method.Name + "(" + paramList + ");"; - editActionHandler.InsertString(str);++numOps; - } else { - string str = "base." + mw.Method.Name + "(" + paramList + ");"; - editActionHandler.InsertString(str);++numOps; - } - Return(); - editActionHandler.InsertChar('}'); - } - ++numOps; - -// caretPos = editActionHandler.Document.Caret.Offset; - - - Return(); - IndentLine(); + foreach (MethodWrapper wrapper in items) { + nodes.Add(codeGen.GetOverridingMethod(wrapper.Method, this.classFinderContext)); } -// if (moveToMethod) { -// editActionHandler.Document.Caret.Offset = caretPos; -// } } class MethodWrapper : IComparable @@ -139,20 +71,37 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands public int CompareTo(object other) { - return method.Name.CompareTo(((MethodWrapper)other).method.Name); + return ToString().CompareTo(((MethodWrapper)other).ToString()); } - public MethodWrapper(IMethod method) { this.method = method; } + public override bool Equals(object obj) + { + MethodWrapper other = (MethodWrapper)obj; + if (method.Name != other.method.Name) + return false; + return 0 == ICSharpCode.SharpDevelop.DiffUtility.Compare(method.Parameters, other.method.Parameters); + } + + public override int GetHashCode() + { + return ToString().GetHashCode(); + } + + string cachedStringRepresentation; + public override string ToString() { - IAmbience ambience = AmbienceService.CurrentAmbience; - ambience.ConversionFlags = ConversionFlags.ShowParameterNames; - return ambience.Convert(method); + if (cachedStringRepresentation == null) { + IAmbience ambience = AmbienceService.CurrentAmbience; + ambience.ConversionFlags = ConversionFlags.ShowParameterNames; + cachedStringRepresentation = ambience.Convert(method); + } + return cachedStringRepresentation; } } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OverridePropertiesCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OverridePropertiesCodeGenerator.cs index 2914dc3f9b..18d4f8088d 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OverridePropertiesCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/OverridePropertiesCodeGenerator.cs @@ -7,14 +7,15 @@ using System; using System.Collections; -using ICSharpCode.TextEditor; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands { - public class OverridePropertiesCodeGenerator : OldCodeGeneratorBase + public class OverridePropertiesCodeGenerator : CodeGeneratorBase { public override string CategoryName { get { @@ -22,7 +23,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { + public override string Hint { get { return "Choose properties to override"; } @@ -30,18 +31,26 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands public override int ImageIndex { get { - return ClassBrowserIconService.PropertyIndex; } } - public OverridePropertiesCodeGenerator(IClass currentClass) : base(currentClass) + protected override void InitContent() { foreach (IClass c in currentClass.ClassInheritanceTree) { if (c.FullyQualifiedName != currentClass.FullyQualifiedName) { foreach (IProperty property in c.Properties) { if (!property.IsPrivate && (property.IsAbstract || property.IsVirtual || property.IsOverride)) { - Content.Add(new PropertyWrapper(property)); + bool alreadyAdded = false; + foreach (PropertyWrapper w in Content) { + if (w.Property.Name == property.Name) { + alreadyAdded = true; + break; + } + } + if (!alreadyAdded) { + Content.Add(new PropertyWrapper(property)); + } } } } @@ -49,117 +58,13 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands Content.Sort(); } - protected override void StartGeneration(IList items, string fileExtension) + public override void GenerateCode(List nodes, IList items) { - for (int i = 0; i < items.Count; ++i) { - PropertyWrapper pw = (PropertyWrapper)items[i]; - - string parameters = String.Empty; - string paramList = String.Empty; - string returnType = (fileExtension == ".vb" ? vba : csa).Convert(pw.Property.ReturnType); - - for (int j = 0; j < pw.Property.Parameters.Count; ++j) { - paramList += pw.Property.Parameters[j].Name; - parameters += (fileExtension == ".vb" ? vba : csa).Convert(pw.Property.Parameters[j]); - if (j + 1 < pw.Property.Parameters.Count) { - parameters += ", "; - paramList += ", "; - } - } - - - if (fileExtension == ".vb"){ - editActionHandler.InsertString(vba.Convert(pw.Property.Modifiers) + "Overrides ");++numOps; - editActionHandler.InsertString("Property ");++numOps; - editActionHandler.InsertString(pw.Property.Name + "()");++numOps; - } else { - editActionHandler.InsertString(csa.Convert(pw.Property.Modifiers) + "override " + returnType + " " + pw.Property.Name);++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {"); - } else { - Return(); - editActionHandler.InsertString("{"); - } - ++numOps; - } - - - Return(); - - if(fileExtension == ".vb") { - editActionHandler.InsertString("Get");++numOps; - } else { - editActionHandler.InsertString("get");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {"); - } else { - Return(); - editActionHandler.InsertString("{"); - } - ++numOps; - } - - Return(); - - if(fileExtension == ".vb") { - editActionHandler.InsertString("Return MyBase." + pw.Property.Name); ++numOps; - } else { - editActionHandler.InsertString("return base." + pw.Property.Name); ++numOps; - } - - Return(); - - if(fileExtension == ".vb") { - editActionHandler.InsertString("End Get"); ++numOps; - } else { - editActionHandler.InsertChar('}'); ++numOps; - } - - Return(); - - if(fileExtension == ".vb") { - editActionHandler.InsertString("Set");++numOps; - } else { - editActionHandler.InsertString("set");++numOps; - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {"); - } else { - Return(); - editActionHandler.InsertString("{"); - } - ++numOps; - } - - Return(); - - if(fileExtension == ".vb") { - editActionHandler.InsertString("MyBase." + pw.Property.Name + " = value"); ++numOps; - } else { - editActionHandler.InsertString("base." + pw.Property.Name + " = value"); ++numOps; - } - - Return(); - - if(fileExtension == ".vb") { - editActionHandler.InsertString("End Set"); ++numOps; - } else { - editActionHandler.InsertChar('}'); ++numOps; - } - - Return(); - - if(fileExtension == ".vb") { - editActionHandler.InsertString("End Property"); ++numOps; - } else { - editActionHandler.InsertChar('}'); ++numOps; - } - - Return(); - Return(); - IndentLine(); + foreach (PropertyWrapper wrapper in items) { + nodes.Add(codeGen.GetOverridingMethod(wrapper.Property, this.classFinderContext)); } } - + class PropertyWrapper : IComparable { IProperty property; diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/SetterCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/SetterCodeGenerator.cs index a8b0f88bef..157f4de6d0 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/SetterCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/SetterCodeGenerator.cs @@ -7,6 +7,8 @@ using System; using System.Collections; +using System.Collections.Generic; +using ICSharpCode.NRefactory.Parser.AST; using ICSharpCode.TextEditor; using ICSharpCode.SharpDevelop.Dom; @@ -22,20 +24,17 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands } } - public override string Hint { + public override string Hint { get { return "Choose fields to generate setters"; } } - public SetterCodeGenerator(IClass currentClass) : base(currentClass) + public override void GenerateCode(List nodes, IList items) { - } - - protected override void GeneratePropertyBody(TextArea editActionHandler, FieldWrapper fw, string fileExtension) - { - GenerateSetter(editActionHandler, fw, fileExtension); + foreach (FieldWrapper w in items) { + nodes.Add(codeGen.CreateProperty(w.Field, false, true)); + } } } - } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/ToStringCodeGenerator.cs b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/ToStringCodeGenerator.cs index 8f2a9ee28d..ef3d7431d9 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/ToStringCodeGenerator.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/CodeGenerators/ToStringCodeGenerator.cs @@ -7,8 +7,11 @@ using System; using System.Collections; -using ICSharpCode.TextEditor; +using System.Collections.Generic; +using System.Text; +using ICSharpCode.NRefactory.Parser.AST; +using ICSharpCode.TextEditor; using ICSharpCode.SharpDevelop.Dom; using ICSharpCode.Core; @@ -16,94 +19,51 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands { public class ToStringCodeGenerator : AbstractFieldCodeGenerator { + public override void GenerateCode(List nodes, IList items) + { + TypeReference stringReference = new TypeReference("System.String"); + MethodDeclaration method = new MethodDeclaration("ToString", + Modifier.Public | Modifier.Override, + stringReference, + null, null); + method.Body = new BlockStatement(); + Expression target = new FieldReferenceExpression(new TypeReferenceExpression(stringReference), + "Format"); + InvocationExpression methodCall = new InvocationExpression(target); + StringBuilder formatString = new StringBuilder(); + formatString.Append('['); + formatString.Append(currentClass.Name); + for (int i = 0; i < items.Count; i++) { + formatString.Append(' '); + formatString.Append(codeGen.GetPropertyName(((FieldWrapper)items[i]).Field.Name)); + formatString.Append("={"); + formatString.Append(i); + formatString.Append('}'); + } + formatString.Append(']'); + methodCall.Arguments.Add(new PrimitiveExpression(formatString.ToString(), formatString.ToString())); + foreach (FieldWrapper w in items) { + methodCall.Arguments.Add(new FieldReferenceExpression(new ThisReferenceExpression(), w.Field.Name)); + } + method.Body.AddChild(new ReturnStatement(methodCall)); + nodes.Add(method); + } + public override string CategoryName { get { return "Generate default ToString() method"; } } - public override string Hint { + public override string Hint { get { return "Choose Properties to include in the description"; } } public override int ImageIndex { get { - return ClassBrowserIconService.MethodIndex; } } - - public ToStringCodeGenerator(IClass currentClass) : base(currentClass) - { - } - - protected override void StartGeneration(IList items, string fileExtension) - { - if (fileExtension == ".vb") { - editActionHandler.InsertString("Public Overrides Function ToString() As String"); - Return(); - } else { - editActionHandler.InsertString("public override string ToString()"); - } - ++numOps; - - if (fileExtension != ".vb") { - if (StartCodeBlockInSameLine) { - editActionHandler.InsertString(" {");++numOps; - } else { - Return(); - editActionHandler.InsertString("{");++numOps; - } - } - Return(); - Indent(); - if (fileExtension == ".vb") { - editActionHandler.InsertString("Return String.Format(\"["); - } else { - editActionHandler.InsertString("return String.Format(\"["); - } - ++numOps; - editActionHandler.InsertString(base.currentClass.Name);++numOps; - if (items.Count > 0) { - editActionHandler.InsertString(":");++numOps; - } - for (int i = 0; i < items.Count; ++i) { - FieldWrapper fieldWrapper = (FieldWrapper)items[i]; - editActionHandler.InsertString(" ");++numOps; - editActionHandler.InsertString(fieldWrapper.Field.Name);++numOps; - editActionHandler.InsertString(" = {" + i + "}");++numOps; - if (i + 1 < items.Count) { - editActionHandler.InsertString(",");++numOps; - } - } - editActionHandler.InsertString("]\"");++numOps; - if (items.Count > 0) { - editActionHandler.InsertString(",");++numOps; - Return(); - } - for (int i = 0; i < items.Count; ++i) { - FieldWrapper fieldWrapper = (FieldWrapper)items[i]; - editActionHandler.InsertString(fieldWrapper.Field.Name); - if (i + 1 < items.Count) { - editActionHandler.InsertString(",");++numOps; - Return(); - } - } - if (fileExtension == ".vb") { - editActionHandler.InsertString(")"); - } else { - editActionHandler.InsertString(");"); - } - ++numOps; - Return(); - if (fileExtension == ".vb") { - editActionHandler.InsertString("End Function"); - } else { - editActionHandler.InsertString("}"); - } - ++numOps; - Return(); - } } } diff --git a/src/Main/Base/Project/Src/TextEditor/Commands/GenerateCodeCommand.cs b/src/Main/Base/Project/Src/TextEditor/Commands/GenerateCodeCommand.cs index 817fe9193a..8d88fc5817 100644 --- a/src/Main/Base/Project/Src/TextEditor/Commands/GenerateCodeCommand.cs +++ b/src/Main/Base/Project/Src/TextEditor/Commands/GenerateCodeCommand.cs @@ -57,19 +57,8 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands if (currentClass != null) { ArrayList categories = new ArrayList(); - using (FormVersion1 form = new FormVersion1(textEditorControl, new OldCodeGeneratorBase[] { - new ConstructorCodeGenerator(currentClass), - new GetterCodeGenerator(currentClass), - new SetterCodeGenerator(currentClass), - new GetterAndSetterCodeGenerator(currentClass), - new OnXXXMethodsCodeGenerator(currentClass), - new OverrideMethodsCodeGenerator(currentClass), - new OverridePropertiesCodeGenerator(currentClass), - new InterfaceImplementorCodeGenerator(currentClass), - new AbstractClassImplementorCodeGenerator(currentClass), - new ToStringCodeGenerator(currentClass), - new EqualsCodeGenerator(currentClass), - })) { + ArrayList generators = AddInTree.BuildItems("/AddIns/DefaultTextEditor/CodeGenerator", this, true); + using (FormVersion1 form = new FormVersion1(textEditorControl, (CodeGeneratorBase[])generators.ToArray(typeof(CodeGeneratorBase)), currentClass)) { form.ShowDialog(ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.MainForm); } } @@ -123,19 +112,23 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands TextEditorControl textEditorControl; - OldCodeGeneratorBase SelectedCodeGenerator { + CodeGeneratorBase SelectedCodeGenerator { get { if (categoryListView.SelectedItems.Count <= 0) { return null; } - return (OldCodeGeneratorBase)categoryListView.SelectedItems[0].Tag; + return (CodeGeneratorBase)categoryListView.SelectedItems[0].Tag; } } - public FormVersion1(TextEditorControl textEditorControl, OldCodeGeneratorBase[] codeGenerators) + public FormVersion1(TextEditorControl textEditorControl, CodeGeneratorBase[] codeGenerators, IClass currentClass) { this.textEditorControl = textEditorControl; + foreach (CodeGeneratorBase generator in codeGenerators) { + generator.Initialize(currentClass); + } + // Must be called for initialization this.InitializeComponents(); // selectionListBox.Sorted = true; @@ -146,11 +139,11 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands int physicalline = textView.Document.GetVisibleLine(caretPos.Y); visualPos = new Point(textView.GetDrawingXPos(caretPos.Y, caretPos.X) + textView.DrawingPosition.X, - (int)((1 + physicalline) * textView.FontHeight) - - textArea.VirtualTop.Y - 1 + textView.DrawingPosition.Y); + (int)((1 + physicalline) * textView.FontHeight) - + textArea.VirtualTop.Y - 1 + textView.DrawingPosition.Y); Point tempLocation = textEditorControl.ActiveTextAreaControl.TextArea.PointToScreen(visualPos); - tempLocation.Y = (tempLocation.Y + Height) > Screen.FromPoint(tempLocation).WorkingArea.Bottom ? + tempLocation.Y = (tempLocation.Y + Height) > Screen.FromPoint(tempLocation).WorkingArea.Bottom ? Screen.FromPoint(tempLocation).WorkingArea.Bottom - Height : tempLocation.Y; tempLocation.X = (tempLocation.X + Width) > Screen.FromPoint(tempLocation).WorkingArea.Right ? Screen.FromPoint(tempLocation).WorkingArea.Right - Width : tempLocation.X; @@ -161,7 +154,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands categoryListView.SmallImageList = categoryListView.LargeImageList = ClassBrowserIconService.ImageList; - foreach (OldCodeGeneratorBase codeGenerator in codeGenerators) { + foreach (CodeGeneratorBase codeGenerator in codeGenerators) { if (codeGenerator.IsActive) { ListViewItem newItem = new ListViewItem(codeGenerator.CategoryName); newItem.ImageIndex = codeGenerator.ImageIndex; @@ -211,7 +204,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands void CategoryListViewItemChanged(object sender, EventArgs e) { - OldCodeGeneratorBase codeGenerator = SelectedCodeGenerator; + CodeGeneratorBase codeGenerator = SelectedCodeGenerator; if (codeGenerator == null) { return; } @@ -226,10 +219,10 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands if (!objs.Contains(o.ToString())) { selectionListBox.Items.Add(o); objs.Add(o.ToString(), ""); - } + } } selectionListBox.SelectedIndex = 0; - } + } selectionListBox.EndUpdate(); selectionListBox.Refresh(); } diff --git a/src/Main/Base/Project/Src/TextEditor/Gui/Editor/CompletionWindow/OverrideCompletionDataProvider.cs b/src/Main/Base/Project/Src/TextEditor/Gui/Editor/CompletionWindow/OverrideCompletionDataProvider.cs index c56b70147a..a2c60917c7 100644 --- a/src/Main/Base/Project/Src/TextEditor/Gui/Editor/CompletionWindow/OverrideCompletionDataProvider.cs +++ b/src/Main/Base/Project/Src/TextEditor/Gui/Editor/CompletionWindow/OverrideCompletionDataProvider.cs @@ -97,7 +97,7 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor CodeGenerator codeGen = ParserService.CurrentProjectContent.Language.CodeGenerator; - string text = codeGen.GenerateCode(GetOverrideAST(member, context), indentation); + string text = codeGen.GenerateCode(codeGen.GetOverridingMethod(member, context), indentation); text = text.TrimEnd(); // remove newline from end textArea.Document.Replace(line.Offset, caretPosition - line.Offset, text); @@ -108,56 +108,5 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor textArea.Refresh(); return true; } - - AttributedNode GetOverrideAST(IMember member, ClassFinder targetContext) - { - AttributedNode node = CodeGenerator.ConvertMember(member, targetContext); - node.Modifier &= ~(Modifier.Virtual | Modifier.Abstract); - node.Modifier |= Modifier.Override; - - MethodDeclaration method = node as MethodDeclaration; - if (method != null) { - method.Body.Children.Clear(); - if (method.TypeReference.SystemType == "System.Void") { - method.Body.AddChild(new StatementExpression(CreateMethodCall(method))); - } else { - method.Body.AddChild(new ReturnStatement(CreateMethodCall(method))); - } - } - PropertyDeclaration property = node as PropertyDeclaration; - if (property != null) { - Expression field = new FieldReferenceExpression(new BaseReferenceExpression(), - property.Name); - if (!property.GetRegion.Block.IsNull) { - property.GetRegion.Block.Children.Clear(); - property.GetRegion.Block.AddChild(new ReturnStatement(field)); - } - if (!property.SetRegion.Block.IsNull) { - property.SetRegion.Block.Children.Clear(); - Expression expr = new AssignmentExpression(field, - AssignmentOperatorType.Assign, - new IdentifierExpression("value")); - property.SetRegion.Block.AddChild(new StatementExpression(expr)); - } - } - return node; - } - - static InvocationExpression CreateMethodCall(MethodDeclaration method) - { - Expression methodName = new FieldReferenceExpression(new BaseReferenceExpression(), - method.Name); - InvocationExpression ie = new InvocationExpression(methodName, null); - foreach (ParameterDeclarationExpression param in method.Parameters) { - Expression expr = new IdentifierExpression(param.ParameterName); - if (param.ParamModifier == ParamModifier.Ref) { - expr = new DirectionExpression(FieldDirection.Ref, expr); - } else if (param.ParamModifier == ParamModifier.Out) { - expr = new DirectionExpression(FieldDirection.Out, expr); - } - ie.Arguments.Add(expr); - } - return ie; - } } } diff --git a/src/Main/Core/Project/Src/Services/ResourceService/ResourceService.cs b/src/Main/Core/Project/Src/Services/ResourceService/ResourceService.cs index 574a2a2443..76cd4dd41a 100644 --- a/src/Main/Core/Project/Src/Services/ResourceService/ResourceService.cs +++ b/src/Main/Core/Project/Src/Services/ResourceService/ResourceService.cs @@ -26,10 +26,10 @@ namespace ICSharpCode.Core /// public static class ResourceService { - readonly static string uiLanguageProperty = "CoreProperties.UILanguage"; + const string uiLanguageProperty = "CoreProperties.UILanguage"; - readonly static string stringResources = "StringResources"; - readonly static string imageResources = "BitmapResources"; + const string stringResources = "StringResources"; + const string imageResources = "BitmapResources"; static string resourceDirectory; @@ -167,21 +167,6 @@ namespace ICSharpCode.Core icons.Add(imageManager); } - /// - /// Take string/bitmap resources from an assembly and merge them in the resource service - /// - [Obsolete("Use should use RegisterStrings or RegisterImages instead.")] - public static void RegisterAssembly(Assembly assembly) - { - if (assembly.GetManifestResourceInfo(stringResources+".resources") != null) { - strings.Add(new ResourceManager(stringResources, assembly)); - } - - if (assembly.GetManifestResourceInfo(imageResources+".resources") != null) { - icons.Add(new ResourceManager(imageResources, assembly)); - } - } - static void OnPropertyChange(object sender, PropertyChangedEventArgs e) { if (e.Key == uiLanguageProperty && e.NewValue != e.OldValue) {