Code snippets: Difference between revisions

From Axel Public Wiki
Jump to navigation Jump to search
Line 57: Line 57:
</syntaxhighlight>
</syntaxhighlight>


In the <code>list</code> variable we defined the following properties:
In the <code>list</code> variable we defined a list of objects with the following properties:
* '''name''': This property represents the display name of the snippet, which includes the localContext (program name) value.
* '''name''': This property represents the display name of the snippet, which includes the localContext (program name) value.
* '''value''': This property contains the actual code snippet.
* '''value''': This property contains the actual code snippet.

Revision as of 15:03, 20 October 2023

LogicLab introduced the ability to define custom code snippets. These snippets can be inserted into your code conveniently, making the development process more efficient.

In this article, we'll explore how to define and use custom ST (Structured Text) code snippets in LogicLab.

Callback Function Definition

To define custom ST code snippets, you need to create a function of callback in the target script. This callback function will be responsible for generating the code snippet that will be inserted into your program.

Here's how you can define the callback function:

var targetID = app.CallFunction("logiclab.get_TargetID");
app.CallFunction("extfunct.SetCustomSTSnippetsFuncName", targetID + ".MyFunctionCallback");

We set the callback function using app.CallFunction("extfunct.SetCustomSTSnippetsFuncName", targetID + ".MyFunctionCallback"). Make sure to replace MyFunctionCallback with the name of your custom callback function.

Callback Function Implementation

Your custom callback function, in this case, MyFunctionCallback, must be implemented to return a string of ST code. LogicLab will call this function when you request to insert a snippet either from the "ST Toolbar" or by using the shortcut Ctrl+Shift+T. The code you return from the callback will be inserted into your program at the current cursor position.

Here's an example of a simple callback function:

function MyFunctionCallback() {
	// Define your custom ST code snippet here
	return "IF condition THEN\n // Your logic here\nEND_IF;"
}

In this example, the MyFunctionCallback function returns an ST code snippet representing a basic if-then structure. You can customize this code snippet to suit your needs, allowing you to insert frequently used logic patterns easily.

Implementation Example

In the current example, a window is opened that allows you to select a code snippet from the predefined list:

function MyFunctionCallback(localContext)
{
	var list = [{name:"custom snippet 1" + " ( "+localContext+" )", value:"hmiSinVal := SIN( x ) * hmiAmplitude;", key:"1"},
				{name:"custom snippet 2" + " ( "+localContext+" )", value:"hmiCosVal := COS( x ) * hmiAmplitude;", key:"2"}]
				
	app.TempVar("GenericList_input") = list
	app.TempVar("GenericList_multipleSel") = false
	app.OpenWindow("GenericListShortcut", app.Translate("ST snippets list"), "")

	var result = app.TempVar("GenericList_result")

	app.TempVar("GenericList_input") = undefined
	app.TempVar("GenericList_multipleSel") = undefined
	app.TempVar("GenericList_result") = undefined

	if (!result || result.length == 0)
	    return ""

	return result[0].value
}

In the list variable we defined a list of objects with the following properties:

  • name: This property represents the display name of the snippet, which includes the localContext (program name) value.
  • value: This property contains the actual code snippet.
  • key: The key property is used for keyboard shortcut access to the snippet.