diff --git a/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerLoader/NRefactoryDesignerLoader.cs b/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerLoader/NRefactoryDesignerLoader.cs index 55c212d7e3..166755511e 100644 --- a/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerLoader/NRefactoryDesignerLoader.cs +++ b/src/AddIns/DisplayBindings/FormDesigner/Project/Src/FormDesigner/DesignerLoader/NRefactoryDesignerLoader.cs @@ -119,7 +119,10 @@ namespace ICSharpCode.FormDesigner // Try to fix the type names to fully qualified ones ParseInformation parseInfo = ParserService.GetParseInformation(textEditorControl.FileName); - FixTypeNames(p.CompilationUnit, parseInfo.BestCompilationUnit); + bool foundInitMethod = false; + FixTypeNames(p.CompilationUnit, parseInfo.BestCompilationUnit, ref foundInitMethod); + if (!foundInitMethod) + throw new FormDesignerLoadException("The InitializeComponent method was not found. Designer cannot be loaded."); CodeDOMVisitor visitor = new CodeDOMVisitor(); visitor.Visit(p.CompilationUnit, null); @@ -133,21 +136,24 @@ namespace ICSharpCode.FormDesigner return visitor.codeCompileUnit; } - void FixTypeNames(object o, ICSharpCode.SharpDevelop.Dom.ICompilationUnit domCu) + /// + /// Fix type names and remove unused methods. + /// + void FixTypeNames(object o, ICSharpCode.SharpDevelop.Dom.ICompilationUnit domCu, ref bool foundInitMethod) { if (domCu == null) return; CompilationUnit cu = o as CompilationUnit; if (cu != null) { foreach (object c in cu.Children) { - FixTypeNames(c, domCu); + FixTypeNames(c, domCu, ref foundInitMethod); } return; } NamespaceDeclaration namespaceDecl = o as NamespaceDeclaration; if (namespaceDecl != null) { foreach (object c in namespaceDecl.Children) { - FixTypeNames(c, domCu); + FixTypeNames(c, domCu, ref foundInitMethod); } return; } @@ -156,9 +162,25 @@ namespace ICSharpCode.FormDesigner foreach (TypeReference tref in typeDecl.BaseTypes) { FixTypeReference(tref, typeDecl.StartLocation, domCu); } - foreach (object c in typeDecl.Children) { - FixTypeNames(c, domCu); + for (int i = 0; i < typeDecl.Children.Count; i++) { + object child = typeDecl.Children[i]; + MethodDeclaration method = child as MethodDeclaration; + if (method != null) { + // remove all methods except InitializeComponents + if ((method.Name == "InitializeComponents" || method.Name == "InitializeComponent") && method.Parameters.Count == 0) { + method.Name = "InitializeComponent"; + foundInitMethod = true; + } else { + typeDecl.Children.RemoveAt(i--); + } + } else if (child is TypeDeclaration || child is FieldDeclaration) { + FixTypeNames(child, domCu, ref foundInitMethod); + } else { + // child is property, event etc. + typeDecl.Children.RemoveAt(i--); + } } + return; } FieldDeclaration fieldDecl = o as FieldDeclaration; diff --git a/src/Libraries/NRefactory/Project/Src/Output/CodeDOM/CodeDOMOutputVisitor.cs b/src/Libraries/NRefactory/Project/Src/Output/CodeDOM/CodeDOMOutputVisitor.cs index 844eded1e6..1e2381fcec 100644 --- a/src/Libraries/NRefactory/Project/Src/Output/CodeDOM/CodeDOMOutputVisitor.cs +++ b/src/Libraries/NRefactory/Project/Src/Output/CodeDOM/CodeDOMOutputVisitor.cs @@ -582,9 +582,9 @@ namespace ICSharpCode.NRefactory.Parser case BinaryOperatorType.Subtract: op = CodeBinaryOperatorType.Subtract; break; - //case BinaryOperatorType.ValueEquality: - // op = CodeBinaryOperatorType.ValueEquality; - // break; + //case BinaryOperatorType.ValueEquality: + // op = CodeBinaryOperatorType.ValueEquality; + // break; case BinaryOperatorType.ShiftLeft: case BinaryOperatorType.ShiftRight: // CodeDOM suxx @@ -737,23 +737,31 @@ namespace ICSharpCode.NRefactory.Parser return null; } bool methodReference = false; + + void AddEventHandler(Expression eventExpr, Expression handler, object data) + { + methodReference = true; + CodeExpression methodInvoker = (CodeExpression)handler.AcceptVisitor(this, data); + methodReference = false; + if (!(methodInvoker is CodeObjectCreateExpression)) { + // we need to create an event handler here + methodInvoker = new CodeObjectCreateExpression(new CodeTypeReference("System.EventHandler"), methodInvoker); + } + + if (eventExpr is IdentifierExpression) { + AddStmt(new CodeAttachEventStatement(new CodeEventReferenceExpression(new CodeThisReferenceExpression(), ((IdentifierExpression)eventExpr).Identifier), + methodInvoker)); + } else { + FieldReferenceExpression fr = (FieldReferenceExpression)eventExpr; + AddStmt(new CodeAttachEventStatement(new CodeEventReferenceExpression((CodeExpression)fr.TargetObject.AcceptVisitor(this, data), fr.FieldName), + methodInvoker)); + } + } + public override object Visit(AssignmentExpression assignmentExpression, object data) { if (assignmentExpression.Op == AssignmentOperatorType.Add) { - - methodReference = true; - CodeExpression methodInvoker = (CodeExpression)assignmentExpression.Right.AcceptVisitor(this, null); - methodReference = false; - - if (assignmentExpression.Left is IdentifierExpression) { - AddStmt(new CodeAttachEventStatement(new CodeEventReferenceExpression(new CodeThisReferenceExpression(), ((IdentifierExpression)assignmentExpression.Left).Identifier), - methodInvoker)); - } else { - FieldReferenceExpression fr = (FieldReferenceExpression)assignmentExpression.Left; - - AddStmt(new CodeAttachEventStatement(new CodeEventReferenceExpression((CodeExpression)fr.TargetObject.AcceptVisitor(this, data), fr.FieldName), - methodInvoker)); - } + AddEventHandler(assignmentExpression.Left, assignmentExpression.Right, data); } else { if (assignmentExpression.Left is IdentifierExpression) { AddStmt(new CodeAssignStatement((CodeExpression)assignmentExpression.Left.AcceptVisitor(this, null), (CodeExpression)assignmentExpression.Right.AcceptVisitor(this, null))); @@ -764,6 +772,16 @@ namespace ICSharpCode.NRefactory.Parser return null; } + public override object Visit(AddHandlerStatement addHandlerStatement, object data) + { + AddEventHandler(addHandlerStatement.EventExpression, addHandlerStatement.HandlerExpression, data); + return null; + } + + public override object Visit(AddressOfExpression addressOfExpression, object data) + { + return addressOfExpression.Expression.AcceptVisitor(this, data); + } public override object Visit(TypeOfExpression typeOfExpression, object data) { diff --git a/src/Main/Base/Project/Src/Commands/AutostartCommands.cs b/src/Main/Base/Project/Src/Commands/AutostartCommands.cs index 35525fe001..56624365a7 100644 --- a/src/Main/Base/Project/Src/Commands/AutostartCommands.cs +++ b/src/Main/Base/Project/Src/Commands/AutostartCommands.cs @@ -94,6 +94,7 @@ namespace ICSharpCode.SharpDevelop.Commands if (LayoutConfiguration.CurrentLayoutName == "Plain") { LayoutConfiguration.CurrentLayoutName = oldLayout; } else { + WorkbenchSingleton.Workbench.WorkbenchLayout.StoreConfiguration(); oldLayout = LayoutConfiguration.CurrentLayoutName; LayoutConfiguration.CurrentLayoutName = "Plain"; } diff --git a/src/Main/Base/Project/Src/Services/Debugger/DebuggerService.cs b/src/Main/Base/Project/Src/Services/Debugger/DebuggerService.cs index 7b3b71190b..91f4ddea41 100644 --- a/src/Main/Base/Project/Src/Services/Debugger/DebuggerService.cs +++ b/src/Main/Base/Project/Src/Services/Debugger/DebuggerService.cs @@ -101,6 +101,7 @@ namespace ICSharpCode.Core static void OnDebugStarted(object sender, EventArgs e) { + WorkbenchSingleton.Workbench.WorkbenchLayout.StoreConfiguration(); oldLayoutConfiguration = LayoutConfiguration.CurrentLayoutName; LayoutConfiguration.CurrentLayoutName = "Debug"; @@ -112,6 +113,7 @@ namespace ICSharpCode.Core static void OnDebugStopped(object sender, EventArgs e) { CurrentLineBookmark.Remove(); + WorkbenchSingleton.Workbench.WorkbenchLayout.StoreConfiguration(); LayoutConfiguration.CurrentLayoutName = oldLayoutConfiguration; if (DebugStopped != null) DebugStopped(null, e); diff --git a/src/Main/Base/Project/Src/TextEditor/Gui/Editor/ErrorDrawer.cs b/src/Main/Base/Project/Src/TextEditor/Gui/Editor/ErrorDrawer.cs index 5ab58e0d6b..83341a5623 100644 --- a/src/Main/Base/Project/Src/TextEditor/Gui/Editor/ErrorDrawer.cs +++ b/src/Main/Base/Project/Src/TextEditor/Gui/Editor/ErrorDrawer.cs @@ -54,8 +54,8 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor TaskService.Removed += new TaskEventHandler(OnRemoved); TaskService.Cleared += new EventHandler(OnCleared); textEditor.FileNameChanged += new EventHandler(SetErrors); - DebuggerService.CurrentDebugger.DebugStarted += OnDebugStarted; - DebuggerService.CurrentDebugger.DebugStopped += OnDebugStopped; + DebuggerService.DebugStarted += OnDebugStarted; + DebuggerService.DebugStopped += OnDebugStopped; } bool isDisposed; @@ -73,8 +73,8 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor TaskService.Removed -= new TaskEventHandler(OnRemoved); TaskService.Cleared -= new EventHandler(OnCleared); textEditor.FileNameChanged -= new EventHandler(SetErrors); - DebuggerService.CurrentDebugger.DebugStarted -= OnDebugStarted; - DebuggerService.CurrentDebugger.DebugStopped -= OnDebugStopped; + DebuggerService.DebugStarted -= OnDebugStarted; + DebuggerService.DebugStopped -= OnDebugStopped; ClearErrors(); }