Skip to content

Commit 37e5cfa

Browse files
committed
Editor: in AutoComplete list display parent extenders for child types
1 parent f535e05 commit 37e5cfa

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

Editor/AGS.Editor/AutoComplete.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ public static void ConstructCache(Script scriptToCache, IEnumerable<Script> impo
214214
else if (state.PreviousWord == "extends")
215215
{
216216
// inherited struct
217-
foreach (ScriptStruct baseStruct in structsLookup)
217+
foreach (ScriptStruct parentStruct in structsLookup)
218218
{
219-
if (baseStruct.Name == state.LastWord)
219+
if (parentStruct.Name == state.LastWord)
220220
{
221-
state.InsideStructDefinition = CreateInheritedStruct(baseStruct, state);
221+
state.InsideStructDefinition = CreateInheritedStruct(parentStruct, state);
222222
functions = state.InsideStructDefinition.Functions;
223223
variables = state.InsideStructDefinition.Variables;
224224
break;
@@ -227,7 +227,7 @@ public static void ConstructCache(Script scriptToCache, IEnumerable<Script> impo
227227
}
228228
else if (state.PreviousWord == "struct")
229229
{
230-
state.InsideStructDefinition = new ScriptStruct(state.LastWord, state.InsideIfDefBlock, state.InsideIfNDefBlock, state.CurrentScriptCharacterIndex);
230+
state.InsideStructDefinition = new ScriptStruct(state.LastWord, string.Empty, state.InsideIfDefBlock, state.InsideIfNDefBlock, state.CurrentScriptCharacterIndex);
231231
functions = state.InsideStructDefinition.Functions;
232232
variables = state.InsideStructDefinition.Variables;
233233
}
@@ -262,6 +262,10 @@ public static void ConstructCache(Script scriptToCache, IEnumerable<Script> impo
262262
// imported script's cache. The struct defs may be duplicated this
263263
// way, but that's mostly fine, as these may be merged together;
264264
// e.g. see ScintillaWrapper.GetAllStructsWithMatchingName().
265+
//
266+
// TODO: it's been a while since the above comment was added,
267+
// reinvestigate, explain the reasons, and find if it's possible to
268+
// improve this situation.
265269
AdjustFunctionListForExtenderFunction(structs, ref functionList, ref newStruct, ref script);
266270
if (newStruct != null)
267271
{
@@ -434,7 +438,7 @@ private static void AdjustFunctionListForExtenderFunction(List<ScriptStruct> str
434438

435439
private static ScriptStruct CreateInheritedStruct(ScriptStruct baseStruct, AutoCompleteParserState state)
436440
{
437-
ScriptStruct newStruct = new ScriptStruct(state.PreviousWord2, state.InsideIfDefBlock, state.InsideIfNDefBlock, state.CurrentScriptCharacterIndex);
441+
ScriptStruct newStruct = new ScriptStruct(state.PreviousWord2, baseStruct.Name, state.InsideIfDefBlock, state.InsideIfNDefBlock, state.CurrentScriptCharacterIndex);
438442
foreach (ScriptFunction func in baseStruct.Functions)
439443
{
440444
if (!func.NoInherit)

Editor/AGS.Editor/Panes/ScintillaWrapper.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,10 +1779,28 @@ private void ShowStructMemberAutoComplete()
17791779

17801780
if (foundType != null)
17811781
{
1782-
ShowAutoComplete(charactersAfterDot.Length, ConstructScintillaAutocompleteList(GetAllStructsWithMatchingName(foundType.Name), staticAccess, isThis, null));
1782+
// NOTE: we display members not just for 1 struct, but for all registered structs
1783+
// with the matching names, because of how local script's extender functions are registered:
1784+
// they are gathered separately into a locally declared struct, which may duplicate existing global struct.
1785+
var allStructs = GetAllStructs();
1786+
var forStructs = GetAllStructsWithMatchingName(foundType.Name);
1787+
ShowAutoComplete(charactersAfterDot.Length, ConstructScintillaAutocompleteList(forStructs, allStructs, staticAccess, isThis, null));
17831788
}
17841789
}
17851790

1791+
private List<ScriptStruct> GetAllStructs()
1792+
{
1793+
List<ScriptStruct> allTypes = new List<ScriptStruct>();
1794+
foreach (IScript script in GetAutoCompleteScriptList())
1795+
{
1796+
foreach (ScriptStruct structDef in script.AutoCompleteData.Structs)
1797+
{
1798+
allTypes.Add(structDef);
1799+
}
1800+
}
1801+
return allTypes;
1802+
}
1803+
17861804
private List<ScriptStruct> GetAllStructsWithMatchingName(string structName)
17871805
{
17881806
List<ScriptStruct> matchingTypes = new List<ScriptStruct>();
@@ -1972,7 +1990,7 @@ private void ShowAutoCompleteIfAppropriate(int minimumLength)
19721990
{
19731991
needMatch = null;
19741992
}
1975-
ShowAutoComplete(previousWord.Length, ConstructScintillaAutocompleteList(null, false, false, needMatch));
1993+
ShowAutoComplete(previousWord.Length, ConstructScintillaAutocompleteList(null, null, false, false, needMatch));
19761994
}
19771995
}
19781996
}
@@ -2388,17 +2406,22 @@ private void AddGlobalsFromScript(List<string> globalsList, IScript script, Dict
23882406
}
23892407
}
23902408

2391-
private void AddMembersOfStruct(List<string> autoCompleteList, List<ScriptStruct> scriptStructs, bool staticOnly, bool isThis)
2409+
private void AddMembersOfStruct(List<string> autoCompleteList, List<ScriptStruct> scriptStructs, List<ScriptStruct> allStructsRef, bool addStatic, bool extenderOnly, bool isThis)
23922410
{
23932411
Dictionary<string, object> alreadyAdded = new Dictionary<string, object>();
2412+
AddMembersOfStruct(autoCompleteList, alreadyAdded, scriptStructs, allStructsRef, addStatic, extenderOnly, isThis);
2413+
}
23942414

2415+
private void AddMembersOfStruct(List<string> autoCompleteList, Dictionary<string, object> alreadyAdded,
2416+
List<ScriptStruct> scriptStructs, List<ScriptStruct> allStructsRef, bool addStatic, bool extenderOnly, bool isThis)
2417+
{
23952418
foreach (ScriptStruct scriptStruct in scriptStructs)
23962419
{
23972420
foreach (ScriptFunction sf in scriptStruct.Functions)
23982421
{
2399-
if (((sf.IsStatic) || (!staticOnly)) &&
2400-
((!sf.IsStaticOnly) || (staticOnly)) &&
2401-
((!sf.IsProtected) || (isThis)) &&
2422+
if (((addStatic && sf.IsStatic) || (!addStatic && !sf.IsStatic)) &&
2423+
(!extenderOnly || sf.IsExtenderMethod) &&
2424+
(isThis || !sf.IsProtected) &&
24022425
ShouldShowThis(sf, null) &&
24032426
!alreadyAdded.ContainsKey(sf.FunctionName))
24042427
{
@@ -2417,14 +2440,26 @@ private void AddMembersOfStruct(List<string> autoCompleteList, List<ScriptStruct
24172440
}
24182441
foreach (ScriptVariable sv in scriptStruct.Variables)
24192442
{
2420-
if (((sv.IsStatic) || (!staticOnly)) &&
2421-
((!sv.IsStaticOnly) || (staticOnly)) &&
2422-
((!sv.IsProtected) || (isThis)) &&
2443+
if (((addStatic && sv.IsStatic) || (!addStatic && !sv.IsStatic)) &&
2444+
// TODO: support extender attributes here
2445+
(!extenderOnly) && // variables cannot be extenders
2446+
(isThis || !sv.IsProtected) &&
24232447
ShouldShowThis(sv, null))
24242448
{
24252449
autoCompleteList.Add(sv.VariableName + "?" + (sv.IsStatic ? IMAGE_INDEX_STATIC_PROPERTY : IMAGE_INDEX_PROPERTY));
24262450
}
24272451
}
2452+
2453+
// Add extenders of parent types separately, because they are not listed
2454+
// as child type members when parsing the script.
2455+
if (!string.IsNullOrEmpty(scriptStruct.ParentType))
2456+
{
2457+
var parentStructs = allStructsRef.FindAll((s) => { return s.Name == scriptStruct.ParentType; });
2458+
if (parentStructs.Count > 0)
2459+
{
2460+
AddMembersOfStruct(autoCompleteList, alreadyAdded, parentStructs, allStructsRef, addStatic, true, isThis);
2461+
}
2462+
}
24282463
}
24292464
}
24302465

@@ -2512,13 +2547,13 @@ private List<ScriptVariable> CheckFunctionForLocalVariables(int currentPos, Scri
25122547
return null;
25132548
}
25142549

2515-
private string ConstructScintillaAutocompleteList(List<ScriptStruct> onlyMembersOf, bool staticOnly, bool isThis, string onlyIfMatchForThis)
2550+
private string ConstructScintillaAutocompleteList(List<ScriptStruct> onlyMembersOf, List<ScriptStruct> allStructsRef, bool staticOnly, bool isThis, string onlyIfMatchForThis)
25162551
{
25172552
List<string> globalsList = new List<string>();
25182553

25192554
if (onlyMembersOf != null)
25202555
{
2521-
AddMembersOfStruct(globalsList, onlyMembersOf, staticOnly, isThis);
2556+
AddMembersOfStruct(globalsList, onlyMembersOf, allStructsRef, staticOnly, false, isThis);
25222557
}
25232558
else
25242559
{

Editor/AGS.Types/EditorFeatures/AutoComplete/ScriptStruct.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ namespace AGS.Types.AutoComplete
66
{
77
public class ScriptStruct : ScriptToken
88
{
9-
public ScriptStruct(string name, string ifDefOnly, string ifNDefOnly, int scriptCharacterIndex)
9+
public ScriptStruct(string name, string parentType, string ifDefOnly, string ifNDefOnly, int scriptCharacterIndex)
1010
{
1111
Name = name;
12+
ParentType = parentType;
1213
IfDefOnly = ifDefOnly;
1314
IfNDefOnly = ifNDefOnly;
1415
FullDefinition = true;
1516
StartsAtCharacterIndex = scriptCharacterIndex;
1617
}
1718

18-
public ScriptStruct(string name, string baseType, string ifDefOnly, string ifNDefOnly, int scriptCharacterIndex)
19+
public ScriptStruct(string name, string parentType, string baseType, string ifDefOnly, string ifNDefOnly, int scriptCharacterIndex)
1920
{
2021
Name = name;
22+
ParentType = parentType;
2123
BaseType = baseType;
2224
IfDefOnly = ifDefOnly;
2325
IfNDefOnly = ifNDefOnly;
@@ -56,8 +58,12 @@ public ScriptFunction FindMemberFunction(string name)
5658
}
5759

5860
public string Name;
59-
// Base type of the complex type, e.g. type of elements for array type
61+
/// Parent type (inherited type)
62+
public string ParentType;
63+
/// Base type of the complex type, e.g. type of elements for multi-dimensional dynamic array,
64+
/// or pointer "type" of another type
6065
public string BaseType;
66+
/// Tells if this is a fully defined struct, or a temporary struct draft
6167
public bool FullDefinition;
6268
public List<ScriptVariable> Variables = new List<ScriptVariable>();
6369
public List<ScriptFunction> Functions = new List<ScriptFunction>();

0 commit comments

Comments
 (0)