Skip to content

Commit a38b9f8

Browse files
committed
Custom syntax highlighting
1 parent 5715210 commit a38b9f8

File tree

9 files changed

+3158
-6
lines changed

9 files changed

+3158
-6
lines changed

web/astro.config.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ export default defineConfig({
1515
},
1616
integrations: [
1717
starlight({
18+
expressiveCode: {
19+
themes: [JSON.parse(fs.readFileSync('./src/themes/mtasa_lua-theme_dark.json', 'utf-8')), JSON.parse(fs.readFileSync('./src/themes/mtasa_lua-theme_light.json', 'utf-8'))],
20+
shiki: {
21+
langs: [
22+
{
23+
id: 'lua',
24+
scopeName: 'source.lua.mta',
25+
...JSON.parse(fs.readFileSync('./src/grammars/lua-mta.tmLanguage.json', 'utf-8')),
26+
},
27+
],
28+
},
29+
},
30+
1831
plugins: [
1932
mtasaStarlightThemePlugin(),
2033
],
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import yaml from 'js-yaml';
4+
import { glob } from 'glob';
5+
6+
const functionsDir = path.resolve('../../functions');
7+
const basePath = path.resolve('./lua-base.tmLanguage.json');
8+
const outputPath = path.resolve('../src/grammars/lua-mta.tmLanguage.json');
9+
10+
function extractFunctionsWithScope(yamlContent) {
11+
if (yamlContent.shared?.name) {
12+
return [{ name: yamlContent.shared.name, scope: 'support.function.mta-shared' }];
13+
} else if (yamlContent.client?.name) {
14+
return [{ name: yamlContent.client.name, scope: 'support.function.mta-client' }];
15+
} else if (yamlContent.server?.name) {
16+
return [{ name: yamlContent.server.name, scope: 'support.function.mta-server' }];
17+
}
18+
return [];
19+
}
20+
21+
async function generateTmLanguage() {
22+
const files = await glob('**/*.yaml', { cwd: functionsDir, absolute: true });
23+
24+
const functionsMap = Object.fromEntries(
25+
['shared', 'server', 'client'].map(scope => [`support.function.mta-${scope}`, new Set()])
26+
);
27+
28+
files.forEach(file => {
29+
const yamlContent = yaml.load(fs.readFileSync(file, 'utf-8'));
30+
console.log('Processing file:', file);
31+
32+
const items = Array.isArray(yamlContent) ? yamlContent : [yamlContent];
33+
items.flatMap(extractFunctionsWithScope).forEach(({ name, scope }) => functionsMap[scope].add(name));
34+
});
35+
36+
const patterns = Object.entries(functionsMap)
37+
.filter(([, namesSet]) => namesSet.size > 0)
38+
.map(([scope, namesSet]) => ({
39+
match: `\\b(${Array.from(namesSet).join('|')})\\b`,
40+
name: scope,
41+
}));
42+
43+
const baseGrammar = JSON.parse(fs.readFileSync(basePath, 'utf-8'));
44+
baseGrammar.patterns = [...patterns, ...(baseGrammar.patterns || [])];
45+
46+
fs.writeFileSync(outputPath, JSON.stringify(baseGrammar, null, 2));
47+
console.log(`Done!`);
48+
}
49+
50+
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)