From 49f391ff81ec2407738a018a0f3e3f86a9ffba50 Mon Sep 17 00:00:00 2001 From: Indrek Ardel Date: Tue, 10 Nov 2015 00:27:23 +0200 Subject: [PATCH 1/5] Prioritize file type matching over first line matching --- src/grammar-registry.coffee | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/grammar-registry.coffee b/src/grammar-registry.coffee index 4a5352275..9c01b48e9 100644 --- a/src/grammar-registry.coffee +++ b/src/grammar-registry.coffee @@ -36,21 +36,21 @@ class GrammarRegistry extends FirstMate.GrammarRegistry if score > highestScore or not bestMatch? bestMatch = grammar highestScore = score - else if score is highestScore and bestMatch?.bundledPackage - bestMatch = grammar unless grammar.bundledPackage bestMatch # Extended: Returns a {Number} representing how well the grammar matches the # `filePath` and `contents`. getGrammarScore: (grammar, filePath, contents) -> + return Infinity if @grammarOverrideForPath(filePath) is grammar.scopeName + contents = fs.readFileSync(filePath, 'utf8') if not contents? and fs.isFileSync(filePath) - if @grammarOverrideForPath(filePath) is grammar.scopeName - 2 + (filePath?.length ? 0) - else if @grammarMatchesContents(grammar, contents) - 1 + (filePath?.length ? 0) - else - @getGrammarPathScore(grammar, filePath) + score = @getGrammarPathScore(grammar, filePath) + if not grammar.bundledPackage + score += 0.25 + if @grammarMatchesContents(grammar, contents) + score += 0.125 + score getGrammarPathScore: (grammar, filePath) -> return -1 unless filePath From 66255baa7166b965e66e63b39a8c5164e2d5c6d8 Mon Sep 17 00:00:00 2001 From: Indrek Ardel Date: Tue, 10 Nov 2015 21:02:41 +0200 Subject: [PATCH 2/5] Only allow bundle bias if a file type was matched --- src/grammar-registry.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar-registry.coffee b/src/grammar-registry.coffee index 9c01b48e9..383d4d776 100644 --- a/src/grammar-registry.coffee +++ b/src/grammar-registry.coffee @@ -46,7 +46,7 @@ class GrammarRegistry extends FirstMate.GrammarRegistry contents = fs.readFileSync(filePath, 'utf8') if not contents? and fs.isFileSync(filePath) score = @getGrammarPathScore(grammar, filePath) - if not grammar.bundledPackage + if score > 0 and not grammar.bundledPackage score += 0.25 if @grammarMatchesContents(grammar, contents) score += 0.125 From 1239ba5f7b0a9e213f5706e37b5c39ccd75a60a5 Mon Sep 17 00:00:00 2001 From: Indrek Ardel Date: Tue, 10 Nov 2015 00:27:23 +0200 Subject: [PATCH 3/5] Prioritize file type matching over first line matching --- src/grammar-registry.coffee | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/grammar-registry.coffee b/src/grammar-registry.coffee index 4a5352275..9c01b48e9 100644 --- a/src/grammar-registry.coffee +++ b/src/grammar-registry.coffee @@ -36,21 +36,21 @@ class GrammarRegistry extends FirstMate.GrammarRegistry if score > highestScore or not bestMatch? bestMatch = grammar highestScore = score - else if score is highestScore and bestMatch?.bundledPackage - bestMatch = grammar unless grammar.bundledPackage bestMatch # Extended: Returns a {Number} representing how well the grammar matches the # `filePath` and `contents`. getGrammarScore: (grammar, filePath, contents) -> + return Infinity if @grammarOverrideForPath(filePath) is grammar.scopeName + contents = fs.readFileSync(filePath, 'utf8') if not contents? and fs.isFileSync(filePath) - if @grammarOverrideForPath(filePath) is grammar.scopeName - 2 + (filePath?.length ? 0) - else if @grammarMatchesContents(grammar, contents) - 1 + (filePath?.length ? 0) - else - @getGrammarPathScore(grammar, filePath) + score = @getGrammarPathScore(grammar, filePath) + if not grammar.bundledPackage + score += 0.25 + if @grammarMatchesContents(grammar, contents) + score += 0.125 + score getGrammarPathScore: (grammar, filePath) -> return -1 unless filePath From dc4d6ad82ac0aeeeac23a02ab8e12c18714f1066 Mon Sep 17 00:00:00 2001 From: Indrek Ardel Date: Tue, 10 Nov 2015 21:02:41 +0200 Subject: [PATCH 4/5] Only allow bundle bias if a file type was matched --- src/grammar-registry.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar-registry.coffee b/src/grammar-registry.coffee index 9c01b48e9..383d4d776 100644 --- a/src/grammar-registry.coffee +++ b/src/grammar-registry.coffee @@ -46,7 +46,7 @@ class GrammarRegistry extends FirstMate.GrammarRegistry contents = fs.readFileSync(filePath, 'utf8') if not contents? and fs.isFileSync(filePath) score = @getGrammarPathScore(grammar, filePath) - if not grammar.bundledPackage + if score > 0 and not grammar.bundledPackage score += 0.25 if @grammarMatchesContents(grammar, contents) score += 0.125 From 6520240652d2a0e55a822d8a002ac661833c4877 Mon Sep 17 00:00:00 2001 From: Indrek Ardel Date: Wed, 11 Nov 2015 01:23:07 +0200 Subject: [PATCH 5/5] Update spec --- spec/grammars-spec.coffee | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/grammars-spec.coffee b/spec/grammars-spec.coffee index 82a29892a..a36a10170 100644 --- a/spec/grammars-spec.coffee +++ b/spec/grammars-spec.coffee @@ -142,9 +142,13 @@ describe "the `grammars` global", -> expect(atom.grammars.selectGrammar('Rakefile', '').scopeName).toBe 'source.coffee' expect(atom.grammars.selectGrammar('Cakefile', '').scopeName).toBe 'source.ruby' - it "favors grammars with matching first-line-regexps even if custom file types match the file", -> + it "favors user-defined file types over grammars with matching first-line-regexps", -> atom.config.set('core.customFileTypes', 'source.ruby': ['bootstrap']) - expect(atom.grammars.selectGrammar('bootstrap', '#!/usr/bin/env node').scopeName).toBe 'source.js' + expect(atom.grammars.selectGrammar('bootstrap', '#!/usr/bin/env node').scopeName).toBe 'source.ruby' + + describe "when there is a grammar with a first line pattern, the file type of the file is known, but from a different grammar", -> + it "favors file type over the matching pattern", -> + expect(atom.grammars.selectGrammar('foo.rb', '#!/usr/bin/env node').scopeName).toBe 'source.ruby' describe ".removeGrammar(grammar)", -> it "removes the grammar, so it won't be returned by selectGrammar", ->