Skip to content

Commit

Permalink
Merge pull request #71 from Numpsy/TryGetValue
Browse files Browse the repository at this point in the history
Fix some CA1854 NetAnalyzers suggestions
  • Loading branch information
danipen authored Aug 25, 2024
2 parents ce83110 + 4361eac commit 3c5fb57
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private int ScopeToLanguage(string scope)
}

string scopeName = m.Groups[1].Value;
return _embeddedLanguages.ContainsKey(scopeName) ? _embeddedLanguages[scopeName] : 0;
return _embeddedLanguages.TryGetValue(scopeName, out int value) ? value : 0;
}

private static int ToStandardTokenType(string tokenType)
Expand Down
4 changes: 2 additions & 2 deletions src/TextMateSharp/Internal/Grammars/Grammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public IRawGrammar GetExternalGrammar(string scopeName)

public IRawGrammar GetExternalGrammar(string scopeName, IRawRepository repository)
{
if (this._includedGrammars.ContainsKey(scopeName))
if (_includedGrammars.TryGetValue(scopeName, out IRawGrammar value))
{
return this._includedGrammars[scopeName];
return value;
}
else if (this._grammarRepository != null)
{
Expand Down
11 changes: 6 additions & 5 deletions src/TextMateSharp/Internal/Grammars/SyncRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,26 @@ public IGrammar GrammarForScopeName(
Dictionary<string, int> tokenTypes,
BalancedBracketSelectors balancedBracketSelectors)
{
if (!this._grammars.ContainsKey(scopeName))
if (!_grammars.TryGetValue(scopeName, out IGrammar value))
{
IRawGrammar rawGrammar = Lookup(scopeName);
if (rawGrammar == null)
{
return null;
}
this._grammars.Add(scopeName,
new Grammar(

value = new Grammar(
scopeName,
rawGrammar,
initialLanguage,
embeddedLanguages,
tokenTypes,
balancedBracketSelectors,
this,
this));
this);
this._grammars.Add(scopeName, value);
}
return this._grammars[scopeName];
return value;
}

private static void CollectIncludedScopes(ICollection<string> result, IRawGrammar grammar)
Expand Down
7 changes: 4 additions & 3 deletions src/TextMateSharp/Themes/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,12 @@ internal List<ThemeTrieElementRule> Match(string scopeName)
{
lock (this._cachedMatchRoot)
{
if (!this._cachedMatchRoot.ContainsKey(scopeName))
if (!_cachedMatchRoot.TryGetValue(scopeName, out List<ThemeTrieElementRule> value))
{
this._cachedMatchRoot[scopeName] = this._root.Match(scopeName);
value = this._root.Match(scopeName);
this._cachedMatchRoot[scopeName] = value;
}
return this._cachedMatchRoot[scopeName];
return value;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/TextMateSharp/Themes/ThemeTrieElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public List<ThemeTrieElementRule> Match(string scope)
tail = scope.Substring(dotIndex + 1);
}

if (this.children.ContainsKey(head))
if (children.TryGetValue(head, out ThemeTrieElement value))
{
return this.children[head].Match(tail);
return value.Match(tail);
}

arr = new List<ThemeTrieElementRule>();
Expand Down Expand Up @@ -130,9 +130,9 @@ public void Insert(string name, int scopeDepth, string scope, List<string> paren
}

ThemeTrieElement child;
if (this.children.ContainsKey(head))
if (children.TryGetValue(head, out ThemeTrieElement value))
{
child = this.children[head];
child = value;
}
else
{
Expand Down

0 comments on commit 3c5fb57

Please sign in to comment.