Skip to content

Commit

Permalink
Merge pull request #81 from migeyusu/master
Browse files Browse the repository at this point in the history
able to load from local configuration files
  • Loading branch information
danipen authored Jan 13, 2025
2 parents c25a3a9 + 90af861 commit 246c940
Show file tree
Hide file tree
Showing 7 changed files with 626 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"comments": {
// symbol used for single line comment. Remove this entry if your language does not support line comments
"lineComment": "//",
// symbols used for start and end a block comment. Remove this entry if your language does not support block comments
"blockComment": [ "/*", "*/" ]
},
// symbols used as brackets
"brackets": [
["{", "}"],
["[", "]"],
["(", ")"]
],
// symbols that are auto closed when typing
"autoClosingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"],
["`", "`"]
],
// symbols that can be used to surround a selection
"surroundingPairs": [
["{", "}"],
["[", "]"],
["(", ")"],
["\"", "\""],
["'", "'"],
["`", "`"]
]
}
105 changes: 105 additions & 0 deletions src/TextMateSharp.Grammars.Tests/GrammarAssets/qml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"name": "felgo",
"displayName": "QML Syntax/Tools",
"description": "QML syntax highlighting and autocompletion. Run, test and develop Qt Quick applications with QML Hot Reload.",
"publisher": "felgo",
"author": {
"name": "Felgo GmbH",
"url": "https://felgo.com",
"email": "[email protected]"
},
"version": "2.0.1",
"engines": {
"vscode": "^1.81.0"
},
"categories": [
"Programming Languages",
"Other"
],
"keywords": [
"QML",
"Mobile App",
"Android",
"iOS",
"Qt"
],
"activationEvents": [
"onCommand:qml.runWebLive",
"onCommand:qml.runLive",
"workspaceContains:*.pro",
"workspaceContains:**/.qml",
"onLanguage:qml"
],
"main": "./src/extension.js",
"preview": false,
"contributes": {
"languages": [
{
"id": "qml",
"aliases": [
"QML",
"qml"
],
"extensions": [
".qml"
],
"configuration": "./language-configuration.json"
}
],
"grammars": [
{
"language": "qml",
"scopeName": "source.qml",
"path": "./syntaxes/qml.tmLanguage.json"
}
],
"commands": [
{
"command": "qml.runWebLive",
"title": "QML: Launch Web Live Client"
},
{
"command": "qml.runLive",
"title": "QML: Connect to Mobile Live Client"
}
]
},
"qna": "https://felgo.com/developers/forums",
"bugs": {
"url": "https://felgo.com/developers/forums"
},
"icon": "images/vscode-qml-logo.png",
"scripts": {
"lint": "eslint .",
"pretest": "npm run lint",
"test": "node ./test/runTest.js"
},
"devDependencies": {
"@types/vscode": "^1.81.0",
"@types/mocha": "^10.0.1",
"@types/node": "16.x",
"eslint": "^8.47.0",
"glob": "^10.3.3",
"mocha": "^10.2.0",
"typescript": "^5.1.6",
"@vscode/test-electron": "^2.3.4"
},
"dependencies": {
"lowdb": "^1.0.0",
"puid": "^1.0.7",
"ws": "^8.2.2"
},
"__metadata": {
"id": "a2715b65-4bbb-49c4-bdc6-fa47994e3bb9",
"publisherId": "ee85075b-b1d9-4fd6-93f5-11b4abeacc3e",
"publisherDisplayName": "Felgo",
"targetPlatform": "undefined",
"isApplicationScoped": false,
"isPreReleaseVersion": false,
"hasPreReleaseVersion": false,
"installedTimestamp": 1727576498238,
"pinned": false,
"preRelease": false,
"source": "gallery"
}
}

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions src/TextMateSharp.Grammars.Tests/GrammarTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;

using System.IO;
using System.Linq;
using NUnit.Framework;

namespace TextMateSharp.Grammars.Tests
Expand Down Expand Up @@ -44,7 +45,7 @@ public void Load_Theme_Should_Return_Theme()
RegistryOptions options = new RegistryOptions(ThemeName.Light);

foreach (ThemeName themeName in Enum.GetValues<ThemeName>())
Assert.That(options.LoadTheme(themeName), Is.Not.Null, "Failed: " + themeName.ToString());
Assert.That(options.LoadTheme(themeName), Is.Not.Null, "Failed: " + themeName.ToString());
}

[Test]
Expand Down Expand Up @@ -146,5 +147,22 @@ public void Assert_Every_Grammar_With_Snippets_Can_Load_Snippets()
}
}
}

[Test]
public void AssertLoadFromLocalDir()
{
RegistryOptions options = new RegistryOptions(ThemeName.Light);
options.LoadFromLocalDir("GrammarAssets");
Assert.NotNull(options.GetAvailableLanguages().First((language => language.Id == "qml")));
}

[Test]
public void AssertLoadFromLocalFile()
{
RegistryOptions options = new RegistryOptions(ThemeName.Light);
var combine = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GrammarAssets", "qml", "package.json");
options.LoadFromLocalFile("QML", combine);
Assert.NotNull(options.GetAvailableLanguages().First((language => language.Id == "qml")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@
<ProjectReference Include="..\TextMateSharp\TextMateSharp.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="GrammarAssets\qml\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="GrammarAssets\qml\syntaxes\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="GrammarAssets\" />
</ItemGroup>

</Project>
30 changes: 29 additions & 1 deletion src/TextMateSharp.Grammars/LanguageConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

using System.Threading;
using TextMateSharp.Grammars.Resources;
using TextMateSharp.Internal.Grammars;

Expand Down Expand Up @@ -57,7 +57,22 @@ public static LanguageConfiguration Load(string grammarName, string configuratio
}
}
}

public static LanguageConfiguration LoadFromLocal(string configurationFile)
{
var fileInfo = new FileInfo(configurationFile);
if (!fileInfo.Exists)
{
return null;
}
using (var fileStream = fileInfo.OpenRead())
{
return JsonSerializer.Deserialize(fileStream, jsonContext.LanguageConfiguration);
}
}

}


public class Region
{
Expand Down Expand Up @@ -195,6 +210,19 @@ public static LanguageSnippets Load(string grammarName, Contributes contributes)

return result;
}

public static LanguageSnippets LoadFromLocal(string filePath)
{
var fileInfo = new FileInfo(filePath);
if (!fileInfo.Exists)
{
return null;
}
using (var fileStream = fileInfo.OpenRead())
{
return JsonSerializer.Deserialize(fileStream, jsonContext.LanguageSnippets);
}
}
}

public class ClosingPairJsonConverter : JsonConverter<AutoClosingPairs>
Expand Down
99 changes: 92 additions & 7 deletions src/TextMateSharp.Grammars/RegistryOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using TextMateSharp.Grammars.Resources;

using TextMateSharp.Internal.Grammars.Reader;
using TextMateSharp.Internal.Themes.Reader;
using TextMateSharp.Internal.Types;
Expand All @@ -15,15 +14,101 @@ namespace TextMateSharp.Grammars
{
public class RegistryOptions : IRegistryOptions
{
ThemeName _defaultTheme;
Dictionary<string, GrammarDefinition> _availableGrammars = new Dictionary<string, GrammarDefinition>();
private readonly ThemeName _defaultTheme;

private readonly Dictionary<string, GrammarDefinition> _availableGrammars
= new Dictionary<string, GrammarDefinition>();

public RegistryOptions(ThemeName defaultTheme)
{
_defaultTheme = defaultTheme;
InitializeAvailableGrammars();
}

/// <summary>
/// load from local folder like structure in project
/// </summary>
/// <param name="dirPath"></param>
/// <param name="overwrite"></param>
public void LoadFromLocalDir(string dirPath, bool overwrite = false)
{
var directories = new DirectoryInfo(dirPath).GetDirectories();
foreach (var directory in directories)
{
var grammar = directory.Name.ToUpper();
var packageFileInfo = directory.GetFiles("package.json").FirstOrDefault();
if (packageFileInfo == null)
continue;
LoadFromLocalFile(grammar, packageFileInfo, overwrite);
}
}


/// <summary>
/// load from local file
/// </summary>
/// <param name="grammarName"></param>
/// <param name="packageJsonFileInfo"></param>
/// <param name="overwrite"></param>
public void LoadFromLocalFile(string grammarName, string packageJsonFileInfo, bool overwrite = false)
{
LoadFromLocalFile(grammarName, new FileInfo(packageJsonFileInfo), overwrite);
}

/// <summary>
/// load from local file
/// </summary>
/// <param name="grammarName"></param>
/// <param name="packageJsonFileInfo"></param>
/// <param name="overwrite"></param>
public void LoadFromLocalFile(string grammarName, FileInfo packageJsonFileInfo, bool overwrite = false)
{
if (_availableGrammars.ContainsKey(grammarName) && !overwrite)
{
return;
}

if (!packageJsonFileInfo.Exists)
return;
var baseDir = packageJsonFileInfo.Directory?.FullName ?? string.Empty;
using (Stream stream = packageJsonFileInfo.OpenRead())
{
var definition = JsonSerializer.Deserialize(
stream, JsonSerializationContext.Default.GrammarDefinition);
if (definition == null)
{
return;
}

foreach (var language in definition.Contributes.Languages)
{
if (language.ConfigurationFile == null)
{
language.Configuration = null;
continue;
}

var path = Path.GetFullPath(Path.Combine(baseDir, language.ConfigurationFile));
language.Configuration = LanguageConfiguration.LoadFromLocal(path);
}

if (definition.Contributes?.Snippets != null)
{
definition.LanguageSnippets = new LanguageSnippets();
foreach (var snippet in definition.Contributes.Snippets)
{
var path = Path.GetFullPath(Path.Combine(baseDir, snippet.Path));
var configuration = LanguageSnippets.LoadFromLocal(path);
if (configuration == null) continue;
definition.LanguageSnippets = configuration;
break;
}
}

_availableGrammars.Add(grammarName, definition);
}
}

public List<Language> GetAvailableLanguages()
{
List<Language> result = new List<Language>();
Expand Down Expand Up @@ -62,7 +147,7 @@ public Language GetLanguageByExtension(string extension)
foreach (var languageExtension in language.Extensions)
{
if (extension.Equals(languageExtension,
StringComparison.OrdinalIgnoreCase))
StringComparison.OrdinalIgnoreCase))
{
return language;
}
Expand All @@ -85,7 +170,7 @@ public string GetScopeByExtension(string extension)
foreach (var languageExtension in language.Extensions)
{
if (extension.Equals(languageExtension,
StringComparison.OrdinalIgnoreCase))
StringComparison.OrdinalIgnoreCase))
{
foreach (var grammar in definition.Contributes.Grammars)
{
Expand Down Expand Up @@ -260,4 +345,4 @@ static bool HasGrammar(string id, List<Grammar> grammars)
return false;
}
}
}
}

0 comments on commit 246c940

Please sign in to comment.