Skip to content

Conditional Replacement

Osaka edited this page Feb 5, 2025 · 1 revision

Conditional replacement allows you to execute code BEFORE and AFTER replacing a snippet marker. Version v1.1.0 introduced new fields for your snippets:

  • BeforeReplace (function that returns a boolean).
  • AfterReplace (function)

BeforeReplace callback

BeforeReplace(LuaSourceContainer currentOpenedScript, number lineNumber) -> boolean

This field is a function which receives 2 parameters, the currently open script and the line of code where the snippet marker to be replaced is found. This function must return a boolean. If it returns true, the marker will be replaced, if it returns false it will not be replaced and will pass to the next one.

AfterReplace callback

AfterReplace(LuaSourceContainer currentOpenedScript, number lineNumber) -> None

This field is a function which receives 2 parameters, the currently open script and the line of code where the snippet marker to be replaced is found.

  • This function will be executed always after a snippet marker is replaced. This function will be called ONLY IF THE SNIPPET MARKER WAS REPLACED.

Example

{
   Name = "foo",
   Args = {},
   Description = "",
   Body = { "Hello" },
   BeforeReplace = function(_, currentLine)
        return currentLine % 2 == 0
   end,
   AfterReplace = function(_, currentLine)
        print("Snippet Marker replaced at line " .. currentLine)
   end,
}
1    --@foo{} <- Won't execute (1 % 2 is not 0)
2    --@foo{} <- Will execute (2 % 2 is 0)
3    --@foo{} <- Won't execute (3 % 2 is not 0)
4    --@foo{} <- Will execute (4 % 2 is 0)

OUTPUT:

Snippet Marker replaced at line 2
Snippet Marker replaced at line 4

NOTE

These functions are optional.

Clone this wiki locally