Merge pull request #18499 from Aerijo/content-regex

Support contentRegex for TextMate grammar
This commit is contained in:
Nathan Sobo
2019-04-19 14:58:58 -06:00
committed by GitHub
2 changed files with 30 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ const TextBuffer = require('text-buffer')
const GrammarRegistry = require('../src/grammar-registry')
const TreeSitterGrammar = require('../src/tree-sitter-grammar')
const FirstMate = require('first-mate')
const { OnigRegExp } = require('oniguruma')
describe('GrammarRegistry', () => {
let grammarRegistry
@@ -730,6 +731,30 @@ describe('GrammarRegistry', () => {
expect(grammar.name).toBe('JavaScript')
})
})
describe('text-mate grammars with content regexes', () => {
it('favors grammars that match the content regex', () => {
const grammar1 = {
name: 'foo',
fileTypes: ['foo']
}
grammarRegistry.addGrammar(grammar1)
const grammar2 = {
name: 'foo++',
contentRegex: new OnigRegExp('.*bar'),
fileTypes: ['foo']
}
grammarRegistry.addGrammar(grammar2)
const grammar = grammarRegistry.selectGrammar(
'test.foo',
dedent`
${'\n'.repeat(50)}bar${'\n'.repeat(50)}
`)
expect(grammar).toBe(grammar2)
})
})
})
describe('.removeGrammar(grammar)', () => {

View File

@@ -215,8 +215,10 @@ class GrammarRegistry {
// If multiple grammars match by one of the above criteria, break ties.
if (score > 0) {
const isTreeSitter = grammar instanceof TreeSitterGrammar
// Prefer either TextMate or Tree-sitter grammars based on the user's settings.
if (grammar instanceof TreeSitterGrammar) {
if (isTreeSitter) {
if (this.shouldUseTreeSitterParser(grammar.scopeName)) {
score += 0.1
} else {
@@ -227,7 +229,8 @@ class GrammarRegistry {
// Prefer grammars with matching content regexes. Prefer a grammar with no content regex
// over one with a non-matching content regex.
if (grammar.contentRegex) {
if (grammar.contentRegex.test(contents)) {
const contentMatch = isTreeSitter ? grammar.contentRegex.test(contents) : grammar.contentRegex.testSync(contents)
if (contentMatch) {
score += 0.05
} else {
score -= 0.05