Code snippets: Difference between revisions

From Axel Public Wiki
Jump to navigation Jump to search
Line 5: Line 5:
== Callback Function Definition ==
== Callback Function Definition ==


To define custom ST code snippets, you need to create a callback function in the target script. This callback function will be responsible for generating the code snippet that will be inserted into your program.
To define custom ST code snippets, you need to create a callback function in the target script. This 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:
Here's how you can define it:


<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">

Revision as of 16:35, 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 callback function in the target script. This function will be responsible for generating the code snippet that will be inserted into your program.

Here's how you can define it:

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 = null;
	switch (localContext) {
		case "main":
			list = [{name:"counter increment" + " ("+localContext+")", value:"cnt := cnt + 1;", key:"A"},
					{name:"counter decrement" + " ("+localContext+")", value:"cnt := cnt - 1;", key:"B"}];
			break;
		case "program1":
			list = [{name:"custom snippet 1" + " ("+localContext+")", value:"hmiSinVal := SIN( x );", key:"1"},
					{name:"custom snippet 2" + " ("+localContext+")", value:"hmiCosVal := COS( x );", key:"2"}];
			break;
		default:
			break;
	}

	if (!list)
		return "";

	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;
}

This script demonstrates how you can dynamically provide different sets of code snippets based on the context: the localContext variable is used to determine which set of snippets should be displayed depending on the program name.


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.


You have the flexibility to implement any type of logic to suit the specific needs of your project.