diff --git a/src/Menees.Diffs.Windows.Forms/Caret.cs b/src/Menees.Diffs.Windows.Forms/Caret.cs index 5cf1da6..aa125bb 100644 --- a/src/Menees.Diffs.Windows.Forms/Caret.cs +++ b/src/Menees.Diffs.Windows.Forms/Caret.cs @@ -129,7 +129,7 @@ public void Dispose() #region Private Methods - private void ControlGotFocus(object sender, EventArgs e) + private void ControlGotFocus(object? sender, EventArgs e) { // Sometimes in the debugger, we'll get focus without ever having been sent // the LostFocus event. So I'll fire it now just to make things balance out. @@ -146,7 +146,7 @@ private void ControlGotFocus(object sender, EventArgs e) } } - private void ControlLostFocus(object sender, EventArgs e) + private void ControlLostFocus(object? sender, EventArgs e) { if (this.createdCaret) { diff --git a/src/Menees.Diffs.Windows.Forms/DiffControl.cs b/src/Menees.Diffs.Windows.Forms/DiffControl.cs index d38b51e..ff52dab 100644 --- a/src/Menees.Diffs.Windows.Forms/DiffControl.cs +++ b/src/Menees.Diffs.Windows.Forms/DiffControl.cs @@ -49,11 +49,11 @@ public DiffControl() #region Public Events - public event EventHandler LineDiffSizeChanged; + public event EventHandler? LineDiffSizeChanged; - public event EventHandler RecompareNeeded; + public event EventHandler? RecompareNeeded; - public event EventHandler ShowTextDifferences; + public event EventHandler? ShowTextDifferences; #endregion @@ -163,11 +163,6 @@ public bool ShowToolBar } [DefaultValue(false)] - [SuppressMessage( - "Microsoft.Naming", - "CA1702:CompoundWordsShouldBeCasedCorrectly", - MessageId = "InLine", - Justification = "'inline' is not an appropriate term here.")] public bool ShowWhiteSpaceInLineDiff { get @@ -258,7 +253,7 @@ public bool CompareSelectedText() string textB = this.ViewB.SelectedText; DifferenceEventArgs diffArgs = new(textA, textB); - this.ShowTextDifferences(this, diffArgs); + this.ShowTextDifferences?.Invoke(this, diffArgs); result = true; } @@ -307,7 +302,7 @@ public bool Recompare() if (this.CanRecompare) { - this.RecompareNeeded(this, EventArgs.Empty); + this.RecompareNeeded?.Invoke(this, EventArgs.Empty); result = true; } @@ -402,7 +397,7 @@ public void ViewFile() #region Internal Methods - internal static void PaintColorLegendItem(ToolStripItem item, PaintEventArgs e) + internal static void PaintColorLegendItem(ToolStripItem? item, PaintEventArgs e) { if (item != null) { @@ -434,57 +429,57 @@ internal static void PaintColorLegendItem(ToolStripItem item, PaintEventArgs e) #region Private Methods - private void Copy_Click(object sender, EventArgs e) + private void Copy_Click(object? sender, EventArgs e) { this.Copy(); } - private void Find_Click(object sender, EventArgs e) + private void Find_Click(object? sender, EventArgs e) { this.Find(); } - private void FindNext_Click(object sender, EventArgs e) + private void FindNext_Click(object? sender, EventArgs e) { this.FindNext(); } - private void FindPrevious_Click(object sender, EventArgs e) + private void FindPrevious_Click(object? sender, EventArgs e) { this.FindPrevious(); } - private void FirstDiff_Click(object sender, EventArgs e) + private void FirstDiff_Click(object? sender, EventArgs e) { this.GoToFirstDiff(); } - private void GotoLine_Click(object sender, EventArgs e) + private void GotoLine_Click(object? sender, EventArgs e) { this.GoToLine(); } - private void LastDiff_Click(object sender, EventArgs e) + private void LastDiff_Click(object? sender, EventArgs e) { this.GoToLastDiff(); } - private void NextDiff_Click(object sender, EventArgs e) + private void NextDiff_Click(object? sender, EventArgs e) { this.GoToNextDiff(); } - private void PrevDiff_Click(object sender, EventArgs e) + private void PrevDiff_Click(object? sender, EventArgs e) { this.GoToPreviousDiff(); } - private void Recompare_Click(object sender, EventArgs e) + private void Recompare_Click(object? sender, EventArgs e) { this.Recompare(); } - private void ViewFile_Click(object sender, EventArgs e) + private void ViewFile_Click(object? sender, EventArgs e) { this.ViewFile(); } @@ -495,17 +490,17 @@ private void ColorLegend_Paint(object sender, PaintEventArgs e) PaintColorLegendItem(sender as ToolStripItem, e); } - private void DiffControl_SizeChanged(object sender, EventArgs e) + private void DiffControl_SizeChanged(object? sender, EventArgs e) { this.pnlLeft.Width = (this.Width - this.pnlLeft.Left - this.MiddleSplitter.Width) / 2; } - private void DiffOptionsChanged(object sender, EventArgs e) + private void DiffOptionsChanged(object? sender, EventArgs e) { this.UpdateColors(); } - private void TextDiff_Click(object sender, EventArgs e) + private void TextDiff_Click(object? sender, EventArgs e) { this.CompareSelectedText(); } @@ -555,11 +550,11 @@ private void UpdateLineDiff() { this.currentDiffLine = line; - DiffViewLine lineOne = null; - DiffViewLine lineTwo = null; + DiffViewLine? lineOne = null; + DiffViewLine? lineTwo = null; if (line < this.ViewA.LineCount) { - lineOne = this.ViewA.Lines[line]; + lineOne = this.ViewA.Lines?[line]; } // Normally, ViewA.LineCount == ViewB.LineCount, but during @@ -567,7 +562,7 @@ private void UpdateLineDiff() // rebuilds its lines. if (line < this.ViewB.LineCount) { - lineTwo = this.ViewB.Lines[line]; + lineTwo = this.ViewB.Lines?[line]; } if (lineOne != null && lineTwo != null) @@ -577,7 +572,7 @@ private void UpdateLineDiff() } } - private void View_PositionChanged(object sender, EventArgs e) + private void View_PositionChanged(object? sender, EventArgs e) { DiffView view = this.ActiveView; DiffViewPosition pos = view.Position; @@ -590,27 +585,27 @@ private void View_PositionChanged(object sender, EventArgs e) } } - private void ViewA_HScrollPosChanged(object sender, EventArgs e) + private void ViewA_HScrollPosChanged(object? sender, EventArgs e) { this.ViewB.HScrollPos = this.ViewA.HScrollPos; } - private void ViewA_VScrollPosChanged(object sender, EventArgs e) + private void ViewA_VScrollPosChanged(object? sender, EventArgs e) { this.ViewB.VScrollPos = this.ViewA.VScrollPos; } - private void ViewB_HScrollPosChanged(object sender, EventArgs e) + private void ViewB_HScrollPosChanged(object? sender, EventArgs e) { this.ViewA.HScrollPos = this.ViewB.HScrollPos; } - private void ViewB_VScrollPosChanged(object sender, EventArgs e) + private void ViewB_VScrollPosChanged(object? sender, EventArgs e) { this.ViewA.VScrollPos = this.ViewB.VScrollPos; } - private void ViewLineDiff_SizeChanged(object sender, EventArgs e) + private void ViewLineDiff_SizeChanged(object? sender, EventArgs e) { this.LineDiffSizeChanged?.Invoke(this, e); } diff --git a/src/Menees.Diffs.Windows.Forms/DiffOptions.cs b/src/Menees.Diffs.Windows.Forms/DiffOptions.cs index c391c9f..a28f413 100644 --- a/src/Menees.Diffs.Windows.Forms/DiffOptions.cs +++ b/src/Menees.Diffs.Windows.Forms/DiffOptions.cs @@ -37,7 +37,7 @@ public static class DiffOptions #region Public Events - public static event EventHandler OptionsChanged; + public static event EventHandler? OptionsChanged; #endregion @@ -211,9 +211,9 @@ public static void Save(ISettingsNode node) node.SetValue(nameof(HatchDeadSpace), HatchDeadSpace); } - public static Brush TryCreateDeadSpaceBrush(Color backColor) + public static Brush? TryCreateDeadSpaceBrush(Color backColor) { - Brush result = null; + Brush? result = null; if (HatchDeadSpace) { result = new HatchBrush(HatchStyle.Percent25, SystemColors.ControlDark, backColor); diff --git a/src/Menees.Diffs.Windows.Forms/DiffOverview.cs b/src/Menees.Diffs.Windows.Forms/DiffOverview.cs index 034ed23..0cb61a8 100644 --- a/src/Menees.Diffs.Windows.Forms/DiffOverview.cs +++ b/src/Menees.Diffs.Windows.Forms/DiffOverview.cs @@ -21,8 +21,8 @@ internal sealed class DiffOverview : Control private readonly BorderStyle borderStyle = BorderStyle.Fixed3D; private bool dragging; private bool useTranslucentView = true; - private Bitmap image; - private DiffView view; + private Bitmap? image; + private DiffView? view; private Rectangle viewRect; #endregion @@ -52,17 +52,13 @@ public DiffOverview() /// /// Fired when the user clicks and/or drags to move the view. /// - public event EventHandler LineClick; + public event EventHandler? LineClick; #endregion #region Public Properties - [SuppressMessage( - "Microsoft.Performance", - "CA1811:AvoidUncalledPrivateCode", - Justification = "The get_DiffView accessor is only called by the Windows Forms designer via reflection.")] - public DiffView DiffView + public DiffView? DiffView { get { @@ -212,7 +208,7 @@ protected override void OnPaint(PaintEventArgs e) // Repaint the view window if any of it is invalid if (r.IntersectsWith(this.viewRect)) { - Pen disposablePen = null; + Pen? disposablePen = null; try { Pen pen = SystemPens.Highlight; @@ -289,7 +285,7 @@ private void CalculateViewRect() } } - private void DiffOptionsChanged(object sender, EventArgs e) + private void DiffOptionsChanged(object? sender, EventArgs e) { // The diff colors changed, so we need to rerender the // image. The current view rect should still be valid. @@ -297,13 +293,13 @@ private void DiffOptionsChanged(object sender, EventArgs e) this.Invalidate(); } - private void DiffView_LinesChanged(object sender, EventArgs e) + private void DiffView_LinesChanged(object? sender, EventArgs e) { // If the Lines changed, we need to update everything. this.UpdateAll(); } - private void DiffView_SizeChanged(object sender, EventArgs e) + private void DiffView_SizeChanged(object? sender, EventArgs e) { // If the DiffView size changed, then our view window // may be longer or shorter, but the rendered image is @@ -313,7 +309,7 @@ private void DiffView_SizeChanged(object sender, EventArgs e) this.Invalidate(); } - private void DiffView_VScrollPosChanged(object sender, EventArgs e) + private void DiffView_VScrollPosChanged(object? sender, EventArgs e) { // The DiffView's FirstVisibleLine has changed, so we // just need to invalidate our view. @@ -425,7 +421,7 @@ private void RenderImage() // Draw delete on the left and dead space on the right. g.FillRectangle(backBrush, GutterWidth, y, fullFillWidth / 2, lineHeight); - using (Brush deadBrush = DiffOptions.TryCreateDeadSpaceBrush(backBrush.Color)) + using (Brush? deadBrush = DiffOptions.TryCreateDeadSpaceBrush(backBrush.Color)) { g.FillRectangle(deadBrush ?? backBrush, GutterWidth + (fullFillWidth / 2), y, fullFillWidth / 2, lineHeight); } @@ -435,7 +431,7 @@ private void RenderImage() case EditType.Insert: // Draw dead space on the left and insert on the right. - using (Brush deadBrush = DiffOptions.TryCreateDeadSpaceBrush(backBrush.Color)) + using (Brush? deadBrush = DiffOptions.TryCreateDeadSpaceBrush(backBrush.Color)) { g.FillRectangle(deadBrush ?? backBrush, GutterWidth, y, fullFillWidth / 2, lineHeight); } diff --git a/src/Menees.Diffs.Windows.Forms/DiffView.Privates.cs b/src/Menees.Diffs.Windows.Forms/DiffView.Privates.cs index a940ce7..2f02120 100644 --- a/src/Menees.Diffs.Windows.Forms/DiffView.Privates.cs +++ b/src/Menees.Diffs.Windows.Forms/DiffView.Privates.cs @@ -33,14 +33,14 @@ internal sealed partial class DiffView : Control private readonly Timer autoScrollTimer; private bool capturedMouse; private bool showWhitespace; - private Caret caret; + private Caret? caret; private int charWidth = 1; private int gutterWidth = 1; private int horizontalAutoScrollAmount; private int lineHeight = 1; private int verticalAutoScrollAmount; private int wheelDelta; - private DiffViewLines lines; + private DiffViewLines? lines; private DiffViewPosition position; private DiffViewPosition selectionStart = DiffViewPosition.Empty; private string gutterFormat = "{0}"; @@ -103,7 +103,7 @@ private static int MeasureString(Graphics g, string displayText, int length, Fon return result.Width; } - private void AutoScrollTimer_Tick(object sender, EventArgs e) + private void AutoScrollTimer_Tick(object? sender, EventArgs e) { this.VScrollPos += this.verticalAutoScrollAmount; this.HScrollPos += this.horizontalAutoScrollAmount; @@ -124,7 +124,7 @@ private void ClearSelection() } } - private void DiffOptionsChanged(object sender, EventArgs e) + private void DiffOptionsChanged(object? sender, EventArgs e) { // The colors and/or tab width changed. this.UpdateTextMetrics(true); @@ -140,7 +140,7 @@ private void DrawBackground(Graphics g, SolidBrush brush, int y, bool deadSpace) { if (deadSpace) { - using (Brush deadBrush = DiffOptions.TryCreateDeadSpaceBrush(brush.Color)) + using (Brush? deadBrush = DiffOptions.TryCreateDeadSpaceBrush(brush.Color)) { // If hatching is turned off, then we have to fallback to the solid brush. g.FillRectangle(deadBrush ?? brush, this.gutterWidth, y, this.ClientSize.Width, this.lineHeight); @@ -271,7 +271,7 @@ private void DrawLine( // extra work until the user requests to see the changed line. It's still // the same amount of work if they view every line, but it makes the // user interface more responsive to split it up like this. - EditScript changeEditScript = line.GetChangeEditScript(this.ChangeDiffOptions); + EditScript? changeEditScript = line.GetChangeEditScript(this.ChangeDiffOptions); if (changeEditScript != null) { this.DrawChangedLineBackground(g, displayLine, lineText, changeEditScript, line.FromA, x, y); @@ -433,7 +433,7 @@ private void FireSelectionChanged() private DisplayLine GetDisplayLine(int line) { DisplayLine result; - if (line >= 0 && line < this.LineCount) + if (line >= 0 && line < this.LineCount && this.lines != null) { result = this.GetDisplayLine(this.lines[line]); } @@ -499,7 +499,7 @@ private void GetForwardOrderSelection(out DiffViewPosition startSel, out DiffVie } } - private bool GetSingleLineSelectedText(out string text) + private bool GetSingleLineSelectedText(out string? text) { text = null; bool result = false; @@ -518,7 +518,7 @@ private bool GetSingleLineSelectedText(out string text) return result; } - private int GetXForColumn(Graphics g, DisplayLine displayLine, string displayText, int column) + private int GetXForColumn(Graphics g, DisplayLine displayLine, string? displayText, int column) { if (displayText == null) { @@ -884,7 +884,7 @@ private void UpdateTextMetrics(bool fontOrTabsChanged) // pixel so we can have a separator line, and then // a small separator window-colored area. int maxLineNumChars = 1; - if (this.LineCount > 0) + if (this.LineCount > 0 && this.lines != null) { // Get the largest number. Add 1 to it because we will // when we display it. This is important when the number @@ -909,7 +909,7 @@ private void UpdateTextMetrics(bool fontOrTabsChanged) StringBuilder sb = new(BufferSize); sb.Append("{0:"); sb.Append('0', maxLineNumChars); - sb.Append("}"); + sb.Append('}'); this.gutterFormat = sb.ToString(); // Update the caret position (Gutter width or Font changes affect it) diff --git a/src/Menees.Diffs.Windows.Forms/DiffView.cs b/src/Menees.Diffs.Windows.Forms/DiffView.cs index de62b73..f0d6d47 100644 --- a/src/Menees.Diffs.Windows.Forms/DiffView.cs +++ b/src/Menees.Diffs.Windows.Forms/DiffView.cs @@ -24,10 +24,6 @@ internal sealed partial class DiffView : Control { #region Constructors - [SuppressMessage( - "Microsoft.Mobility", - "CA1601:DoNotUseTimersThatPreventPowerStateChanges", - Justification = "This timer is only used to auto-scroll while the mouse is captured and dragging.")] public DiffView() { // Set some important control styles @@ -44,9 +40,11 @@ public DiffView() this.UpdateTextMetrics(true); - this.autoScrollTimer = new Timer(); - this.autoScrollTimer.Enabled = false; - this.autoScrollTimer.Interval = 100; + this.autoScrollTimer = new Timer + { + Enabled = false, + Interval = 100, + }; this.autoScrollTimer.Tick += this.AutoScrollTimer_Tick; DiffOptions.OptionsChanged += this.DiffOptionsChanged; @@ -58,15 +56,15 @@ public DiffView() #region Public Events - public event EventHandler HScrollPosChanged; + public event EventHandler? HScrollPosChanged; - public event EventHandler LinesChanged; + public event EventHandler? LinesChanged; - public event EventHandler PositionChanged; + public event EventHandler? PositionChanged; - public event EventHandler SelectionChanged; + public event EventHandler? SelectionChanged; - public event EventHandler VScrollPosChanged; + public event EventHandler? VScrollPosChanged; #endregion @@ -143,10 +141,6 @@ public bool CanGoToPreviousDiff } [Browsable(false)] - [SuppressMessage( - "Microsoft.Performance", - "CA1811:AvoidUncalledPrivateCode", - Justification = "The get_CenterVisibleLine accessor is only called by the Windows Forms designer via reflection.")] public int CenterVisibleLine { get @@ -206,7 +200,7 @@ public int HScrollPos /// Stores each line's text, color, and original number. /// [Browsable(false)] - public DiffViewLines Lines => this.lines; + public DiffViewLines? Lines => this.lines; [Browsable(false)] public DiffViewPosition Position @@ -336,7 +330,7 @@ public bool Find(FindData data) { // If text is selected on a single line, then use that for the new Find text. string originalFindText = data.Text; - if (this.GetSingleLineSelectedText(out string selectedText)) + if (this.GetSingleLineSelectedText(out string? selectedText) && selectedText != null) { data.Text = selectedText; } @@ -617,7 +611,7 @@ public bool GoToFirstDiff() { bool result = false; - if (this.CanGoToFirstDiff) + if (this.CanGoToFirstDiff && this.lines != null) { this.GoToPosition(this.lines.DiffStartLines[0], this.position.Column); result = true; @@ -630,7 +624,7 @@ public bool GoToLastDiff() { bool result = false; - if (this.CanGoToLastDiff) + if (this.CanGoToLastDiff && this.lines != null) { int[] starts = this.lines.DiffStartLines; this.GoToPosition(starts[starts.Length - 1], this.position.Column); @@ -643,14 +637,17 @@ public bool GoToLastDiff() public bool GoToLine() { int maxLineNumber = 0; - for (int i = this.lines.Count - 1; i >= 0; i--) + if (this.lines != null) { - DiffViewLine line = this.lines[i]; - if (line.Number.HasValue) + for (int i = this.lines.Count - 1; i >= 0; i--) { - // Add 1 because display numbers are 1-based. - maxLineNumber = line.Number.Value + 1; - break; + DiffViewLine line = this.lines[i]; + if (line.Number.HasValue) + { + // Add 1 because display numbers are 1-based. + maxLineNumber = line.Number.Value + 1; + break; + } } } @@ -695,7 +692,7 @@ public bool GoToNextDiff() { bool result = false; - if (this.CanGoToNextDiff) + if (this.CanGoToNextDiff && this.lines != null) { int[] starts = this.lines.DiffStartLines; int numStarts = starts.Length; @@ -717,7 +714,7 @@ public bool GoToPreviousDiff() { bool result = false; - if (this.CanGoToPreviousDiff) + if (this.CanGoToPreviousDiff && this.lines != null) { int[] ends = this.lines.DiffEndLines; int numEnds = ends.Length; @@ -795,14 +792,17 @@ public void SetCounterpartLines(DiffView counterpartView) throw new ArgumentException("The counterpart view has a different number of view lines.", nameof(counterpartView)); } - for (int i = 0; i < numLines; i++) + if (this.lines != null && counterpartView.lines != null) { - DiffViewLine line = this.lines[i]; - DiffViewLine counterpart = counterpartView.lines[i]; + for (int i = 0; i < numLines; i++) + { + DiffViewLine line = this.lines[i]; + DiffViewLine counterpart = counterpartView.lines[i]; - // Make the counterpart lines refer to each other. - line.Counterpart = counterpart; - counterpart.Counterpart = line; + // Make the counterpart lines refer to each other. + line.Counterpart = counterpart; + counterpart.Counterpart = line; + } } } @@ -1144,23 +1144,26 @@ protected override void OnPaint(PaintEventArgs e) this.GetForwardOrderSelection(out DiffViewPosition startSel, out DiffViewPosition endSel); // Paint each line - for (int i = firstLine; i <= lastLine; i++) + if (this.lines != null) { - // If we get inside this loop there must be at least one line. - Debug.Assert(this.LineCount > 0, "There must be at least one line."); + for (int i = firstLine; i <= lastLine; i++) + { + // If we get inside this loop there must be at least one line. + Debug.Assert(this.LineCount > 0, "There must be at least one line."); - int x = (this.charWidth * (-posX)) + this.gutterWidth; - int y = this.lineHeight * (i - posY); + int x = (this.charWidth * (-posX)) + this.gutterWidth; + int y = this.lineHeight * (i - posY); - DiffViewLine line = this.lines[i]; - if (paintLine) - { - this.DrawLine(g, fontBrush, backBrush, hasFocus, i, line, x, y, hasSelection, startSel, endSel); - } + DiffViewLine line = this.lines[i]; + if (paintLine) + { + this.DrawLine(g, fontBrush, backBrush, hasFocus, i, line, x, y, hasSelection, startSel, endSel); + } - if (paintGutter) - { - this.DrawGutter(g, fontBrush, backBrush, hasFocus, i, line, y, gutterBrush, lineNumIndent); + if (paintGutter) + { + this.DrawGutter(g, fontBrush, backBrush, hasFocus, i, line, y, gutterBrush, lineNumIndent); + } } } diff --git a/src/Menees.Diffs.Windows.Forms/DiffViewLine.cs b/src/Menees.Diffs.Windows.Forms/DiffViewLine.cs index 249fbea..b7fa7f2 100644 --- a/src/Menees.Diffs.Windows.Forms/DiffViewLine.cs +++ b/src/Menees.Diffs.Windows.Forms/DiffViewLine.cs @@ -25,7 +25,7 @@ internal sealed class DiffViewLine private readonly int? number; private readonly string text; private readonly EditType editType; - private EditScript changeEditScript; + private EditScript? changeEditScript; #endregion @@ -48,9 +48,8 @@ private DiffViewLine() #region Public Properties - public DiffViewLine Counterpart { get; internal set; } + public DiffViewLine? Counterpart { get; internal set; } - [SuppressMessage("", "SA1101", Justification = "The EditType reference is to the type not to this.EditType.")] public bool Edited => this.editType != EditType.None; public EditType EditType => this.editType; @@ -65,7 +64,7 @@ private DiffViewLine() #region Public Methods - public EditScript GetChangeEditScript(ChangeDiffOptions options) + public EditScript? GetChangeEditScript(ChangeDiffOptions options) { if (this.changeEditScript == null && this.editType == EditType.Change && this.Counterpart != null) { @@ -188,7 +187,9 @@ public void Clear() throw new NotSupportedException(); } +#pragma warning disable CA2249 // Consider using 'string.Contains' instead of 'string.IndexOf'. net48 doesn't support Contains(string). public bool Contains(char item) => this.text.IndexOf(item) >= 0; +#pragma warning restore CA2249 // Consider using 'string.Contains' instead of 'string.IndexOf' public void CopyTo(char[] array, int arrayIndex) { diff --git a/src/Menees.Diffs.Windows.Forms/DiffViewLines.cs b/src/Menees.Diffs.Windows.Forms/DiffViewLines.cs index 66e8b5b..c44cb85 100644 --- a/src/Menees.Diffs.Windows.Forms/DiffViewLines.cs +++ b/src/Menees.Diffs.Windows.Forms/DiffViewLines.cs @@ -84,15 +84,14 @@ public DiffViewLines(DiffViewLine lineOne, DiffViewLine lineTwo) { this.AddLine(lineOne); this.AddLine(lineTwo); - - this.diffStartLines = CollectionUtility.EmptyArray(); - this.diffEndLines = CollectionUtility.EmptyArray(); } private DiffViewLines() : base(new List()) { // Called by the other constructors. + this.diffStartLines = CollectionUtility.EmptyArray(); + this.diffEndLines = CollectionUtility.EmptyArray(); } #endregion diff --git a/src/Menees.Diffs.Windows.Forms/DiffViewPosition.cs b/src/Menees.Diffs.Windows.Forms/DiffViewPosition.cs index 27ab2a4..1fc9cef 100644 --- a/src/Menees.Diffs.Windows.Forms/DiffViewPosition.cs +++ b/src/Menees.Diffs.Windows.Forms/DiffViewPosition.cs @@ -89,7 +89,7 @@ public int CompareTo(DiffViewPosition position) return result; } - public override bool Equals(object value) + public override bool Equals(object? value) { bool result = false; diff --git a/src/Menees.Diffs.Windows.Forms/DirectoryDiffControl.cs b/src/Menees.Diffs.Windows.Forms/DirectoryDiffControl.cs index 46922cf..8bee5e6 100644 --- a/src/Menees.Diffs.Windows.Forms/DirectoryDiffControl.cs +++ b/src/Menees.Diffs.Windows.Forms/DirectoryDiffControl.cs @@ -43,9 +43,9 @@ public DirectoryDiffControl() #region Public Events - public event EventHandler RecompareNeeded; + public event EventHandler? RecompareNeeded; - public event EventHandler ShowFileDifferences; + public event EventHandler? ShowFileDifferences; #endregion @@ -58,7 +58,7 @@ public bool CanShowDifferences { get { - DirectoryDiffEntry entry = this.SelectedEntry; + DirectoryDiffEntry? entry = this.SelectedEntry; bool result = entry != null && entry.IsFile && entry.InA && entry.InB && this.ShowFileDifferences != null; return result; } @@ -134,12 +134,12 @@ public bool ShowToolBar #region Private Properties - private DirectoryDiffEntry SelectedEntry + private DirectoryDiffEntry? SelectedEntry { get { TreeNode selectedNode = this.TreeB.Focused ? this.TreeB.SelectedNode : this.TreeA.SelectedNode; - DirectoryDiffEntry result = DirectoryDiffTreeView.GetEntryForNode(selectedNode); + DirectoryDiffEntry? result = DirectoryDiffTreeView.GetEntryForNode(selectedNode); return result; } } @@ -154,7 +154,7 @@ public bool Recompare() if (this.CanRecompare) { - this.RecompareNeeded(this, EventArgs.Empty); + this.RecompareNeeded?.Invoke(this, EventArgs.Empty); result = true; } @@ -192,13 +192,18 @@ public void ShowDifferences() { if (this.CanShowDifferences) { - DirectoryDiffEntry entry = this.SelectedEntry; - - GetNodes(entry, out TreeNode nodeA, out TreeNode nodeB); - string fileA = this.TreeA.GetFullNameForNode(nodeA); - string fileB = this.TreeB.GetFullNameForNode(nodeB); + DirectoryDiffEntry? entry = this.SelectedEntry; + if (entry != null) + { + GetNodes(entry, out TreeNode? nodeA, out TreeNode? nodeB); + if (nodeA != null && nodeB != null) + { + string fileA = this.TreeA.GetFullNameForNode(nodeA); + string fileB = this.TreeB.GetFullNameForNode(nodeB); - this.ShowFileDifferences(this, new DifferenceEventArgs(fileA, fileB)); + this.ShowFileDifferences?.Invoke(this, new DifferenceEventArgs(fileA, fileB)); + } + } } } @@ -214,10 +219,10 @@ public void View() #region Private Methods - private static void GetNodes(DirectoryDiffEntry entry, out TreeNode nodeA, out TreeNode nodeB) + private static void GetNodes(DirectoryDiffEntry entry, out TreeNode? nodeA, out TreeNode? nodeB) { - nodeA = (TreeNode)entry.TagA; - nodeB = (TreeNode)entry.TagB; + nodeA = (TreeNode?)entry.TagA; + nodeB = (TreeNode?)entry.TagB; } private static bool IsScrollingKey(KeyEventArgs e) @@ -242,43 +247,43 @@ private static bool IsScrollingKey(KeyEventArgs e) return result; } - private void Recompare_Click(object sender, EventArgs e) + private void Recompare_Click(object? sender, EventArgs e) { this.Recompare(); } - private void ShowDifferences_Click(object sender, EventArgs e) + private void ShowDifferences_Click(object? sender, EventArgs e) { this.ShowDifferences(); } - private void View_Click(object sender, EventArgs e) + private void View_Click(object? sender, EventArgs e) { this.View(); } [SuppressMessage("Design", "CC0091:Use static method", Justification = "Windows Forms designer prefers non-static.")] - private void ColorLegend_Paint(object sender, PaintEventArgs e) + private void ColorLegend_Paint(object? sender, PaintEventArgs e) { DiffControl.PaintColorLegendItem(sender as ToolStripItem, e); } - private void DiffOptionsChanged(object sender, EventArgs e) + private void DiffOptionsChanged(object? sender, EventArgs e) { this.UpdateColors(); } - private void DirDiffControl_SizeChanged(object sender, EventArgs e) + private void DirDiffControl_SizeChanged(object? sender, EventArgs e) { this.pnlLeft.Width = (this.Width - this.Splitter.Width) / 2; } private void SyncTreeViewScrollPositions(DirectoryDiffTreeView source) { - DirectoryDiffEntry entry = DirectoryDiffTreeView.GetEntryForNode(source.TopNode); + DirectoryDiffEntry? entry = DirectoryDiffTreeView.GetEntryForNode(source.TopNode); if (entry != null) { - GetNodes(entry, out TreeNode nodeA, out TreeNode nodeB); + GetNodes(entry, out TreeNode? nodeA, out TreeNode? nodeB); if (nodeA != null && nodeB != null) { this.TreeA.TopNode = nodeA; @@ -308,12 +313,12 @@ private void TreeA_KeyDown(object sender, KeyEventArgs e) } } - private void TreeA_MouseWheelMsg(object sender, EventArgs e) + private void TreeA_MouseWheelMsg(object? sender, EventArgs e) { this.SyncTreeViewScrollPositions(this.TreeA); } - private void TreeA_VScroll(object sender, EventArgs e) + private void TreeA_VScroll(object? sender, EventArgs e) { this.SyncTreeViewScrollPositions(this.TreeA); } @@ -327,22 +332,22 @@ private void TreeB_KeyDown(object sender, KeyEventArgs e) } } - private void TreeB_MouseWheelMsg(object sender, EventArgs e) + private void TreeB_MouseWheelMsg(object? sender, EventArgs e) { this.SyncTreeViewScrollPositions(this.TreeB); } - private void TreeB_VScroll(object sender, EventArgs e) + private void TreeB_VScroll(object? sender, EventArgs e) { this.SyncTreeViewScrollPositions(this.TreeB); } private void TreeNode_SelectChanged(object sender, TreeViewEventArgs e) { - DirectoryDiffEntry entry = DirectoryDiffTreeView.GetEntryForNode(e.Node); + DirectoryDiffEntry? entry = DirectoryDiffTreeView.GetEntryForNode(e.Node); if (entry != null) { - GetNodes(entry, out TreeNode nodeA, out TreeNode nodeB); + GetNodes(entry, out TreeNode? nodeA, out TreeNode? nodeB); if (nodeA != null && nodeB != null) { this.TreeA.SelectedNode = nodeA; @@ -356,10 +361,10 @@ private void TreeNode_SelectChanged(object sender, TreeViewEventArgs e) [SuppressMessage("Design", "CC0091:Use static method", Justification = "Windows Forms designer prefers non-static.")] private void TreeNode_StateChange(object sender, TreeViewEventArgs e) { - DirectoryDiffEntry entry = DirectoryDiffTreeView.GetEntryForNode(e.Node); + DirectoryDiffEntry? entry = DirectoryDiffTreeView.GetEntryForNode(e.Node); if (entry != null) { - GetNodes(entry, out TreeNode nodeA, out TreeNode nodeB); + GetNodes(entry, out TreeNode? nodeA, out TreeNode? nodeB); if (nodeA != null && nodeB != null) { if (e.Action == TreeViewAction.Collapse) @@ -394,14 +399,14 @@ private void TreeNode_StateChange(object sender, TreeViewEventArgs e) } } - private void TreeView_DoubleClick(object sender, EventArgs e) + private void TreeView_DoubleClick(object? sender, EventArgs e) { this.ShowDifferences(); } - private void TreeView_Enter(object sender, EventArgs e) + private void TreeView_Enter(object? sender, EventArgs e) { - this.activeTree = (DirectoryDiffTreeView)sender; + this.activeTree = (DirectoryDiffTreeView)sender!; this.UpdateButtons(); } diff --git a/src/Menees.Diffs.Windows.Forms/DirectoryDiffTreeView.cs b/src/Menees.Diffs.Windows.Forms/DirectoryDiffTreeView.cs index c7a61e4..94b4c0c 100644 --- a/src/Menees.Diffs.Windows.Forms/DirectoryDiffTreeView.cs +++ b/src/Menees.Diffs.Windows.Forms/DirectoryDiffTreeView.cs @@ -24,7 +24,7 @@ internal sealed class DirectoryDiffTreeView : TreeView private const int FolderOpenIndex = 1; private bool useA; - private DirectoryDiffResults results; + private DirectoryDiffResults? results; #endregion @@ -43,9 +43,9 @@ public DirectoryDiffTreeView() #region Internal Events - internal event EventHandler MouseWheelMsg; + internal event EventHandler? MouseWheelMsg; - internal event EventHandler VScroll; + internal event EventHandler? VScroll; #endregion @@ -60,7 +60,7 @@ public bool CanView TreeNode node = this.SelectedNode; if (node != null) { - DirectoryDiffEntry entry = GetEntryForNode(node); + DirectoryDiffEntry? entry = GetEntryForNode(node); if (entry != null) { result = (this.useA && entry.InA) || (!this.useA && entry.InB); @@ -75,18 +75,23 @@ public bool CanView #region Public Methods - public static DirectoryDiffEntry GetEntryForNode(TreeNode node) => node?.Tag as DirectoryDiffEntry; + public static DirectoryDiffEntry? GetEntryForNode(TreeNode? node) => node?.Tag as DirectoryDiffEntry; public string GetFullNameForNode(TreeNode node) { - string nodePath = node.FullPath; - string basePath = this.useA ? this.results.DirectoryA.FullName : this.results.DirectoryB.FullName; - if (!basePath.EndsWith("\\")) + string result = node.FullPath; + if (this.results != null) { - basePath += "\\"; + string basePath = this.useA ? this.results.DirectoryA.FullName : this.results.DirectoryB.FullName; + if (!basePath.EndsWith("\\")) + { + basePath += "\\"; + } + + result = basePath + result; } - return basePath + nodePath; + return result; } public void SetData(DirectoryDiffResults results, bool useA) @@ -122,13 +127,25 @@ protected override void Dispose(bool disposing) protected override void OnAfterCollapse(TreeViewEventArgs e) { - this.SetNodeImage(e.Node, GetEntryForNode(e.Node)); + TreeNode? node = e.Node; + DirectoryDiffEntry? entry = GetEntryForNode(node); + if (node != null && entry != null) + { + this.SetNodeImage(node, entry); + } + base.OnAfterCollapse(e); } protected override void OnAfterExpand(TreeViewEventArgs e) { - this.SetNodeImage(e.Node, GetEntryForNode(e.Node)); + TreeNode? node = e.Node; + DirectoryDiffEntry? entry = GetEntryForNode(node); + if (node != null && entry != null) + { + this.SetNodeImage(node, entry); + } + base.OnAfterExpand(e); } @@ -161,7 +178,7 @@ protected override void WndProc(ref Message m) #region Private Methods - private void AddEntry(DirectoryDiffEntry entry, TreeNode parentNode) + private void AddEntry(DirectoryDiffEntry entry, TreeNode? parentNode) { TreeNode node = new() { @@ -194,14 +211,14 @@ private void AddEntry(DirectoryDiffEntry entry, TreeNode parentNode) if (!entry.IsFile) { - foreach (DirectoryDiffEntry subEntry in entry.Subentries) + foreach (DirectoryDiffEntry subEntry in entry.Subentries!) { this.AddEntry(subEntry, node); } } } - private void DiffOptionsChanged(object sender, EventArgs e) + private void DiffOptionsChanged(object? sender, EventArgs e) { this.SetNodesColors(this.Nodes); } @@ -213,9 +230,12 @@ private void PopulateTree() { this.Nodes.Clear(); - foreach (DirectoryDiffEntry entry in this.results.Entries) + if (this.results != null) { - this.AddEntry(entry, null); + foreach (DirectoryDiffEntry entry in this.results.Entries) + { + this.AddEntry(entry, null); + } } this.ExpandAll(); @@ -280,7 +300,7 @@ private void SetNodeImage(TreeNode node, DirectoryDiffEntry entry) // // Also, we should only show a folder open if we're // showing recursive differences. - if (this.results.Recursive && node.IsExpanded && entry.InA && entry.InB) + if (this.results != null && this.results.Recursive && node.IsExpanded && entry.InA && entry.InB) { index = FolderOpenIndex; } @@ -302,7 +322,12 @@ private void SetNodesColors(TreeNodeCollection nodes) { foreach (TreeNode node in nodes) { - this.SetNodeColor(node, GetEntryForNode(node)); + DirectoryDiffEntry? entry = GetEntryForNode(node); + if (entry != null) + { + this.SetNodeColor(node, entry); + } + this.SetNodesColors(node.Nodes); } } diff --git a/src/Menees.Diffs.Windows.Forms/Menees.Diffs.Windows.Forms.csproj b/src/Menees.Diffs.Windows.Forms/Menees.Diffs.Windows.Forms.csproj index 2d8a1fd..2126275 100644 --- a/src/Menees.Diffs.Windows.Forms/Menees.Diffs.Windows.Forms.csproj +++ b/src/Menees.Diffs.Windows.Forms/Menees.Diffs.Windows.Forms.csproj @@ -1,10 +1,11 @@ - + $(MeneesTargetNetCoreWindows);$(MeneesTargetNetFramework) true true Windows Forms controls for showing diffs of text, files, and directories + enable @@ -27,4 +28,9 @@ + + + IDE0079 + +