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

Commit cd49d8c

Browse files
reimplement "Insert 'TODO' comment in new event handler" option in Windows Forms Designer
1 parent 0759063 commit cd49d8c

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormsDesigner/CSharpEventBindingService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using ICSharpCode.SharpDevelop;
3232
using ICSharpCode.SharpDevelop.Editor;
3333
using ICSharpCode.SharpDevelop.Project;
34+
using ICSharpCode.SharpDevelop.Refactoring;
3435
using CSharpBinding.Refactoring;
3536

3637
namespace CSharpBinding.FormsDesigner
@@ -147,7 +148,8 @@ void InsertEventHandlerInternal(string methodName, IEvent evt)
147148
var primary = loader.GetPrimaryTypeDefinition();
148149
var evtHandler = primary.GetMethods(m => m.Name == methodName, GetMemberOptions.IgnoreInheritedMembers).FirstOrDefault();
149150
if (evtHandler == null) {
150-
generator.InsertEventHandler(primary, methodName, evt, true);
151+
var insertionType = GeneralOptionsPanel.InsertTodoComment ? InsertEventHandlerBodyKind.TodoComment : InsertEventHandlerBodyKind.Nothing;
152+
generator.InsertEventHandler(primary, methodName, evt, true, insertionType);
151153
} else {
152154
CSharpBinding.Parser.CSharpFullParseInformation parseInfo;
153155
var node = evtHandler.GetDeclaration(out parseInfo) as MethodDeclaration;

src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CSharpCodeGenerator.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public override void AddReturnTypeAttribute(IMethod target, IAttribute attribute
6666
AddAttribute(target.Region, attribute, "return");
6767
}
6868

69-
public override void InsertEventHandler(ITypeDefinition target, string name, IEvent eventDefinition, bool jumpTo)
69+
public override void InsertEventHandler(ITypeDefinition target, string name, IEvent eventDefinition, bool jumpTo, InsertEventHandlerBodyKind bodyKind = InsertEventHandlerBodyKind.ThrowNotImplementedException)
7070
{
7171
IUnresolvedTypeDefinition match = null;
7272

@@ -103,12 +103,30 @@ public override void InsertEventHandler(ITypeDefinition target, string name, IEv
103103
decl.Parameters.AddRange(param);
104104

105105
using (Script script = context.StartScript()) {
106-
// FIXME : will not work properly if there are no members.
106+
int eolLen = 0;
107107
if (last == match) {
108-
script.InsertWithCursor("Insert event handler", Script.InsertPosition.End, decl).FireAndForget();
108+
eolLen = 2;
109+
script.AddTo((TypeDeclaration)node, decl);
109110
} else {
110111
script.InsertAfter(node, decl);
111-
script.Select(throwStmt);
112+
}
113+
switch (bodyKind) {
114+
case InsertEventHandlerBodyKind.TodoComment:
115+
Comment comment = new Comment(" TODO: Implement " + name);
116+
script.Replace(throwStmt, comment);
117+
script.Select(comment);
118+
break;
119+
case InsertEventHandlerBodyKind.Nothing:
120+
var segment = script.GetSegment(throwStmt);
121+
if (script is DocumentScript && eolLen > 0) {
122+
eolLen = ((DocumentScript)script).CurrentDocument.GetLineByOffset(segment.Offset).DelimiterLength;
123+
}
124+
script.RemoveText(segment.Offset, segment.Length - eolLen);
125+
script.Select(segment.Offset, segment.Offset);
126+
break;
127+
case InsertEventHandlerBodyKind.ThrowNotImplementedException:
128+
script.Select(throwStmt);
129+
break;
112130
}
113131
}
114132
}

src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/EditorScript.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public override void Select(AstNode node)
8181
editor.Select(startOffset, endOffset - startOffset);
8282
}
8383

84+
public override void Select(int startOffset, int endOffset)
85+
{
86+
editor.Select(Math.Min(startOffset, endOffset), Math.Abs(endOffset - startOffset));
87+
}
88+
89+
public override void Select(TextLocation start, TextLocation end)
90+
{
91+
Select(editor.Document.GetOffset(start), editor.Document.GetOffset(end));
92+
}
93+
8494
public override Task Link(params AstNode[] nodes)
8595
{
8696
var segs = nodes.Select(node => GetSegment(node)).ToArray();

src/Main/Base/Project/Refactoring/CodeGenerator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public virtual void AddReturnTypeAttribute(IMethod target, IAttribute attribute)
4343
throw new NotSupportedException("Feature not supported!");
4444
}
4545

46-
public virtual void InsertEventHandler(ITypeDefinition target, string name, IEvent eventDefinition, bool jumpTo)
46+
public virtual void InsertEventHandler(ITypeDefinition target, string name, IEvent eventDefinition, bool jumpTo, InsertEventHandlerBodyKind bodyKind = InsertEventHandlerBodyKind.ThrowNotImplementedException)
4747
{
4848
throw new NotSupportedException("Feature not supported!");
4949
}
@@ -123,4 +123,11 @@ public virtual void AddImport(FileName fileName, string namespaceName)
123123
throw new NotSupportedException("Feature not supported!");
124124
}
125125
}
126+
127+
public enum InsertEventHandlerBodyKind
128+
{
129+
Nothing,
130+
TodoComment,
131+
ThrowNotImplementedException
132+
}
126133
}

0 commit comments

Comments
 (0)