Skip to content
Merged
Show file tree
Hide file tree
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
179 changes: 148 additions & 31 deletions src/TextMateSharp.Grammars/GrammarDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;

using SimpleJSON;

using TextMateSharp.Grammars.Resources;

namespace TextMateSharp.Grammars
{
public class Engines
{
[JsonPropertyName("engines")]
public string VsCode { get; set; }
}

public class Scripts
{
[JsonPropertyName("update-grammar")]
public string UpdateGrammar { get; set; }
}

public class Language
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("extensions")]
public List<string> Extensions { get; set; }
[JsonPropertyName("aliases")]
public List<string> Aliases { get; set; }
[JsonPropertyName("configuration")]
public string ConfigurationFile { get; set; }
public LanguageConfiguration Configuration {get; set;}

// May be null
[JsonPropertyName("mimetypes")]
public List<string> MimeTypes { get; set; }

public override string ToString()
Expand All @@ -46,62 +37,188 @@ public override string ToString()

public class Grammar
{
[JsonPropertyName("language")]
public string Language { get; set; }
[JsonPropertyName("scopeName")]
public string ScopeName { get; set; }
[JsonPropertyName("path")]
public string Path { get; set; }
}

public class Snippet
{
[JsonPropertyName("language")]
public string Language { get; set; }
[JsonPropertyName("path")]
public string Path { get; set; }
}

public class Contributes
{
[JsonPropertyName("languages")]
public List<Language> Languages { get; set; }
[JsonPropertyName("grammars")]
public List<Grammar> Grammars { get; set; }
[JsonPropertyName("snippets")]
public List<Snippet> Snippets { get; set; }
}

public class Repository
{
[JsonPropertyName("type")]
public string Type { get; set; }
[JsonPropertyName("url")]
public string Url { get; set; }
}

public class GrammarDefinition
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("displayName")]
public string DisplayName { get; set; }
[JsonPropertyName("description")]
public string Description { get; set; }
[JsonPropertyName("version")]
public string Version { get; set; }
[JsonPropertyName("publisher")]
public string Publisher { get; set; }
[JsonPropertyName("license")]
public string License { get; set; }
[JsonPropertyName("engines")]
public Engines Engines { get; set; }
[JsonPropertyName("scripts")]
public Scripts Scripts { get; set; }
[JsonPropertyName("contributes")]
public Contributes Contributes { get; set; }
[JsonPropertyName("repository")]
public Repository Repository { get; set; }
public LanguageSnippets LanguageSnippets { get; set; }

public static GrammarDefinition Parse(Stream stream)
{
using (StreamReader reader = new StreamReader(stream))
{
return Parse(reader.ReadToEnd());
}
}

public static GrammarDefinition Parse(string jsonContent)
{
JSONNode json = JSON.Parse(jsonContent);
if (json == null)
return null;

var definition = new GrammarDefinition
{
Name = json["name"],
DisplayName = json["displayName"],
Description = json["description"],
Version = json["version"],
Publisher = json["publisher"],
License = json["license"]
};

if (json["engines"] != null && !json["engines"].IsNull)
{
definition.Engines = new Engines
{
VsCode = json["engines"]["vscode"]
};
}

if (json["scripts"] != null && !json["scripts"].IsNull)
{
definition.Scripts = new Scripts
{
UpdateGrammar = json["scripts"]["update-grammar"]
};
}

if (json["repository"] != null && !json["repository"].IsNull)
{
definition.Repository = new Repository
{
Type = json["repository"]["type"],
Url = json["repository"]["url"]
};
}

if (json["contributes"] != null && !json["contributes"].IsNull)
{
definition.Contributes = ParseContributes(json["contributes"]);
}

return definition;
}

private static Contributes ParseContributes(JSONNode node)
{
var contributes = new Contributes();

if (node["languages"] != null && node["languages"].IsArray)
{
contributes.Languages = new List<Language>();
foreach (JSONNode langNode in node["languages"].Children)
{
var language = new Language
{
Id = langNode["id"],
ConfigurationFile = GetNullableString(langNode["configuration"])
};

if (langNode["extensions"] != null && langNode["extensions"].IsArray)
{
language.Extensions = new List<string>();
foreach (JSONNode ext in langNode["extensions"].Children)
{
language.Extensions.Add(ext.Value);
}
}

if (langNode["aliases"] != null && langNode["aliases"].IsArray)
{
language.Aliases = new List<string>();
foreach (JSONNode alias in langNode["aliases"].Children)
{
language.Aliases.Add(alias.Value);
}
}

if (langNode["mimetypes"] != null && langNode["mimetypes"].IsArray)
{
language.MimeTypes = new List<string>();
foreach (JSONNode mime in langNode["mimetypes"].Children)
{
language.MimeTypes.Add(mime.Value);
}
}

contributes.Languages.Add(language);
}
}

if (node["grammars"] != null && node["grammars"].IsArray)
{
contributes.Grammars = new List<Grammar>();
foreach (JSONNode grammarNode in node["grammars"].Children)
{
contributes.Grammars.Add(new Grammar
{
Language = grammarNode["language"],
ScopeName = grammarNode["scopeName"],
Path = grammarNode["path"]
});
}
}

if (node["snippets"] != null && node["snippets"].IsArray)
{
contributes.Snippets = new List<Snippet>();
foreach (JSONNode snippetNode in node["snippets"].Children)
{
contributes.Snippets.Add(new Snippet
{
Language = snippetNode["language"],
Path = snippetNode["path"]
});
}
}

return contributes;
}

/// <summary>
/// Helper to convert SimpleJSON string values to null when empty.
/// SimpleJSON returns "" for missing keys, but we need null for proper semantics.
/// </summary>
private static string GetNullableString(JSONNode node)
{
if (node == null || node.IsNull)
return null;

string value = node.Value;
return string.IsNullOrEmpty(value) ? null : value;
}
}
}
16 changes: 0 additions & 16 deletions src/TextMateSharp.Grammars/JsonSerializationContext.cs

This file was deleted.

Loading
Loading