forked from GreenXenith/luanti-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets.js
100 lines (87 loc) · 3.67 KB
/
snippets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const fs = require("fs");
function docLink(api, search) {
const line = api.substring(0, api.indexOf(search)).split("\n").length;
// return line > 1 ? `\n\n[View in lua_api.txt](https://github.com/minetest/minetest/blob/5.4.1/doc/lua_api.txt#L${line}-L${line + search.split("\n").length - 1})` : ""
if (line > 1) return `L${line}-L${line + search.split("\n").length - 1}`;
}
let types = [];
const objects = (entry, api) => {
entry = entry.substr(1);
// The documented thing (* `between here`:)
const name = entry.match(/^\* `([^\n`:]+)/)[1];
// The specific name (* `minetest.justthisfunction(but, not, the, args)`:)
const lookfor = name.match(/(?:\S+?\.)?([^\(]+)/)[1];
// The completed thing without namespaces and with argument tabstops if applicable
let i = 0;
const complete = name.match(/^(?:\w+\.)?(.*)$/)[1].replace(/\(([^(]+)\)/, args => {
return args.replace(/(?! )(?:\[, )?[\w\- .\]]+/g, arg => `\${${++i}:${arg}}`);
}).replace(/(\(function\(.+)\)$/, `$1\n\t$0\nend)`);
// Full method documentation
const doc = entry.match(/^\* ([\S\s]*?)\n*$/)[1].replace(/\n /g, "\n")// + docLink(api, entry); // Remove extra indent
// Type of thing
const type = name.match(/\(/) && (name.match(/\./) && "Function" || "Method") || "Object";
// Namespace or method character (:)
const namespace = ((name.match(/^HTTPApiTable/) && {1: "."}) || name.match(/^(\S*?\.)/) || {1: ":"})[1];
return {
prefix: lookfor, // look for this
body: complete, // set to this
desc: doc, // documentation description
doc_lines: docLink(api, entry),
kind: {Function: 2, Method: 1, Object: 5}[type], // this type
detail: type, // header detail
token: namespace, // look after this
};
}
// Functions and methods
types.push([/\n\* `(?!dump)[^\(][^`:]+\).*(?:\n+ +.+)*/g, objects]);
// Lists and objects
types.push([/\n\* `minetest\.(?!conf)(?![A-Z_\-]+?`)[^\(\)]+?`.*(?:\n+ +.+)*/g, objects]);
// Formspec elements
types.push([/### `.+\[.*\n?\n[\S\s]+?\n\n/g, (entry, api) => {
return {
prefix: entry.match(/^### `(.+?)\[/)[1],
body: entry.match(/^### `(.+?\])/)[1].replace(/<(.+?)>/g, "$1") + "$0",
desc: entry.match(/^([\S\s]*?)\n*$/)[1],// + docLink(api, entry),
doc_lines: docLink(api, entry),
kind: 13,
detail: "Formspec Element",
// token: entry.match(/^### `(.)/)[1],
};
}]);
// Texture modifiers
types.push([/#### `\[.+\n\n(?:[^\n]+\n+(?!(?:####)|(?:\-+\n)))+/g, (entry, api) => {
let i = 0;
return {
prefix: entry.match(/\[([\w_-]+)/)[1],
body: entry.match(/\[(.+?)`/)[1].replace(/<(.+?)>|(\.\.\.)/g, (arg1, arg2) => `\${${++i}:${arg1 || arg2}}`) + "$0",
desc: entry.match(/^([\S\s]+?)\n*$/)[1],// + docLink(api, entry),
doc_lines: docLink(api, entry),
kind: 7,
detail: "Texture Modifier",
token: "[",
};
}]);
// Constants
types.push([/[`']minetest\.[A-Z_\-]+[`'](?:.{5,}|.{0})/g, (entry, api) => {
return {
prefix: entry.match(/[`'](.+?)[`']/)[1],
body: entry.match(/\.(.+?)[`']/)[1],
desc: entry.replace(/'/g, "`"),// + docLink(api, entry),
doc_lines: docLink(api, entry),
kind: 11,
detail: "Constant",
token: "minetest.",
};
}]);
function getSnippets(api) {
let snippets = [];
for (const type of types) {
for (let entry of api.matchAll(type[0])) {
snippets.push(type[1](entry[0], api));
}
}
return snippets;
}
fs.readFile("./lua_api.txt", "utf8", (err, data) => {
if (!err) fs.writeFile("./smartsnippets.json", JSON.stringify(getSnippets(data)), "utf8", () => {});
});