Monaco TextMate grammar helpers
npm install monaco-textmate> This repository is a heavily modified version of original vscode-textmate package. It's been adjusted here and there to run inside web browsers. All of the file system calls have been removed, most of the API now uses Promises and grammars can no longer be loaded synchronously.
⚠ I'd prefer to see this repository where it really belongs. I request anyone from Microsoft to adopt this package as soon as possible.
An interpreter for grammar files as defined by TextMate that runs on the web. Supports loading grammar files from JSON or PLIST format. Cross - grammar injections are currently not supported.
``sh`
npm install monaco-textmate
monaco-textmate relies on onigasm package to provide oniguruma regex engine in browsers. onigasm itself relies on WebAssembly. Therefore tomonaco-textmate
get working in your browser, it must have WebAssembly support and onigasm loaded and ready-to-go.
Make sure the example code below runs after onigasm bootstraping sequence described here has finished.
> Example below is just a demostration of available API. To wire it up with monaco-editor use monaco-editor-textmate.
`javascript
import { Registry } from 'monaco-textmate'
(async function test() {
const registry = new Registry({
// Since we're in browser, getFilePath has been removed, therefore you must provide getGrammarDefinition hook for things to workRegistry.loadGrammar
getGrammarDefinition: async (scopeName) => {
// Whenever is called first time per scope name, this function will be called asking you to provideplist
// raw grammar definition. Both JSON and plist formats are accepted.
if (scopeName === 'source.css') {
return {
format: 'json', // can also be static/grammars/css.tmGrammar.json
content: await (await fetch()).text() // when format is 'json', parsed JSON also works
}
}
}
})
const grammar = await registry.loadGrammar('source.css')
console.log(grammar.tokenizeLine('html, body { height: 100%; margin: 0 }'))
// > {tokens: Array(19), ruleStack: StackElement}
})()
`
> onigasm is peer dependency that you must install yourself
To tokenize multiple lines, you must pass in the previous returned ruleStack.
`javascript`
var ruleStack = null;
for (var i = 0; i < lines.length; i++) {
var r = grammar.tokenizeLine(lines[i], ruleStack);
console.log('Line: #' + i + ', tokens: ' + r.tokens);
ruleStack = r.ruleStack;
}
See the main.ts file
* Clone the repository
* Run npm installnpm run watch
* Compile in the background with
, which is MIT licensed.