Skip to content

Commit b0876c1

Browse files
authored
Merge pull request #16 from danipen/fix-change-non-concurrent-collections
Fix change non concurrent collections
2 parents 7b2415e + eda2d2d commit b0876c1

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

src/TextMateSharp/Grammar/IGrammar.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace TextMateSharp.Grammars
44
{
55
public interface IGrammar
66
{
7+
bool IsCompiling { get; }
78
string GetName();
89
string GetScopeName();
910
ICollection<string> GetFileTypes();

src/TextMateSharp/Internal/Grammars/Grammar.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using TextMateSharp.Grammars;
55
using TextMateSharp.Internal.Grammars.Parser;
66
using TextMateSharp.Internal.Matcher;
7-
using TextMateSharp.Internal.Oniguruma;
87
using TextMateSharp.Internal.Rules;
98
using TextMateSharp.Internal.Types;
109
using TextMateSharp.Themes;
@@ -15,6 +14,7 @@ public class Grammar : IGrammar, IRuleFactoryHelper
1514
{
1615
private int? _rootId;
1716
private int _lastRuleId;
17+
private volatile bool _isCompiling;
1818
private Dictionary<int?, Rule> _ruleId2desc;
1919
private Dictionary<string, IRawGrammar> _includedGrammars;
2020
private IGrammarRepository _grammarRepository;
@@ -45,6 +45,8 @@ public ScopeMetadata GetMetadataForScope(string scope)
4545
return this._scopeMetadataProvider.GetMetadataForScope(scope);
4646
}
4747

48+
public bool IsCompiling => _isCompiling;
49+
4850
public List<Injection> GetInjections()
4951
{
5052
if (this._injections == null)
@@ -196,8 +198,7 @@ private object Tokenize(string lineText, StackElement prevState, bool emitBinary
196198
{
197199
if (this._rootId == null)
198200
{
199-
this._rootId = RuleFactory.GetCompiledRuleId(this._grammar.GetRepository().GetSelf(), this,
200-
this._grammar.GetRepository());
201+
GenerateRootId();
201202
}
202203

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

213-
string rootScopeName = this.GetRule(this._rootId.Value).GetName(null, null);
214+
string rootScopeName = this.GetRule(this._rootId.Value)?.GetName(null, null);
215+
if (rootScopeName == null)
216+
return null;
214217
ScopeMetadata rawRootMetadata = this._scopeMetadataProvider.GetMetadataForScope(rootScopeName);
215218
int rootMetadata = ScopeListElement.mergeMetadata(defaultMetadata, null, rawRootMetadata);
216219

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

247+
private void GenerateRootId()
248+
{
249+
_isCompiling = true;
250+
try
251+
{
252+
this._rootId = RuleFactory.GetCompiledRuleId(this._grammar.GetRepository().GetSelf(), this,
253+
this._grammar.GetRepository());
254+
}
255+
finally
256+
{
257+
_isCompiling = false;
258+
}
259+
}
260+
244261
public string GetName()
245262
{
246263
return _grammar.GetName();

src/TextMateSharp/Model/TMModel.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ void ThreadWorker(object state)
6969
{
7070
int toProcess = -1;
7171

72+
if (model._grammar.IsCompiling)
73+
{
74+
this.model._resetEvent.Reset();
75+
this.model._resetEvent.WaitOne();
76+
continue;
77+
}
78+
7279
lock (this.model._lock)
7380
{
7481
if (model._invalidLines.Count > 0)
@@ -174,6 +181,12 @@ public int UpdateTokensInRange(ModelTokensChangedEventBuilder eventBuilder, int
174181
int lineIndex = startIndex;
175182
while (lineIndex <= endLineIndex && lineIndex < model._lines.GetNumberOfLines())
176183
{
184+
if (model._grammar.IsCompiling)
185+
{
186+
lineIndex++;
187+
continue;
188+
}
189+
177190
int endStateIndex = lineIndex + 1;
178191
LineTokens r = null;
179192
string text = null;

src/TextMateSharp/Themes/Theme.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,14 @@ static ParsedTheme ResolveParsedThemeRules(
318318

319319
internal List<ThemeTrieElementRule> Match(string scopeName)
320320
{
321-
if (!this.cache.ContainsKey(scopeName))
321+
lock (this.cache)
322322
{
323-
this.cache[scopeName] = this.root.Match(scopeName);
323+
if (!this.cache.ContainsKey(scopeName))
324+
{
325+
this.cache[scopeName] = this.root.Match(scopeName);
326+
}
327+
return this.cache[scopeName];
324328
}
325-
return this.cache[scopeName];
326329
}
327330

328331
internal ThemeTrieElementRule GetDefaults()

0 commit comments

Comments
 (0)