This commit is contained in:
Martin Aeschlimann 2018-06-16 00:45:19 +02:00
Родитель 4044ff682e
Коммит 2733385ce9
2 изменённых файлов: 37 добавлений и 31 удалений

Просмотреть файл

@ -11,42 +11,38 @@ npm install vscode-textmate
## Using
```javascript
var Registry = require('vscode-textmate').Registry;
var registry = new Registry();
var grammar = registry.loadGrammarFromPathSync('./javascript.tmbundle/Syntaxes/JavaScript.plist');
var vsctm = require('vscode-textmate');
var grammarPaths = {
'source.js': './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:
```javascript
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';
var registry = new vsctm.Registry({
loadGrammar: function (scopeName) {
var path = grammarPaths[scopeName];
if (path) {
return new Promise((c,e) => {
fs.readFile(path, (error, content) => {
if (error) {
e(error);
} else {
var rawGrammar = vsctm.parseRawGrammar(content.toString(), path);
c(rawGrammar);
}
});
});
}
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;
}
registry.loadGrammar('source.js').then(grammar => {
// at this point `grammar` is available...
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);
}
});
```

Просмотреть файл

@ -126,14 +126,24 @@ export class Resolver implements RegistryOptions {
let grammar = this._grammars[i];
if (grammar.scopeName === scopeName) {
if (!grammar.grammar) {
let content = fs.readFileSync(grammar.path).toString();
grammar.grammar = Promise.resolve(parseRawGrammar(content, grammar.path));
grammar.grammar = readGrammarFromPath(grammar.path);
}
return grammar.grammar;
}
}
//console.warn('test resolver: missing grammar for ' + scopeName);
return null;
}
}
function readGrammarFromPath(path: string) : Thenable<IRawGrammar> {
return new Promise((c,e) => {
fs.readFile(path, (error, content) => {
if (error) {
e(error);
} else {
c(parseRawGrammar(content.toString(), path));
}
});
});
}