Skip to content

Commit 531aa64

Browse files
author
slavara
committed
fixes crashes
1 parent bd41a2d commit 531aa64

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

PostfixCodeCompletion/Helpers/Reflector.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics;
23
using System.Reflection;
34
using System.Windows.Forms;
45
using ASCompletion.Completion;
56
using ASCompletion.Context;
7+
using ASCompletion.Model;
68
using PluginCore;
79
using PluginCore.Controls;
810
using ScintillaNet;
911

1012
namespace PostfixCodeCompletion.Helpers
1113
{
12-
static class Reflector
14+
internal static class Reflector
1315
{
1416
internal static readonly CompletionListReflector CompletionList = new CompletionListReflector();
1517
internal static readonly ASCompleteReflector ASComplete = new ASCompleteReflector();
@@ -18,21 +20,23 @@ static class Reflector
1820

1921
internal class CompletionListReflector
2022
{
21-
internal ListBox completionList
23+
internal ListBox CompletionList
2224
{
2325
get
2426
{
25-
var member = typeof(CompletionList).GetField("completionList", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
26-
return (ListBox)member.GetValue(typeof(ListBox));
27+
var fieldInfo = typeof(CompletionList).GetField("completionList", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
28+
Debug.Assert(fieldInfo != null, "fieldInfo != null");
29+
return (ListBox) fieldInfo.GetValue(typeof(ListBox));
2730
}
2831
}
2932

30-
internal List<ICompletionListItem> allItems
33+
internal List<ICompletionListItem> AllItems
3134
{
3235
get
3336
{
34-
var member = typeof(CompletionList).GetField("allItems", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
35-
return (List<ICompletionListItem>)member.GetValue(typeof(List<ICompletionListItem>));
37+
var fieldInfo = typeof(CompletionList).GetField("allItems", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
38+
Debug.Assert(fieldInfo != null, "fieldInfo != null");
39+
return (List<ICompletionListItem>) fieldInfo.GetValue(typeof(List<ICompletionListItem>));
3640
}
3741
}
3842
}
@@ -42,7 +46,8 @@ internal class ASCompleteReflector
4246
internal bool HandleDotCompletion(ScintillaControl sci, bool autoHide)
4347
{
4448
var methodInfo = typeof(ASComplete).GetMethod("HandleDotCompletion", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
45-
return (bool)methodInfo.Invoke(null, new object[] { sci, autoHide });
49+
Debug.Assert(methodInfo != null, "methodInfo != null");
50+
return (bool) methodInfo.Invoke(null, new object[] {sci, autoHide});
4651
}
4752
}
4853

@@ -51,31 +56,43 @@ internal class ASGeneratorReflector
5156
internal string CleanType(string type)
5257
{
5358
var methodInfo = typeof(ASGenerator).GetMethod("CleanType", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
54-
return (string)methodInfo.Invoke(null, new object[] { type });
59+
Debug.Assert(methodInfo != null, "methodInfo != null");
60+
return (string) methodInfo.Invoke(null, new object[] {type});
5561
}
5662

5763
internal string GuessVarName(string name, string type)
5864
{
5965
var methodInfo = typeof(ASGenerator).GetMethod("GuessVarName", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
60-
return (string)methodInfo.Invoke(null, new object[] { name, type });
66+
Debug.Assert(methodInfo != null, "methodInfo != null");
67+
return (string) methodInfo.Invoke(null, new object[] {name, type});
6168
}
6269

6370
internal string GetShortType(string type)
6471
{
6572
var methodInfo = typeof(ASGenerator).GetMethod("GetShortType", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
66-
return (string)methodInfo.Invoke(null, new object[] { type });
73+
Debug.Assert(methodInfo != null, "methodInfo != null");
74+
return (string) methodInfo.Invoke(null, new object[] {type});
6775
}
6876

6977
internal StatementReturnType GetStatementReturnType(ScintillaControl sci, string line, int positionFromLine)
7078
{
79+
var currentClass = ASContext.Context.CurrentClass;
80+
if (currentClass.InFile.Context == null)
81+
{
82+
currentClass = (ClassModel) currentClass.Clone();
83+
var language = PluginBase.MainForm.SciConfig.GetLanguageFromFile(currentClass.InFile.BasePath);
84+
currentClass.InFile.Context = ASContext.GetLanguageContext(language) ?? ASContext.Context;
85+
}
7186
var methodInfo = typeof(ASGenerator).GetMethod("GetStatementReturnType", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
72-
var returnType = methodInfo.Invoke(null, new object[] { sci, ASContext.Context.CurrentClass, line, positionFromLine });
87+
Debug.Assert(methodInfo != null, "methodInfo != null");
88+
var returnType = methodInfo.Invoke(null, new object[] {sci, currentClass, line, positionFromLine});
7389
if (returnType == null) return null;
90+
var type = returnType.GetType();
7491
var result = new StatementReturnType
7592
{
76-
Resolve = (ASResult) returnType.GetType().GetField("resolve").GetValue(returnType),
77-
Position = (int) returnType.GetType().GetField("position").GetValue(returnType),
78-
Word = (string) returnType.GetType().GetField("word").GetValue(returnType)
93+
Resolve = (ASResult) type.GetField("resolve").GetValue(returnType),
94+
Position = (int) type.GetField("position").GetValue(returnType),
95+
Word = (string) type.GetField("word").GetValue(returnType)
7996
};
8097
return result;
8198
}

PostfixCodeCompletion/PluginMain.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void Dispose()
7474
/// </summary>
7575
public void HandleEvent(object sender, NotifyEvent e, HandlingPriority priority)
7676
{
77-
var completionList = Reflector.CompletionList.completionList;
77+
var completionList = Reflector.CompletionList.CompletionList;
7878
switch (e.Type)
7979
{
8080
case EventType.UIStarted:
@@ -175,7 +175,7 @@ static void UpdateCompletionList(MemberModel target, ASResult expr)
175175
{
176176
if (target == null || !TemplateUtils.GetHasTemplates()) return;
177177
var items = GetPostfixCompletionItems(target, expr);
178-
var allItems = Reflector.CompletionList.allItems;
178+
var allItems = Reflector.CompletionList.AllItems;
179179
if (allItems != null)
180180
{
181181
var labels = new HashSet<string>();
@@ -200,7 +200,7 @@ static void UpdateCompletionList(MemberModel target, ASResult expr)
200200
});
201201
}
202202
CompletionList.Show(items, false, word);
203-
var list = Reflector.CompletionList.completionList;
203+
var list = Reflector.CompletionList.CompletionList;
204204
completionListItemCount = list.Items.Count;
205205
list.SelectedValueChanged -= OnCompletionListSelectedValueChanged;
206206
list.SelectedValueChanged += OnCompletionListSelectedValueChanged;
@@ -435,15 +435,15 @@ static void OnCharAdded(ScintillaControl sender, int value)
435435
if (sci.PositionIsOnComment(sci.CurrentPos)) return;
436436
if (ASComplete.OnChar(sci, value, false))
437437
{
438-
if (Reflector.CompletionList.completionList.Visible) UpdateCompletionList();
438+
if (Reflector.CompletionList.CompletionList.Visible) UpdateCompletionList();
439439
return;
440440
}
441441
if (!Reflector.ASComplete.HandleDotCompletion(sci, true) || CompletionList.Active) return;
442442
var expr = GetPostfixCompletionExpr();
443443
if (expr == null || expr.IsNull()) return;
444-
Reflector.CompletionList.completionList.VisibleChanged -= OnCompletionListVisibleChanged;
444+
Reflector.CompletionList.CompletionList.VisibleChanged -= OnCompletionListVisibleChanged;
445445
UpdateCompletionList(expr);
446-
Reflector.CompletionList.completionList.VisibleChanged += OnCompletionListVisibleChanged;
446+
Reflector.CompletionList.CompletionList.VisibleChanged += OnCompletionListVisibleChanged;
447447
}
448448
catch (Exception e)
449449
{
@@ -453,14 +453,14 @@ static void OnCharAdded(ScintillaControl sender, int value)
453453

454454
static void OnCompletionListVisibleChanged(object o, EventArgs args)
455455
{
456-
var list = Reflector.CompletionList.completionList;
456+
var list = Reflector.CompletionList.CompletionList;
457457
if (list.Visible) UpdateCompletionList();
458458
else list.SelectedValueChanged -= OnCompletionListSelectedValueChanged;
459459
}
460460

461461
static void OnCompletionListSelectedValueChanged(object sender, EventArgs args)
462462
{
463-
var list = Reflector.CompletionList.completionList;
463+
var list = Reflector.CompletionList.CompletionList;
464464
list.SelectedValueChanged -= OnCompletionListSelectedValueChanged;
465465
if (completionListItemCount != list.Items.Count) UpdateCompletionList();
466466
}
@@ -526,7 +526,7 @@ static void OnFunctionTypeResult(HaxeComplete hc, HaxeCompleteResult result, Hax
526526
if (hc.AutoHide) CompletionList.Hide();
527527
break;
528528
case HaxeCompleteStatus.Type:
529-
var list = Reflector.CompletionList.completionList;
529+
var list = Reflector.CompletionList.CompletionList;
530530
list.VisibleChanged -= OnCompletionListVisibleChanged;
531531
var expr = hc.Expr;
532532
if (result.Type is ClassModel)

0 commit comments

Comments
 (0)