Add tree-sitter highlighting test with nested scopes

This commit is contained in:
Max Brunsfeld 2017-12-04 10:18:38 -08:00
parent d893fb25a8
commit 6282cd639a

View File

@ -14,12 +14,11 @@ describe('TreeSitterLanguageMode', () => {
beforeEach(async () => {
editor = await atom.workspace.open('')
buffer = editor.getBuffer()
atom.config.set('core.useTreeSitterParsers', true)
})
describe('highlighting', () => {
it('applies the most specific scope mapping to each token in the syntax tree', () => {
grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
it('applies the most specific scope mapping to each node in the syntax tree', () => {
const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
parser: 'tree-sitter-javascript',
scopes: {
'program': 'source',
@ -31,7 +30,7 @@ describe('TreeSitterLanguageMode', () => {
buffer.setLanguageMode(new TreeSitterLanguageMode({buffer, grammar}))
buffer.setText('aa.bbb = cc(d.eee());')
expect(getTokens(editor).slice(0, 1)).toEqual([[
expectTokensToEqual(editor, [
{text: 'aa.', scopes: ['source']},
{text: 'bbb', scopes: ['source', 'property']},
{text: ' = ', scopes: ['source']},
@ -39,16 +38,42 @@ describe('TreeSitterLanguageMode', () => {
{text: '(d.', scopes: ['source']},
{text: 'eee', scopes: ['source', 'method']},
{text: '());', scopes: ['source']}
]])
])
})
it('can start or end multiple scopes at the same position', () => {
const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
parser: 'tree-sitter-javascript',
scopes: {
'program': 'source',
'call_expression': 'call',
'member_expression': 'member',
'identifier': 'variable',
'"("': 'open-paren',
'")"': 'close-paren',
}
})
buffer.setLanguageMode(new TreeSitterLanguageMode({buffer, grammar}))
buffer.setText('a = bb.ccc();')
expectTokensToEqual(editor, [
{text: 'a', scopes: ['source', 'variable']},
{text: ' = ', scopes: ['source']},
{text: 'bb', scopes: ['source', 'call', 'member', 'variable']},
{text: '.ccc', scopes: ['source', 'call', 'member']},
{text: '(', scopes: ['source', 'call', 'open-paren']},
{text: ')', scopes: ['source', 'call', 'close-paren']},
{text: ';', scopes: ['source']}
])
})
})
})
function getTokens (editor) {
const result = []
function expectTokensToEqual (editor, expectedTokens) {
const tokens = []
for (let row = 0, lastRow = editor.getLastScreenRow(); row <= lastRow; row++) {
result.push(
editor.tokensForScreenRow(row).map(({text, scopes}) => ({
tokens.push(
...editor.tokensForScreenRow(row).map(({text, scopes}) => ({
text,
scopes: scopes.map(scope => scope
.split(' ')
@ -57,5 +82,9 @@ function getTokens (editor) {
}))
)
}
return result
expect(tokens.length).toEqual(expectedTokens.length)
for (let i = 0; i < tokens.length; i++) {
expect(tokens[i]).toEqual(expectedTokens[i], `Token ${i}`)
}
}