vscode-textmate

VSCode TextMate grammar helpers

MIT 77 个版本
安装
npm install vscode-textmate
yarn add vscode-textmate
pnpm add vscode-textmate
bun add vscode-textmate
README

VSCode TextMate Build Status

An interpreter for grammar files as defined by TextMate. TextMate grammars use the oniguruma dialect (https://github.com/kkos/oniguruma). Supports loading grammar files from JSON or PLIST format. This library is used in VS Code. Cross - grammar injections are currently not supported.

Installing

npm install vscode-textmate

Using

const fs = require('fs');
const path = require('path');
const vsctm = require('vscode-textmate');
const oniguruma = require('vscode-oniguruma');

/**
 * Utility to read a file as a promise
 */
function readFile(path) {
    return new Promise((resolve, reject) => {
        fs.readFile(path, (error, data) => error ? reject(error) : resolve(data));
    })
}

const wasmBin = fs.readFileSync(path.join(__dirname, './node_modules/vscode-oniguruma/release/onig.wasm')).buffer;
const vscodeOnigurumaLib = oniguruma.loadWASM(wasmBin).then(() => {
    return {
        createOnigScanner(patterns) { return new oniguruma.OnigScanner(patterns); },
        createOnigString(s) { return new oniguruma.OnigString(s); }
    };
});

// Create a registry that can create a grammar from a scope name.
const registry = new vsctm.Registry({
    onigLib: vscodeOnigurumaLib,
    loadGrammar: (scopeName) => {
        if (scopeName === 'source.js') {
            // https://github.com/textmate/javascript.tmbundle/blob/master/Syntaxes/JavaScript.plist
            return readFile('./JavaScript.plist').then(data => vsctm.parseRawGrammar(data.toString()))
        }
        console.log(`Unknown scope name: ${scopeName}`);
        return null;
    }
});

// Load the JavaScript grammar and any other grammars included by it async.
registry.loadGrammar('source.js').then(grammar => {
    const text = [
        `function sayHello(name) {`,
        `\treturn "Hello, " + name;`,
        `}`
    ];
    let ruleStack = vsctm.INITIAL;
    for (let i = 0; i < text.length; i++) {
        const line = text[i];
        const lineTokens = grammar.tokenizeLine(line, ruleStack);
        console.log(`\nTokenizing line: ${line}`);
        for (let j = 0; j < lineTokens.tokens.length; j++) {
            const token = lineTokens.tokens[j];
            console.log(` - token from ${token.startIndex} to ${token.endIndex} ` +
              `(${line.substring(token.startIndex, token.endIndex)}) ` +
              `with scopes ${token.scopes.join(', ')}`
            );
        }
        ruleStack = lineTokens.ruleStack;
    }
});

/* OUTPUT:

Unknown scope name: source.js.regexp

Tokenizing line: function sayHello(name) {
 - token from 0 to 8 (function) with scopes source.js, meta.function.js, storage.type.function.js
 - token from 8 to 9 ( ) with scopes source.js, meta.function.js
 - token from 9 to 17 (sayHello) with scopes source.js, meta.function.js, entity.name.function.js
 - token from 17 to 18 (() with scopes source.js, meta.function.js, punctuation.definition.parameters.begin.js
 - token from 18 to 22 (name) with scopes source.js, meta.function.js, variable.parameter.function.js
 - token from 22 to 23 ()) with scopes source.js, meta.function.js, punctuation.definition.parameters.end.js
 - token from 23 to 24 ( ) with scopes source.js
 - token from 24 to 25 ({) with scopes source.js, punctuation.section.scope.begin.js

Tokenizing line:        return "Hello, " + name;
 - token from 0 to 1 (  ) with scopes source.js
 - token from 1 to 7 (return) with scopes source.js, keyword.control.js
 - token from 7 to 8 ( ) with scopes source.js
 - token from 8 to 9 (") with scopes source.js, string.quoted.double.js, punctuation.definition.string.begin.js
 - token from 9 to 16 (Hello, ) with scopes source.js, string.quoted.double.js
 - token from 16 to 17 (") with scopes source.js, string.quoted.double.js, punctuation.definition.string.end.js
 - token from 17 to 18 ( ) with scopes source.js
 - token from 18 to 19 (+) with scopes source.js, keyword.operator.arithmetic.js
 - token from 19 to 20 ( ) with scopes source.js
 - token from 20 to 24 (name) with scopes source.js, support.constant.dom.js
 - token from 24 to 25 (;) with scopes source.js, punctuation.terminator.statement.js

Tokenizing line: }
 - token from 0 to 1 (}) with scopes source.js, punctuation.section.scope.end.js

*/

For grammar authors

See vscode-tmgrammar-test that can help you write unit tests against your grammar.

API doc

See the main.ts file

Developing

  • Clone the repository
  • Run npm install
  • Compile in the background with npm run watch
  • Run tests with npm test
  • Run benchmark with npm run benchmark
  • Troubleshoot a grammar with npm run inspect -- PATH_TO_GRAMMAR PATH_TO_FILE

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

License

MIT

版本列表
9.3.2 2026-01-22
9.3.1 2026-01-06
9.3.0 2025-12-02
9.2.1 2025-10-04
9.2.0 2024-12-18
9.1.0 2024-07-12
9.0.0 2023-02-17
8.0.0 2022-12-05
7.0.4 2022-11-25
7.0.3 2022-10-13
7.0.2 2022-10-13
7.0.1 2022-04-07
7.0.0 2022-04-07
6.0.0 2021-12-17
5.5.0 2021-11-19
5.4.1 2021-10-28
5.4.0 2021-03-26
5.3.1 2021-03-23
5.3.0 2021-03-23
5.2.0 2020-06-29
5.1.2 2020-06-24
5.1.1 2020-04-27
5.1.0 2020-04-27
5.0.2 2020-04-23
5.0.1 2020-04-23
5.0.0 2020-04-22
4.4.0 2019-11-20
4.3.0 2019-10-28
4.2.2 2019-07-12
4.2.1 2019-07-12
4.2.0 2019-07-12
4.1.1 2019-05-02
4.1.0 2019-03-21
4.0.1 2018-06-22
4.0.0 2018-06-21
4.0.0-next.3 2018-06-19
4.0.0-next.2 2018-06-18
4.0.0-next.1 2018-06-18
3.3.3 2018-04-03
3.3.2 2018-03-19
3.3.1 2018-03-16
3.3.0 2018-03-16
3.2.0 2017-10-30
3.1.5 2017-05-29
3.1.4 2017-04-13
3.1.3 2017-03-29
3.1.2 2017-03-29
3.1.1 2017-02-28
3.1.0 2017-01-05
3.0.1 2017-01-04
3.0.0 2017-01-03
2.3.2 2016-12-08
2.3.1 2016-11-08
2.3.0 2016-10-22
2.2.0 2016-09-21
2.1.1 2016-08-17
2.1.0 2016-08-17
2.0.1 2016-06-30
2.0.0 2016-06-30
1.2.0 2016-06-30
1.1.0 2016-05-23
1.0.11 2016-01-29
1.0.10 2016-01-29
1.0.9 2015-12-06
1.0.8 2015-11-30
1.0.7 2015-11-25
1.0.6 2015-11-13
1.0.5 2015-11-12
1.0.4 2015-11-04
1.0.3 2015-11-02
1.0.2 2015-10-31
1.0.1 2015-10-30
1.0.0 2015-10-21
0.0.4 2015-09-21
0.0.3 2015-09-17
0.0.2 2015-09-16
0.0.1 2015-09-16