From 31f511aae738449cc1a2f0f71d98230f05204a21 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 5 Jun 2018 14:27:29 -0700 Subject: [PATCH] Don't boost tree-sitter grammars' score unless they match in some way --- spec/grammar-registry-spec.js | 8 ++++++++ src/grammar-registry.js | 8 +++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/spec/grammar-registry-spec.js b/spec/grammar-registry-spec.js index dbcd03e93..85b5ba8ee 100644 --- a/spec/grammar-registry-spec.js +++ b/spec/grammar-registry-spec.js @@ -415,6 +415,14 @@ describe('GrammarRegistry', () => { expect(grammar.id).toBe('javascript') expect(grammar instanceof TreeSitterGrammar).toBe(true) }) + + it('only favors a tree-sitter grammar if it actually matches in some way (regression)', () => { + atom.config.set('core.useTreeSitterParsers', true) + grammarRegistry.loadGrammarSync(require.resolve('language-javascript/grammars/tree-sitter-javascript.cson')) + + const grammar = grammarRegistry.selectGrammar('test', '') + expect(grammar.name).toBe('Null Grammar') + }) }) describe('tree-sitter grammars with content regexes', () => { diff --git a/src/grammar-registry.js b/src/grammar-registry.js index d494d4f55..e92091286 100644 --- a/src/grammar-registry.js +++ b/src/grammar-registry.js @@ -214,11 +214,7 @@ class GrammarRegistry { } if (grammar instanceof TreeSitterGrammar) { - if (this.config.get('core.useTreeSitterParsers')) { - score += 0.05 - } else { - score = -Infinity - } + if (!this.config.get('core.useTreeSitterParsers')) return -Infinity if (grammar.contentRegExp) { if (grammar.contentRegExp.test(contents)) { @@ -227,6 +223,8 @@ class GrammarRegistry { score -= 0.25 } } + + if (score > 0) score += 0.05 } else if (this.grammarMatchesPrefix(grammar, contents)) { score += 0.25 }