-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.lua
185 lines (149 loc) · 4.31 KB
/
handler.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
-- Handler.lua
--!nocheck
local ScriptEditorService = game:GetService("ScriptEditorService")
local ServerScriptService = game:GetService("ServerScriptService")
local AutocompleteHelper = require(script:WaitForChild("HelperFunctions"))
-- local WidgetHandler = require(script:WaitForChild("WidgetHandler"))
local Modules = {}
local Services = {}
local Active = false
--> Types
type Request = {
position: {
line: number,
character: number,
},
textDocument: {
document: ScriptDocument?,
script: LuaSourceContainer?,
},
}
type Response = {
items: {
{
label: string,
kind: Enum.CompletionItemKind?,
tags: { Enum.CompletionItemTag }?,
detail: string?,
documentation: {
value: string,
}?,
overloads: number?,
learnMoreLink: string?,
codeSample: string?,
preselect: boolean?,
textEdit: {
newText: string,
replace: {
start: { line: number, character: number },
["end"]: { line: number, character: number },
},
}?,
}
},
}
--> Functions
local function onChange(document: ScriptDocument, changesArray)
local lineValues = document:GetLine()
local Symbol = script.Parent.Symbol
-- original hacky code xD
Active = string.match(lineValues, Symbol.Value) ~= nil
end
local function onAdded(descendant: Instance)
if not AutocompleteHelper.isModuleScript(descendant) then return end
table.insert(Modules, descendant)
end
local function onRemoving(descendant: Instance)
if not AutocompleteHelper.isModuleScript(descendant) then return end
for i, module in ipairs(Modules) do
if module == descendant then
table.remove(Modules, i)
break
end
end
end
local function getAllModulesAndServices()
Services = {}
Modules = {}
for _, instance in ipairs(game:GetChildren()) do
if AutocompleteHelper.isService(instance) then
table.insert(Services, instance)
end
end
for _, module in ipairs(game:GetDescendants()) do
if AutocompleteHelper.isModuleScript(module) then
table.insert(Modules, module)
end
end
end
local function autocompleteCallback(request, response)
if not Active then
return response
end
local replaceTemplate = nil
for _, item in ipairs(response.items) do
if item.textEdit then
replaceTemplate = table.clone(item.textEdit.replace)
replaceTemplate.start.character -= 1
break
end
end
if not replaceTemplate then return response end
-- modules
for _, module in ipairs(Modules) do
local moduleName = tostring(module)
if not AutocompleteHelper.shouldProcessName(moduleName) then continue end
local moduleService = AutocompleteHelper.getServiceForModule(module)
if not moduleService then continue end
local path = AutocompleteHelper.getModuleFullNamePath(module)
local finalText = AutocompleteHelper.getModuleInitializationString(moduleService, moduleName, path, request.textDocument.document)
local item = {
label = moduleName,
detail = path,
textEdit = {
newText = finalText,
replace = replaceTemplate
}
}
table.insert(response.items, item)
end
-- services
for _, service in ipairs(Services) do
local serviceName = service.ClassName
if not AutocompleteHelper.shouldProcessName(serviceName) then continue end
local finalText = AutocompleteHelper.getServiceInitializationString(serviceName)
local item = {
label = serviceName,
detail = "Service",
textEdit = {
newText = finalText,
replace = replaceTemplate
}
}
table.insert(response.items, item)
end
return response
end
local function createToolbar()
-- local toolbar = plugin:CreateToolbar("AutoCompleteModules")
-- local newButton = toolbar:CreateButton("Symbol Change", "Change the symbol used for autocomplete", "rbxassetid://11963352805")
--
-- local Widget = WidgetHandler:CreateWidget(plugin) -- module scripts dont have the plugin object so we pass dat
--
-- newButton.Click:Connect(function()
-- Widget.Enabled = not Widget.Enabled
-- end)
--
-- Widget:BindToClose(function()
-- newButton:SetActive(false)
-- end)
end
--> Init
pcall(function()
ScriptEditorService:DeregisterAutocompleteCallback("somenameitreallydoesntmatter")
end)
ScriptEditorService:RegisterAutocompleteCallback("somenameitreallydoesntmatter", 69, autocompleteCallback)
ScriptEditorService.TextDocumentDidChange:Connect(onChange)
game.DescendantAdded:Connect(onAdded)
game.DescendantRemoving:Connect(onRemoving)
getAllModulesAndServices()