Skip to content

Commit 7da0956

Browse files
committed
Better events generator when using Haxe not for Flash. eg. DOM addEventListener shouldn't import Event or even suggest to generate a DataEvent handler.
1 parent e7a9f41 commit 7da0956

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

External/Plugins/ASCompletion/Completion/ASGenerator.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -801,15 +801,22 @@ private static void ShowEventList(FoundDeclaration found, List<ICompletionListIt
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)
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
}

0 commit comments

Comments
 (0)