Skip to content

Commit 3264f8c

Browse files
FileEXNico8340
andauthored
Custom syntax highlighting & clickable keywords (#14)
Co-authored-by: Nico <[email protected]>
1 parent 81a607b commit 3264f8c

13 files changed

+2286
-7
lines changed

web/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ pnpm-debug.log*
1919

2020
# macOS-specific files
2121
.DS_Store
22+
23+
# tmLanguage file
24+
src/grammars/

web/ec.config.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import fs from 'fs';
2+
3+
export default {
4+
themes: [
5+
JSON.parse(fs.readFileSync('./src/themes/mtasa_lua-theme_dark.json', 'utf-8')),
6+
JSON.parse(fs.readFileSync('./src/themes/mtasa_lua-theme_light.json', 'utf-8')),
7+
],
8+
shiki: {
9+
langs: [
10+
{
11+
id: 'lua',
12+
scopeName: 'source.lua.mta',
13+
...JSON.parse(fs.readFileSync('./src/grammars/lua-mta.tmLanguage.json', 'utf-8')),
14+
},
15+
],
16+
},
17+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
node generate-lua-tmlanguage.js
3+
pause

0 commit comments

Comments
 (0)