| 1 | // Lenguajes de VS Code (contributes.languages + language-configuration.json) |
| 2 | // → registro de Monaco y su configuración (comentarios, corchetes, sangría…). |
| 3 | |
| 4 | // Expresión de VS Code (cadena o { pattern, flags }) → RegExp. Las que no |
| 5 | // compilan en JavaScript se descartan, como hace VS Code. |
| 6 | function re(v) { |
| 7 | try { |
| 8 | if (typeof v === 'string') return new RegExp(v); |
| 9 | if (v && typeof v.pattern === 'string') return new RegExp(v.pattern, String(v.flags || '').replace(/[^dgimsuy]/g, '')); |
| 10 | } catch { /* inválida */ } |
| 11 | return undefined; |
| 12 | } |
| 13 | const par = p => (Array.isArray(p) && p.length === 2 && p.every(x => typeof x === 'string') ? { open: p[0], close: p[1] } |
| 14 | : p && typeof p.open === 'string' && typeof p.close === 'string' ? { open: p.open, close: p.close, ...(Array.isArray(p.notIn) && { notIn: p.notIn.filter(x => typeof x === 'string') }) } |
| 15 | : null); |
| 16 | const corchetes = l => (Array.isArray(l) ? l.filter(p => Array.isArray(p) && p.length === 2 && p.every(x => typeof x === 'string')) : undefined); |
| 17 | |
| 18 | // language-configuration.json → LanguageConfiguration de Monaco. |
| 19 | export function toLanguageConfiguration(j, monaco) { |
| 20 | if (!j || typeof j !== 'object') return {}; |
| 21 | const out = {}; |
| 22 | if (j.comments && typeof j.comments === 'object') { |
| 23 | const c = {}; |
| 24 | if (typeof j.comments.lineComment === 'string') c.lineComment = j.comments.lineComment; |
| 25 | if (corchetes([j.comments.blockComment])?.length) c.blockComment = j.comments.blockComment; |
| 26 | out.comments = c; |
| 27 | } |
| 28 | if (corchetes(j.brackets)) out.brackets = corchetes(j.brackets); |
| 29 | if (corchetes(j.colorizedBracketPairs)) out.colorizedBracketPairs = corchetes(j.colorizedBracketPairs); |
| 30 | if (Array.isArray(j.autoClosingPairs)) out.autoClosingPairs = j.autoClosingPairs.map(par).filter(Boolean); |
| 31 | if (Array.isArray(j.surroundingPairs)) out.surroundingPairs = j.surroundingPairs.map(par).filter(Boolean).map(({ open, close }) => ({ open, close })); |
| 32 | if (typeof j.autoCloseBefore === 'string') out.autoCloseBefore = j.autoCloseBefore; |
| 33 | const wp = re(j.wordPattern); |
| 34 | if (wp) out.wordPattern = wp; |
| 35 | if (j.indentationRules && typeof j.indentationRules === 'object') { |
| 36 | const ir = j.indentationRules; |
| 37 | const inc = re(ir.increaseIndentPattern), dec = re(ir.decreaseIndentPattern); |
| 38 | if (inc && dec) { |
| 39 | out.indentationRules = { increaseIndentPattern: inc, decreaseIndentPattern: dec }; |
| 40 | const nl = re(ir.indentNextLinePattern), un = re(ir.unIndentedLinePattern); |
| 41 | if (nl) out.indentationRules.indentNextLinePattern = nl; |
| 42 | if (un) out.indentationRules.unIndentedLinePattern = un; |
| 43 | } |
| 44 | } |
| 45 | if (j.folding && typeof j.folding === 'object') { |
| 46 | const f = {}; |
| 47 | if (typeof j.folding.offSide === 'boolean') f.offSide = j.folding.offSide; |
| 48 | const s = re(j.folding.markers?.start), e = re(j.folding.markers?.end); |
| 49 | if (s && e) f.markers = { start: s, end: e }; |
| 50 | out.folding = f; |
| 51 | } |
| 52 | if (Array.isArray(j.onEnterRules) && monaco) { |
| 53 | const IA = monaco.languages.IndentAction; |
| 54 | const acciones = { none: IA.None, indent: IA.Indent, indentOutdent: IA.IndentOutdent, outdent: IA.Outdent }; |
| 55 | out.onEnterRules = j.onEnterRules.map(r => { |
| 56 | const beforeText = re(r?.beforeText); |
| 57 | const indentAction = acciones[r?.action?.indent]; |
| 58 | if (!beforeText || indentAction === undefined) return null; |
| 59 | const regla = { beforeText, action: { indentAction } }; |
| 60 | const at = re(r.afterText), pl = re(r.previousLineText); |
| 61 | if (at) regla.afterText = at; |
| 62 | if (pl) regla.previousLineText = pl; |
| 63 | if (typeof r.action.appendText === 'string') regla.action.appendText = r.action.appendText; |
| 64 | if (Number.isInteger(r.action.removeText)) regla.action.removeText = r.action.removeText; |
| 65 | return regla; |
| 66 | }).filter(Boolean); |
| 67 | } |
| 68 | return out; |
| 69 | } |
| 70 | |
| 71 | // Glob de VS Code (filenamePatterns) → RegExp: **, *, ?, {a,b}. |
| 72 | export function globToRegExp(glob) { |
| 73 | let out = ''; |
| 74 | for (let i = 0; i < glob.length; i++) { |
| 75 | const c = glob[i]; |
| 76 | if (c === '*' && glob[i + 1] === '*') { out += glob[i + 2] === '/' ? '(?:.*/)?' : '.*'; i += glob[i + 2] === '/' ? 2 : 1; } |
| 77 | else if (c === '*') out += '[^/]*'; |
| 78 | else if (c === '?') out += '[^/]'; |
| 79 | else if (c === '{') { const e = glob.indexOf('}', i); if (e < 0) { out += '\\{'; continue; } out += '(?:' + glob.slice(i + 1, e).split(',').map(x => x.replace(/[.+^$()|[\]\\]/g, '\\$&').replace(/\*/g, '[^/]*')).join('|') + ')'; i = e; } |
| 80 | else out += c.replace(/[.+^$()|[\]\\]/g, '\\$&'); |
| 81 | } |
| 82 | return new RegExp('^' + out + '$', 'i'); |
| 83 | } |
| 84 | |
| 85 | // Lenguaje para una ruta según las contribuciones activas (las últimas |
| 86 | // instaladas mandan). Como VS Code: nombre exacto, luego patrones, luego la |
| 87 | // extensión más larga y, si se da el texto, la primera línea. |
| 88 | export function matchLanguage(contribs, path, primeraLinea = null) { |
| 89 | const base = String(path).split('/').pop(); |
| 90 | const low = base.toLowerCase(); |
| 91 | for (let i = contribs.length - 1; i >= 0; i--) if ((contribs[i].filenames || []).some(f => f === base)) return contribs[i].id; |
| 92 | for (let i = contribs.length - 1; i >= 0; i--) { |
| 93 | for (const g of contribs[i].filenamePatterns || []) { |
| 94 | try { if (globToRegExp(g).test(g.includes('/') ? path : base)) return contribs[i].id; } catch { /* patrón roto */ } |
| 95 | } |
| 96 | } |
| 97 | let mejor = null, largo = 0; |
| 98 | for (let i = contribs.length - 1; i >= 0; i--) { |
| 99 | for (const e of contribs[i].extensions || []) { |
| 100 | const el = String(e).toLowerCase(); |
| 101 | if (el && el.length > largo && low.endsWith(el)) { mejor = contribs[i].id; largo = el.length; } |
| 102 | } |
| 103 | } |
| 104 | if (mejor) return mejor; |
| 105 | if (primeraLinea != null) { |
| 106 | for (let i = contribs.length - 1; i >= 0; i--) { |
| 107 | const fl = contribs[i].firstLine; |
| 108 | if (typeof fl !== 'string') continue; |
| 109 | try { if (new RegExp(fl).test(primeraLinea)) return contribs[i].id; } catch { /* inválida */ } |
| 110 | } |
| 111 | } |
| 112 | return null; |
| 113 | } |