-
Notifications
You must be signed in to change notification settings - Fork 7
/
plugin.lua
50 lines (43 loc) · 1.56 KB
/
plugin.lua
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
local str_find = string.find
local str_sub = string.sub
local str_gmatch = string.gmatch
---@param uri string # The uri of file
---@param text string # The content of file
---@return { start: integer, finish: integer, text: string }[] | string | nil
function OnSetText(uri, text)
-- ignore .vscode dir, extension files (i.e. natives), and other meta files
if str_find(uri, '[\\/]%.vscode[\\/]') or str_sub(text, 1, 8) == '---@meta' then return end
-- ignore files using fx asset protection
if str_sub(text, 1, 4) == 'FXAP' then return '' end
local diffs = {}
local count = 0
-- prevent diagnostic errors in fxmanifest.lua and __resource.lua files
if str_find(uri, 'fxmanifest%.lua$') or str_find(uri, '__resource%.lua$') then
count = count + 1
diffs[count] = {
start = 1,
finish = 0,
text = '---@diagnostic disable: undefined-global\n'
}
end
-- prevent diagnostic errors from safe navigation (foo?.bar and foo?[bar])
for safeNav in str_gmatch(text, '()%?[%.%[]+') do
count = count + 1
diffs[count] = {
start = safeNav,
finish = safeNav,
text = '',
}
end
-- prevent "need-check-nil" diagnostic when using safe navigation
-- only works for the first index, and requires dot notation (i.e. mytable.index, not mytable["index"])
for pre, whitespace, tableStart, tableName, tableEnd in str_gmatch(text, '([=,;%s])([%s]*)()([_%w]+)()%?[%.%[]+') do
count = count + 1
diffs[count] = {
start = tableStart - 1,
finish = tableEnd - 1,
text = ('%s(%s or {})'):format(whitespace == '' and pre or '', tableName)
}
end
return diffs
end