@@ -26,7 +26,7 @@ local ScriptEditorService = game:GetService('ScriptEditorService')
26
26
local StudioService = game :GetService ('StudioService' )
27
27
local ServerStorage = game :GetService ('ServerStorage' )
28
28
29
- local PLUGIN_VERSION = '1.0 .0'
29
+ local PLUGIN_VERSION = '1.1 .0'
30
30
local PLUGIN_NAME = 'Smart Script Snippets ' .. PLUGIN_VERSION
31
31
local USER_SNIPPETS_MODULE = 'ProjectSnippets'
32
32
local SNIPPET_ARGUMENT_DELIMETER = '#'
@@ -101,7 +101,7 @@ local function refreshSnippetsAutocompleteCallbacks()
101
101
label = '@' .. snippet .Name ,
102
102
preselect = true ,
103
103
kind = Enum.CompletionItemKind.Snippet ,
104
- codeSample = table.concat (snippet .Body , '\n ' ),
104
+ codeSample = table.concat (snippet .Body or {} , '\n ' ),
105
105
documentation = {
106
106
value = snippet .Description ,
107
107
},
@@ -146,14 +146,28 @@ local function parseMarkerArguments(arguments: string)
146
146
return map
147
147
end
148
148
149
- local function getSnippetMarkerReplacement (snippet , markerArgs ): { string }
149
+ local function canSnippetBeReplaced (snippet , lineNumber )
150
+ return snippet .BeforeReplace
151
+ and type (snippet .BeforeReplace ) == 'function'
152
+ and snippet .BeforeReplace (StudioService .ActiveScript , lineNumber ) == true
153
+ end
154
+
155
+ local function getSnippetMarkerReplacement (
156
+ snippet ,
157
+ markerArgs ,
158
+ lineNumber
159
+ ): ({ string }, boolean )
150
160
markerArgs = parseMarkerArguments (markerArgs )
151
161
152
162
local replacementStringTable = {}
153
163
154
164
local snippetArguments = snippet .Args
155
165
local snippetBody = snippet .Body
156
166
167
+ if not canSnippetBeReplaced (snippet , lineNumber ) then
168
+ return {}, false
169
+ end
170
+
157
171
for _ , lineContent in ipairs (snippetBody ) do
158
172
local currentLineReplacement = lineContent :gsub (
159
173
SNIPPET_TEMPLATE_REPLACEMENT ,
@@ -173,7 +187,7 @@ local function getSnippetMarkerReplacement(snippet, markerArgs): { string }
173
187
table.insert (replacementStringTable , currentLineReplacement )
174
188
end
175
189
176
- return replacementStringTable
190
+ return replacementStringTable , true
177
191
end
178
192
179
193
local function replaceSnippetsMarker ()
@@ -182,7 +196,7 @@ local function replaceSnippetsMarker()
182
196
local scriptContent = currentDocument .Source :split ('\n ' )
183
197
local newScriptContent = {}
184
198
185
- for _ , lineContent in ipairs (scriptContent ) do
199
+ for lineNumber , lineContent in ipairs (scriptContent ) do
186
200
if not lineContent :match (MARKER_VALID_STRUCTURE ) then
187
201
table.insert (newScriptContent , lineContent )
188
202
continue
@@ -197,11 +211,24 @@ local function replaceSnippetsMarker()
197
211
continue
198
212
end
199
213
200
- local replacement = getSnippetMarkerReplacement (snippetContent , snippetMarkerArgs )
214
+ local replacement , didReplace =
215
+ getSnippetMarkerReplacement (snippetContent , snippetMarkerArgs , lineNumber )
216
+
217
+ if not didReplace then
218
+ table.insert (newScriptContent , lineContent ) -- Don't delete the snippet marker
219
+ continue
220
+ end
201
221
202
222
for _ , v in ipairs (replacement ) do
203
223
table.insert (newScriptContent , v )
204
224
end
225
+
226
+ if
227
+ snippetContent .AfterReplace
228
+ and type (snippetContent .AfterReplace ) == 'function'
229
+ then
230
+ snippetContent .AfterReplace (currentDocument , lineNumber )
231
+ end
205
232
end
206
233
207
234
ScriptEditorService :UpdateSourceAsync (currentDocument , function ()
0 commit comments