Skip to content

Commit

Permalink
vhdl textmate
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Jul 10, 2023
1 parent b1fd1d5 commit a06cc1f
Show file tree
Hide file tree
Showing 11 changed files with 1,467 additions and 49 deletions.
40 changes: 0 additions & 40 deletions src/OneWare.Core/Data/CustomTextMateRegistryOptions.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Avalonia.Platform;
using TextMateSharp.Grammars;
using TextMateSharp.Internal.Grammars.Reader;
using TextMateSharp.Internal.Types;
using TextMateSharp.Registry;
using TextMateSharp.Themes;

namespace OneWare.Core.Extensions.TextMate;

public class CustomTextMateRegistryOptions : IRegistryOptions
{
private List<TextMateLanguage> _availableLanguages = new();

private readonly RegistryOptions _defaultRegistryOptions = new(ThemeName.DarkPlus);

public void RegisterLanguage(string id, string grammarPath, params string[] extensions)
{
_availableLanguages.Add(new TextMateLanguage()
{
Id = id,
GrammarPath = grammarPath,
Extensions = extensions
});
}

public IRawGrammar GetGrammar(string scopeName)
{
var g = _availableLanguages.FirstOrDefault(x => x.Id == scopeName);

if (g == null) return _defaultRegistryOptions.GetGrammar(scopeName);
using var s = new StreamReader(AssetLoader.Open(new Uri(g.GrammarPath)));
{
return GrammarReader.ReadGrammarSync(s);
}
}

public ICollection<string> GetInjections(string scopeName)
{
return _defaultRegistryOptions.GetInjections(scopeName);
}

public Language? GetLanguageByExtension(string extension)
{
var def = _availableLanguages.FirstOrDefault(x => x.Extensions.Contains(extension));
if (def != null)
return new Language()
{
Id = def.Id,
};
return _defaultRegistryOptions.GetLanguageByExtension(extension);
}

public string GetScopeByLanguageId(string languageId)
{
var r = _availableLanguages.FirstOrDefault(x => x.Id == languageId);
return r?.Id ?? _defaultRegistryOptions.GetScopeByLanguageId(languageId);
}

public IRawTheme GetDefaultTheme()
{
return _defaultRegistryOptions.GetDefaultTheme();
}
public IRawTheme GetTheme(string scopeName)
{
return _defaultRegistryOptions.GetTheme(scopeName);
}
public IRawTheme LoadTheme(ThemeName name) => _defaultRegistryOptions.LoadTheme(name);
}
15 changes: 15 additions & 0 deletions src/OneWare.Core/Extensions/TextMate/JsonSerializationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
using TextMateSharp.Grammars;

namespace OneWare.Core.Extensions.TextMate;

[JsonSerializable(typeof(GrammarDefinition))]
[JsonSerializable(typeof(LanguageSnippets))]
[JsonSerializable(typeof(LanguageSnippet))]
[JsonSerializable(typeof(LanguageConfiguration))]
[JsonSerializable(typeof(EnterRule))]
[JsonSerializable(typeof(AutoPair))]
[JsonSerializable(typeof(IList<string>))]
internal sealed partial class JsonSerializationContext : JsonSerializerContext
{
}
8 changes: 8 additions & 0 deletions src/OneWare.Core/Extensions/TextMate/TextMateLanguage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OneWare.Core.Extensions.TextMate;

public class TextMateLanguage
{
public IEnumerable<string> Extensions { get; set; }
public string GrammarPath { get; set; }
public string Id { get; set; }
}
17 changes: 11 additions & 6 deletions src/OneWare.Core/Services/LanguageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using AvaloniaEdit.Highlighting.Xshd;
using CommunityToolkit.Mvvm.ComponentModel;
using OneWare.Core.Data;
using OneWare.Core.Extensions.TextMate;
using OneWare.Shared;
using OneWare.Shared.LanguageService;
using OneWare.Shared.Services;
Expand All @@ -24,8 +25,8 @@ internal class LanguageManager : ILanguageManager
private readonly Dictionary<Type, Dictionary<string,ILanguageService>> _workspaceServers = new();

private readonly Dictionary<string, string> _highlightingDefinitions = new();
private readonly CustomTextMateRegistryOptions _registryOptions = new();
public IRegistryOptions RegistryOptions => _registryOptions;
private readonly CustomTextMateRegistryOptions _textMateRegistryOptions = new();
public IRegistryOptions RegistryOptions => _textMateRegistryOptions;
public IObservable<IRawTheme> CurrentEditorTheme { get; }

public LanguageManager(ISettingsService settingsService)
Expand All @@ -35,9 +36,13 @@ public LanguageManager(ISettingsService settingsService)
? "Editor_SyntaxTheme_Dark"
: "Editor_SyntaxTheme_Light")
.SelectMany(settingsService.GetSettingObservable<ThemeName>)
.Select(b => _registryOptions.LoadTheme(b));
.Select(b => _textMateRegistryOptions.LoadTheme(b));
}

public void RegisterTextMateLanguage(string id, string grammarPath, params string[] extensions)
{
_textMateRegistryOptions.RegisterLanguage(id, grammarPath, extensions);
}

public void RegisterHighlighting(string path, params string[] supportedFileTypes)
{
foreach (var fileType in supportedFileTypes)
Expand Down Expand Up @@ -67,8 +72,8 @@ public void RegisterHighlighting(string path, params string[] supportedFileTypes

public string? GetTextMateScopeByExtension(string fileExtension)
{
var lang = _registryOptions.GetLanguageByExtension(fileExtension);
return lang == null ? null : _registryOptions.GetScopeByLanguageId(lang.Id);
var lang = _textMateRegistryOptions.GetLanguageByExtension(fileExtension);
return lang == null ? null : _textMateRegistryOptions.GetScopeByLanguageId(lang.Id);
}

public void RegisterService(Type type, bool workspaceDependent, params string[] supportedFileTypes)
Expand Down
1 change: 1 addition & 0 deletions src/OneWare.Shared/Services/ILanguageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface ILanguageManager
public IObservable<IRawTheme> CurrentEditorTheme { get; }
public IRegistryOptions RegistryOptions { get; }
public void RegisterHighlighting(string path, params string[] supportedFileTypes);
public void RegisterTextMateLanguage(string id, string grammarPath, params string[] extensions);
public IHighlightingDefinition? GetHighlighting(string fileExtension);
public string? GetTextMateScopeByExtension(string fileExtension);
public void RegisterService(Type type, bool workspaceDependent, params string[] supportedFileTypes);
Expand Down
11 changes: 11 additions & 0 deletions src/OneWare.Vhdl/Assets/vhdl.configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"comments": {
"lineComment": "--",
"blockComment": []
},
"brackets": [
["{", "}"],
["[", "]"],
["(", ")"]
]
}
Loading

0 comments on commit a06cc1f

Please sign in to comment.