mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Merge pull request #18499 from Aerijo/content-regex
Support contentRegex for TextMate grammar
This commit is contained in:
@@ -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)', () => {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user