Skip to content

Commit 7d8e4f6

Browse files
authored
Add annotations for lua_ls (#30)
* add annotations for lua_ls * cleanup
1 parent 9679fdd commit 7d8e4f6

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

.luarc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
3+
"Lua.runtime.version": "LuaJIT",
4+
"Lua.workspace.checkThirdParty": false,
5+
"Lua.workspace.library": [
6+
"EmmyLua"
7+
]
8+
}

EmmyLua/jsregexp/core.lua

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
---@meta
2+
3+
local jsregexp = {}
4+
5+
---Compile a regular expression. Throws if an error occurs.
6+
---```lua
7+
--- local re = jsregexp.compile("\\w+", "g")
8+
---```
9+
---@param re string
10+
---@param flags? string
11+
---@return JSRegExp.RegExp RegExp
12+
function jsregexp.compile(re, flags) end
13+
14+
---Safely compile a regular expression.
15+
---```lua
16+
--- local re, err = jsregexp.compile_safe("\\w+", "g")
17+
--- if not re then
18+
--- print(err)
19+
--- end
20+
---```
21+
---@param re string
22+
---@param flags? string
23+
---@return JSRegExp.RegExp? RegExp
24+
---@return string? Error
25+
function jsregexp.compile_safe(re, flags) end
26+
27+
---Convert a lua utf8 lua string to a utf16 js string. For internal use.
28+
---@param str string
29+
---@return JSRegExp.JSString
30+
function jsregexp.to_jsstring(str) end
31+
32+
---UTF-16 representation of a string. For internal use.
33+
---@class JSRegExp.JSString
34+
35+
---A compiled JavaScript regular expression object.
36+
---@class JSRegExp.RegExp
37+
---@field last_index integer the position at wchich the next match will be searched in re:exec or re:test (see notes below)
38+
---@field source string the regexp string
39+
---@field flags string a string representing the active flags
40+
---@field dot_all boolean is the dod_all flag set?
41+
---@field global boolean is the global flag set?
42+
---@field has_indices boolean is the indices flag set?
43+
---@field ignore_case boolean is the ignore_case flag set?
44+
---@field multiline boolean is the multiline flag set?
45+
---@field sticky boolean is the sticky flag set?
46+
---@field unicode boolean is the unicode flag set?
47+
local re = {}
48+
49+
---Execute the regular expression against a string.
50+
---```lua
51+
--- local re = jsregexp.compile("\\w+", "g")
52+
--- local match = re:exec("Hello World")
53+
--- if match then
54+
--- print(match) -- Hello
55+
--- end
56+
---```
57+
---@param string string|JSRegExp.JSString
58+
---@return JSRegExp.Match?
59+
function re:exec(string) end
60+
61+
---Test the regular expression against a string.
62+
---```lua
63+
--- local re = jsregexp.compile("\\w+", "g")
64+
--- if re:test("Hello World") then
65+
--- print("Matched!")
66+
--- end
67+
---```
68+
---@param string string|JSRegExp.JSString
69+
---@return boolean
70+
function re:test(string) end
71+
72+
---Returns a list of all matches or nil if no match.
73+
---```lua
74+
--- local re = jsregexp.compile("\\w+", "g")
75+
--- local matches = re:match("Hello World")
76+
--- if matches then
77+
--- for _, match in ipairs(matches) do
78+
--- print(match)
79+
--- end
80+
--- end
81+
---```
82+
---@param string string
83+
---@return JSRegExp.Match[]?
84+
function re:match(string) end
85+
86+
---Returns a closure that repeatedly calls re:exec, to be used in for-loops.
87+
---```lua
88+
--- local re = jsregexp.compile("\\w+", "g")
89+
--- for match in re:match_all("Hello World") do
90+
--- print(match)
91+
--- end
92+
---```
93+
---@param string string
94+
---@return fun():JSRegExp.Match
95+
function re:match_all(string) end
96+
97+
---Returns a list of all matches.
98+
---```lua
99+
--- local re = jsregexp.compile("\\w+", "g")
100+
--- local matches = re:match("Hello World")
101+
--- for _, match in ipairs(matches) do
102+
--- print(match)
103+
--- end
104+
---```
105+
---@param string string
106+
---@return JSRegExp.Match[]
107+
function re:match_all_list(string) end
108+
109+
---Returns the 1-based index of the first match of re in str, or -1 if no match
110+
---```lua
111+
--- local re = jsregexp.compile("Wo\\w+")
112+
--- local idx = re:match("Hello World")
113+
--- if idx > 0 then
114+
--- print("Matched at index " .. idx)
115+
--- end
116+
---```
117+
---@param string string
118+
---@return integer
119+
function re:search(string) end
120+
121+
---Splits str at re, at most `limit` times
122+
---```lua
123+
--- local re = jsregexp.compile("\\s+")
124+
--- local res = re:split("Hello World")
125+
--- for _, str in ipairs(res) do
126+
--- print(str)
127+
--- end
128+
---```
129+
---@param string string
130+
---@param limit? integer
131+
---@return string[]
132+
function re:split(string, limit) end
133+
134+
---Relplace the first match (all matches, if global) of re in str by replacement.
135+
---```lua
136+
--- local re = jsregexp.compile("\\w+")
137+
--- local res = re:replace("Hello World", "Hey")
138+
--- print(res) -- Hey World
139+
--- local res2 = re:replace("Hello World", function(match, str)
140+
--- if match[0] == "Hello" then
141+
--- return "Hey"
142+
--- elseif match[0] == "World" then
143+
--- return "Joe"
144+
--- else
145+
--- return ""
146+
--- end
147+
--- end)
148+
--- print(res2) -- Hey World
149+
---```
150+
---@param string string
151+
---@param replacement (fun(match: JSRegExp.Match, str: string):string)|string
152+
---@return string
153+
function re:replace(string, replacement) end
154+
155+
---Relplace all occurances of re in str by replacement.
156+
---```lua
157+
--- local re = jsregexp.compile("\\w+", "g")
158+
--- local res = re:replace_all("Hello World", "Hey")
159+
--- print(res) -- Hey Hey
160+
--- local res2 = re:replace_all("Hello World", function(match, str)
161+
--- if match[0] == "Hello" then
162+
--- return "Hey"
163+
--- elseif match[0] == "World" then
164+
--- return "Joe"
165+
--- else
166+
--- return ""
167+
--- end
168+
--- end)
169+
--- print(res2) -- Hey Joe
170+
---```
171+
---@param string string
172+
---@param replacement (fun(match: JSRegExp.Match, str: string):string)|string
173+
---@return string
174+
function re:replace_all(string, replacement) end
175+
176+
---@class JSRegExp.Match : table
177+
---@field input string The input string
178+
---@field capture_count integer The number of capture groups
179+
---@field index integer The start of the capture (1-based)
180+
---@field groups table<string,string> Table of the named groups and their content
181+
---@field indices JSRegExp.MatchIndices Array of begin/end indices of numbered match groups (if `"d"` flag is set)
182+
183+
---@class JSRegExp.MatchIndices : table
184+
---@field groups table Table of named groups and their begin/end indices (if `"d"` flag is set)
185+
186+
return jsregexp

0 commit comments

Comments
 (0)