{"id":20059684,"url":"https://github.com/danipen/textmatesharp","last_synced_at":"2025-05-05T15:31:58.246Z","repository":{"id":37102290,"uuid":"379581724","full_name":"danipen/TextMateSharp","owner":"danipen","description":"A port of tm4e to bring TextMate grammars to dotnet ecosystem","archived":false,"fork":false,"pushed_at":"2025-02-28T09:03:32.000Z","size":6134,"stargazers_count":120,"open_issues_count":6,"forks_count":25,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-10T02:09:54.948Z","etag":null,"topics":["cross-platform","csharp","dotnet-core","syntax-highlighting","textmate","textmate-grammar","textmate-port"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danipen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"danipen"}},"created_at":"2021-06-23T11:36:14.000Z","updated_at":"2025-04-09T18:19:01.000Z","dependencies_parsed_at":"2024-06-11T12:00:15.623Z","dependency_job_id":"5a60a86c-c2a9-431b-be78-37e04c4ece37","html_url":"https://github.com/danipen/TextMateSharp","commit_stats":{"total_commits":295,"total_committers":6,"mean_commits":"49.166666666666664","dds":"0.057627118644067776","last_synced_commit":"c1d4ad804efeede2475412f788731277ed170640"},"previous_names":[],"tags_count":67,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danipen%2FTextMateSharp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danipen%2FTextMateSharp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danipen%2FTextMateSharp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danipen%2FTextMateSharp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danipen","download_url":"https://codeload.github.com/danipen/TextMateSharp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252524159,"owners_count":21762038,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-platform","csharp","dotnet-core","syntax-highlighting","textmate","textmate-grammar","textmate-port"],"created_at":"2024-11-13T13:08:47.453Z","updated_at":"2025-05-05T15:31:57.313Z","avatar_url":"https://github.com/danipen.png","language":"C#","funding_links":["https://github.com/sponsors/danipen"],"categories":[],"sub_categories":[],"readme":"![dotnet-workflow](https://github.com/danipen/TextMateSharp/actions/workflows/dotnet.yml/badge.svg)\n![GitHub license](https://img.shields.io/github/license/danipen/TextMateSharp)\n[![Current stable version](https://img.shields.io/nuget/v/TextMateSharp.svg)](https://www.nuget.org/packages/TextMateSharp)\n[![Downloads](https://img.shields.io/nuget/dt/TextMateSharp)](https://www.nuget.org/packages/TextMateSharp)\n![Size](https://img.shields.io/github/repo-size/danipen/textmatesharp.svg) \n![GitHub language count](https://img.shields.io/github/languages/count/danipen/TextMateSharp)\n![GitHub top language](https://img.shields.io/github/languages/top/danipen/TextMateSharp)\n\n# TextMateSharp\nAn interpreter for grammar files as defined by TextMate. TextMate grammars use the oniguruma dialect (https://github.com/kkos/oniguruma). Supports loading grammar files only from JSON format. Cross - grammar injections are currently not supported.\n\nTextMateSharp is a port of [microsoft/vscode-textmate](https://github.com/microsoft/vscode-textmate) that brings TextMate grammars to dotnet ecosystem. The implementation is based the Java port [eclipse/tm4e](https://github.com/eclipse/tm4e).\n\nTextMateSharp uses a [wrapper](https://github.com/aikawayataro/Onigwrap) via [nuget packges](https://www.nuget.org/packages/Onigwrap) around [Oniguruma](https://github.com/kkos/oniguruma) regex engine. Thanks [@aikawayataro](https://github.com/aikawayataro) for your contribution.\n\nTextMateSharp is used by [AvaloniaEdit](https://github.com/AvaloniaUI/AvaloniaEdit).\n\n## Building\nJust execute `dotnet build` under the folder [TextMateSharp](https://github.com/danipen/TextMateSharp/tree/master/src/TextMateSharp)\n\n## Using\n```csharp\nclass Program\n{\n    static void Main(string[] args)\n    {\n        try\n        {\n            IRegistryOptions options = new LocalRegistryOptions();\n            Registry.Registry registry = new Registry.Registry(options);\n\n            List\u003cstring\u003e textLines = new List\u003cstring\u003e();\n            textLines.Add(\"using static System; /* comment here */\");\n            textLines.Add(\"namespace Example\");\n            textLines.Add(\"{\");\n            textLines.Add(\"}\");\n\n            IGrammar grammar = registry.LoadGrammar(\"source.cs\");\n\n            StackElement ruleStack = null;\n\n            foreach (string line in textLines)\n            {\n                Console.WriteLine(string.Format(\"Tokenizing line: {0}\", line));\n\n                ITokenizeLineResult result = grammar.TokenizeLine(line, ruleStack);\n\n                ruleStack = result.RuleStack;\n\n                foreach (IToken token in result.GetTokens())\n                {\n                    int startIndex = (token.StartIndex \u003e line.Length) ?\n                        line.Length : token.StartIndex;\n                    int endIndex = (token.EndIndex \u003e line.Length) ?\n                        line.Length : token.EndIndex;\n\n                    Console.WriteLine(string.Format(\n                        \"  - token from {0} to {1} --\u003e{2}\u003c-- with scopes {3}\",\n                        startIndex,\n                        endIndex,\n                        line.SubstringAtIndexes(startIndex, endIndex),\n                        string.Join(\",\", token.Scopes)));\n\n                    foreach (string scopeName in token.Scopes)\n                    {\n                        Theme theme = registry.GetTheme();\n                        List\u003cThemeTrieElementRule\u003e themeRules =\n                            theme.Match(scopeName);\n\n                        foreach (ThemeTrieElementRule themeRule in themeRules)\n                        {\n                            Console.WriteLine(\n                                \"      - Matched theme rule: \" + \n                                \"[bg: {0}, fg:{1}, fontStyle: {2}]\",\n                                theme.GetColor(themeRule.background),\n                                theme.GetColor(themeRule.foreground),\n                                themeRule.fontStyle);\n                        }\n                    }\n                }\n            }\n        }\n        catch (Exception ex)\n        {\n            Console.WriteLine(\"ERROR: \" + ex.Message);\n        }\n    }\n\n    class LocalRegistryOptions : IRegistryOptions\n    {\n        public ICollection\u003cstring\u003e GetInjections(string scopeName)\n        {\n            return null;\n        }\n\n        public IRawGrammar GetGrammar(string scopeName)\n        {\n            return null;\n        }\n\n        public IRawTheme GetTheme(string scopeName)\n        {\n            return null;\n        }\n\n        public IRawTheme GetDefaultTheme()\n        {\n            string themePath = Path.GetFullPath(\n                @\"../../../../test/themes/dark_vs.json\");\n\n            using (StreamReader reader = new StreamReader(themePath))\n            {\n                return ThemeReader.ReadThemeSync(reader);\n            }\n        }\n    }\n}\n```\n\nOUTPUT:\n```\nTokenizing line: using static System; /* comment here */\n  - token from 0 to 5 --\u003eusing\u003c-- with scopes source.cs,keyword.other.using.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:#569CD6, fontStyle: -1]\n  - token from 5 to 6 --\u003e \u003c-- with scopes source.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n  - token from 6 to 12 --\u003estatic\u003c-- with scopes source.cs,keyword.other.static.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:#569CD6, fontStyle: -1]\n  - token from 12 to 13 --\u003e \u003c-- with scopes source.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n  - token from 13 to 19 --\u003eSystem\u003c-- with scopes source.cs,storage.type.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:#569CD6, fontStyle: -1]\n  - token from 19 to 20 --\u003e;\u003c-- with scopes source.cs,punctuation.terminator.statement.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n  - token from 20 to 21 --\u003e \u003c-- with scopes source.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n  - token from 21 to 23 --\u003e/*\u003c-- with scopes source.cs,comment.block.cs,punctuation.definition.comment.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:#6A9955, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n  - token from 23 to 37 --\u003e comment here \u003c-- with scopes source.cs,comment.block.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:#6A9955, fontStyle: -1]\n  - token from 37 to 39 --\u003e*/\u003c-- with scopes source.cs,comment.block.cs,punctuation.definition.comment.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:#6A9955, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\nTokenizing line: namespace Example\n  - token from 0 to 9 --\u003enamespace\u003c-- with scopes source.cs,keyword.other.namespace.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:#569CD6, fontStyle: -1]\n  - token from 9 to 10 --\u003e \u003c-- with scopes source.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n  - token from 10 to 17 --\u003eExample\u003c-- with scopes source.cs,entity.name.type.namespace.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\nTokenizing line: {\n  - token from 0 to 1 --\u003e{\u003c-- with scopes source.cs,punctuation.curlybrace.open.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\nTokenizing line: }\n  - token from 0 to 1 --\u003e}\u003c-- with scopes source.cs\n      - Matched theme rule: [bg: , fg:, fontStyle: -1]\n```\n\n## Demo\n\nThere is a demo project in [TextMateSharp.Demo](https://github.com/danipen/TextMateSharp/tree/master/src/TextMateSharp.Demo) folder.\n\n![image](https://user-images.githubusercontent.com/501613/154065980-44b416ab-3b01-45f7-a8b3-7185413e769c.png)\n\nBuild and run:\n\n```\ncd src/TestMateSharp.Demo\ndotnet build\ndotnet run -- ./testdata/samplefiles/sample.cs\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanipen%2Ftextmatesharp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanipen%2Ftextmatesharp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanipen%2Ftextmatesharp/lists"}