Custom design-time actions may need to access the script code displayed in the Code Editor. For example, if you are creating an action that generates script code, you may want to insert this code directly into the user script.
To access the Code Editor contents from script extensions, you can use the CodeEditor object. However, in order for you to be able to do this, the following conditions must be met:
- The script must be opened in the Code Editor.
- The Code Editor must be active in the Workspace panel.
To check if there is an active Code Editor, you can use the
CodeEditor.IsEditorActive property. It returns True if the currently active editor in the Workspace panel is the Code Editor; otherwise False. So, if
CodeEditor.IsEditorActive property returns True, this means that you can proceed with accessing the Code Editor contents; otherwise it is not available. The following code snippet demonstrates how to perform this kind of check:
// Access the Code Editor contents
// The Code Editor is not available
If CodeEditor.IsEditorActive Then
' Access the Code Editor contents
' The Code Editor is not available
After you have verified that there is an active Code Editor instance, you can retrieve its contents and determine the insertion point position. For this purpose, use the following properties of the
Text- Provides access to the entire contents of the script unit that is currently opened in the Code Editor. This property is read-write, so you can both obtain the script code and modify it.
CursorPos- Returns the position of the insertion point within the Code Editor’s text.
SelectedText- Returns the text that is currently selected in the Code Editor, or the word that is around the insertion point.
Thus, with the
CodeEditor object you can obtain and analyze the script code, insert new code snippets, modify the code, search and replace specific code portions and so on. For example, your extension could generate the code and insert it directly into the script. Or, you could create a simple refactoring extension that would replace hard-coded numbers and string literals with global script variables.
Any changes made to the script code via the
CodeEditor.Text property are immediately reflected in the Code Editor. TestComplete keeps track of these changes in addition to those made by the user, so that the user can undo code modifications performed by the script extension if something went wrong.
|Be careful when modifying the Code Editor’s contents via the
The following example demonstrates how you can access and modify the script in the Code Editor. It inserts the
Log.Message method call to the current insertion point:
// Generate the Log.Message code
var oMethodCall = Syntax.CreateInvoke();
oMethodCall.ClassValue = "Log";
oMethodCall.InvokeName = "Message";
oMethodCall.IsProperty = false;
var strCode = Syntax.GenerateSource(oMethodCall) + "\r\n";
// If the insertion point is at the end of the script, ...
if (CodeEditor.CursorPos == CodeEditor.Text.length)
// ... append the code to the script
CodeEditor.Text += strCode;
// Insert the code to the insertion point
CodeEditor.Text = aqString.Insert(CodeEditor.Text, strCode, CodeEditor.CursorPos);
Dim oMethodCall, strCode
' Generate the Log.Message code
Set oMethodCall = Syntax.CreateInvoke
oMethodCall.ClassValue = "Log"
oMethodCall.InvokeName = "Message"
oMethodCall.IsProperty = False
oMethodCall.AddParameter "Hello, world!"
strCode = Syntax.GenerateSource(oMethodCall) & vbNewLine
If CodeEditor.IsEditorActive Then
' If the insertion point is at the end of the script...
If CodeEditor.CursorPos = Len(CodeEditor.Text) Then
' ... append the code to the script
CodeEditor.Text = CodeEditor.Text & strCode
' Insert the code to the insertion point
CodeEditor.Text = aqString.Insert(CodeEditor.Text, strCode, CodeEditor.CursorPos)