Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit e183540

Browse files
fix #254: Event Handler Code Completion not implemented
1 parent 1190f0a commit e183540

4 files changed

Lines changed: 79 additions & 2 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/CSharpBinding.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="Src\Completion\CSharpCompletionContext.cs" />
7373
<Compile Include="Src\Completion\CSharpInsightItem.cs" />
7474
<Compile Include="Src\Completion\CSharpMethodInsight.cs" />
75+
<Compile Include="Src\Completion\EventCreationCompletionData.cs" />
7576
<Compile Include="Src\Completion\ImportCompletionData.cs" />
7677
<Compile Include="Src\Completion\OverrideCompletionData.cs" />
7778
<Compile Include="Src\Completion\OverrideEqualsGetHashCodeCompletionData.cs" />

src/AddIns/BackendBindings/CSharpBinding/Project/Src/Completion/CSharpCompletionDataFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ ICompletionData ICompletionDataFactory.CreateVariableCompletionData(ITypeParamet
105105

106106
ICompletionData ICompletionDataFactory.CreateEventCreationCompletionData(string varName, IType delegateType, IEvent evt, string parameterDefinition, IUnresolvedMember currentMember, IUnresolvedTypeDefinition currentType)
107107
{
108-
return new CompletionData("TODO: event creation");
108+
return new EventCreationCompletionData(varName, delegateType, evt, parameterDefinition, currentMember, currentType, contextAtCaret);
109109
}
110110

111111
ICompletionData ICompletionDataFactory.CreateNewOverrideCompletionData(int declarationBegin, IUnresolvedTypeDefinition type, IMember m)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Refactoring/Script.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ public virtual void CreateNewType(AstNode newType, NewTypeContext context = NewT
615615
}
616616
}
617617

618-
static class ExtMethods
618+
public static class ExtMethods
619619
{
620620
public static void ContinueScript (this Task task, Action act)
621621
{

0 commit comments

Comments
 (0)