Skip to content

Commit

Permalink
Merge pull request #16 from danipen/fix-change-non-concurrent-collect…
Browse files Browse the repository at this point in the history
…ions

Fix change non concurrent collections
  • Loading branch information
danipen authored May 12, 2022
2 parents 7b2415e + eda2d2d commit b0876c1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/TextMateSharp/Grammar/IGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace TextMateSharp.Grammars
{
public interface IGrammar
{
bool IsCompiling { get; }
string GetName();
string GetScopeName();
ICollection<string> GetFileTypes();
Expand Down
25 changes: 21 additions & 4 deletions src/TextMateSharp/Internal/Grammars/Grammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using TextMateSharp.Grammars;
using TextMateSharp.Internal.Grammars.Parser;
using TextMateSharp.Internal.Matcher;
using TextMateSharp.Internal.Oniguruma;
using TextMateSharp.Internal.Rules;
using TextMateSharp.Internal.Types;
using TextMateSharp.Themes;
Expand All @@ -15,6 +14,7 @@ public class Grammar : IGrammar, IRuleFactoryHelper
{
private int? _rootId;
private int _lastRuleId;
private volatile bool _isCompiling;
private Dictionary<int?, Rule> _ruleId2desc;
private Dictionary<string, IRawGrammar> _includedGrammars;
private IGrammarRepository _grammarRepository;
Expand Down Expand Up @@ -45,6 +45,8 @@ public ScopeMetadata GetMetadataForScope(string scope)
return this._scopeMetadataProvider.GetMetadataForScope(scope);
}

public bool IsCompiling => _isCompiling;

public List<Injection> GetInjections()
{
if (this._injections == null)
Expand Down Expand Up @@ -196,8 +198,7 @@ private object Tokenize(string lineText, StackElement prevState, bool emitBinary
{
if (this._rootId == null)
{
this._rootId = RuleFactory.GetCompiledRuleId(this._grammar.GetRepository().GetSelf(), this,
this._grammar.GetRepository());
GenerateRootId();
}

bool isFirstLine;
Expand All @@ -210,7 +211,9 @@ private object Tokenize(string lineText, StackElement prevState, bool emitBinary
rawDefaultMetadata.TokenType, defaultTheme.fontStyle, defaultTheme.foreground,
defaultTheme.background);

string rootScopeName = this.GetRule(this._rootId.Value).GetName(null, null);
string rootScopeName = this.GetRule(this._rootId.Value)?.GetName(null, null);
if (rootScopeName == null)
return null;
ScopeMetadata rawRootMetadata = this._scopeMetadataProvider.GetMetadataForScope(rootScopeName);
int rootMetadata = ScopeListElement.mergeMetadata(defaultMetadata, null, rawRootMetadata);

Expand Down Expand Up @@ -241,6 +244,20 @@ private object Tokenize(string lineText, StackElement prevState, bool emitBinary
return new TokenizeLineResult(lineTokens.GetResult(nextState, lineLength), nextState);
}

private void GenerateRootId()
{
_isCompiling = true;
try
{
this._rootId = RuleFactory.GetCompiledRuleId(this._grammar.GetRepository().GetSelf(), this,
this._grammar.GetRepository());
}
finally
{
_isCompiling = false;
}
}

public string GetName()
{
return _grammar.GetName();
Expand Down
13 changes: 13 additions & 0 deletions src/TextMateSharp/Model/TMModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ void ThreadWorker(object state)
{
int toProcess = -1;

if (model._grammar.IsCompiling)
{
this.model._resetEvent.Reset();
this.model._resetEvent.WaitOne();
continue;
}

lock (this.model._lock)
{
if (model._invalidLines.Count > 0)
Expand Down Expand Up @@ -174,6 +181,12 @@ public int UpdateTokensInRange(ModelTokensChangedEventBuilder eventBuilder, int
int lineIndex = startIndex;
while (lineIndex <= endLineIndex && lineIndex < model._lines.GetNumberOfLines())
{
if (model._grammar.IsCompiling)
{
lineIndex++;
continue;
}

int endStateIndex = lineIndex + 1;
LineTokens r = null;
string text = null;
Expand Down
9 changes: 6 additions & 3 deletions src/TextMateSharp/Themes/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,14 @@ static ParsedTheme ResolveParsedThemeRules(

internal List<ThemeTrieElementRule> Match(string scopeName)
{
if (!this.cache.ContainsKey(scopeName))
lock (this.cache)
{
this.cache[scopeName] = this.root.Match(scopeName);
if (!this.cache.ContainsKey(scopeName))
{
this.cache[scopeName] = this.root.Match(scopeName);
}
return this.cache[scopeName];
}
return this.cache[scopeName];
}

internal ThemeTrieElementRule GetDefaults()
Expand Down

0 comments on commit b0876c1

Please sign in to comment.