sourcegraph/vscode-textmate

Name: vscode-textmate

Owner: Sourcegraph

Description: A library that helps tokenize text using Text Mate grammars.

Forked from: Microsoft/vscode-textmate

Created: 2017-05-08 07:19:24.0

Updated: 2017-05-08 07:19:27.0

Pushed: 2017-07-04 03:38:00.0

Homepage: null

Size: 2176

Language: TypeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

VSCode TextMate Build Status Coverage Status

An interpreter for grammar files as defined by TextMate. Supports loading grammar files from JSON or PLIST format. Cross - grammar injections are currently not supported.

Installing
install vscode-textmate
Using
Registry = require('vscode-textmate').Registry;
registry = new Registry();
grammar = registry.loadGrammarFromPathSync('./javascript.tmbundle/Syntaxes/JavaScript.plist');

lineTokens = grammar.tokenizeLine('function add(a,b) { return a+b; }');
(var i = 0; i < lineTokens.tokens.length; i++) {
var token = lineTokens.tokens[i];
console.log('Token from ' + token.startIndex + ' to ' + token.endIndex + ' with scopes ' + token.scopes);

Using asynchronously

Sometimes, it is necessary to manage the list of known grammars outside of vscode-textmate. The sample below shows how this works:

Registry = require('vscode-textmate').Registry;

registry = new Registry({
getFilePath: function (scopeName) {
    // Return here the path to the grammar file for `scopeName`
    if (scopeName === 'source.js') {
        return './javascript.tmbundle/Syntaxes/JavaScript.plist';
    }
    return null;
}


oad the JavaScript grammar and any other grammars included by it async.
stry.loadGrammar('source.js', function(err, grammar) {
if (err) {
    console.error(err);
    return;
}

// at this point `grammar` is available...

Tokenizing multiple lines

To tokenize multiple lines, you must pass in the previous returned ruleStack.

ruleStack = null;
(var i = 0; i < lines.length; i++) {
var r = grammar.tokenizeLine(lines[i], ruleStack);
console.log('Line: #' + i + ', tokens: ' + r.tokens);
ruleStack = r.ruleStack;

API doc

See the main.ts file

Developing
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


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.