Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LanguageConfiguration.SurroundingPairs #84

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/TextMateSharp.Grammars.Tests/GrammarTests.cs
Original file line number Diff line number Diff line change
@@ -126,6 +126,20 @@ public void Assert_Every_Grammar_With_Language_Configuration_File_Has_Language_C
}
}

[Test]
public void Assert_Language_Configuration_Has_Surrounding_Pairs()
{
RegistryOptions options = new RegistryOptions(ThemeName.Light);

var surroundingPairs = options.GetLanguageByExtension(".cs").Configuration.SurroundingPairs;
Assert.IsNotEmpty(surroundingPairs);
Assert.IsTrue(surroundingPairs.Any(pair => pair[0] == '(' && pair[1] == ')'));

surroundingPairs = options.GetLanguageByExtension(".html").Configuration.SurroundingPairs;
Assert.IsNotEmpty(surroundingPairs);
Assert.IsTrue(surroundingPairs.Any(pair => pair[0] == '<' && pair[1] == '>'));
}

[Test]
public void Assert_Every_Grammar_With_Snippets_Can_Load_Snippets()
{
70 changes: 70 additions & 0 deletions src/TextMateSharp.Grammars/LanguageConfiguration.cs
Original file line number Diff line number Diff line change
@@ -31,6 +31,10 @@ public class LanguageConfiguration
[JsonConverter(typeof(ClosingPairJsonConverter))]
public AutoClosingPairs AutoClosingPairs { get; set; }

[JsonPropertyName("surroundingPairs")]
[JsonConverter(typeof(SurroundingPairJsonConverter))]
public CharacterPair[] SurroundingPairs { get; set; }

[JsonPropertyName("indentationRules")]
[JsonConverter(typeof(IntentationRulesJsonConverter))]
public Indentation IndentationRules { get; set; }
@@ -301,6 +305,72 @@ public override void Write(Utf8JsonWriter writer, AutoClosingPairs value, JsonSe
}
}

public class SurroundingPairJsonConverter : JsonConverter<CharacterPair[]>
{
public override CharacterPair[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var surroundingPairs = new List<CharacterPair>();
if (reader.TokenType == JsonTokenType.StartArray)
{
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
switch (reader.TokenType)
{
case JsonTokenType.StartArray:
var pair = new List<char>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
switch (reader.TokenType)
{
case JsonTokenType.String:
pair.Add(reader.GetString().First());
break;
}
}
surroundingPairs.Add(pair.ToArray());
break;

case JsonTokenType.StartObject:
string propName = string.Empty;
string open = null;
string close = null;
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
switch (reader.TokenType)
{
case JsonTokenType.PropertyName:
propName = reader.GetString();
break;
case JsonTokenType.String:
switch (propName)
{
case "open":
open = reader.GetString();
break;
case "close":
close = reader.GetString();
break;
}
break;
}
}

if (open != null && close != null)
Copy link
Preview

Copilot AI Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing the first character of the 'open' and 'close' strings without checking may lead to exceptions if these strings are empty. Consider validating that both strings contain at least one character before calling First().

Suggested change
if (open != null && close != null)
if (!string.IsNullOrEmpty(open) && !string.IsNullOrEmpty(close))

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

surroundingPairs.Add(new char[] { open.First(), close.First() });

break;
}
}
}

return surroundingPairs.ToArray();
}

public override void Write(Utf8JsonWriter writer, CharacterPair[] value, JsonSerializerOptions options)
{
}
}

public class IntentationRulesJsonConverter : JsonConverter<Indentation>
{
public override Indentation Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)