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 () => { beforeEach(async () => {
editor = await atom.workspace.open('') editor = await atom.workspace.open('')
buffer = editor.getBuffer() buffer = editor.getBuffer()
atom.config.set('core.useTreeSitterParsers', true)
}) })
describe('highlighting', () => { describe('highlighting', () => {
it('applies the most specific scope mapping to each token in the syntax tree', () => { it('applies the most specific scope mapping to each node in the syntax tree', () => {
grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, { const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
parser: 'tree-sitter-javascript', parser: 'tree-sitter-javascript',
scopes: { scopes: {
'program': 'source', 'program': 'source',
@ -31,7 +30,7 @@ describe('TreeSitterLanguageMode', () => {
buffer.setLanguageMode(new TreeSitterLanguageMode({buffer, grammar})) buffer.setLanguageMode(new TreeSitterLanguageMode({buffer, grammar}))
buffer.setText('aa.bbb = cc(d.eee());') buffer.setText('aa.bbb = cc(d.eee());')
expect(getTokens(editor).slice(0, 1)).toEqual([[ expectTokensToEqual(editor, [
{text: 'aa.', scopes: ['source']}, {text: 'aa.', scopes: ['source']},
{text: 'bbb', scopes: ['source', 'property']}, {text: 'bbb', scopes: ['source', 'property']},
{text: ' = ', scopes: ['source']}, {text: ' = ', scopes: ['source']},
@ -39,16 +38,42 @@ describe('TreeSitterLanguageMode', () => {
{text: '(d.', scopes: ['source']}, {text: '(d.', scopes: ['source']},
{text: 'eee', scopes: ['source', 'method']}, {text: 'eee', scopes: ['source', 'method']},
{text: '());', scopes: ['source']} {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) { function expectTokensToEqual (editor, expectedTokens) {
const result = [] const tokens = []
for (let row = 0, lastRow = editor.getLastScreenRow(); row <= lastRow; row++) { for (let row = 0, lastRow = editor.getLastScreenRow(); row <= lastRow; row++) {
result.push( tokens.push(
editor.tokensForScreenRow(row).map(({text, scopes}) => ({ ...editor.tokensForScreenRow(row).map(({text, scopes}) => ({
text, text,
scopes: scopes.map(scope => scope scopes: scopes.map(scope => scope
.split(' ') .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}`)
}
} }