Skip to content

Commit dbb875e

Browse files
committed
Feature: Conditional Replacement
1 parent 202a05e commit dbb875e

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

src/Plugin/Templates/ProjectSnippets.luau

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,22 @@
77
Example: --@Loop { num=5 }
88
99
SNIPPET STRUCTURE
10-
- Name: Trigger word (string)
11-
- Body: Body of the snippet (table<string>)
12-
- Description: Description of the snippet (string)
13-
- Args: Arguments of the snippet (table)
14-
Default: Default value of the argument (any)
10+
- Name: Trigger word (string)
11+
12+
- Body: Body of the snippet (table<string>)
13+
14+
- Description: Description of the snippet (string)
15+
16+
- BeforeReplace: Function to execute
17+
before replacing the
18+
snippet markers with code (function(Script, LineNumber) -> boolean) <optional>
19+
20+
- AfterReplace: Function to execute
21+
after replacing the
22+
snippet markers with code (function(Script, LineNumber)) <optional>
23+
24+
- Args: Arguments of the snippet (table)
25+
Default: Default value of the argument (any)
1526
1627
Example Snippet:
1728

src/Plugin/init.server.luau

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ local ScriptEditorService = game:GetService('ScriptEditorService')
2626
local StudioService = game:GetService('StudioService')
2727
local ServerStorage = game:GetService('ServerStorage')
2828

29-
local PLUGIN_VERSION = '1.0.0'
29+
local PLUGIN_VERSION = '1.1.0'
3030
local PLUGIN_NAME = 'Smart Script Snippets ' .. PLUGIN_VERSION
3131
local USER_SNIPPETS_MODULE = 'ProjectSnippets'
3232
local SNIPPET_ARGUMENT_DELIMETER = '#'
@@ -101,7 +101,7 @@ local function refreshSnippetsAutocompleteCallbacks()
101101
label = '@' .. snippet.Name,
102102
preselect = true,
103103
kind = Enum.CompletionItemKind.Snippet,
104-
codeSample = table.concat(snippet.Body, '\n'),
104+
codeSample = table.concat(snippet.Body or {}, '\n'),
105105
documentation = {
106106
value = snippet.Description,
107107
},
@@ -146,14 +146,28 @@ local function parseMarkerArguments(arguments: string)
146146
return map
147147
end
148148

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)
150160
markerArgs = parseMarkerArguments(markerArgs)
151161

152162
local replacementStringTable = {}
153163

154164
local snippetArguments = snippet.Args
155165
local snippetBody = snippet.Body
156166

167+
if not canSnippetBeReplaced(snippet, lineNumber) then
168+
return {}, false
169+
end
170+
157171
for _, lineContent in ipairs(snippetBody) do
158172
local currentLineReplacement = lineContent:gsub(
159173
SNIPPET_TEMPLATE_REPLACEMENT,
@@ -173,7 +187,7 @@ local function getSnippetMarkerReplacement(snippet, markerArgs): { string }
173187
table.insert(replacementStringTable, currentLineReplacement)
174188
end
175189

176-
return replacementStringTable
190+
return replacementStringTable, true
177191
end
178192

179193
local function replaceSnippetsMarker()
@@ -182,7 +196,7 @@ local function replaceSnippetsMarker()
182196
local scriptContent = currentDocument.Source:split('\n')
183197
local newScriptContent = {}
184198

185-
for _, lineContent in ipairs(scriptContent) do
199+
for lineNumber, lineContent in ipairs(scriptContent) do
186200
if not lineContent:match(MARKER_VALID_STRUCTURE) then
187201
table.insert(newScriptContent, lineContent)
188202
continue
@@ -197,11 +211,24 @@ local function replaceSnippetsMarker()
197211
continue
198212
end
199213

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
201221

202222
for _, v in ipairs(replacement) do
203223
table.insert(newScriptContent, v)
204224
end
225+
226+
if
227+
snippetContent.AfterReplace
228+
and type(snippetContent.AfterReplace) == 'function'
229+
then
230+
snippetContent.AfterReplace(currentDocument, lineNumber)
231+
end
205232
end
206233

207234
ScriptEditorService:UpdateSourceAsync(currentDocument, function()

0 commit comments

Comments
 (0)