Skip to content

Commit 71d2fef

Browse files
authored
feat: add support for compound file paths like .blade.php (#1308)
1 parent 640d423 commit 71d2fef

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/ace/modelist.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,44 @@ export function initModes() {
66
"ace/ext/modelist",
77
["require", "exports", "module"],
88
function (require, exports, module) {
9+
/**
10+
* Calculates a specificity score for a mode.
11+
* Higher score means more specific.
12+
* - Anchored patterns (e.g., "^Dockerfile") get a base score of 1000.
13+
* - Non-anchored patterns (extensions) are scored by length.
14+
*/
15+
function getModeSpecificityScore(modeInstance) {
16+
const extensionsStr = modeInstance.extensions;
17+
if (!extensionsStr) return 0;
18+
19+
const patterns = extensionsStr.split("|");
20+
let maxScore = 0;
21+
22+
for (const pattern of patterns) {
23+
let currentScore = 0;
24+
if (pattern.startsWith("^")) {
25+
// Exact filename match or anchored pattern
26+
currentScore = 1000 + (pattern.length - 1); // Subtract 1 for '^'
27+
} else {
28+
// Extension match
29+
currentScore = pattern.length;
30+
}
31+
if (currentScore > maxScore) {
32+
maxScore = currentScore;
33+
}
34+
}
35+
return maxScore;
36+
}
937
module.exports = {
1038
getModeForPath(path) {
1139
let mode = modesByName.text;
1240
let fileName = path.split(/[\/\\]/).pop();
13-
for (const iMode of modes) {
41+
// Sort modes by specificity (descending) to check most specific first
42+
const sortedModes = [...modes].sort((a, b) => {
43+
return getModeSpecificityScore(b) - getModeSpecificityScore(a);
44+
});
45+
46+
for (const iMode of sortedModes) {
1447
if (iMode.supportsFile?.(fileName)) {
1548
mode = iMode;
1649
break;

0 commit comments

Comments
 (0)