Skip to content

Commit e215b69

Browse files
author
SlavaRa
authored
Handle 'ASCompletion.DotCompletion'. fixes #70 (#74)
1 parent c3bd0f5 commit e215b69

File tree

2 files changed

+44
-92
lines changed

2 files changed

+44
-92
lines changed

PostfixCodeCompletion/Completion/Complete.cs

Lines changed: 44 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Diagnostics;
43
using System.IO;
54
using System.Linq;
@@ -54,11 +53,11 @@ internal static void Stop()
5453

5554
internal static bool OnCharAdded(int value) => current?.OnCharAdded(value) ?? false;
5655

57-
internal static void UpdateCompletionList() => current?.UpdateCompletionList();
56+
internal static void UpdateCompletionList(IList<ICompletionListItem> options) => current?.UpdateCompletionList(options);
5857

59-
internal static void UpdateCompletionList(ASResult expr) => current?.UpdateCompletionList(expr);
58+
internal static void UpdateCompletionList(ASResult expr, IList<ICompletionListItem> options) => current?.UpdateCompletionList(expr, options);
6059

61-
internal static void UpdateCompletionList(MemberModel target, ASResult expr) => current?.UpdateCompletionList(target, expr);
60+
internal static void UpdateCompletionList(MemberModel target, ASResult expr, IList<ICompletionListItem> options) => current?.UpdateCompletionList(target, expr, options);
6261
}
6362

6463
public interface IPCCComplete
@@ -67,9 +66,9 @@ public interface IPCCComplete
6766
void Stop();
6867
bool OnShortcut(Keys keys);
6968
bool OnCharAdded(int value);
70-
void UpdateCompletionList();
71-
bool UpdateCompletionList(ASResult expr);
72-
void UpdateCompletionList(MemberModel target, ASResult expr);
69+
void UpdateCompletionList(IList<ICompletionListItem> options);
70+
bool UpdateCompletionList(ASResult expr, IList<ICompletionListItem> options);
71+
void UpdateCompletionList(MemberModel target, ASResult expr, IList<ICompletionListItem> options);
7372
}
7473

7574
public class PCCComplete : IPCCComplete
@@ -86,122 +85,89 @@ public virtual void Stop()
8685

8786
public virtual bool OnCharAdded(int value) => false;
8887

89-
public virtual void UpdateCompletionList() => UpdateCompletionList(CompleteHelper.GetCurrentExpressionType());
88+
public virtual void UpdateCompletionList(IList<ICompletionListItem> options) => UpdateCompletionList(CompleteHelper.GetCurrentExpressionType(), options);
9089

91-
public virtual bool UpdateCompletionList(ASResult expr) => false;
90+
public virtual bool UpdateCompletionList(ASResult expr, IList<ICompletionListItem> options) => false;
9291

93-
public virtual void UpdateCompletionList(MemberModel target, ASResult expr)
92+
public virtual void UpdateCompletionList(MemberModel target, ASResult expr, IList<ICompletionListItem> options)
9493
{
9594
}
9695
}
9796

98-
public class PCCASComplete : PCCComplete
97+
public class PCCASComplete : PCCComplete, IEventHandler
9998
{
100-
static int completionListItemCount;
99+
public override void Start() => EventManager.AddEventHandler(this, EventType.Command);
101100

102-
public override void Start()
103-
{
104-
var completionList = Reflector.CompletionList.CompletionList;
105-
completionList.VisibleChanged -= OnCompletionListVisibleChanged;
106-
completionList.VisibleChanged += OnCompletionListVisibleChanged;
107-
}
108-
109-
public override void Stop()
110-
{
111-
var completionList = Reflector.CompletionList.CompletionList;
112-
completionList.VisibleChanged -= OnCompletionListVisibleChanged;
113-
completionList.SelectedValueChanged -= OnCompletionListSelectedValueChanged;
114-
}
101+
public override void Stop() => EventManager.RemoveEventHandler(this);
115102

116103
public override bool OnShortcut(Keys keys)
117104
{
118-
if (keys != (Keys.Control | Keys.Space)) return false;
119-
var completionList = Reflector.CompletionList.CompletionList;
120-
if (CompletionList.Active) return false;
105+
if (CompletionList.Active || keys != (Keys.Control | Keys.Space)) return false;
121106
var expr = CompleteHelper.GetCurrentExpressionType();
122107
if (expr == null || expr.IsNull()) return false;
123108
var result = ASComplete.OnShortcut(keys, PluginBase.MainForm.CurrentDocument.SciControl);
124-
completionList.VisibleChanged -= OnCompletionListVisibleChanged;
125-
UpdateCompletionList(expr);
126-
completionList.VisibleChanged += OnCompletionListVisibleChanged;
109+
UpdateCompletionList(expr, new List<ICompletionListItem>());
127110
return result;
128111
}
129112

130113
public override bool OnCharAdded(int value)
131114
{
115+
if (CompletionList.Active) return false;
132116
if ((char) value != '.' || !TemplateUtils.GetHasTemplates()) return false;
133117
var sci = PluginBase.MainForm.CurrentDocument.SciControl;
134118
if (sci.PositionIsOnComment(sci.CurrentPos)) return false;
135-
if (ASComplete.OnChar(sci, value, false))
136-
{
137-
if (Reflector.CompletionList.CompletionList.Visible) UpdateCompletionList();
138-
return false;
139-
}
140-
if (!Reflector.ASComplete.HandleDotCompletion(sci, true) || CompletionList.Active) return false;
119+
if (ASComplete.OnChar(sci, value, false) || Reflector.ASComplete.HandleDotCompletion(sci, true)) return false;
141120
var expr = CompleteHelper.GetCurrentExpressionType();
142121
if (expr == null || expr.IsNull()) return false;
143-
Reflector.CompletionList.CompletionList.VisibleChanged -= OnCompletionListVisibleChanged;
144-
UpdateCompletionList(expr);
145-
Reflector.CompletionList.CompletionList.VisibleChanged += OnCompletionListVisibleChanged;
122+
UpdateCompletionList(expr, new List<ICompletionListItem>());
146123
return true;
147124
}
148125

149-
public override bool UpdateCompletionList(ASResult expr)
126+
public override bool UpdateCompletionList(ASResult expr, IList<ICompletionListItem> options)
150127
{
151128
if (expr == null || expr.IsNull()) return false;
152129
var target = CompleteHelper.GetCompletionTarget(expr);
153130
if (target == null) return false;
154-
UpdateCompletionList(target, expr);
131+
UpdateCompletionList(target, expr, options);
155132
return true;
156133
}
157134

158-
public override void UpdateCompletionList(MemberModel target, ASResult expr)
135+
public override void UpdateCompletionList(MemberModel target, ASResult expr, IList<ICompletionListItem> options)
159136
{
160137
if (target == null || !TemplateUtils.GetHasTemplates()) return;
138+
var labels = new HashSet<string>();
139+
foreach (var item in options)
140+
{
141+
if (item is PostfixCompletionItem) labels.Add(item.Label);
142+
}
161143
var items = CompleteHelper.GetCompletionItems(target, expr);
162-
var allItems = Reflector.CompletionList.AllItems;
163-
if (allItems != null)
144+
foreach (var item in items)
164145
{
165-
var labels = new HashSet<string>();
166-
foreach (var item in allItems)
167-
{
168-
if (item is PostfixCompletionItem) labels.Add(item.Label);
169-
}
170-
foreach (var item in items)
171-
{
172-
if (!labels.Contains(item.Label)) allItems.Add(item);
173-
}
174-
items = allItems;
146+
if (!labels.Contains(item.Label)) options.Add(item);
175147
}
148+
if (CompletionList.Active) return;
176149
var sci = PluginBase.MainForm.CurrentDocument.SciControl;
177150
var word = sci.GetWordLeft(sci.CurrentPos - 1, false);
178151
if (!string.IsNullOrEmpty(word))
179152
{
180-
items = items.FindAll(it =>
153+
options = options.Where(it =>
181154
{
182155
var score = CompletionList.SmartMatch(it.Label, word, word.Length);
183156
return score > 0 && score < 6;
184-
});
157+
}).ToList();
185158
}
186-
CompletionList.Show(items, false, word);
187-
var list = Reflector.CompletionList.CompletionList;
188-
completionListItemCount = list.Items.Count;
189-
list.SelectedValueChanged -= OnCompletionListSelectedValueChanged;
190-
list.SelectedValueChanged += OnCompletionListSelectedValueChanged;
191-
}
192-
193-
protected void OnCompletionListVisibleChanged(object o, EventArgs args)
194-
{
195-
var list = Reflector.CompletionList.CompletionList;
196-
if (list.Visible) UpdateCompletionList();
197-
else list.SelectedValueChanged -= OnCompletionListSelectedValueChanged;
159+
CompletionList.Show((List<ICompletionListItem>) options, false, word);
198160
}
199161

200-
protected void OnCompletionListSelectedValueChanged(object sender, EventArgs args)
162+
public void HandleEvent(object sender, NotifyEvent e, HandlingPriority priority)
201163
{
202-
var list = Reflector.CompletionList.CompletionList;
203-
list.SelectedValueChanged -= OnCompletionListSelectedValueChanged;
204-
if (completionListItemCount != list.Items.Count) UpdateCompletionList();
164+
switch (e.Type)
165+
{
166+
case EventType.Command:
167+
var de = (DataEvent)e;
168+
if (de.Action == "ASCompletion.DotCompletion") UpdateCompletionList((IList<ICompletionListItem>) de.Data);
169+
break;
170+
}
205171
}
206172
}
207173

@@ -227,9 +193,9 @@ public override void Stop()
227193
completionModeHandler = null;
228194
}
229195

230-
public override bool UpdateCompletionList(ASResult expr)
196+
public override bool UpdateCompletionList(ASResult expr, IList<ICompletionListItem> options)
231197
{
232-
var result = base.UpdateCompletionList(expr);
198+
var result = base.UpdateCompletionList(expr, options);
233199
if (result) return true;
234200
if (expr == null || expr.IsNull() || expr.Context == null || completionModeHandler == null) return false;
235201
var sci = PluginBase.MainForm.CurrentDocument.SciControl;
@@ -285,22 +251,19 @@ void OnFunctionTypeResult(HaxeComplete hc, HaxeCompleteResult result, HaxeComple
285251
if (hc.AutoHide) CompletionList.Hide();
286252
break;
287253
case HaxeCompleteStatus.Type:
288-
var list = Reflector.CompletionList.CompletionList;
289-
list.VisibleChanged -= OnCompletionListVisibleChanged;
290254
var expr = hc.Expr;
291255
if (result.Type is ClassModel)
292256
{
293257
expr.Type = (ClassModel) result.Type;
294258
expr.Member = null;
295-
UpdateCompletionList(expr.Type, expr);
259+
UpdateCompletionList(expr.Type, expr, Reflector.CompletionList.AllItems);
296260
}
297261
else
298262
{
299263
expr.Type = ASContext.Context.ResolveType(result.Type.Type, result.Type.InFile);
300264
expr.Member = result.Type;
301-
UpdateCompletionList(expr.Member, expr);
265+
UpdateCompletionList(expr.Member, expr, Reflector.CompletionList.AllItems);
302266
}
303-
list.VisibleChanged += OnCompletionListVisibleChanged;
304267
break;
305268
}
306269
}

PostfixCodeCompletion/Helpers/Reflector.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
22
using System.Diagnostics;
33
using System.Reflection;
4-
using System.Windows.Forms;
54
using ASCompletion.Completion;
65
using ASCompletion.Context;
76
using ASCompletion.Model;
@@ -20,16 +19,6 @@ internal static class Reflector
2019

2120
internal class CompletionListReflector
2221
{
23-
internal ListBox CompletionList
24-
{
25-
get
26-
{
27-
var fieldInfo = typeof(CompletionList).GetField("completionList", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
28-
Debug.Assert(fieldInfo != null, "fieldInfo is null");
29-
return (ListBox) fieldInfo.GetValue(typeof(ListBox));
30-
}
31-
}
32-
3322
internal List<ICompletionListItem> AllItems
3423
{
3524
get

0 commit comments

Comments
 (0)