Skip to content

Commit 6ec4541

Browse files
committed
Fix crash after changing syntax highlight setting (#7)
1 parent c3c372a commit 6ec4541

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

UndertaleModToolAvalonia/ResourceViews/UndertaleCodeView.axaml.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@ public partial class UndertaleCodeView : UserControl, IUndertaleCodeView
3535

3636
private readonly List<string> codeLocalsCache = new();
3737

38+
private readonly NumberGenerator gmlNumberGenerator;
39+
private readonly NameGenerator gmlNameGenerator;
40+
private readonly NameGenerator asmNameGenerator;
41+
3842
public (int, int) LastCaretOffsets;
3943

4044
public UndertaleCodeView()
4145
{
4246
InitializeComponent();
4347

48+
gmlNumberGenerator = new(this);
49+
gmlNameGenerator = new(this);
50+
asmNameGenerator = new(this);
51+
4452
DataContextChanged += (_, __) =>
4553
{
4654
if (DataContext is UndertaleCodeViewModel vm)
@@ -63,17 +71,25 @@ public UndertaleCodeView()
6371
UndertaleCodeView.ASMHighlightingDefinition ??= LoadHighlightingDefinition("ASM");
6472
ASMTextEditor.SyntaxHighlighting = UndertaleCodeView.ASMHighlightingDefinition;
6573

66-
GMLTextEditor.TextArea.TextView.ElementGenerators.Add(new NumberGenerator(this));
67-
GMLTextEditor.TextArea.TextView.ElementGenerators.Add(new NameGenerator(this));
74+
if (!GMLTextEditor.TextArea.TextView.ElementGenerators.Contains(gmlNumberGenerator))
75+
GMLTextEditor.TextArea.TextView.ElementGenerators.Add(gmlNumberGenerator);
76+
77+
if (!GMLTextEditor.TextArea.TextView.ElementGenerators.Contains(gmlNameGenerator))
78+
GMLTextEditor.TextArea.TextView.ElementGenerators.Add(gmlNameGenerator);
6879

69-
ASMTextEditor.TextArea.TextView.ElementGenerators.Add(new NameGenerator(this));
80+
if (!ASMTextEditor.TextArea.TextView.ElementGenerators.Contains(asmNameGenerator))
81+
ASMTextEditor.TextArea.TextView.ElementGenerators.Add(asmNameGenerator);
7082
}
7183
else
7284
{
7385
GMLTextEditor.SyntaxHighlighting = null;
7486
ASMTextEditor.SyntaxHighlighting = null;
7587
UndertaleCodeView.GMLHighlightingDefinition = null;
7688
UndertaleCodeView.ASMHighlightingDefinition = null;
89+
90+
GMLTextEditor.TextArea.TextView.ElementGenerators.Remove(gmlNumberGenerator);
91+
GMLTextEditor.TextArea.TextView.ElementGenerators.Remove(gmlNameGenerator);
92+
ASMTextEditor.TextArea.TextView.ElementGenerators.Remove(asmNameGenerator);
7793
}
7894

7995
if (this.IsAttachedToVisualTree())
@@ -136,9 +152,16 @@ void UpdateHighlightingCache()
136152
if (DataContext is not UndertaleCodeViewModel vm)
137153
return;
138154

139-
UndertaleData data = vm.MainVM.Data!;
140-
141155
ScriptsCache.Clear();
156+
FunctionsCache.Clear();
157+
CodeCache.Clear();
158+
NamedResourcesCache.Clear();
159+
codeLocalsCache.Clear();
160+
161+
if (!vm.MainVM.Settings!.EnableSyntaxHighlighting)
162+
return;
163+
164+
UndertaleData data = vm.MainVM.Data!;
142165

143166
foreach (var script in data.Scripts)
144167
{
@@ -147,24 +170,20 @@ void UpdateHighlightingCache()
147170
ScriptsCache[script.Name.Content] = script;
148171
}
149172

150-
FunctionsCache.Clear();
151173
foreach (var function in data.Functions)
152174
{
153175
if (function is null)
154176
continue;
155177
FunctionsCache[function.Name.Content] = function;
156178
}
157179

158-
CodeCache.Clear();
159180
foreach (var code in data.Code)
160181
{
161182
if (code is null)
162183
continue;
163184
CodeCache[code.Name.Content] = code;
164185
}
165186

166-
NamedResourcesCache.Clear();
167-
168187
// NOTE: Remember to add new types
169188
IEnumerable?[] objLists = [
170189
data.Sounds,
@@ -195,8 +214,6 @@ void UpdateHighlightingCache()
195214
}
196215
}
197216

198-
codeLocalsCache.Clear();
199-
200217
UndertaleCodeLocals? locals = data.CodeLocals?.ByName(vm.Code.Name.Content);
201218
if (locals != null)
202219
{
@@ -323,7 +340,6 @@ public class NumberGenerator : VisualLineElementGenerator
323340
public NumberGenerator(UndertaleCodeView codeView)
324341
{
325342
this.codeView = codeView;
326-
327343
contextMenu.Placement = PlacementMode.Pointer;
328344
}
329345

0 commit comments

Comments
 (0)