Skip to content

Commit ff0f65b

Browse files
committed
Clickable keywords and functions
1 parent e48cd01 commit ff0f65b

File tree

9 files changed

+99
-13
lines changed

9 files changed

+99
-13
lines changed

web/astro.config.mjs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ 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-
3118
plugins: [
3219
mtasaStarlightThemePlugin(),
3320
],

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+
};

web/mta_highlighting/generate-lua-tmlanguage.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const functionsDir = path.resolve(__dirname, '../../functions');
1111
const basePath = path.resolve(__dirname, './lua-base.tmLanguage.json');
1212
const outputPath = path.resolve(__dirname, '../src/grammars/lua-mta.tmLanguage.json');
1313

14+
const mtaKeywords = ['string','bool','boolean','number','int','float','element','player','vehicle','ped','object','building'];
15+
1416
function extractFunctionsWithScope(yamlContent) {
1517
if (yamlContent.shared?.name) {
1618
return [{ name: yamlContent.shared.name, scope: 'support.function.mta-shared' }];
@@ -44,6 +46,13 @@ async function generateTmLanguage() {
4446
name: scope,
4547
}));
4648

49+
if (mtaKeywords.length > 0) {
50+
patterns.push({
51+
match: `\\b(${mtaKeywords.join('|')})\\b`,
52+
name: 'keyword.mta',
53+
});
54+
}
55+
4756
const baseGrammar = JSON.parse(fs.readFileSync(basePath, 'utf-8'));
4857
baseGrammar.patterns = [...patterns, ...(baseGrammar.patterns || [])];
4958

web/public/mta-keywords_linker.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import tmLanguage from "../src/grammars/lua-mta.tmLanguage.json";
2+
3+
function extractFunctions() {
4+
const wantedScopes = new Set([
5+
"support.function.mta-shared",
6+
"support.function.mta-server",
7+
"support.function.mta-client",
8+
'keyword.mta'
9+
]);
10+
11+
return tmLanguage.patterns?.reduce((funcs, { match, name }) => {
12+
if (match && wantedScopes.has(name)) {
13+
funcs.push(...match.match(/\\b\(([^)]+)\)\\b/)?.[1]?.split("|") || []);
14+
}
15+
return funcs;
16+
}, []) || [];
17+
}
18+
19+
const allFunctions = new Set(extractFunctions());
20+
21+
function initKeywordLinker() {
22+
function onDomReady() {
23+
const spans = [
24+
...document.querySelectorAll(".examples-section .code-example pre code span"),
25+
...document.querySelectorAll(".function-syntax span, .expressive-code span")
26+
];
27+
28+
spans.forEach(span => {
29+
const text = span.textContent;
30+
if (allFunctions.has(text)) {
31+
span.innerHTML = `<a href="/${text}" class="mta-keyword-link">${text}</a>`;
32+
}
33+
});
34+
}
35+
36+
document.readyState === "loading"
37+
? window.addEventListener("DOMContentLoaded", onDomReady)
38+
: onDomReady();
39+
}
40+
41+
initKeywordLinker();

web/src/components/CodeExamplesSection.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ const { codeExamples } = Astro.props;
2727
))}
2828
</div>
2929
)}
30+
31+
<script type="module" src="/mta-keywords_linker.js"></script>

web/src/grammars/lua-mta.tmLanguage.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
"match": "\\b(setCursorPosition|setCursorAlpha|getCursorPosition|getCursorAlpha)\\b",
1111
"name": "support.function.mta-client"
1212
},
13+
{
14+
"match": "\\b(string|bool|boolean|number|int|float|element|player|vehicle|ped|object|building)\\b",
15+
"name": "keyword.mta"
16+
},
1317
{
1418
"begin": "\\b(?:(local)\\s+)?(function)\\b(?![,:])",
1519
"beginCaptures": {

web/src/styles/custom.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,12 @@
4646
.side-server {
4747
color: var(--color-type-server);
4848
}
49+
50+
.mta-keyword-link {
51+
color: inherit;
52+
text-decoration: none;
53+
}
54+
55+
.mta-keyword-link:hover {
56+
font-weight: 100;
57+
}

web/src/themes/mtasa_lua-theme_dark.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
}
4848
},
4949

50+
{
51+
"scope": "keyword.mta",
52+
"settings": {
53+
"foreground": "#6edbdb",
54+
"fontStyle": "bold"
55+
}
56+
},
57+
5058
{
5159
"scope": "meta.function.lua",
5260
"settings": {

web/src/themes/mtasa_lua-theme_light.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
"fontStyle": "bold"
4343
}
4444
},
45+
46+
{
47+
"scope": "keyword.mta",
48+
"settings": {
49+
"foreground": "#6edb9a",
50+
"fontStyle": "bold"
51+
}
52+
},
53+
4554
{
4655
"scope": "meta.function.lua",
4756
"settings": {

0 commit comments

Comments
 (0)