A library that helps tokenize text using Text Mate grammars.
Перейти к файлу
Matt Bierner 67649c2826 Add test for captures in whileCondition 2016-09-26 14:41:18 -07:00
.vscode Fix issue with path; fix release script to include new files 2016-09-12 10:13:48 +02:00
benchmark Adopt oniguruma with OnigScanner 2015-12-06 22:06:48 +01:00
release Prepare for new release 2016-09-21 10:27:30 +03:00
scripts Fix issue with path; fix release script to include new files 2016-09-12 10:13:48 +02:00
src Use whileCaptures group if one exists 2016-09-26 14:39:09 -07:00
test-cases Add test for captures in whileCondition 2016-09-26 14:41:18 -07:00
typings Clean up /src/ structure 2016-08-18 11:48:31 +02:00
.gitignore Fixes #19: Bring back the /release/ folder to git 2016-08-22 10:24:33 +02:00
.npmignore Add scripts/release 2016-08-18 12:03:41 +02:00
.travis.yml Travis compile with node 6.3 2016-09-21 10:28:28 +03:00
LICENSE.md Update LICENSE.md 2015-11-06 14:32:12 +01:00
README.md Update README.md 2016-08-18 14:40:35 +02:00
ThirdPartyNotices.txt Add ThirdPartyNotices.txt 2015-11-18 14:38:28 +01:00
package.json 2.2.0 2016-09-21 10:31:17 +03:00
tsconfig.json Clean up /src/ structure 2016-08-18 11:48:31 +02:00
typings.json Adopt typings 2016-08-18 11:34:18 +02:00

README.md

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

npm install vscode-textmate

Using

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

var lineTokens = grammar.tokenizeLine('function add(a,b) { return a+b; }');
for (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:

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

var 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;
	}
});

// Load the JavaScript grammar and any other grammars included by it async.
registry.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.

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;
}

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