TextMateSharp

repository·master·Indexed 41 days ago

https://github.com/danipen/textmatesharp

A .NET port of the VS Code TextMate grammar interpreter that enables high-fidelity syntax highlighting in .NET applications using the Oniguruma regex engine and JSON TextMate grammar files. It provides a Registry for loading grammars and themes, and supports tokenizing lines with state management via RuleStack for multi-line parsing.

Tokens
1.9K
Snippets
8
Records
10
Agent score
50%

What's inside TextMateSharp

  1. Update TypeScript grammars

    master

    To update the TypeScript grammars to the latest version, navigate to the TypeScript extension directory and run the update command via npm. After updating, it is recommended to run the integration tests to ensure correctness.

    1. cd extensions/typescript
    2. npm run update-grammars
    3. ./scripts/test-integration.sh
    cd extensions/typescript
    npm run update-grammars
    ./scripts/test-integration.sh
  2. How to use TextMateSharp for syntax highlighting

    master

    To perform syntax highlighting, you follow a workflow of initializing a Registry, loading a grammar, and then tokenizing lines while maintaining a ruleStack to handle multi-line state.

    1. Initialize the Registry: Create a Registry.Registry instance with RegistryOptions (e.g., specifying a ThemeName).
    2. Load a Grammar: Use registry.LoadGrammar(string filename) to get an IGrammar instance.
    3. Tokenize Lines: Iterate through your text lines. For each line, call grammar.TokenizeLine(line, ruleStack, timeout).
    4. Maintain State: The ITokenizeLineResult returned by TokenizeLine contains a RuleStack. You must pass this RuleStack back into the next call to TokenizeLine to ensure correct multi-line parsing (e.g., for comments or strings).
    5. Apply Themes: Use registry.GetTheme() to get the current theme and call theme.Match(token.Scopes) to find the matching visual rules for a token's scopes.
    using TextMateSharp.Grammars;
    using TextMateSharp.Themes;
    
    // 1. Initialize Registry
    var registry = new Registry.Registry(new RegistryOptions(ThemeName.DarkPlus));
    
    // 2. Load Grammar
    IGrammar grammar = registry.LoadGrammar("source.cs");
    
    // 3. Tokenize and maintain state
    IStateStack? ruleStack = null;
    string line = "using static System; /* comment */";
    
    ITokenizeLineResult result = grammar.TokenizeLine(line, ruleStack, TimeSpan.MaxValue);
    ruleStack = result.RuleStack; // Crucial for next line
    
    // 4. Apply Theme
    Theme theme = registry.GetTheme();
    foreach (var token in result.Tokens)
    {
        foreach (var themeRule in theme.Match(token.Scopes))
        {
            var fg = theme.GetColor(themeRule.foreground);
            // Use fg color for rendering
        }
    }
  3. Install TextMateSharp via NuGet

    master

    TextMateSharp is distributed via NuGet. To use the core library and the bundled grammars/themes, install both packages using the .NET CLI:

    dotnet add package TextMateSharp
    dotnet add package TextMateSharp.Grammars
  4. Update JavaScript grammars from TypeScriptReact source

    master

    To update the JavaScript.tmLanguage.json grammar using the latest source from TypeScriptReact.tmLanguage, follow these steps:

    1. Navigate to the typescript extensions directory: cd extensions/typescript
    2. Run the update command: npm run update-grammars
    3. Verify the changes by running the integration tests: ./scripts/test-integration.sh (from the repository root)

    The update script automatically performs the following transformations:

    • Updates fileTypes from .tsx to .js and .jsx.
    • Updates scopeName from scope.tsx to scope.js.
    • Updates all rule names by replacing .tsx with .js.
    cd extensions/typescript
    npm run update-grammars
    ./scripts/test-integration.sh
  5. Supported languages in TextMateSharp.Grammars

    master

    The TextMateSharp.Grammars package provides syntax highlighting for over 50 languages, including:

    • C#, C/C++, Java, JavaScript, TypeScript, Python, Ruby, Rust, Go, Swift, Dart, PHP, SQL, HTML, CSS, SCSS, Less, JSON, XML, YAML, Markdown, LaTeX, Lua, PowerShell, Bash, F#, VB.NET
  6. Available themes in TextMateSharp.Grammars

    master

    The TextMateSharp.Grammars package includes the following themes:

    • Abyss
    • Dark
    • Dark+
    • Dimmed Monokai
    • Kimbie Dark
    • Light
    • Light+
    • One Dark
    • Monokai
    • Quiet Light
    • Red
    • Solarized Dark
    • Solarized Light
    • Tomorrow Night Blue
    • High Contrast Light
    • High Contrast Dark
    • Dracula
    • Atom One Light
    • Atom One Dark
    • Visual Studio Light
    • Visual Studio Dark
  7. Retrieve colors from a Theme via ColorMap

    master

    The Theme class provides methods to interact with its internal ColorMap. You can retrieve the full collection of colors, or look up specific colors using their integer IDs or hex strings.

    • GetColorMap(): Returns an ICollection<string> of all colors defined in the theme.
    • GetColorId(string color): Returns the integer ID associated with a hex color string.
    • GetColor(int id): Returns the hex color string associated with a specific integer ID.
    // Get all available colors
    var allColors = theme.GetColorMap();
    
    // Get ID for a specific color
    int colorId = theme.GetColorId("#ffffff");
    
    // Get hex string from ID
    string hex = theme.GetColor(colorId);
  8. Match theme rules for a list of scopes

    master

    Use the Match method on a Theme instance to find all applicable ThemeTrieElementRule objects for a given list of scope names. The method searches through both the local theme rules and any included themes. It returns a list of rules that should be applied to the provided scopes, typically used during the syntax highlighting process to determine colors and font styles.

    // Given a list of scopes from a tokenizer
    IList<string> scopes = new List<string> { "source.js", "variable.other" };
    
    // Retrieve the matching rules
    List<ThemeTrieElementRule> rules = theme.Match(scopes);
  9. Create a Theme using CreateFromRawTheme

    master

    To initialize a Theme object from a raw theme source (such as a VS Code-compatible theme file), use the Theme.CreateFromRawTheme static method. This method handles parsing the theme rules, resolving includes, and building the color map. It requires an implementation of IRawTheme and IRegistryOptions to resolve theme dependencies.

    // Example usage (requires IRawTheme and IRegistryOptions implementations)
    Theme theme = Theme.CreateFromRawTheme(myRawTheme, myRegistryOptions);
  10. Access GUI colors from a Theme

    master

    Themes can contain GUI-specific color definitions (e.g., for the editor UI). You can retrieve these as a read-only dictionary of color names to hex strings using GetGuiColorDictionary().

    var guiColors = theme.GetGuiColorDictionary();
    if (guiColors.TryGetValue("editor.background", out string hexColor)) 
    {
        // Use the hex color
    }