vscode-textmate
Advanced tools
Comparing version 4.2.0 to 4.2.1
{ | ||
"name": "vscode-textmate", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"description": "VSCode TextMate grammar helpers", | ||
@@ -5,0 +5,0 @@ "author": { |
112
README.md
# VSCode TextMate [![Build Status](https://dev.azure.com/ms/vscode-textmate/_apis/build/status/microsoft.vscode-textmate?branchName=master)](https://dev.azure.com/ms/vscode-textmate/_build/latest?definitionId=172&branchName=master) | ||
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. | ||
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. | ||
@@ -14,24 +14,24 @@ ## Installing | ||
```javascript | ||
var vsctm = require('vscode-textmate'); | ||
var grammarPaths = { | ||
'source.js': './javascript.tmbundle/Syntaxes/JavaScript.plist' | ||
}; | ||
const fs = require('fs'); | ||
const vsctm = require('vscode-textmate'); | ||
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; | ||
} | ||
/** | ||
* 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)); | ||
}) | ||
} | ||
// Create a registry that can create a grammar from a scope name. | ||
const registry = new vsctm.Registry({ | ||
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; | ||
} | ||
}); | ||
@@ -41,25 +41,61 @@ | ||
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); | ||
} | ||
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: | ||
## Tokenizing multiple lines | ||
Unknown scope name: source.js.regexp | ||
To tokenize multiple lines, you must pass in the previous returned `ruleStack`. | ||
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 | ||
```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; | ||
} | ||
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](https://github.com/PanAeon/vscode-tmgrammar-test) that can help you write unit tests against your grammar. | ||
## API doc | ||
@@ -66,0 +102,0 @@ |
@@ -181,2 +181,2 @@ import { IRawGrammar, IOnigLib } from './types'; | ||
export declare const INITIAL: StackElement; | ||
export declare const parseRawGrammar: (content: string, filePath: string) => IRawGrammar; | ||
export declare const parseRawGrammar: (content: string, filePath?: string) => IRawGrammar; |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1496342
12
3947
120