|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import yaml from 'js-yaml'; |
| 4 | +import { glob } from 'glob'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
| 10 | +const functionsDir = path.resolve(__dirname, '../../functions'); |
| 11 | +const basePath = path.resolve(__dirname, './lua-base.tmLanguage.json'); |
| 12 | +const outputPath = path.resolve(__dirname, '../src/grammars/lua-mta.tmLanguage.json'); |
| 13 | + |
| 14 | +const mtaKeywords = ['string','bool','boolean','number','int','float','element','player','vehicle','ped','object','building']; |
| 15 | + |
| 16 | +function extractFunctionsWithScope(yamlContent) { |
| 17 | + if (yamlContent.shared?.name) { |
| 18 | + return [{ name: yamlContent.shared.name, scope: 'support.function.mta-shared' }]; |
| 19 | + } else if (yamlContent.client?.name) { |
| 20 | + return [{ name: yamlContent.client.name, scope: 'support.function.mta-client' }]; |
| 21 | + } else if (yamlContent.server?.name) { |
| 22 | + return [{ name: yamlContent.server.name, scope: 'support.function.mta-server' }]; |
| 23 | + } |
| 24 | + return []; |
| 25 | +} |
| 26 | + |
| 27 | +async function generateTmLanguage() { |
| 28 | + const files = await glob('**/*.yaml', { cwd: functionsDir, absolute: true }); |
| 29 | + |
| 30 | + const functionsMap = Object.fromEntries( |
| 31 | + ['shared', 'server', 'client'].map(scope => [`support.function.mta-${scope}`, new Set()]) |
| 32 | + ); |
| 33 | + |
| 34 | + files.forEach(file => { |
| 35 | + const yamlContent = yaml.load(fs.readFileSync(file, 'utf-8')); |
| 36 | + console.log('Processing file:', file); |
| 37 | + |
| 38 | + const items = Array.isArray(yamlContent) ? yamlContent : [yamlContent]; |
| 39 | + items.flatMap(extractFunctionsWithScope).forEach(({ name, scope }) => functionsMap[scope].add(name)); |
| 40 | + }); |
| 41 | + |
| 42 | + const patterns = Object.entries(functionsMap) |
| 43 | + .filter(([, namesSet]) => namesSet.size > 0) |
| 44 | + .map(([scope, namesSet]) => ({ |
| 45 | + match: `\\b(${Array.from(namesSet).join('|')})\\b`, |
| 46 | + name: scope, |
| 47 | + })); |
| 48 | + |
| 49 | + if (mtaKeywords.length > 0) { |
| 50 | + patterns.push({ |
| 51 | + match: `\\b(${mtaKeywords.join('|')})\\b`, |
| 52 | + name: 'keyword.mta', |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + const baseGrammar = JSON.parse(fs.readFileSync(basePath, 'utf-8')); |
| 57 | + baseGrammar.patterns = [...patterns, ...(baseGrammar.patterns || [])]; |
| 58 | + |
| 59 | + fs.writeFileSync(outputPath, JSON.stringify(baseGrammar, null, 2)); |
| 60 | + console.log(`Done!`); |
| 61 | +} |
| 62 | + |
| 63 | +generateTmLanguage(); |
0 commit comments