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

Commit

Permalink
added exception handling to GoToDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
maxijabase committed Dec 7, 2021
1 parent 534946c commit 3823ff3
Showing 1 changed file with 53 additions and 56 deletions.
109 changes: 53 additions & 56 deletions UI/Components/EditorElement/EditorElementGoToDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -19,56 +18,63 @@ public partial class EditorElement

public async Task GoToDefinition(MouseButtonEventArgs e)
{
var word = GetWordAtMousePosition(e);
if (word.Trim().Length == 0)
try
{
return;
}
var word = GetWordAtMousePosition(e);
if (word.Trim().Length == 0)
{
return;
}

e.Handled = true;
e.Handled = true;

// First search across all scripting directories
// First search across all scripting directories

var sm = MatchDefinition(Program.Configs[Program.SelectedConfig].GetSMDef(), word, e);
if (sm != null)
{
var config = Program.Configs[Program.SelectedConfig].SMDirectories;

foreach (var cfg in config)
var sm = MatchDefinition(Program.Configs[Program.SelectedConfig].GetSMDef(), word, e);
if (sm != null)
{
var file = Path.GetFullPath(Path.Combine(cfg, "include", sm.File)) + ".inc";
var config = Program.Configs[Program.SelectedConfig].SMDirectories;

if (!File.Exists(file))
foreach (var cfg in config)
{
file = Path.GetFullPath(Path.Combine(cfg, sm.File)) + ".inc";
var file = Path.GetFullPath(Path.Combine(cfg, "include", sm.File)) + ".inc";

if (!File.Exists(file))
{
file = Path.GetFullPath(Path.Combine(cfg, sm.File)) + ".inc";
}

await Task.Delay(100);
if (Program.MainWindow.TryLoadSourceFile(file, out var newEditor, true, false, true) && newEditor != null)
{
newEditor.editor.TextArea.Caret.Offset = sm.Index;
newEditor.editor.TextArea.Caret.BringCaretToView();
newEditor.editor.TextArea.Selection = Selection.Create(newEditor.editor.TextArea, sm.Index, sm.Index + sm.Length);
return;
}
else
{
continue;
}
}
}

// If not, try to match variables in the current file
// (shit solution to fix some symbols getting read first inside of the file inaproppiately)

sm = MatchDefinition(currentSmDef, word, e, true);
if (sm != null)
{
editor.TextArea.Caret.Offset = sm.Index;
editor.TextArea.Caret.BringCaretToView();
await Task.Delay(100);
if (Program.MainWindow.TryLoadSourceFile(file, out var newEditor, true, false, true) && newEditor != null)
{
newEditor.editor.TextArea.Caret.Offset = sm.Index;
newEditor.editor.TextArea.Caret.BringCaretToView();
newEditor.editor.TextArea.Selection = Selection.Create(newEditor.editor.TextArea, sm.Index, sm.Index + sm.Length);
return;
}
else
{
LoggingControl.LogAction($"File {file} not found!");
continue;
}
editor.TextArea.Selection = Selection.Create(editor.TextArea, sm.Index, sm.Index + sm.Length);
}
}

// If not, try to match variables in the current file
// (shit solution to fix some symbols getting read first inside of the file inaproppiately)

sm = MatchDefinition(currentSmDef, word, e, true);
if (sm != null)
catch (Exception ex)
{
editor.TextArea.Caret.Offset = sm.Index;
editor.TextArea.Caret.BringCaretToView();
await Task.Delay(100);
editor.TextArea.Selection = Selection.Create(editor.TextArea, sm.Index, sm.Index + sm.Length);
LoggingControl.LogAction($"Exception caught on go to definition: {ex.Message}. Report this bug!");
return;
}
}

Expand Down Expand Up @@ -106,12 +112,10 @@ private SMBaseDefinition MatchDefinition(SMDefinition smDef, string word, MouseB
}

// variables
sm ??= smDef.Variables.FirstOrDefault(i =>
i.Name.Equals(word));
sm ??= smDef.Variables.FirstOrDefault(i => i.Name.Equals(word));

// constants
sm ??= smDef.Constants.FirstOrDefault(i =>
i.Name.Equals(word));
sm ??= smDef.Constants.FirstOrDefault(i => i.Name.Equals(word));

// defines
sm ??= smDef.Defines.FirstOrDefault(i => i.Name.Equals(word));
Expand All @@ -123,8 +127,7 @@ private SMBaseDefinition MatchDefinition(SMDefinition smDef, string word, MouseB
{
foreach (var smEnum in smDef.Enums)
{
var str = smEnum.Entries.FirstOrDefault(
i => i.Equals(word));
var str = smEnum.Entries.FirstOrDefault(i => i.Equals(word));

if (str == null)
{
Expand All @@ -137,24 +140,18 @@ private SMBaseDefinition MatchDefinition(SMDefinition smDef, string word, MouseB
}

// enum structs
sm ??= smDef.EnumStructs.FirstOrDefault(i =>
i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
sm ??= smDef.EnumStructs.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));

sm ??= smDef.EnumStructs.FirstOrDefault(
i => i.Fields.Any(j => j.Name == word));
sm ??= smDef.EnumStructs.FirstOrDefault(i => i.Fields.Any(j => j.Name == word));

sm ??= smDef.EnumStructs.FirstOrDefault(
i => i.Methods.Any(j => j.Name == word));
sm ??= smDef.EnumStructs.FirstOrDefault(i => i.Methods.Any(j => j.Name == word));

// methodmaps
sm ??= smDef.Methodmaps.FirstOrDefault(
i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
sm ??= smDef.Methodmaps.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));

sm ??= smDef.Methodmaps.FirstOrDefault(
i => i.Fields.Any(j => j.Name == word));
sm ??= smDef.Methodmaps.FirstOrDefault(i => i.Fields.Any(j => j.Name == word));

sm ??= smDef.Methodmaps.FirstOrDefault(
i => i.Methods.Any(j => j.Name == word));
sm ??= smDef.Methodmaps.FirstOrDefault(i => i.Methods.Any(j => j.Name == word));

// structs?
sm ??= smDef.Structs.FirstOrDefault(i => i.Name.Equals(word, StringComparison.InvariantCultureIgnoreCase));
Expand Down

0 comments on commit 3823ff3

Please sign in to comment.