{"_id":"@anche/semantic-tokens-utilities","_rev":"2-5977d55b0c6240fcf4b56166b788bdd5","name":"@anche/semantic-tokens-utilities","dist-tags":{"latest":"0.0.2"},"versions":{"0.0.1":{"name":"@anche/semantic-tokens-utilities","version":"0.0.1","description":"Semantic Tokens Utilities","main":"lib/index.js","module":"lib/index.es.js","typings":"lib/semantic-tokens-utilities/src/index.d.ts","keywords":["semantic","highlighting","tokens","vscode","theme","grammar","parser"],"author":{"name":"Angelo Abdoelsamath","email":"angelo.a@me.com"},"homepage":"https://github.com/el-angel/theme-editor#readme","license":"MIT","directories":{"lib":"lib","test":"__tests__"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"repository":{"type":"git","url":"git+https://github.com/el-angel/theme-editor.git"},"scripts":{"test":"jest","test:watch":"jest --watch","clean":"rimraf ./lib","prebuild":"yarn clean","build":"rollup -c","prepack":"yarn build","watch":"rollup --watch -c","typecheck":"tsc --noEmit","lint":"eslint src --ext .ts,.tsx .","precommit":"yarn test && tsc --noEmit --composite false && lint-staged"},"lint-staged":{"src/**/*.{ts,tsx}":["pretty-quick --staged","eslint --fix"]},"bugs":{"url":"https://github.com/el-angel/theme-editor/issues"},"dependencies":{"@anche/shared":"^1.0.0","typescript":"^3.9.7","typescript-vscode-sh-plugin":"^0.6.14"},"devDependencies":{"@rollup/plugin-commonjs":"^14.0.0","@rollup/plugin-node-resolve":"^8.4.0","@types/jest":"^26.0.5","@zerollup/ts-transform-paths":"^1.7.18","jest":"^26.1.0","rimraf":"^3.0.2","rollup":"^2.22.1","rollup-plugin-typescript2":"^0.27.1","ts-jest":"^26.1.3","ttypescript":"^1.5.10"},"gitHead":"1761a7da1b5654f62db95a0bb69a1592911b086c","_id":"@anche/semantic-tokens-utilities@0.0.1","_nodeVersion":"12.14.1","_npmVersion":"lerna/3.22.0/node@v12.14.1+x64 (darwin)","dist":{"integrity":"sha512-I94qGYlEzPS687I5lT56s70Z02FsMZljABwicZ5tFu73duNleNe8BopEljpmlg5HqN/vz/O7z+3ZrCmi37iCIQ==","shasum":"58c54d8e414774b90ddda03e58bde779a10f8266","tarball":"https://registry.npmjs.org/@anche/semantic-tokens-utilities/-/semantic-tokens-utilities-0.0.1.tgz","fileCount":35,"unpackedSize":84166,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfKx1YCRA9TVsSAnZWagAAnJcQAIy8R9IvLzfFQq2H7wxo\n8Pryqft6Wyb2JdSoXCWgd5E4GT9UtVs6vj5+QiDp8phg3YqosLN0CzF9OCPI\nxC4yXbI6tjTGBPATK8xYLhm9Y3lzMNbC3sAZ6yIcKoO/0eqUVQGiuJJcT+X2\nQRuZvGnSQ9SJHRqqLSWnnn9yR+mKJZOl/wzy0GQ1ai4P8xK1g59MMtrl4+3P\nG6dvMo9tmBIhCL1RbO95QmJuRI+Io82XWqxYHo/eg8WAbOnATWrPv0UBxlGV\nUYjhNHq7wo7Ic5/C7O8Z1QUi08N2fTR692B6hMeVtJ2E/dVA5LyqWUvaOg8U\n5Q0983T7PTgu6wUe/jmm7bbsE2KxMzAzGjVA5ADW3pv5E4S9seKc+pyeJz7F\nXwKG5Stehu8YOGmztaV39c15VsRJlXDLc2d6avnXt6ce1I3C3UxCIL+PTNPO\nm+XPKwPcz12dOqGRGc+3u8CifgUjUU8D4gSvWaq3dm2bL60aklJx1fh7Mk0i\nJjKvb6/62BVUy+zKg57WJxb63kOlHElkRR3nmkpJ5XFReEneHUOPRFhVAvbc\nKiEYEjynJpmaJ/+nyMWLI85C3vp0ypXf6eo7cb61Ma/AssVCR5yXqJTfVMtn\ncKNOZret9AJRieiQ2T2em4Pk5X7QGGEIbpsOelpDzNuLimIYNjI7WZMQqNFa\n2Wcu\r\n=ixQC\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQChx7QqMn9oFYvOrp/foo3zsp3/n5G1tEV4C/RiU5pzEwIgCfkZHf7WulGxQWPPP9jZi54Fkto0vMftutiwEByi9+A="}]},"maintainers":[{"name":"godlevel","email":"angelo.a@me.com"}],"_npmUser":{"name":"godlevel","email":"angelo.a@me.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/semantic-tokens-utilities_0.0.1_1596661080161_0.1354317774804754"},"_hasShrinkwrap":false},"0.0.2":{"name":"@anche/semantic-tokens-utilities","version":"0.0.2","description":"Semantic Tokens Utilities","main":"lib/index.js","module":"lib/index.es.js","typings":"lib/semantic-tokens-utilities/src/index.d.ts","keywords":["semantic","highlighting","tokens","vscode","theme","grammar","parser"],"author":{"name":"Angelo Abdoelsamath","email":"angelo.a@me.com"},"homepage":"https://github.com/el-angel/theme-editor#readme","license":"MIT","directories":{"lib":"lib","test":"__tests__"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"repository":{"type":"git","url":"git+https://github.com/el-angel/theme-editor.git"},"scripts":{"test":"jest","test:watch":"jest --watch","clean":"rimraf ./lib","prebuild":"yarn clean","build":"rollup -c","prepack":"yarn build","watch":"rollup --watch -c","typecheck":"tsc --noEmit","lint":"eslint src --ext .ts,.tsx .","precommit":"yarn test && tsc --noEmit --composite false && lint-staged"},"lint-staged":{"src/**/*.{ts,tsx}":["pretty-quick --staged","eslint --fix"]},"bugs":{"url":"https://github.com/el-angel/theme-editor/issues"},"dependencies":{"typescript":"^3.9.7","typescript-vscode-sh-plugin":"^0.6.14"},"devDependencies":{"@rollup/plugin-commonjs":"^14.0.0","@rollup/plugin-node-resolve":"^8.4.0","@types/jest":"^26.0.5","@zerollup/ts-transform-paths":"^1.7.18","jest":"^26.1.0","rimraf":"^3.0.2","rollup":"^2.22.1","rollup-plugin-typescript2":"^0.27.1","ts-jest":"^26.1.3","ttypescript":"^1.5.10"},"gitHead":"856189d1ee79de64a7fc0072711907d24ba5d62c","_id":"@anche/semantic-tokens-utilities@0.0.2","_nodeVersion":"12.14.1","_npmVersion":"lerna/3.22.0/node@v12.14.1+x64 (darwin)","dist":{"integrity":"sha512-5Wm0Y8j7LnVr/VTyrorN94Zvws0EzdhZ+Di64KkCHZSv9HHggjzT5OCEbCgJwOdgKJsfsfo2cRUXk0w+tJZuTA==","shasum":"4bd5f2c7c5f0423103c3d70e82a807574c52166f","tarball":"https://registry.npmjs.org/@anche/semantic-tokens-utilities/-/semantic-tokens-utilities-0.0.2.tgz","fileCount":35,"unpackedSize":84131,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfMr9dCRA9TVsSAnZWagAA99wQAJpSGzDK7Dv0WRrdDDKc\nsL4F/lHOx8J6enAoWK8y6psvQ2wHsWG0fJmgt1UE9tROB487wDJ6hdoCFJip\nkcf7lIAyW115JOCor6v/c0VKabpqLrWHZurOZ2CWnvIk3hj67J7rTkVBhksy\nHvntG4XA6Wf461SofhhLvfuuP3bDCK4GzuH/f5WNwaxfaf8d73w88teoJr5Z\nnapJPCEpGJgWYOVqe6uNUQFln8guMv1UM3z3cRmYC4GDRgb5vLKab7SRqNn2\nf5TdJ9Q/fefy88mmq86rdSthW3ImDO+gN9NQjw9NLbJh6V4I9pLYKzvU+vGi\nymcdLO1Qy8PPRQzMpM6KzzjZmP4v99Z+vJwuN0bFPG0l37AnnTN0BnoF3kj8\n4qfyT02DEOZajzLuzK6XY4ymZHQpTcWrq+aLcGFw4r6Uqh8ALrQNR4BPx6QF\nO7fniO9sFENJ4le0AL3jLJGTl6GFSrQ1jJhvYMZ4FaoK4s4iJUrs/CkNFC1l\nRZfYtTy7Zuw6A9eFOMnrwibFOZeKkFRUEWDVeBydcO0nVhXyGBi9B8IDVNj3\nLTXMErlywz01T9UkDL744NoD1W+CINQul1fwY40LC81elHkSm4P/wh0l3SLh\nDOD1GHwORtrvq2idzt4vBMIS5kZcBgzsfuuzH9J+FF1MSGPoAnOM0Ie4wtFV\n0P4B\r\n=ueer\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDI61o6it8vgNlDzarSWtEjSN5i5HVDMhYbpblM22qNaAIhAJRCP4LEyGTkuuUwz5t6PnQHJhIZcSGNXZYjOOvGKGNz"}]},"maintainers":[{"name":"godlevel","email":"angelo.a@me.com"}],"_npmUser":{"name":"godlevel","email":"angelo.a@me.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/semantic-tokens-utilities_0.0.2_1597161308672_0.05757503801460717"},"_hasShrinkwrap":false}},"time":{"created":"2020-08-05T20:57:59.889Z","0.0.1":"2020-08-05T20:58:00.274Z","modified":"2022-04-04T13:56:50.855Z","0.0.2":"2020-08-11T15:55:08.817Z"},"maintainers":[{"name":"godlevel","email":"angelo.a@me.com"}],"description":"Semantic Tokens Utilities","homepage":"https://github.com/el-angel/theme-editor#readme","keywords":["semantic","highlighting","tokens","vscode","theme","grammar","parser"],"repository":{"type":"git","url":"git+https://github.com/el-angel/theme-editor.git"},"author":{"name":"Angelo Abdoelsamath","email":"angelo.a@me.com"},"bugs":{"url":"https://github.com/el-angel/theme-editor/issues"},"license":"MIT","readme":"# `semantic-tokens-utilities`\n\nThis package exposes several utilities that help with Semantic Tokens.\n\n```typescript\ninterface Position {\n    line: number;\n    start: number;\n    length: number;\n}\n\ninterface Token {\n    type: string;\n    modifiers: string[];\n    language?: string;\n}\n\ninterface SemanticToken extends Position, Token {}\n```\n\n## Usage\n\nFirst you should/could initialize the match helper with the scope fallbacks before you use the match utility from this package. Those fallbacks are TextMate scopes. (https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide).\n\n(You could call the initialize module before starting your app)\n\n```typescript\nimport { initialize } from '@anche/semantic-tokens-utilities';\n\nexport type FallbackRegister = (tokenString: string, fallbackScopes: string[]) => void;\n\n(() => {\n    initialize((registerTokenFallback: FallbackRegister) => {\n        // Define your fallbacks:\n\n        registerTokenFallback('namespace', 'entity.name.namespace');\n    });\n    App.start();\n})();\n```\n\n```typescript\n// If you want to have the same fallbacks as VSCode\nimport { initialize, Presets } from '@anche/semantic-tokens-utilities';\n\ninitialize(Presets.vscode);\n```\n\n## Parser\n\nThe parser transforms a string into a CodeDocument and returns an array of Semantic Tokens\n\n(This can only be executed on a node server)\n\n```typescript\ninterface CodeDocument {\n    getTextAtPosition(position: Position): string;\n    getLines(): string[];\n    getRaw(): string;\n}\n\ntype SemanticTokensParserResult = <T extends Position>{\n    code: CodeDocument;\n    tokens: T[];\n}\n\nimport { parser } from '@anche/semantic-tokens-utilities';\n\nconst rawCode = `const semanticTokens = () => {}`;\n\nconst parserResult = parser({ code: rawCode, language: 'tsx' })\n;\n\nconst firstNode = parserResult.tokens[0];\n// `semanticTokens` is a Semantic Token:\n// {\n//     line: 0,\n//     start: 6,\n//     length: 13,\n//     type: 'function',\n//     modifiers: ['declaration', 'readonly'],\n//     language: undefined\n// }\n```\n\n### Matcher\n\n```typescript\ninterface Token {\n    type: string;\n    modifiers: string[];\n    language?: string;\n}\n\ninterface TokenFallback {\n    token: Token;\n    fallbackScopes: string[];\n}\n\ninterface TokenWinner<T extends Token> {\n    token: T;\n    score: number;\n}\n\ninterface Matcher {\n    // returns a Semantic Token or TextMate fallback scopes\n    matchToken: <T extends Token>(\n        token: string | Token,\n        semanticTokens: T[],\n    ) =>\n        | {\n              token?: T;\n              fallbackScopes: string[];\n          }\n        | undefined;\n\n    //  will return TextMate fallback scopes defined during initialization\n    getFallback: (token: Token) => TokenFallback | undefined;\n\n    // will return a matching Semantic Token or undefined\n    getMatch: <T extends Token>(token: Token, semanticTokens: T[]) => TokenWinner<T> | undefined;\n}\n\n// Defined Semantic Tokens\nconst semanticTokens = [\n    { scope: 'member.defaultLibrary', color: '#000000' }\n    { scope: 'variable.declaration.readonly', color: '#ff00ff'\n    { scope: 'function', color: '#00ff00' }\n];\n\nconst code = `const semanticTokens = () => {};`;\n\n// after parsing you have the node for `semanticTokens`:\nconst firstNode = parserResult.tokens[0];\n// {\n//     line: 0,\n//     start: 6,\n//     length: 13,\n//     type: 'function',\n//     modifiers: ['declaration', 'readonly'],\n//     language: undefined\n// }\n\nimport { Matcher } from '@anche/semantic-tokens-utilities';\n\n// semanticTokens[2] will return\nconst rule = Matcher.matchToken(firstNode, semanticTokens);\n\n\n```\n","readmeFilename":"README.md"}