Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit c4bb8aa

Browse files
committed
fix Close All not working properly
In this commit I've gotten rid of the GetAllEditorElements method in favor of directly utilizing the global variable that stores the editors, since it was completely redundant. This allowed me to remove a lot of dumb null checks on what that function used to return.
1 parent 66b8f5e commit c4bb8aa

16 files changed

+232
-281
lines changed

Interop/TranslationProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void UpdateTranslations()
171171
try
172172
{
173173
// Clear temp folder before beggining
174-
DirUtils.ClearSPCodeTempFolder();
174+
DirHelper.ClearSPCodeTempFolder();
175175

176176
// Download latest release zip file
177177
var wc = new WebClient();
@@ -197,7 +197,7 @@ public void UpdateTranslations()
197197
}
198198

199199
// Delete all temp folder contents
200-
DirUtils.ClearSPCodeTempFolder();
200+
DirHelper.ClearSPCodeTempFolder();
201201

202202
// Update version to options object
203203
Program.OptionsObject.TranslationsVersion = int.Parse(_latestVersion.Name);

Spcode.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@
198198
</Compile>
199199
<Compile Include="UI\Components\EditorElement\Highlighting\BracketHighlightHelpers.cs" />
200200
<Compile Include="UI\Components\EditorElement\Highlighting\BracketSearchResult.cs" />
201-
<Compile Include="Utils\DirUtils.cs" />
201+
<Compile Include="Utils\DirHelper.cs" />
202202
<Compile Include="Utils\Models\ComboboxItem.cs" />
203203
<Compile Include="Utils\Models\Config.cs" />
204204
<Compile Include="Utils\Constants.cs" />

UI/Components/EditorElement/EditorElement.xaml.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -663,21 +663,21 @@ private void ParseIncludes(object sender, EventArgs e)
663663
return;
664664
}
665665

666-
var ee = Program.MainWindow.GetAllEditorElements();
667-
var ce = Program.MainWindow.GetCurrentEditorElement();
666+
var editors = Program.MainWindow.EditorReferences;
667+
var currentEditor = Program.MainWindow.GetCurrentEditorElement();
668668

669669
var caret = -1;
670670

671-
if (ee == null || ce == null)
671+
if (!editors.Any() || currentEditor == null)
672672
{
673673
return;
674674
}
675675

676-
var definitions = new List<SMDefinition>(ee.Count);
676+
var definitions = new List<SMDefinition>(editors.Count);
677677

678678
List<SMFunction> currentFunctions = null;
679679

680-
foreach (var el in ee)
680+
foreach (var el in editors)
681681
{
682682
var fInfo = new FileInfo(el.FullFilePath);
683683
var text = el.editor.Document.Text;
@@ -694,7 +694,7 @@ private void ParseIncludes(object sender, EventArgs e)
694694
definitions.Add(def);
695695

696696

697-
if (el != ce)
697+
if (el != currentEditor)
698698
continue;
699699

700700
currentFunctions = def.Functions;
@@ -730,14 +730,14 @@ private void ParseIncludes(object sender, EventArgs e)
730730
.ProduceTemporaryExpandedDefinition(definitions, caret, currentFunctions);
731731

732732
// Lags the hell out when typing a lot.
733-
ce.editor.SyntaxHighlighting = new AeonEditorHighlighting(smDef);
733+
currentEditor.editor.SyntaxHighlighting = new AeonEditorHighlighting(smDef);
734734

735-
foreach (var el in ee)
735+
foreach (var el in editors)
736736
{
737-
if (el == ce)
737+
if (el == currentEditor)
738738
{
739-
Debug.Assert(ce != null, nameof(ce) + " != null");
740-
if (ce._isTooltipOpen)
739+
Debug.Assert(currentEditor != null, nameof(currentEditor) + " != null");
740+
if (currentEditor._isTooltipOpen)
741741
{
742742
continue;
743743
}

UI/MainWindow/MainWindow.xaml.cs

Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,8 @@ private async void MetroWindow_Closing(object sender, CancelEventArgs e)
262262
if (!ClosingBuffer)
263263
{
264264
// Close directly if no files need to be saved
265-
var editors = GetAllEditorElements()?.ToList();
266265

267-
if (editors == null || (editors != null && !editors.Any(x => x.NeedsSave)) || Program.OptionsObject.ActionOnClose != ActionOnClose.Prompt)
266+
if (!EditorReferences.Any() || !EditorReferences.Any(x => x.NeedsSave) || Program.OptionsObject.ActionOnClose != ActionOnClose.Prompt)
268267
{
269268
ClosingBuffer = true;
270269
CloseProgram(true);
@@ -277,7 +276,7 @@ private async void MetroWindow_Closing(object sender, CancelEventArgs e)
277276
// Build list of unsaved files to show
278277
var sb = new StringBuilder();
279278

280-
foreach (var editor in editors.Where(x => x.NeedsSave))
279+
foreach (var editor in EditorReferences.Where(x => x.NeedsSave))
281280
{
282281
sb.AppendLine($" - {editor.Parent.Title.Substring(1)}");
283282
}
@@ -345,99 +344,98 @@ public bool TryLoadSourceFile(string filePath, out EditorElement outEditor, bool
345344
{
346345
outEditor = null;
347346
var fileInfo = new FileInfo(filePath);
348-
if (fileInfo.Exists)
347+
348+
if (!fileInfo.Exists)
349+
{
350+
return false;
351+
}
352+
353+
if (DirHelper.HasValidTextExtension(fileInfo))
349354
{
350-
if (fileInfo.Extension == ".sp" ||
351-
fileInfo.Extension == ".inc" ||
352-
fileInfo.Extension == ".txt" ||
353-
fileInfo.Extension == ".cfg" ||
354-
fileInfo.Extension == ".ini")
355+
var finalPath = fileInfo.FullName;
356+
if (!DirHelper.CanAccess(finalPath))
355357
{
356-
var finalPath = fileInfo.FullName;
357-
if (!DirUtils.CanAccess(finalPath))
358-
{
359-
return false;
360-
}
358+
return false;
359+
}
361360

362-
var editors = GetAllEditorElements();
363-
if (editors != null)
361+
if (EditorReferences.Any())
362+
{
363+
foreach (var editor in EditorReferences)
364364
{
365-
foreach (var editor in editors)
365+
if (editor.FullFilePath == finalPath)
366366
{
367-
if (editor.FullFilePath == finalPath)
367+
if (SelectMe)
368368
{
369-
if (SelectMe)
370-
{
371-
editor.Parent.IsSelected = true;
372-
editor.editor.TextArea.Caret.Show();
373-
EditorToFocus = editor;
374-
SelectDocumentTimer.Start();
375-
}
376-
377-
outEditor = editor;
378-
return true;
369+
editor.Parent.IsSelected = true;
370+
editor.editor.TextArea.Caret.Show();
371+
EditorToFocus = editor;
372+
SelectDocumentTimer.Start();
379373
}
374+
375+
outEditor = editor;
376+
return true;
380377
}
381378
}
379+
}
382380

383-
AddEditorElement(fileInfo, fileInfo.Name, SelectMe, out outEditor);
384-
if (TryOpenIncludes && Program.OptionsObject.Program_OpenCustomIncludes)
381+
AddEditorElement(fileInfo, fileInfo.Name, SelectMe, out outEditor);
382+
if (TryOpenIncludes && Program.OptionsObject.Program_OpenCustomIncludes)
383+
{
384+
using var textReader = fileInfo.OpenText();
385+
var source = Regex.Replace(textReader.ReadToEnd(), @"/\*.*?\*/", string.Empty,
386+
RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Singleline);
387+
var regex = new Regex(@"^\s*\#include\s+((\<|"")(?<name>.+?)(\>|""))",
388+
RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Multiline);
389+
var mc = regex.Matches(source);
390+
for (var i = 0; i < mc.Count; ++i)
385391
{
386-
using var textReader = fileInfo.OpenText();
387-
var source = Regex.Replace(textReader.ReadToEnd(), @"/\*.*?\*/", string.Empty,
388-
RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Singleline);
389-
var regex = new Regex(@"^\s*\#include\s+((\<|"")(?<name>.+?)(\>|""))",
390-
RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.Multiline);
391-
var mc = regex.Matches(source);
392-
for (var i = 0; i < mc.Count; ++i)
392+
try
393393
{
394-
try
394+
var fileName = mc[i].Groups["name"].Value;
395+
if (!(fileName.EndsWith(".inc", StringComparison.InvariantCultureIgnoreCase) ||
396+
fileName.EndsWith(".sp", StringComparison.InvariantCultureIgnoreCase)))
395397
{
396-
var fileName = mc[i].Groups["name"].Value;
397-
if (!(fileName.EndsWith(".inc", StringComparison.InvariantCultureIgnoreCase) ||
398-
fileName.EndsWith(".sp", StringComparison.InvariantCultureIgnoreCase)))
399-
{
400-
fileName += ".inc";
401-
}
402-
403-
fileName = Path.Combine(
404-
fileInfo.DirectoryName ?? throw new NullReferenceException(), fileName);
405-
TryLoadSourceFile(fileName, out _, false,
406-
Program.OptionsObject.Program_OpenIncludesRecursively);
407-
}
408-
catch (Exception)
409-
{
410-
// ignored
398+
fileName += ".inc";
411399
}
400+
401+
fileName = Path.Combine(
402+
fileInfo.DirectoryName ?? throw new NullReferenceException(), fileName);
403+
TryLoadSourceFile(fileName, out _, false,
404+
Program.OptionsObject.Program_OpenIncludesRecursively);
405+
}
406+
catch (Exception)
407+
{
408+
// ignored
412409
}
413410
}
414411
}
415-
else if (fileInfo.Extension == ".smx")
412+
}
413+
else if (DirHelper.IsBinary(fileInfo))
414+
{
415+
if (DASMReferences.Any())
416416
{
417-
var viewers = GetAllDASMElements();
418-
if (viewers != null)
417+
foreach (var dasmviewer in DASMReferences)
419418
{
420-
foreach (var dasmviewer in viewers)
419+
if (dasmviewer.FilePath == fileInfo.FullName)
421420
{
422-
if (dasmviewer.FilePath == fileInfo.FullName)
423-
{
424-
DockingManager.ActiveContent = dasmviewer;
425-
return true;
426-
}
421+
DockingManager.ActiveContent = dasmviewer;
422+
return true;
427423
}
428424
}
429-
AddDASMElement(fileInfo);
430-
}
431-
432-
if (UseBlendoverEffect)
433-
{
434-
BlendOverEffect.Begin();
435425
}
426+
AddDASMElement(fileInfo);
427+
}
428+
else
429+
{
430+
return false;
431+
}
436432

437-
return true;
433+
if (UseBlendoverEffect)
434+
{
435+
BlendOverEffect.Begin();
438436
}
439437

440-
return false;
438+
return true;
441439
}
442440

443441
/// <summary>
@@ -549,9 +547,8 @@ private void CloseProgram(bool saveAll)
549547
{
550548
// Save all the last open files
551549
var lastOpenFiles = new List<string>();
552-
var editors = GetAllEditorElements()?.ToList();
553550

554-
editors?.ForEach(x =>
551+
EditorReferences.ForEach(x =>
555552
{
556553
if (File.Exists(x.FullFilePath))
557554
{
@@ -562,11 +559,11 @@ private void CloseProgram(bool saveAll)
562559

563560
if (saveAll)
564561
{
565-
editors?.ForEach(x => x.Close(true));
562+
EditorReferences.ForEach(x => x.Close(true));
566563
}
567564
else
568565
{
569-
editors?.ForEach(x => x.Close(false, false));
566+
EditorReferences.ForEach(x => x.Close(false, false));
570567
}
571568

572569
// Kill children process from "Server Start" feature
@@ -601,7 +598,7 @@ public void RestoreMainWindow()
601598
public void EvaluateRTL()
602599
{
603600
FlowDirection = Program.IsRTL ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
604-
GetAllEditorElements()?.ForEach(x => x.EvaluateRTL());
601+
EditorReferences.ForEach(x => x.EvaluateRTL());
605602
}
606603
#endregion
607604
}

0 commit comments

Comments
 (0)