Working With the Code Editor

Applies to TestComplete 15.63, last modified on April 23, 2024

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:

JScript

if (CodeEditor.IsEditorActive)
{
  // Access the Code Editor contents
  ...
}
else
{
  // The Code Editor is not available
  ...
}

VBScript

If CodeEditor.IsEditorActive Then
  ' Access the Code Editor contents
  ...
Else
  ' The Code Editor is not available
  ...
End If

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 CodeEditor object:

  • 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.

  • SelectionStart and SelectionEnd - Return the positions of the first and last selected characters within the Code Editor’s entire text.

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 CodeEditor.Text property value, so you don’t corrupt the user’s code.

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:

JScript

function InsertLogMessage()
{
  // Generate the Log.Message code
  var oMethodCall = Syntax.CreateInvoke();
  oMethodCall.ClassValue = "Log";
  oMethodCall.InvokeName = "Message";
  oMethodCall.IsProperty = false;
  oMethodCall.AddParameter("Hello, world!");

  var strCode = Syntax.GenerateSource(oMethodCall) + "\r\n";

  if (CodeEditor.IsEditorActive)
  {
    // 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;
    else
      // Insert the code to the insertion point
      CodeEditor.Text = aqString.Insert(CodeEditor.Text, strCode, CodeEditor.CursorPos);
  }
}

VBScript

Sub InsertLogMessage
  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
    Else
      ' Insert the code to the insertion point
      CodeEditor.Text = aqString.Insert(CodeEditor.Text, strCode, CodeEditor.CursorPos)
    End If
  End If
End Sub

See Also

Script Extensions
Creating Script Extensions
Creating Custom Actions
Code Editor
Generating Script Code
CodeEditor Object

Highlight search results