From 1da8c56bfedb5eb576c14991e92c446b910a4ce9 Mon Sep 17 00:00:00 2001 From: Dave Black <656118+udlose@users.noreply.github.com> Date: Sat, 7 Mar 2026 10:28:29 -0600 Subject: [PATCH 1/4] Part 1 of 2 Restrict visibility, add immutability, refactor internals to match partity with upstream: TM4e repo: https://github.com/eclipse-tm4e/tm4e/tree/main/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal Converted many classes and members to internal or sealed, and marked fields as readonly to improve encapsulation and immutability. Refactored utility classes to static, updated method visibility, and simplified JSONPListParser logic. These changes clarify API boundaries, prevent misuse, and enhance maintainability and thread safety. --- src/TextMateSharp/Grammar/Injection.cs | 12 ++-- src/TextMateSharp/Grammar/StateStack.cs | 16 +++--- .../Internal/Grammars/Grammar.cs | 37 ++++++------ .../Internal/Grammars/LineTokenizer.cs | 57 +++++++++---------- .../Internal/Grammars/LocalStackElement.cs | 6 +- .../Internal/Grammars/MetadataConsts.cs | 14 ++--- .../Grammars/OptionalStandardTokenType.cs | 10 ++-- .../Internal/Grammars/StandardTokenType.cs | 10 ++-- .../Internal/Grammars/SyncRegistry.cs | 7 +-- .../Internal/Grammars/TokenTypeMatcher.cs | 8 +-- .../Internal/Grammars/TokenizeLineResult.cs | 4 +- .../Internal/Grammars/TokenizeLineResult2.cs | 4 +- .../Internal/Grammars/TokenizeStringResult.cs | 8 +-- .../Internal/Grammars/reader/GrammarReader.cs | 2 +- .../Internal/Matcher/MatchInjectionResult.cs | 2 +- .../Internal/Matcher/MatchResult.cs | 4 +- .../Internal/Matcher/MatcherBuilder.cs | 8 +-- .../Internal/Matcher/MatcherWithPriority.cs | 4 +- .../Internal/Parser/Json/JSONPListParser.cs | 17 +----- .../Internal/Rules/RuleFactory.cs | 2 +- src/TextMateSharp/Internal/Rules/RuleId.cs | 2 +- .../Internal/Utils/RegexSource.cs | 2 +- 22 files changed, 109 insertions(+), 127 deletions(-) diff --git a/src/TextMateSharp/Grammar/Injection.cs b/src/TextMateSharp/Grammar/Injection.cs index 567a179..78111cd 100644 --- a/src/TextMateSharp/Grammar/Injection.cs +++ b/src/TextMateSharp/Grammar/Injection.cs @@ -6,15 +6,15 @@ namespace TextMateSharp.Grammars { - public class Injection + internal sealed class Injection { - public int Priority { get; private set; } // -1 | 0 | 1; // 0 is the default. -1 for 'L' and 1 for 'R' - public RuleId RuleId { get; private set; } - public IRawGrammar Grammar { get; private set; } + internal int Priority { get; private set; } // -1 | 0 | 1; // 0 is the default. -1 for 'L' and 1 for 'R' + internal RuleId RuleId { get; private set; } + internal IRawGrammar Grammar { get; private set; } - private Predicate> _matcher; + private readonly Predicate> _matcher; - public Injection(Predicate> matcher, RuleId ruleId, IRawGrammar grammar, int priority) + internal Injection(Predicate> matcher, RuleId ruleId, IRawGrammar grammar, int priority) { RuleId = ruleId; Grammar = grammar; diff --git a/src/TextMateSharp/Grammar/StateStack.cs b/src/TextMateSharp/Grammar/StateStack.cs index f5d4528..28c6b59 100644 --- a/src/TextMateSharp/Grammar/StateStack.cs +++ b/src/TextMateSharp/Grammar/StateStack.cs @@ -12,9 +12,9 @@ public interface IStateStack string EndRule { get; } } - public class StateStack : IStateStack, IEquatable + public sealed class StateStack : IStateStack, IEquatable { - public static StateStack NULL = new StateStack( + public static readonly StateStack NULL = new StateStack( null, RuleId.NO_RULE, 0, @@ -28,9 +28,9 @@ public class StateStack : IStateStack, IEquatable public int Depth { get; private set; } public RuleId RuleId { get; private set; } public string EndRule { get; private set; } - public AttributedScopeStack NameScopesList { get; private set; } - public AttributedScopeStack ContentNameScopesList { get; private set; } - public bool BeginRuleCapturedEOL { get; private set; } + internal AttributedScopeStack NameScopesList { get; private set; } + internal AttributedScopeStack ContentNameScopesList { get; private set; } + internal bool BeginRuleCapturedEOL { get; private set; } private int _enterPos; private int _anchorPos; @@ -40,7 +40,7 @@ public class StateStack : IStateStack, IEquatable // ContentNameScopesList) are not mutated after construction private readonly int _hashCode; - public StateStack( + internal StateStack( StateStack parent, RuleId ruleId, int enterPos, @@ -249,7 +249,7 @@ public StateStack SafePop() return this; } - public StateStack Push( + internal StateStack Push( RuleId ruleId, int enterPos, int anchorPos, @@ -316,7 +316,7 @@ public override string ToString() return builder.ToString(); } - public StateStack WithContentNameScopesList(AttributedScopeStack contentNameScopesList) + internal StateStack WithContentNameScopesList(AttributedScopeStack contentNameScopesList) { // Null-safe comparison matching Java upstream's Objects.equals() pattern if (AttributedScopeStack.Equals(this.ContentNameScopesList, contentNameScopesList)) diff --git a/src/TextMateSharp/Internal/Grammars/Grammar.cs b/src/TextMateSharp/Internal/Grammars/Grammar.cs index fe09793..0afd375 100644 --- a/src/TextMateSharp/Internal/Grammars/Grammar.cs +++ b/src/TextMateSharp/Internal/Grammars/Grammar.cs @@ -10,20 +10,20 @@ namespace TextMateSharp.Internal.Grammars { - public class Grammar : IGrammar, IRuleFactoryHelper + public sealed class Grammar : IGrammar, IRuleFactoryHelper { - private string _rootScopeName; + private readonly string _rootScopeName; private RuleId _rootId; private int _lastRuleId; private volatile bool _isCompiling; - private Dictionary _ruleId2desc; - private Dictionary _includedGrammars; - private IGrammarRepository _grammarRepository; - private IRawGrammar _rawGrammar; + private readonly Dictionary _ruleId2desc; + private readonly Dictionary _includedGrammars; + private readonly IGrammarRepository _grammarRepository; + private readonly IRawGrammar _rawGrammar; private List _injections; - private BasicScopeAttributesProvider _basicScopeAttributesProvider; - private List _tokenTypeMatchers; - private BalancedBracketSelectors _balancedBracketSelectors; + private readonly BasicScopeAttributesProvider _basicScopeAttributesProvider; + private readonly List _tokenTypeMatchers; + private readonly BalancedBracketSelectors _balancedBracketSelectors; public Grammar( string scopeName, @@ -53,14 +53,14 @@ public void OnDidChangeTheme() this._basicScopeAttributesProvider.OnDidChangeTheme(); } - public BasicScopeAttributes GetMetadataForScope(string scope) + internal BasicScopeAttributes GetMetadataForScope(string scope) { return this._basicScopeAttributesProvider.GetBasicScopeAttributes(scope); } public bool IsCompiling => _isCompiling; - public List GetInjections() + internal List GetInjections() { if (this._injections == null) { @@ -118,7 +118,7 @@ public List GetInjections() return this._injections; } - private void CollectInjections(List result, string selector, IRawRule rule, + private static void CollectInjections(List result, string selector, IRawRule rule, IRuleFactoryHelper ruleFactoryHelper, IRawGrammar grammar) { var matchers = Matcher.Matcher.CreateMatchers(selector); @@ -173,7 +173,7 @@ public IRawGrammar GetExternalGrammar(string scopeName, IRawRepository repositor return null; } - private IRawGrammar InitGrammar(IRawGrammar grammar, IRawRule ruleBase) + private static IRawGrammar InitGrammar(IRawGrammar grammar, IRawRule ruleBase) { grammar = grammar.Clone(); if (grammar.GetRepository() == null) @@ -271,7 +271,7 @@ private object Tokenize(ReadOnlyMemory lineText, StateStack prevState, boo } int lineLength = effectiveLineText.Length; - LineTokens lineTokens = new LineTokens(emitBinaryTokens, effectiveLineText, _tokenTypeMatchers, + LineTokens lineTokens = new LineTokens(emitBinaryTokens, _tokenTypeMatchers, _balancedBracketSelectors); TokenizeStringResult tokenizeResult = LineTokenizer.TokenizeString(this, effectiveLineText, isFirstLine, 0, prevState, @@ -301,13 +301,12 @@ private void GenerateRootId() } } - private List GenerateTokenTypeMatchers(Dictionary tokenTypes) + private static List GenerateTokenTypeMatchers(Dictionary tokenTypes) { - var result = new List(); - if (tokenTypes == null) - return result; + return new List(); + var result = new List(tokenTypes.Keys.Count); foreach (var selector in tokenTypes.Keys) { foreach (var matcher in Matcher.Matcher.CreateMatchers(selector)) @@ -336,7 +335,7 @@ public ICollection GetFileTypes() class GrammarRepository : IGrammarRepository { - private Grammar _grammar; + private readonly Grammar _grammar; internal GrammarRepository(Grammar grammar) { _grammar = grammar; diff --git a/src/TextMateSharp/Internal/Grammars/LineTokenizer.cs b/src/TextMateSharp/Internal/Grammars/LineTokenizer.cs index 22fe9a6..21d575c 100644 --- a/src/TextMateSharp/Internal/Grammars/LineTokenizer.cs +++ b/src/TextMateSharp/Internal/Grammars/LineTokenizer.cs @@ -9,7 +9,7 @@ namespace TextMateSharp.Internal.Grammars { - class LineTokenizer + internal sealed class LineTokenizer { private readonly Grammar _grammar; private readonly ReadOnlyMemory _lineText; @@ -24,7 +24,7 @@ class LineTokenizer private readonly List _localStackBuffer = new List(); private readonly List _whileRulesBuffer = new List(); - public LineTokenizer(Grammar grammar, ReadOnlyMemory lineText, bool isFirstLine, int linePos, StateStack stack, + private LineTokenizer(Grammar grammar, ReadOnlyMemory lineText, bool isFirstLine, int linePos, StateStack stack, LineTokens lineTokens) { this._grammar = grammar; @@ -36,7 +36,7 @@ public LineTokenizer(Grammar grammar, ReadOnlyMemory lineText, bool isFirs this._lineTokens = lineTokens; } - public TokenizeStringResult Scan(bool checkWhileConditions, TimeSpan timeLimit) + private TokenizeStringResult Scan(bool checkWhileConditions, TimeSpan timeLimit) { _stop = false; @@ -138,22 +138,20 @@ private void ScanNext() nameScopesList, nameScopesList); - if (rule is BeginEndRule) + if (rule is BeginEndRule beginEndRule) { - BeginEndRule pushedRule = (BeginEndRule)rule; - HandleCaptures( _grammar, _lineText, _isFirstLine, _stack, _lineTokens, - pushedRule.BeginCaptures, + beginEndRule.BeginCaptures, captureIndices); _lineTokens.Produce(_stack, captureIndices[0].End); _anchorPosition = captureIndices[0].End; - string contentName = pushedRule.GetContentName( + string contentName = beginEndRule.GetContentName( _lineText, captureIndices); @@ -163,10 +161,10 @@ private void ScanNext() _stack = _stack.WithContentNameScopesList(contentNameScopesList); - if (pushedRule.EndHasBackReferences) + if (beginEndRule.EndHasBackReferences) { _stack = _stack.WithEndRule( - pushedRule.GetEndWithResolvedBackReferences( + beginEndRule.GetEndWithResolvedBackReferences( _lineText, captureIndices)); } @@ -181,9 +179,8 @@ private void ScanNext() return; } } - else if (rule is BeginWhileRule) + else if (rule is BeginWhileRule beginWhileRule) { - BeginWhileRule pushedRule = (BeginWhileRule)rule; // if (IN_DEBUG_MODE) { // console.log(' pushing ' + pushedRule.debugName); // } @@ -194,19 +191,19 @@ private void ScanNext() _isFirstLine, _stack, _lineTokens, - pushedRule.BeginCaptures, + beginWhileRule.BeginCaptures, captureIndices); _lineTokens.Produce(_stack, captureIndices[0].End); _anchorPosition = captureIndices[0].End; - string contentName = pushedRule.GetContentName(_lineText, captureIndices); + string contentName = beginWhileRule.GetContentName(_lineText, captureIndices); AttributedScopeStack contentNameScopesList = nameScopesList.PushAttributed(contentName, _grammar); _stack = _stack.WithContentNameScopesList(contentNameScopesList); - if (pushedRule.WhileHasBackReferences) + if (beginWhileRule.WhileHasBackReferences) { _stack = _stack.WithEndRule( - pushedRule.getWhileWithResolvedBackReferences(_lineText, captureIndices)); + beginWhileRule.getWhileWithResolvedBackReferences(_lineText, captureIndices)); } if (!hasAdvanced && beforePush.HasSameRuleAs(_stack)) @@ -254,7 +251,7 @@ private void ScanNext() } } - private MatchResult MatchRule(Grammar grammar, ReadOnlyMemory lineText, in bool isFirstLine, in int linePos, + private static MatchResult MatchRule(Grammar grammar, ReadOnlyMemory lineText, in bool isFirstLine, in int linePos, StateStack stack, in int anchorPosition) { Rule rule = stack.GetRule(grammar); @@ -499,9 +496,9 @@ private WhileCheckResult CheckWhileConditions(Grammar grammar, ReadOnlyMemory= 0; i--) @@ -544,32 +541,32 @@ private WhileCheckResult CheckWhileConditions(Grammar grammar, ReadOnlyMemory lineText, bool isFirstLine, int linePos, + internal static TokenizeStringResult TokenizeString(Grammar grammar, ReadOnlyMemory lineText, bool isFirstLine, int linePos, StateStack stack, LineTokens lineTokens, bool checkWhileConditions, TimeSpan timeLimit) { return new LineTokenizer(grammar, lineText, isFirstLine, linePos, stack, lineTokens).Scan(checkWhileConditions, timeLimit); } - class WhileStack + sealed class WhileStack { - public StateStack Stack { get; private set; } - public BeginWhileRule Rule { get; private set; } + internal StateStack Stack { get; private set; } + internal BeginWhileRule Rule { get; private set; } - public WhileStack(StateStack stack, BeginWhileRule rule) + internal WhileStack(StateStack stack, BeginWhileRule rule) { Stack = stack; Rule = rule; } } - class WhileCheckResult + sealed class WhileCheckResult { - public StateStack Stack { get; private set; } - public int LinePos { get; private set; } - public int AnchorPosition { get; private set; } - public bool IsFirstLine { get; private set; } + internal StateStack Stack { get; private set; } + internal int LinePos { get; private set; } + internal int AnchorPosition { get; private set; } + internal bool IsFirstLine { get; private set; } - public WhileCheckResult(StateStack stack, int linePos, int anchorPosition, bool isFirstLine) + internal WhileCheckResult(StateStack stack, int linePos, int anchorPosition, bool isFirstLine) { Stack = stack; LinePos = linePos; diff --git a/src/TextMateSharp/Internal/Grammars/LocalStackElement.cs b/src/TextMateSharp/Internal/Grammars/LocalStackElement.cs index 8ea2d18..dc93867 100644 --- a/src/TextMateSharp/Internal/Grammars/LocalStackElement.cs +++ b/src/TextMateSharp/Internal/Grammars/LocalStackElement.cs @@ -2,10 +2,10 @@ namespace TextMateSharp.Internal.Grammars { class LocalStackElement { - public AttributedScopeStack Scopes { get; private set; } - public int EndPos { get; private set; } + internal AttributedScopeStack Scopes { get; private set; } + internal int EndPos { get; private set; } - public LocalStackElement(AttributedScopeStack scopes, int endPos) + internal LocalStackElement(AttributedScopeStack scopes, int endPos) { Scopes = scopes; EndPos = endPos; diff --git a/src/TextMateSharp/Internal/Grammars/MetadataConsts.cs b/src/TextMateSharp/Internal/Grammars/MetadataConsts.cs index 069a79f..455a7da 100644 --- a/src/TextMateSharp/Internal/Grammars/MetadataConsts.cs +++ b/src/TextMateSharp/Internal/Grammars/MetadataConsts.cs @@ -22,14 +22,14 @@ namespace TextMateSharp.Internal.Grammars * - f = foreground color (9 bits) * - b = background color (9 bits) */ - public class MetadataConsts + public static class MetadataConsts { - public static uint LANGUAGEID_MASK = 0b00000000000000000000000011111111; - public static uint TOKEN_TYPE_MASK = 0b00000000000000000000001100000000; - public static uint BALANCED_BRACKETS_MASK = 0b00000000000000000000010000000000; - public static uint FONT_STYLE_MASK = 0b00000000000000000111100000000000; - public static uint FOREGROUND_MASK = 0b00000000111111111000000000000000; - public static uint BACKGROUND_MASK = 0b11111111000000000000000000000000; + public static readonly uint LANGUAGEID_MASK = 0b00000000000000000000000011111111; + public static readonly uint TOKEN_TYPE_MASK = 0b00000000000000000000001100000000; + public static readonly uint BALANCED_BRACKETS_MASK = 0b00000000000000000000010000000000; + public static readonly uint FONT_STYLE_MASK = 0b00000000000000000111100000000000; + public static readonly uint FOREGROUND_MASK = 0b00000000111111111000000000000000; + public static readonly uint BACKGROUND_MASK = 0b11111111000000000000000000000000; public const int LANGUAGEID_OFFSET = 0; public const int TOKEN_TYPE_OFFSET = 8; diff --git a/src/TextMateSharp/Internal/Grammars/OptionalStandardTokenType.cs b/src/TextMateSharp/Internal/Grammars/OptionalStandardTokenType.cs index cc19cb6..cdb9878 100644 --- a/src/TextMateSharp/Internal/Grammars/OptionalStandardTokenType.cs +++ b/src/TextMateSharp/Internal/Grammars/OptionalStandardTokenType.cs @@ -2,14 +2,14 @@ { public static class OptionalStandardTokenType { - public static int Other = StandardTokenType.Other; - public static int Comment = StandardTokenType.Comment; - public static int String = StandardTokenType.String; - public static int RegEx = StandardTokenType.RegEx; + public static readonly int Other = StandardTokenType.Other; + public static readonly int Comment = StandardTokenType.Comment; + public static readonly int String = StandardTokenType.String; + public static readonly int RegEx = StandardTokenType.RegEx; /** ** Indicates that no token type is set. **/ - public static int NotSet = 8; + public static readonly int NotSet = 8; } } diff --git a/src/TextMateSharp/Internal/Grammars/StandardTokenType.cs b/src/TextMateSharp/Internal/Grammars/StandardTokenType.cs index 65e2249..939a25e 100644 --- a/src/TextMateSharp/Internal/Grammars/StandardTokenType.cs +++ b/src/TextMateSharp/Internal/Grammars/StandardTokenType.cs @@ -1,10 +1,10 @@ namespace TextMateSharp.Internal.Grammars { - public class StandardTokenType + internal static class StandardTokenType { - public const int Other = 0; - public const int Comment = 1; - public const int String = 2; - public const int RegEx = 3; + internal const int Other = 0; + internal const int Comment = 1; + internal const int String = 2; + internal const int RegEx = 3; } } diff --git a/src/TextMateSharp/Internal/Grammars/SyncRegistry.cs b/src/TextMateSharp/Internal/Grammars/SyncRegistry.cs index 5702f67..ebc176a 100644 --- a/src/TextMateSharp/Internal/Grammars/SyncRegistry.cs +++ b/src/TextMateSharp/Internal/Grammars/SyncRegistry.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using TextMateSharp.Grammars; @@ -11,9 +10,9 @@ namespace TextMateSharp.Internal.Grammars { public class SyncRegistry : IGrammarRepository, IThemeProvider { - private Dictionary _grammars; - private Dictionary _rawGrammars; - private Dictionary> _injectionGrammars; + private readonly Dictionary _grammars; + private readonly Dictionary _rawGrammars; + private readonly Dictionary> _injectionGrammars; private Theme _theme; public SyncRegistry(Theme theme) diff --git a/src/TextMateSharp/Internal/Grammars/TokenTypeMatcher.cs b/src/TextMateSharp/Internal/Grammars/TokenTypeMatcher.cs index 6264aa2..0037f2a 100644 --- a/src/TextMateSharp/Internal/Grammars/TokenTypeMatcher.cs +++ b/src/TextMateSharp/Internal/Grammars/TokenTypeMatcher.cs @@ -3,12 +3,12 @@ namespace TextMateSharp.Internal.Grammars { - internal class TokenTypeMatcher + internal sealed class TokenTypeMatcher { - public int Type { get; } - public Predicate> Matcher { get; } + internal int Type { get; } + internal Predicate> Matcher { get; } - public TokenTypeMatcher(int type, Predicate> matcher) + internal TokenTypeMatcher(int type, Predicate> matcher) { Type = type; Matcher = matcher; diff --git a/src/TextMateSharp/Internal/Grammars/TokenizeLineResult.cs b/src/TextMateSharp/Internal/Grammars/TokenizeLineResult.cs index 6c2707f..0189ac7 100644 --- a/src/TextMateSharp/Internal/Grammars/TokenizeLineResult.cs +++ b/src/TextMateSharp/Internal/Grammars/TokenizeLineResult.cs @@ -2,12 +2,12 @@ namespace TextMateSharp.Internal.Grammars { - public class TokenizeLineResult : ITokenizeLineResult + internal sealed class TokenizeLineResult : ITokenizeLineResult { public IToken[] Tokens { get; private set; } public IStateStack RuleStack { get; private set; } public bool StoppedEarly { get; private set; } - public TokenizeLineResult(IToken[] tokens, IStateStack ruleStack, bool stoppedEarly) + internal TokenizeLineResult(IToken[] tokens, IStateStack ruleStack, bool stoppedEarly) { Tokens = tokens; RuleStack = ruleStack; diff --git a/src/TextMateSharp/Internal/Grammars/TokenizeLineResult2.cs b/src/TextMateSharp/Internal/Grammars/TokenizeLineResult2.cs index 18595ae..beb0eb9 100644 --- a/src/TextMateSharp/Internal/Grammars/TokenizeLineResult2.cs +++ b/src/TextMateSharp/Internal/Grammars/TokenizeLineResult2.cs @@ -2,11 +2,11 @@ namespace TextMateSharp.Internal.Grammars { - public class TokenizeLineResult2 : ITokenizeLineResult2 + internal class TokenizeLineResult2 : ITokenizeLineResult2 { public int[] Tokens { get; private set; } public IStateStack RuleStack { get; private set; } - public bool StoppedEarly { get; private set; } + internal bool StoppedEarly { get; private set; } public TokenizeLineResult2(int[] tokens, IStateStack ruleStack, bool stoppedEarly) { diff --git a/src/TextMateSharp/Internal/Grammars/TokenizeStringResult.cs b/src/TextMateSharp/Internal/Grammars/TokenizeStringResult.cs index f74ae5b..8b4872c 100644 --- a/src/TextMateSharp/Internal/Grammars/TokenizeStringResult.cs +++ b/src/TextMateSharp/Internal/Grammars/TokenizeStringResult.cs @@ -2,12 +2,12 @@ namespace TextMateSharp.Internal.Grammars { - public class TokenizeStringResult + internal class TokenizeStringResult { - public StateStack Stack { get; private set; } - public bool StoppedEarly { get; private set; } + internal StateStack Stack { get; private set; } + internal bool StoppedEarly { get; private set; } - public TokenizeStringResult(StateStack stack, bool stoppedEarly) + internal TokenizeStringResult(StateStack stack, bool stoppedEarly) { this.Stack = stack; this.StoppedEarly = stoppedEarly; diff --git a/src/TextMateSharp/Internal/Grammars/reader/GrammarReader.cs b/src/TextMateSharp/Internal/Grammars/reader/GrammarReader.cs index 883a529..850372d 100644 --- a/src/TextMateSharp/Internal/Grammars/reader/GrammarReader.cs +++ b/src/TextMateSharp/Internal/Grammars/reader/GrammarReader.cs @@ -5,7 +5,7 @@ namespace TextMateSharp.Internal.Grammars.Reader { - public class GrammarReader + public static class GrammarReader { public static IRawGrammar ReadGrammarSync(StreamReader reader) { diff --git a/src/TextMateSharp/Internal/Matcher/MatchInjectionResult.cs b/src/TextMateSharp/Internal/Matcher/MatchInjectionResult.cs index fa7ab7e..1cd57bc 100644 --- a/src/TextMateSharp/Internal/Matcher/MatchInjectionResult.cs +++ b/src/TextMateSharp/Internal/Matcher/MatchInjectionResult.cs @@ -6,7 +6,7 @@ namespace TextMateSharp.Internal.Matcher { internal class MatchInjectionsResult : MatchResult { - public bool IsPriorityMatch { get; private set; } + internal bool IsPriorityMatch { get; private set; } internal MatchInjectionsResult( IOnigCaptureIndex[] captureIndexes, diff --git a/src/TextMateSharp/Internal/Matcher/MatchResult.cs b/src/TextMateSharp/Internal/Matcher/MatchResult.cs index 970fbc9..d0ca5aa 100644 --- a/src/TextMateSharp/Internal/Matcher/MatchResult.cs +++ b/src/TextMateSharp/Internal/Matcher/MatchResult.cs @@ -6,8 +6,8 @@ namespace TextMateSharp.Internal.Matcher { class MatchResult { - public IOnigCaptureIndex[] CaptureIndexes { get; private set; } - public RuleId MatchedRuleId { get; private set; } + internal IOnigCaptureIndex[] CaptureIndexes { get; private set; } + internal RuleId MatchedRuleId { get; private set; } internal MatchResult(IOnigCaptureIndex[] captureIndexes, RuleId matchedRuleId) { diff --git a/src/TextMateSharp/Internal/Matcher/MatcherBuilder.cs b/src/TextMateSharp/Internal/Matcher/MatcherBuilder.cs index 753e5ba..5aed009 100644 --- a/src/TextMateSharp/Internal/Matcher/MatcherBuilder.cs +++ b/src/TextMateSharp/Internal/Matcher/MatcherBuilder.cs @@ -4,14 +4,14 @@ namespace TextMateSharp.Internal.Matcher { - public class MatcherBuilder + internal sealed class MatcherBuilder { - public List> Results; + internal List> Results { get; } private readonly Tokenizer _tokenizer; private readonly IMatchesName _matchesName; private string _token; - public MatcherBuilder(string expression, IMatchesName matchesName) + internal MatcherBuilder(string expression, IMatchesName matchesName) { if (expression == null) throw new ArgumentNullException(nameof(expression)); if (matchesName == null) throw new ArgumentNullException(nameof(matchesName)); @@ -217,7 +217,7 @@ private static bool IsIdentifier(string token) class Tokenizer { - private static Regex REGEXP = new Regex("([LR]:|[\\w\\.:][\\w\\.:\\-]*|[\\,\\|\\-\\(\\)])"); + private static readonly Regex REGEXP = new Regex("([LR]:|[\\w\\.:][\\w\\.:\\-]*|[\\,\\|\\-\\(\\)])"); private readonly string _input; Match _currentMatch; diff --git a/src/TextMateSharp/Internal/Matcher/MatcherWithPriority.cs b/src/TextMateSharp/Internal/Matcher/MatcherWithPriority.cs index 2250b44..e70a020 100644 --- a/src/TextMateSharp/Internal/Matcher/MatcherWithPriority.cs +++ b/src/TextMateSharp/Internal/Matcher/MatcherWithPriority.cs @@ -2,12 +2,12 @@ namespace TextMateSharp.Internal.Matcher { - public class MatcherWithPriority + public sealed class MatcherWithPriority { public Predicate Matcher { get; private set; } public int Priority { get; private set; } - public MatcherWithPriority(Predicate matcher, int priority) + internal MatcherWithPriority(Predicate matcher, int priority) { Matcher = matcher; Priority = priority; diff --git a/src/TextMateSharp/Internal/Parser/Json/JSONPListParser.cs b/src/TextMateSharp/Internal/Parser/Json/JSONPListParser.cs index b475c70..b293114 100644 --- a/src/TextMateSharp/Internal/Parser/Json/JSONPListParser.cs +++ b/src/TextMateSharp/Internal/Parser/Json/JSONPListParser.cs @@ -1,6 +1,5 @@ -using System.IO; - using SimpleJSON; +using System.IO; namespace TextMateSharp.Internal.Parser.Json { @@ -25,7 +24,7 @@ public T Parse(StreamReader contents) return pList.GetResult(); } - private void ProcessNode(JSONNode node, PList pList) + private static void ProcessNode(JSONNode node, PList pList) { if (node == null) return; @@ -58,18 +57,6 @@ private void ProcessNode(JSONNode node, PList pList) pList.AddString(node.Value); pList.EndElement("string"); } - else if (node.IsNumber) - { - pList.StartElement("string"); - pList.AddString(node.Value); - pList.EndElement("string"); - } - else if (node.IsBoolean) - { - pList.StartElement("string"); - pList.AddString(node.Value); - pList.EndElement("string"); - } } } } \ No newline at end of file diff --git a/src/TextMateSharp/Internal/Rules/RuleFactory.cs b/src/TextMateSharp/Internal/Rules/RuleFactory.cs index b13e33f..e946214 100644 --- a/src/TextMateSharp/Internal/Rules/RuleFactory.cs +++ b/src/TextMateSharp/Internal/Rules/RuleFactory.cs @@ -5,7 +5,7 @@ namespace TextMateSharp.Internal.Rules { - public class RuleFactory + public static class RuleFactory { public static CaptureRule CreateCaptureRule(IRuleFactoryHelper helper, string name, string contentName, RuleId retokenizeCapturedWithRuleId) diff --git a/src/TextMateSharp/Internal/Rules/RuleId.cs b/src/TextMateSharp/Internal/Rules/RuleId.cs index c8fbcf2..e960c8e 100644 --- a/src/TextMateSharp/Internal/Rules/RuleId.cs +++ b/src/TextMateSharp/Internal/Rules/RuleId.cs @@ -2,7 +2,7 @@ namespace TextMateSharp.Internal.Rules { - public class RuleId + public sealed class RuleId //: IEquatable { public static RuleId NO_RULE = new RuleId(0); diff --git a/src/TextMateSharp/Internal/Utils/RegexSource.cs b/src/TextMateSharp/Internal/Utils/RegexSource.cs index 68b1df1..0b30871 100644 --- a/src/TextMateSharp/Internal/Utils/RegexSource.cs +++ b/src/TextMateSharp/Internal/Utils/RegexSource.cs @@ -5,7 +5,7 @@ namespace TextMateSharp.Internal.Utils { - public class RegexSource + public static class RegexSource { private static readonly Regex CAPTURING_REGEX_SOURCE = new Regex( From e5378f6194ee52d8a56c95b82cb239ee53cec41f Mon Sep 17 00:00:00 2001 From: Dave Black <656118+udlose@users.noreply.github.com> Date: Sat, 7 Mar 2026 10:35:59 -0600 Subject: [PATCH 2/4] Remove unused field `_lineText`, refactor LineTokens: sealed, readonly, internal methods Refactored LineTokens to be sealed and updated fields to readonly for improved immutability. Removed unused _lineText field and constructor parameter. Changed public methods to internal for better encapsulation. Cleaned up code and removed unnecessary fields. --- .../Internal/Grammars/LineTokens.cs | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/TextMateSharp/Internal/Grammars/LineTokens.cs b/src/TextMateSharp/Internal/Grammars/LineTokens.cs index dcf9f34..36e1e0a 100644 --- a/src/TextMateSharp/Internal/Grammars/LineTokens.cs +++ b/src/TextMateSharp/Internal/Grammars/LineTokens.cs @@ -1,35 +1,30 @@ -using System; using System.Collections.Generic; using TextMateSharp.Grammars; using TextMateSharp.Themes; namespace TextMateSharp.Internal.Grammars { - internal class LineTokens + internal sealed class LineTokens { - private ReadOnlyMemory _lineText; - // used only if `_emitBinaryTokens` is false. - private List _tokens; + private readonly List _tokens; - private bool _emitBinaryTokens; + private readonly bool _emitBinaryTokens; // used only if `_emitBinaryTokens` is true. - private List binaryTokens; + private readonly List binaryTokens; private int _lastTokenEndIndex = 0; - private List _tokenTypeOverrides; - - private BalancedBracketSelectors _balancedBracketSelectors; + private readonly List _tokenTypeOverrides; + + private readonly BalancedBracketSelectors _balancedBracketSelectors; internal LineTokens( bool emitBinaryTokens, - ReadOnlyMemory lineText, List tokenTypeOverrides, BalancedBracketSelectors balancedBracketSelectors) { this._emitBinaryTokens = emitBinaryTokens; - this._lineText = lineText; if (this._emitBinaryTokens) { this._tokens = null; @@ -44,12 +39,12 @@ internal LineTokens( this._balancedBracketSelectors = balancedBracketSelectors; } - public void Produce(StateStack stack, int endIndex) + internal void Produce(StateStack stack, int endIndex) { this.ProduceFromScopes(stack.ContentNameScopesList, endIndex); } - public void ProduceFromScopes(AttributedScopeStack scopesList, int endIndex) + internal void ProduceFromScopes(AttributedScopeStack scopesList, int endIndex) { if (this._lastTokenEndIndex >= endIndex) { @@ -129,7 +124,7 @@ public void ProduceFromScopes(AttributedScopeStack scopesList, int endIndex) } - public IToken[] GetResult(StateStack stack, int lineLength) + internal IToken[] GetResult(StateStack stack, int lineLength) { if (this._tokens.Count != 0 && this._tokens[this._tokens.Count - 1].StartIndex == lineLength - 1) { @@ -147,7 +142,7 @@ public IToken[] GetResult(StateStack stack, int lineLength) return this._tokens.ToArray(); } - public int[] GetBinaryResult(StateStack stack, int lineLength) + internal int[] GetBinaryResult(StateStack stack, int lineLength) { if (this.binaryTokens.Count != 0 && this.binaryTokens[this.binaryTokens.Count - 2] == lineLength - 1) { From c5189b24ce4cbf30ab26e49ab3ddfdf16c7dc9e9 Mon Sep 17 00:00:00 2001 From: Dave Black <656118+udlose@users.noreply.github.com> Date: Sat, 7 Mar 2026 10:57:44 -0600 Subject: [PATCH 3/4] Make TokenizeLineResult2 a sealed class TokenizeLineResult2 is now sealed, preventing inheritance and potential subclassing. This change improves code safety and may offer performance benefits. --- src/TextMateSharp/Internal/Grammars/TokenizeLineResult2.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TextMateSharp/Internal/Grammars/TokenizeLineResult2.cs b/src/TextMateSharp/Internal/Grammars/TokenizeLineResult2.cs index beb0eb9..5c3e406 100644 --- a/src/TextMateSharp/Internal/Grammars/TokenizeLineResult2.cs +++ b/src/TextMateSharp/Internal/Grammars/TokenizeLineResult2.cs @@ -2,7 +2,7 @@ namespace TextMateSharp.Internal.Grammars { - internal class TokenizeLineResult2 : ITokenizeLineResult2 + internal sealed class TokenizeLineResult2 : ITokenizeLineResult2 { public int[] Tokens { get; private set; } public IStateStack RuleStack { get; private set; } From a3704fd35e3ed4fe2955cd48b2df045834f4501d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Pe=C3=B1alba?= Date: Sun, 8 Mar 2026 14:04:09 +0100 Subject: [PATCH 4/4] Update src/TextMateSharp/Internal/Rules/RuleId.cs --- src/TextMateSharp/Internal/Rules/RuleId.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TextMateSharp/Internal/Rules/RuleId.cs b/src/TextMateSharp/Internal/Rules/RuleId.cs index e960c8e..f78b132 100644 --- a/src/TextMateSharp/Internal/Rules/RuleId.cs +++ b/src/TextMateSharp/Internal/Rules/RuleId.cs @@ -2,7 +2,7 @@ namespace TextMateSharp.Internal.Rules { - public sealed class RuleId //: IEquatable + public sealed class RuleId { public static RuleId NO_RULE = new RuleId(0);