Skip to content

Commit f5e5016

Browse files
committed
Merge pull request #1086 from fdorg/fix/event-generator
Better events generator when using Haxe not for Flash.
2 parents 1a25740 + 66413a0 commit f5e5016

File tree

5 files changed

+123
-29
lines changed

5 files changed

+123
-29
lines changed

External/Plugins/ASCompletion/Completion/ASGenerator.cs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public class ASGenerator
3434
static private Regex reModifier = new Regex("(public |private |protected )", RegexOptions.Compiled);
3535
static private Regex reSuperCall = new Regex("^super\\s*\\(", RegexOptions.Compiled);
3636

37-
static private string contextToken;
38-
static private string contextParam;
39-
static private Match contextMatch;
40-
static private ASResult contextResolved;
41-
static private MemberModel contextMember;
37+
static internal string contextToken;
38+
static internal string contextParam;
39+
static internal Match contextMatch;
40+
static internal ASResult contextResolved;
41+
static internal MemberModel contextMember;
4242
static private bool firstVar;
4343

4444
static private bool IsHaxe
@@ -795,21 +795,28 @@ private static void ShowDelegateList(FoundDeclaration found, List<ICompletionLis
795795
options.Add(new GeneratorItem(label, GeneratorJobType.Delegate, found.member, found.inClass));
796796
}
797797

798-
private static void ShowEventList(FoundDeclaration found, List<ICompletionListItem> options)
798+
internal static void ShowEventList(FoundDeclaration found, List<ICompletionListItem> options)
799799
{
800800
string tmp = TextHelper.GetString("ASCompletion.Label.GenerateHandler");
801801
string labelEvent = String.Format(tmp, "Event");
802802
string labelDataEvent = String.Format(tmp, "DataEvent");
803803
string labelContext = String.Format(tmp, contextParam);
804-
string[] choices = (contextParam != "Event") ?
805-
new string[] { labelContext, labelEvent } :
806-
new string[] { labelEvent, labelDataEvent };
804+
string[] choices;
805+
if (contextParam != "Event") choices = new string[] { labelContext, labelEvent };
806+
else if (HasDataEvent()) choices = new string[] { labelEvent, labelDataEvent };
807+
else choices = new string[] { labelEvent };
808+
807809
for (int i = 0; i < choices.Length; i++)
808810
{
809811
options.Add(new GeneratorItem(choices[i],
810812
choices[i] == labelContext ? GeneratorJobType.ComplexEvent : GeneratorJobType.BasicEvent,
811813
found.member, found.inClass));
812814
}
815+
}
816+
817+
private static bool HasDataEvent()
818+
{
819+
return !ASContext.Context.ResolveType("flash.events.DataEvent", ASContext.Context.CurrentModel).IsVoid();
813820
}
814821

815822
private static void ShowGetSetList(FoundDeclaration found, List<ICompletionListItem> options)
@@ -3569,22 +3576,12 @@ private static void GenerateEventHandler(string name, string type, MemberModel a
35693576
ClassModel eventClass = ASContext.Context.ResolveType(type, ASContext.Context.CurrentModel);
35703577
if (eventClass.IsVoid())
35713578
{
3572-
if (type == "Event")
3573-
{
3574-
List<string> typesUsed = new List<string>();
3575-
typesUsed.Add("flash.events.Event");
3576-
delta = AddImportsByName(typesUsed, sci.LineFromPosition(position));
3577-
position += delta;
3578-
sci.SetSel(position, position);
3579-
}
3580-
else if (type == "DataEvent")
3581-
{
3582-
List<string> typesUsed = new List<string>();
3583-
typesUsed.Add("flash.events.DataEvent");
3584-
delta = AddImportsByName(typesUsed, sci.LineFromPosition(position));
3585-
position += delta;
3586-
sci.SetSel(position, position);
3587-
}
3579+
if (TryImportType("flash.events." + type, ref delta, sci.LineFromPosition(position)))
3580+
{
3581+
position += delta;
3582+
sci.SetSel(position, position);
3583+
}
3584+
else type = null;
35883585
}
35893586
lookupPosition += delta;
35903587
string acc = GetPrivateAccessor(afterMethod, inClass);
@@ -3608,6 +3605,18 @@ private static void GenerateEventHandler(string name, string type, MemberModel a
36083605
{
36093606
sci.EndUndoAction();
36103607
}
3608+
}
3609+
3610+
private static bool TryImportType(string type, ref int delta, int atLine)
3611+
{
3612+
ClassModel eventClass = ASContext.Context.ResolveType(type, ASContext.Context.CurrentModel);
3613+
if (eventClass.IsVoid())
3614+
return false;
3615+
3616+
List<string> typesUsed = new List<string>();
3617+
typesUsed.Add(type);
3618+
delta += AddImportsByName(typesUsed, atLine);
3619+
return true;
36113620
}
36123621

36133622
static private string AddRemoveEvent(string eventName)
@@ -4602,7 +4611,7 @@ public Object Data
46024611
}
46034612
}
46044613

4605-
class FoundDeclaration
4614+
internal class FoundDeclaration
46064615
{
46074616
public MemberModel member;
46084617
public ClassModel inClass;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
$(Modifiers) function $(Name)(e:$(Type)):$(Void) $(CSLB){
1+
$(Modifiers) function $(Name)(e<<:$(Type)>>):$(Void) $(CSLB){
22
$(EntryPoint)
33
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
$(Modifiers) function $(Name)(e:$(Type)):$(Void) $(CSLB){
2+
$(Modifiers) function $(Name)(e<<:$(Type)>>):$(Void) $(CSLB){
33
$(EntryPoint)
44
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
<<$(Modifiers) >>function $(Name)(e:$(Type)):$(Void) $(CSLB){
2+
<<$(Modifiers) >>function $(Name)(e<<:$(Type)>>)<<:$(Void)>> $(CSLB){
33
$(EntryPoint)
44
}

Tests/External/Plugins/ASCompletion.Tests/Completion/ASGeneratorTests.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using PluginCore;
1414
using ScintillaNet;
1515
using ScintillaNet.Enums;
16+
using System.Text.RegularExpressions;
1617

1718
namespace ASCompletion.Completion
1819
{
@@ -103,6 +104,90 @@ public void Common(string text, int lineStart, int lineEnd, string resultText, i
103104
}
104105
}
105106

107+
[TestFixture]
108+
public class ContextualActions : ASGeneratorTests
109+
{
110+
[TestFixtureSetUp]
111+
public void ContextualActionsSetup()
112+
{
113+
var pluginMain = Substitute.For<PluginMain>();
114+
var pluginUiMock = new PluginUIMock(pluginMain);
115+
pluginMain.MenuItems.Returns(new List<System.Windows.Forms.ToolStripItem>());
116+
pluginMain.Settings.Returns(new GeneralSettings());
117+
pluginMain.Panel.Returns(pluginUiMock);
118+
ASContext.GlobalInit(pluginMain);
119+
ASContext.Context = Substitute.For<IASContext>();
120+
}
121+
122+
[TestFixture]
123+
class ShowEventsList : ContextualActions
124+
{
125+
ClassModel dataEventModel;
126+
FoundDeclaration found;
127+
128+
[TestFixtureSetUp]
129+
public void ShowEventsListSetup()
130+
{
131+
ASContext.Context.SetAs3Features();
132+
ASContext.Context.CurrentModel.Returns(new FileModel());
133+
dataEventModel = CreateDataEventModel();
134+
found = new FoundDeclaration
135+
{
136+
inClass = new ClassModel(),
137+
member = new MemberModel()
138+
};
139+
}
140+
141+
[Test]
142+
public void ShowEventsList_EventWithDataEvent()
143+
{
144+
ASContext.Context.ResolveType(null, null).ReturnsForAnyArgs(dataEventModel);
145+
ASGenerator.contextParam = "Event";
146+
var options = new List<ICompletionListItem>();
147+
ASGenerator.ShowEventList(found, options);
148+
Assert.AreEqual(2, options.Count);
149+
Assert.IsTrue(Regex.IsMatch(options[0].Label, "\\bEvent\\b"));
150+
Assert.IsTrue(Regex.IsMatch(options[1].Label, "\\bDataEvent\\b"));
151+
}
152+
153+
[Test]
154+
public void ShowEventsList_EventWithoutDataEvent()
155+
{
156+
ASContext.Context.ResolveType(null, null).ReturnsForAnyArgs(ClassModel.VoidClass);
157+
var options = new List<ICompletionListItem>();
158+
ASGenerator.contextParam = "Event";
159+
ASGenerator.ShowEventList(found, options);
160+
Assert.AreEqual(1, options.Count);
161+
Assert.IsTrue(Regex.IsMatch(options[0].Label, "\\bEvent\\b"));
162+
}
163+
164+
[Test]
165+
public void ShowEventsList_CustomEventWithDataEvent()
166+
{
167+
ASContext.Context.ResolveType(null, null).ReturnsForAnyArgs(dataEventModel);
168+
var options = new List<ICompletionListItem>();
169+
ASGenerator.contextParam = "CustomEvent";
170+
ASGenerator.ShowEventList(found, options);
171+
Assert.AreEqual(2, options.Count);
172+
Assert.IsTrue(Regex.IsMatch(options[0].Label, "\\bCustomEvent\\b"));
173+
Assert.IsTrue(Regex.IsMatch(options[1].Label, "\\bEvent\\b"));
174+
}
175+
176+
private ClassModel CreateDataEventModel()
177+
{
178+
var dataEventFile = new FileModel();
179+
var dataEventModel = new ClassModel
180+
{
181+
Name = "DataEvent",
182+
InFile = dataEventFile
183+
};
184+
dataEventFile.Classes.Add(dataEventModel);
185+
return dataEventModel;
186+
}
187+
}
188+
189+
}
190+
106191
[TestFixture]
107192
public class GenerateJob : ASGeneratorTests
108193
{

0 commit comments

Comments
 (0)