Skip to content

Commit 3efc650

Browse files
committed
Further minor code refactorings and cleanup.
1 parent 12ecd8c commit 3efc650

1 file changed

Lines changed: 52 additions & 80 deletions

File tree

GunWin/VisualAnalyzer.cs

Lines changed: 52 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
using Microsoft.Msagl.Drawing;
6060
using Microsoft.Msagl.GraphViewerGdi;
6161
using Microsoft.Msagl.Layout.Layered;
62-
63-
using Org.Edgerunner.ANTLR4.Tools.Common.Extensions;
6462
using Org.Edgerunner.ANTLR4.Tools.Common.Grammar;
6563
using Org.Edgerunner.ANTLR4.Tools.Common.Syntax;
6664
using Org.Edgerunner.ANTLR4.Tools.Graphing;
@@ -121,7 +119,7 @@ public partial class VisualAnalyzer : Form
121119

122120
private bool _EnableTrackBarZoom = true;
123121

124-
private bool _Reloading = false;
122+
private bool _Reloading;
125123

126124
#region Constructors And Finalizers
127125

@@ -301,10 +299,10 @@ public void SetGrammar([NotNull] GrammarReference grammar)
301299
LoadParserRules();
302300

303301
// If our grammar is only being reloaded, no reason to load a new monitor and guide
304-
if (oldGrammar != null)
305-
if (oldGrammar.GrammarName == grammar.GrammarName &&
306-
oldGrammar.AssemblyPath == grammar.AssemblyPath)
307-
return;
302+
if (oldGrammar != null &&
303+
oldGrammar.GrammarName == grammar.GrammarName &&
304+
oldGrammar.AssemblyPath == grammar.AssemblyPath)
305+
return;
308306

309307
stripLabelGrammarName.Text = grammar.GrammarName;
310308
_GrammarMonitor = new GrammarMonitor(grammar, SynchronizationContext.Current);
@@ -402,23 +400,6 @@ private void Viewer_MouseWheel(object sender, MouseEventArgs e)
402400
Debug.WriteLine($"Scroll zoom factor {factor}");
403401
}
404402

405-
private void AddTreeBranchesAndLeaves(TreeNode treeNode, ITree tree)
406-
{
407-
for (var i = 0; i < tree.ChildCount; i++)
408-
{
409-
var child = tree.GetChild(i);
410-
var newNode =
411-
new TreeNode(Trees.GetNodeText(child, _Grammar.ParserRules))
412-
{
413-
Tag = child,
414-
Name = child.GetHashCode().ToString()
415-
};
416-
treeNode.Nodes.Add(newNode);
417-
Application.DoEvents();
418-
AddTreeBranchesAndLeaves(newNode, child);
419-
}
420-
}
421-
422403
private void BuildParseTreeTreeViewGuide(ITree tree)
423404
{
424405
ParseTreeView.SuspendLayout();
@@ -438,7 +419,7 @@ private void BuildParseTreeTreeViewGuide(ITree tree)
438419
ParseTreeView.ResumeLayout();
439420
}
440421

441-
private void SetNewWorkingDirectoryUsingFile(string fileName)
422+
private static void SetNewWorkingDirectoryUsingFile(string fileName)
442423
{
443424
if (!string.IsNullOrEmpty(fileName))
444425
{
@@ -464,28 +445,27 @@ private void CodeEditor_DragDrop(object sender, DragEventArgs e)
464445
CodeEditor.SelectAll();
465446
CodeEditor.Text = e.Data.GetData(DataFormats.UnicodeText).ToString();
466447
}
467-
else if (e.Data.GetDataPresent(DataFormats.FileDrop))
468-
if (e.Data.GetData(DataFormats.FileDrop) is string[] files)
448+
else if (e.Data.GetDataPresent(DataFormats.FileDrop) && e.Data.GetData(DataFormats.FileDrop) is string[] files)
449+
{
450+
SetNewWorkingDirectoryUsingFile(files[0]);
451+
var assemblyFiles = (from file in files where file.EndsWith(".dll") select file).ToList();
452+
var otherFiles = (from file in files where !file.EndsWith(".dll") select file).ToList();
453+
if (assemblyFiles.Count > 1 || (assemblyFiles.Count + otherFiles.Count > 2))
454+
{
455+
MessageBox.Show(Resources.DragDropLoadErrorMessage);
456+
return;
457+
}
458+
459+
if (assemblyFiles.Count != 0)
469460
{
470-
SetNewWorkingDirectoryUsingFile(files[0]);
471-
var assemblyFiles = (from file in files where file.EndsWith(".dll") select file).ToList();
472-
var otherFiles = (from file in files where !file.EndsWith(".dll") select file).ToList();
473-
if (assemblyFiles.Count > 1 || (assemblyFiles.Count + otherFiles.Count > 2))
474-
{
475-
MessageBox.Show(Resources.DragDropLoadErrorMessage);
476-
return;
477-
}
478-
479-
if (assemblyFiles.Count != 0)
480-
{
481-
var grammar = FetchGrammarInternal(assemblyFiles[0]);
482-
if (grammar != null)
483-
SetGrammar(grammar);
484-
}
485-
486-
if (otherFiles.Count != 0)
487-
LoadSourceFileInternal(otherFiles[0]);
461+
var grammar = FetchGrammarInternal(assemblyFiles[0]);
462+
if (grammar != null)
463+
SetGrammar(grammar);
488464
}
465+
466+
if (otherFiles.Count != 0)
467+
LoadSourceFileInternal(otherFiles[0]);
468+
}
489469
}
490470
catch (Exception ex)
491471
{
@@ -554,11 +534,8 @@ private void ConfigureGraphWorker()
554534

555535
private void ThrottleStatusChanged(object sender, EventArgs e)
556536
{
557-
if (sender is IGraphWorker worker)
558-
{
559-
if (worker.LongDelayActive && StripLabelDelay.Text != Resources.Long)
560-
StripLabelDelay.Text = Resources.Long;
561-
}
537+
if (sender is IGraphWorker worker && worker.LongDelayActive && StripLabelDelay.Text != Resources.Long)
538+
StripLabelDelay.Text = Resources.Long;
562539
}
563540

564541
private void DiagnosticsToolStripMenuItem_Click(object sender, EventArgs e)
@@ -572,7 +549,7 @@ private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
572549
Application.Exit();
573550
}
574551

575-
private IList<DetailedToken> FindTokensInRange(IList<DetailedToken> tokens, Range range)
552+
private static IList<DetailedToken> FindTokensInRange(IList<DetailedToken> tokens, Range range)
576553
{
577554
var results = new List<DetailedToken>();
578555
var startLine = range.FromLine;
@@ -591,9 +568,8 @@ private IList<DetailedToken> FindTokensInRange(IList<DetailedToken> tokens, Rang
591568

592569
private void GraphZoomTrackBar_ValueChanged(object sender, EventArgs e)
593570
{
594-
if (_EnableTrackBarZoom)
595-
if (_Viewer != null)
596-
_Viewer.ZoomF = (GraphZoomTrackBar.Value * _TrackBarZoomIncrement) + 1.0;
571+
if (_EnableTrackBarZoom && _Viewer != null)
572+
_Viewer.ZoomF = (GraphZoomTrackBar.Value * _TrackBarZoomIncrement) + 1.0;
597573
}
598574

599575
private void HeuristicHighlightingToolStripMenuItem_Click(object sender, EventArgs e)
@@ -684,7 +660,7 @@ private GrammarReference FetchGrammarInternal(string fileToSearch, string gramma
684660
MessageBox.Show(string.Format(Resources.NoGrammarsFoundInAssembly, Path.GetFileName(fileToSearch)));
685661
return null;
686662
case 1:
687-
foundGrammar = selectableGrammars.First();
663+
foundGrammar = selectableGrammars[0];
688664
break;
689665
default:
690666
{
@@ -829,9 +805,8 @@ private void ParseMessageListView_Click(object sender, EventArgs e)
829805
{
830806
OLVListItem selected;
831807
object rowObject = null;
832-
if ((selected = ParseMessageListView.SelectedItem) != null)
833-
if (selected.RowObject != null)
834-
rowObject = selected.RowObject;
808+
if ((selected = ParseMessageListView.SelectedItem) != null && selected.RowObject != null)
809+
rowObject = selected.RowObject;
835810

836811
if (rowObject != null)
837812
{
@@ -967,27 +942,25 @@ private void SelectParserRuleToolStripMenuItem_Click(object sender, EventArgs e)
967942
if (graph != null)
968943
{
969944
OLVListItem selected;
970-
if ((selected = TraceListView.SelectedItem) != null)
971-
if (selected.RowObject != null)
972-
{
973-
var traceEvent = (TraceEvent)selected.RowObject;
974-
CodeEditor.SelectSource(traceEvent.ParserRuleContext);
975-
}
945+
if ((selected = TraceListView.SelectedItem) != null && selected.RowObject != null)
946+
{
947+
var traceEvent = (TraceEvent)selected.RowObject;
948+
CodeEditor.SelectSource(traceEvent.ParserRuleContext);
949+
}
976950
}
977951
}
978952

979953
private void SelectTokenToolStripMenuItem_Click(object sender, EventArgs e)
980954
{
981955
OLVListItem selected;
982-
if ((selected = TraceListView.SelectedItem) != null)
983-
if (selected.RowObject != null)
984-
{
985-
var traceEvent = (TraceEvent)selected.RowObject;
986-
CodeEditor.SelectSource(traceEvent.Token);
987-
var model =
988-
(from token in _Tokens where token == traceEvent.Token select token).First();
989-
tokenListView.SelectObject(model);
990-
}
956+
if ((selected = TraceListView.SelectedItem) != null && selected.RowObject != null)
957+
{
958+
var traceEvent = (TraceEvent)selected.RowObject;
959+
CodeEditor.SelectSource(traceEvent.Token);
960+
var model =
961+
(from token in _Tokens where token == traceEvent.Token select token).First();
962+
tokenListView.SelectObject(model);
963+
}
991964
}
992965

993966
private void SimpleLLModeToolStripMenuItem_Click(object sender, EventArgs e)
@@ -999,12 +972,11 @@ private void SimpleLLModeToolStripMenuItem_Click(object sender, EventArgs e)
999972
private void TokenListView_Click(object sender, EventArgs e)
1000973
{
1001974
OLVListItem selected;
1002-
if ((selected = tokenListView.SelectedItem) != null)
1003-
if (selected.RowObject != null)
1004-
{
1005-
var tokenView = (DetailedToken)selected.RowObject;
1006-
CodeEditor.SelectSource(tokenView);
1007-
}
975+
if ((selected = tokenListView.SelectedItem) != null && selected.RowObject != null)
976+
{
977+
var tokenView = (DetailedToken)selected.RowObject;
978+
CodeEditor.SelectSource(tokenView);
979+
}
1008980
}
1009981

1010982
private void TraceListView_BeforeSorting(object sender, BeforeSortingEventArgs e)
@@ -1202,7 +1174,7 @@ private void selectParserRuleToolStripMenuItem1_Click(object sender, EventArgs e
12021174
var end = new Common.Grammar.Place(CodeEditor.Selection.End.iLine + 1, CodeEditor.Selection.End.iChar);
12031175

12041176
// If we don't actually have a selection, then create a selection range extending one character past the cursor
1205-
if (end.Equals(start))
1177+
if (end.Line == start.Line && end.Position == start.Position)
12061178
end = new Common.Grammar.Place(end.Line, end.Position + 1);
12071179

12081180
if (end.Line < start.Line || (end.Line == start.Line && end.Position < start.Position))

0 commit comments

Comments
 (0)