|
| 1 | +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) |
| 2 | +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) |
| 3 | +// |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using System.Threading; |
| 7 | +using CSharpBinding.Refactoring; |
| 8 | +using ICSharpCode.NRefactory.CSharp; |
| 9 | +using ICSharpCode.NRefactory.CSharp.Refactoring; |
| 10 | +using ICSharpCode.NRefactory.CSharp.Resolver; |
| 11 | +using ICSharpCode.NRefactory.TypeSystem; |
| 12 | +using ICSharpCode.SharpDevelop.Editor.CodeCompletion; |
| 13 | + |
| 14 | +namespace CSharpBinding.Completion |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Completion item that creates an event handler for an event. |
| 18 | + /// </summary> |
| 19 | + class EventCreationCompletionData : EntityCompletionData |
| 20 | + { |
| 21 | + IEvent eventDefinition; |
| 22 | + IType delegateType; |
| 23 | + |
| 24 | + public EventCreationCompletionData(string varName, IType delegateType, IEvent evt, string parameterList, IUnresolvedMember callingMember, IUnresolvedTypeDefinition declaringType, CSharpResolver contextAtCaret) : base(evt) |
| 25 | + { |
| 26 | + if (string.IsNullOrEmpty(varName)) { |
| 27 | + this.DisplayText = "Create handler for " + (evt != null ? evt.Name : ""); |
| 28 | + } |
| 29 | + else { |
| 30 | + this.DisplayText = "Create handler for " + char.ToUpper(varName[0]) + varName.Substring(1) + (evt != null ? evt.Name : ""); |
| 31 | + } |
| 32 | + |
| 33 | + this.DisplayText = "<" + this.DisplayText + ">"; |
| 34 | + this.eventDefinition = evt; |
| 35 | + this.delegateType = delegateType; |
| 36 | + } |
| 37 | + |
| 38 | + public override void Complete(CompletionContext context) |
| 39 | + { |
| 40 | + var invokeSignature = delegateType.GetMethods(m => m.Name == "Invoke").Single(); |
| 41 | + var refactoringContext = SDRefactoringContext.Create(context.Editor, CancellationToken.None); |
| 42 | + var builder = refactoringContext.CreateTypeSystemAstBuilder(); |
| 43 | + var handlerName = eventDefinition.Name; |
| 44 | + |
| 45 | + var throwStatement = new ThrowStatement(); |
| 46 | + var decl = new MethodDeclaration { |
| 47 | + ReturnType = refactoringContext.CreateShortType(invokeSignature.ReturnType), |
| 48 | + Name = handlerName, |
| 49 | + Body = new BlockStatement { |
| 50 | + throwStatement |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + decl.Parameters.AddRange(invokeSignature.Parameters.Select(builder.ConvertParameter)); |
| 55 | + |
| 56 | + if (eventDefinition.IsStatic) |
| 57 | + decl.Modifiers |= Modifiers.Static; |
| 58 | + |
| 59 | + throwStatement.Expression = new ObjectCreateExpression(refactoringContext.CreateShortType("System", "NotImplementedException")); |
| 60 | + |
| 61 | + // begin insertion |
| 62 | + using (context.Editor.Document.OpenUndoGroup()) { |
| 63 | + context.Editor.Document.Replace(context.StartOffset, context.Length, handlerName); |
| 64 | + context.EndOffset = context.StartOffset + handlerName.Length; |
| 65 | + |
| 66 | + using (var script = refactoringContext.StartScript()) { |
| 67 | + script.InsertWithCursor(this.DisplayText, Script.InsertPosition.Before, decl) |
| 68 | + // TODO : replace with Link, once that is implemented |
| 69 | + .ContinueScript(() => script.Select(throwStatement)); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | + |
0 commit comments