From 749f3b964035f93934f53d41d5dbcc2b8b16b67a Mon Sep 17 00:00:00 2001 From: Henrik Gedionsen Date: Thu, 11 Jun 2026 23:54:34 +0200 Subject: [PATCH] Made some classes sealed, made it possible to pass some structs through refs instead of copying & collection tweaks --- .../Document/LineManager/LineSegmentTree.cs | 4 ++-- .../Src/Document/MarkerStrategy/TextMarker.cs | 4 ++-- .../Document/Selection/SelectionManager.cs | 21 ++++++++++--------- .../GapTextBufferStrategy.cs | 4 ++-- Project/Src/Gui/AbstractMargin.cs | 4 ++-- Project/Src/Gui/Caret.cs | 4 ++-- Project/Src/Gui/DrawableLine.cs | 2 +- Project/Src/Gui/FoldMargin.cs | 4 ++-- Project/Src/Gui/GutterMargin.cs | 2 +- Project/Src/Gui/IconBarMargin.cs | 2 +- Project/Src/Gui/Ime.cs | 12 +++++------ .../Src/Gui/InsightWindow/InsightWindow.cs | 4 ++-- Project/Src/Gui/TextEditorControl.cs | 4 ++-- Project/Src/Gui/TextView.cs | 12 +++++------ Project/Src/Undo/UndoStack.cs | 4 ++-- Project/Src/Util/LookupTable.cs | 4 ++-- Project/Src/Util/MouseWheelHandler.cs | 4 ++-- Project/Src/Util/TipSpacer.cs | 4 ++-- Project/Src/Util/TipSplitter.cs | 4 ++-- Project/Src/Util/TipText.cs | 2 +- 20 files changed, 53 insertions(+), 52 deletions(-) diff --git a/Project/Src/Document/LineManager/LineSegmentTree.cs b/Project/Src/Document/LineManager/LineSegmentTree.cs index 48afb8b..6dfc559 100644 --- a/Project/Src/Document/LineManager/LineSegmentTree.cs +++ b/Project/Src/Document/LineManager/LineSegmentTree.cs @@ -260,7 +260,7 @@ public Enumerator GetEnumeratorForOffset(int offset) internal struct RBNode { - internal LineSegment lineSegment; + internal readonly LineSegment lineSegment; internal int count; internal int totalLength; @@ -449,4 +449,4 @@ public string GetTreeAsString() } #endif } -} \ No newline at end of file +} diff --git a/Project/Src/Document/MarkerStrategy/TextMarker.cs b/Project/Src/Document/MarkerStrategy/TextMarker.cs index c044283..d139b00 100644 --- a/Project/Src/Document/MarkerStrategy/TextMarker.cs +++ b/Project/Src/Document/MarkerStrategy/TextMarker.cs @@ -31,7 +31,7 @@ public TextMarker(int offset, int length, TextMarkerType textMarkerType) : this( { } - public TextMarker(int offset, int length, TextMarkerType textMarkerType, Color color) + public TextMarker(int offset, int length, TextMarkerType textMarkerType, in Color color) { if (length < 1) length = 1; this.offset = offset; @@ -40,7 +40,7 @@ public TextMarker(int offset, int length, TextMarkerType textMarkerType, Color c Color = color; } - public TextMarker(int offset, int length, TextMarkerType textMarkerType, Color color, Color foreColor) + public TextMarker(int offset, int length, TextMarkerType textMarkerType, in Color color, in Color foreColor) { if (length < 1) length = 1; this.offset = offset; diff --git a/Project/Src/Document/Selection/SelectionManager.cs b/Project/Src/Document/Selection/SelectionManager.cs index 618142e..655193d 100644 --- a/Project/Src/Document/Selection/SelectionManager.cs +++ b/Project/Src/Document/Selection/SelectionManager.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; +using System.Runtime.InteropServices; using System.Text; namespace ICSharpCode.TextEditor.Document @@ -21,7 +22,7 @@ public class SelectionManager : IDisposable private IDocument document; internal SelectFrom selectFrom = new SelectFrom(); - internal List selectionCollection = new List(); + internal readonly List selectionCollection = []; private TextLocation selectionStart; /// @@ -69,7 +70,7 @@ public bool SelectionIsReadonly { if (document.ReadOnly) return true; - foreach (var sel in selectionCollection) + foreach (ISelection sel in CollectionsMarshal.AsSpan(selectionCollection)) if (SelectionIsReadOnly(document, sel)) return true; return false; @@ -87,7 +88,7 @@ public string SelectedText // PriorityQueue queue = new PriorityQueue(); - foreach (var s in selectionCollection) builder.Append(s.SelectedText); + foreach (ISelection s in CollectionsMarshal.AsSpan(selectionCollection)) builder.Append(s.SelectedText); // queue.Insert(-s.Offset, s); // while (queue.Count > 0) { @@ -297,7 +298,7 @@ public void RemoveSelectedText() var offset = -1; var oneLine = true; // PriorityQueue queue = new PriorityQueue(); - foreach (var s in selectionCollection) + foreach (ISelection s in CollectionsMarshal.AsSpan(selectionCollection)) { // ISelection s = ((ISelection)queue.Remove()); if (oneLine) @@ -325,7 +326,7 @@ public void RemoveSelectedText() if (offset != -1) { if (oneLine) - foreach (var i in lines) + foreach (int i in CollectionsMarshal.AsSpan(lines)) document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.SingleLine, i)); else document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); @@ -358,7 +359,7 @@ public bool IsSelected(int offset) /// public ISelection GetSelectionAt(int offset) { - foreach (var s in selectionCollection) + foreach (ISelection s in CollectionsMarshal.AsSpan(selectionCollection)) if (s.ContainsOffset(offset)) return s; return null; @@ -408,7 +409,7 @@ internal void Replace(int offset, int length, string text) public ColumnRange GetSelectionAtLine(int lineNumber) { - foreach (var selection in selectionCollection) + foreach (ISelection selection in CollectionsMarshal.AsSpan(selectionCollection)) { var startLine = selection.StartPosition.Y; var endLine = selection.EndPosition.Y; @@ -447,17 +448,17 @@ protected virtual void OnSelectionChanged(EventArgs e) } // selection initiated from... - internal class SelectFrom + internal sealed class SelectFrom { public int first = WhereFrom.None; // first selection initiator public int where = WhereFrom.None; // last selection initiator } // selection initiated from type... - internal class WhereFrom + internal sealed class WhereFrom { public const int None = 0; public const int Gutter = 1; public const int TArea = 2; } -} \ No newline at end of file +} diff --git a/Project/Src/Document/TextBufferStrategy/GapTextBufferStrategy.cs b/Project/Src/Document/TextBufferStrategy/GapTextBufferStrategy.cs index 5acb387..8f929f9 100644 --- a/Project/Src/Document/TextBufferStrategy/GapTextBufferStrategy.cs +++ b/Project/Src/Document/TextBufferStrategy/GapTextBufferStrategy.cs @@ -16,7 +16,7 @@ public class GapTextBufferStrategy : ITextBufferStrategy private const int minGapLength = 128; private const int maxGapLength = 2048; - private char[] buffer = new char[0]; + private char[] buffer = []; private string cachedContent; private int gapBeginOffset; @@ -181,4 +181,4 @@ private void CheckThread() } #endif } -} \ No newline at end of file +} diff --git a/Project/Src/Gui/AbstractMargin.cs b/Project/Src/Gui/AbstractMargin.cs index b9e78f2..1f5fbd8 100644 --- a/Project/Src/Gui/AbstractMargin.cs +++ b/Project/Src/Gui/AbstractMargin.cs @@ -14,7 +14,7 @@ namespace ICSharpCode.TextEditor { public delegate void MarginMouseEventHandler(AbstractMargin sender, Point mousepos, MouseButtons mouseButtons); - public delegate void MarginPaintEventHandler(AbstractMargin sender, Graphics g, Rectangle rect); + public delegate void MarginPaintEventHandler(AbstractMargin sender, Graphics g, in Rectangle rect); /// /// This class views the line numbers and folding markers. @@ -78,7 +78,7 @@ public virtual void SelectedLineChanged(int line) textArea.Invalidate(); } - public virtual void Paint(Graphics g, Rectangle rect) + public virtual void Paint(Graphics g, in Rectangle rect) { Painted?.Invoke(this, g, rect); } diff --git a/Project/Src/Gui/Caret.cs b/Project/Src/Gui/Caret.cs index 5edb5bb..2eda7a1 100644 --- a/Project/Src/Gui/Caret.cs +++ b/Project/Src/Gui/Caret.cs @@ -398,7 +398,7 @@ public virtual void Dispose() public abstract void Destroy(); } - private class ManagedCaret : CaretImplementation + private sealed class ManagedCaret : CaretImplementation { private readonly Caret parentCaret; private readonly TextArea textArea; @@ -467,7 +467,7 @@ public override void Dispose() } } - private class Win32Caret : CaretImplementation + private sealed class Win32Caret : CaretImplementation { private readonly TextArea textArea; diff --git a/Project/Src/Gui/DrawableLine.cs b/Project/Src/Gui/DrawableLine.cs index c69376d..3200780 100644 --- a/Project/Src/Gui/DrawableLine.cs +++ b/Project/Src/Gui/DrawableLine.cs @@ -172,7 +172,7 @@ public float MeasureWidth(Graphics g, float xPos) return xPos; } - private class SimpleTextWord + private sealed class SimpleTextWord { internal static readonly SimpleTextWord Space = new SimpleTextWord(TextWordType.Space, " ", Bold: false, SystemColors.WindowText); internal static readonly SimpleTextWord Tab = new SimpleTextWord(TextWordType.Tab, "\t", Bold: false, SystemColors.WindowText); diff --git a/Project/Src/Gui/FoldMargin.cs b/Project/Src/Gui/FoldMargin.cs index 0553c5f..6d7208c 100644 --- a/Project/Src/Gui/FoldMargin.cs +++ b/Project/Src/Gui/FoldMargin.cs @@ -29,7 +29,7 @@ public FoldMargin(TextArea textArea) : base(textArea) public override bool IsVisible => textArea.TextEditorProperties.EnableFolding; - public override void Paint(Graphics g, Rectangle rect) + public override void Paint(Graphics g, in Rectangle rect) { if (rect.Width <= 0 || rect.Height <= 0) return; @@ -229,7 +229,7 @@ public override void HandleMouseLeave(EventArgs e) #region Drawing functions - private void DrawFoldMarker(Graphics g, RectangleF rectangle, bool isOpened, bool isSelected) + private void DrawFoldMarker(Graphics g, in RectangleF rectangle, bool isOpened, bool isSelected) { var foldMarkerColor = textArea.Document.HighlightingStrategy.GetColorFor("FoldMarker"); var foldLineColor = textArea.Document.HighlightingStrategy.GetColorFor("FoldLine"); diff --git a/Project/Src/Gui/GutterMargin.cs b/Project/Src/Gui/GutterMargin.cs index 289573c..e5ca288 100644 --- a/Project/Src/Gui/GutterMargin.cs +++ b/Project/Src/Gui/GutterMargin.cs @@ -51,7 +51,7 @@ public void Dispose() numberStringFormat.Dispose(); } - public override void Paint(Graphics g, Rectangle rect) + public override void Paint(Graphics g, in Rectangle rect) { if (rect.Width <= 0 || rect.Height <= 0) return; diff --git a/Project/Src/Gui/IconBarMargin.cs b/Project/Src/Gui/IconBarMargin.cs index 107e9f5..873c24e 100644 --- a/Project/Src/Gui/IconBarMargin.cs +++ b/Project/Src/Gui/IconBarMargin.cs @@ -29,7 +29,7 @@ public IconBarMargin(TextArea textArea) : base(textArea) public override bool IsVisible => textArea.TextEditorProperties.IsIconBarVisible; - public override void Paint(Graphics g, Rectangle rect) + public override void Paint(Graphics g, in Rectangle rect) { if (rect.Width <= 0 || rect.Height <= 0) return; diff --git a/Project/Src/Gui/Ime.cs b/Project/Src/Gui/Ime.cs index 3d202a1..12b8e7d 100644 --- a/Project/Src/Gui/Ime.cs +++ b/Project/Src/Gui/Ime.cs @@ -15,7 +15,7 @@ namespace ICSharpCode.TextEditor /// /// Used internally, not for own use. /// - internal class Ime + internal sealed class Ime { private const int WM_IME_CONTROL = 0x0283; @@ -147,7 +147,7 @@ private static void Handle(Exception ex) } [StructLayout(LayoutKind.Sequential)] - private class COMPOSITIONFORM + private sealed class COMPOSITIONFORM { public int dwStyle; public POINT ptCurrentPos; @@ -155,14 +155,14 @@ private class COMPOSITIONFORM } [StructLayout(LayoutKind.Sequential)] - private class POINT + private sealed class POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] - private class RECT + private sealed class RECT { public int bottom = 0; public int left = 0; @@ -171,7 +171,7 @@ private class RECT } [StructLayout(LayoutKind.Sequential)] - private class LOGFONT + private sealed class LOGFONT { public byte lfCharSet = 0; public byte lfClipPrecision = 0; @@ -192,4 +192,4 @@ private class LOGFONT public int lfWidth = 0; } } -} \ No newline at end of file +} diff --git a/Project/Src/Gui/InsightWindow/InsightWindow.cs b/Project/Src/Gui/InsightWindow/InsightWindow.cs index 8f7fd06..ae70092 100644 --- a/Project/Src/Gui/InsightWindow/InsightWindow.cs +++ b/Project/Src/Gui/InsightWindow/InsightWindow.cs @@ -192,7 +192,7 @@ private void CloseCurrentDataProvider() Refresh(); } - private class InsightDataProviderStackElement + private sealed class InsightDataProviderStackElement { public readonly IInsightDataProvider dataProvider; public int currentData; @@ -206,4 +206,4 @@ public InsightDataProviderStackElement(IInsightDataProvider dataProvider) #endregion } -} \ No newline at end of file +} diff --git a/Project/Src/Gui/TextEditorControl.cs b/Project/Src/Gui/TextEditorControl.cs index 01698cf..1f76591 100644 --- a/Project/Src/Gui/TextEditorControl.cs +++ b/Project/Src/Gui/TextEditorControl.cs @@ -315,7 +315,7 @@ private float MeasurePrintingHeight(Graphics g, LineSegment line, float maxWidth return yPos + fontHeight; } - private void DrawLine(Graphics g, LineSegment line, float yPos, RectangleF margin) + private void DrawLine(Graphics g, LineSegment line, float yPos, in RectangleF margin) { float xPos = 0; var fontHeight = Font.GetHeight(g); @@ -377,4 +377,4 @@ private void PrintPage(object sender, PrintPageEventArgs ev) #endregion } -} \ No newline at end of file +} diff --git a/Project/Src/Gui/TextView.cs b/Project/Src/Gui/TextView.cs index 9e109c3..a07d925 100644 --- a/Project/Src/Gui/TextView.cs +++ b/Project/Src/Gui/TextView.cs @@ -89,7 +89,7 @@ public void OptionsChanged() #region Paint functions - public override void Paint(Graphics g, Rectangle rect) + public override void Paint(Graphics g, in Rectangle rect) { if (rect.Width <= 0 || rect.Height <= 0) return; @@ -117,7 +117,7 @@ public override void Paint(Graphics g, Rectangle rect) { // var fvl = textArea.Document.GetVisibleLine(FirstVisibleLine); var currentLine = textArea.Document.GetFirstLogicalLine(textArea.Document.GetVisibleLine(FirstVisibleLine) + y); - PaintDocumentLine(g, currentLine, lineRectangle); + PaintDocumentLine(g, currentLine, in lineRectangle); } } @@ -128,7 +128,7 @@ public override void Paint(Graphics g, Rectangle rect) textArea.Caret.PaintCaret(g); } - private void PaintDocumentLine(Graphics g, int lineNumber, Rectangle lineRectangle) + private void PaintDocumentLine(Graphics g, int lineNumber, in Rectangle lineRectangle) { Debug.Assert(lineNumber >= 0); var bgColorBrush = GetBgColorBrush(lineNumber); @@ -268,7 +268,7 @@ private readonly struct MarkerToDraw internal readonly TextMarker marker; internal readonly RectangleF drawingRect; - public MarkerToDraw(TextMarker marker, RectangleF drawingRect) + public MarkerToDraw(TextMarker marker, in RectangleF drawingRect) { this.marker = marker; this.drawingRect = drawingRect; @@ -277,7 +277,7 @@ public MarkerToDraw(TextMarker marker, RectangleF drawingRect) private readonly List markersToDraw = new List(); - private void DrawMarker(TextMarker marker, RectangleF drawingRect) + private void DrawMarker(TextMarker marker, in RectangleF drawingRect) { // draw markers later so they can overdraw the following text markersToDraw.Add(new MarkerToDraw(marker, drawingRect)); @@ -1092,7 +1092,7 @@ private int DrawEOLMarker(Graphics g, Brush backBrush, int x, int y, EolMarker e return eolMarkerWidth; } - private void DrawVerticalRuler(Graphics g, Rectangle lineRectangle) + private void DrawVerticalRuler(Graphics g, in Rectangle lineRectangle) { var xpos = WideSpaceWidth*TextEditorProperties.VerticalRulerRow - textArea.VirtualTop.X; if (xpos <= 0) diff --git a/Project/Src/Undo/UndoStack.cs b/Project/Src/Undo/UndoStack.cs index c7fbd5b..181665e 100644 --- a/Project/Src/Undo/UndoStack.cs +++ b/Project/Src/Undo/UndoStack.cs @@ -179,7 +179,7 @@ protected void OnActionRedone() ActionRedone?.Invoke(sender: null, e: null); } - private class UndoableSetCaretPosition : IUndoableOperation + private sealed class UndoableSetCaretPosition : IUndoableOperation { private readonly TextLocation pos; private readonly UndoStack stack; @@ -217,4 +217,4 @@ public OperationEventArgs(IUndoableOperation op) } public delegate void OperationEventHandler(object sender, OperationEventArgs e); -} \ No newline at end of file +} diff --git a/Project/Src/Util/LookupTable.cs b/Project/Src/Util/LookupTable.cs index 4daf565..13e3347 100644 --- a/Project/Src/Util/LookupTable.cs +++ b/Project/Src/Util/LookupTable.cs @@ -124,7 +124,7 @@ public object this[string keyword] } } - private class Node + private sealed class Node { private Node[] children; public object color; @@ -151,4 +151,4 @@ public Node this[int index] } } } -} \ No newline at end of file +} diff --git a/Project/Src/Util/MouseWheelHandler.cs b/Project/Src/Util/MouseWheelHandler.cs index 62d98f9..87b1be8 100644 --- a/Project/Src/Util/MouseWheelHandler.cs +++ b/Project/Src/Util/MouseWheelHandler.cs @@ -13,7 +13,7 @@ namespace ICSharpCode.TextEditor.Util /// /// Accumulates mouse wheel deltas and reports the actual number of lines to scroll. /// - internal class MouseWheelHandler + internal sealed class MouseWheelHandler { // CODE DUPLICATION: See ICSharpCode.SharpDevelop.Widgets.MouseWheelHandler @@ -33,4 +33,4 @@ public int GetScrollAmount(MouseEventArgs e) return scrollDistance; } } -} \ No newline at end of file +} diff --git a/Project/Src/Util/TipSpacer.cs b/Project/Src/Util/TipSpacer.cs index 069d223..324b764 100644 --- a/Project/Src/Util/TipSpacer.cs +++ b/Project/Src/Util/TipSpacer.cs @@ -10,7 +10,7 @@ namespace ICSharpCode.TextEditor.Util { - internal class TipSpacer : TipSection + internal sealed class TipSpacer : TipSection { private readonly SizeF spacerSize; @@ -34,4 +34,4 @@ protected override void OnMaximumSizeChanged() Math.Min(MaximumSize.Height, spacerSize.Height))); } } -} \ No newline at end of file +} diff --git a/Project/Src/Util/TipSplitter.cs b/Project/Src/Util/TipSplitter.cs index 041f3d7..2ace84b 100644 --- a/Project/Src/Util/TipSplitter.cs +++ b/Project/Src/Util/TipSplitter.cs @@ -11,7 +11,7 @@ namespace ICSharpCode.TextEditor.Util { - internal class TipSplitter : TipSection + internal sealed class TipSplitter : TipSection { private readonly bool isHorizontal; private readonly float[] offsets; @@ -94,4 +94,4 @@ protected override void OnMaximumSizeChanged() SetRequiredSize(new SizeF(otherDim, currentDim)); } } -} \ No newline at end of file +} diff --git a/Project/Src/Util/TipText.cs b/Project/Src/Util/TipText.cs index 8a280d0..66c29df 100644 --- a/Project/Src/Util/TipText.cs +++ b/Project/Src/Util/TipText.cs @@ -10,7 +10,7 @@ namespace ICSharpCode.TextEditor.Util { - internal class CountTipText : TipText + internal sealed class CountTipText : TipText { private readonly float triHeight = 10; private readonly float triWidth = 10;