In This Article

Edit Actions

Edit actions are classes that implement IEditAction and contain code to perform different simple tasks related to a SyntaxEditor. They are effectively commands with can-execute and executed handlers that perform all the work.

Edit Actions and Commands

SyntaxEditor includes over 100 unique edit actions, covering everything such as movements, selection types, clipboard operations, and more.

The built-in edit actions are all located in the ActiproSoftware.Windows.Controls.SyntaxEditor.EditActions namespace.

All commands related to the built-in edit actions are available via static properties on the EditorCommands class.

Executing Edit Actions

To execute an edit action, use the IEditorView.ExecuteEditAction method. Simply pass it an instance of the IEditAction to execute.

This code shows how to execute an IndentAction on the active view:

editor.ActiveView.ExecuteEditAction(new IndentAction());

Most of the edit action classes defined by SyntaxEditor also have helper methods which make running the edit actions easier. For instance, the IEditorViewSelection interface has a method SelectAll, which runs the SelectAllAction. Another example is the IEditorView interface has a method CopyToClipboard, which runs the CopyToClipboardAction.

Canceling an Edit Action

The IEditorView.ExecuteEditAction method immediately raises the SyntaxEditor.ViewActionExecuting event, which is passed an EditActionEventArgs that specifies the IEditorView and IEditAction being executed. The event args has a Cancel property that can be set to true to prevent the edit action from executing. This provides a way for certain edit actions to be externally filtered out if desired.

Creating a Custom Edit Action

Custom edit action classes may be defined. The only requirement is that they implement IEditAction. When defining an edit action, it's easiest to inherit EditActionBase.

This code shows how to define an edit action that surrounds the selected text with custom tags.

/// <summary>
/// Provides a custom <see cref="IEditAction"/> implementation that inserts a
/// <c>custom</c> tag surrounding the selected text.
/// </summary>
public class CustomAction : ActiproSoftware.Windows.Controls.SyntaxEditor.Implementation.EditActionBase {

	/// <summary>
	/// Initializes an instance of the <c>CustomAction</c> class.
	/// </summary>
	public CustomAction() : base("Custom") {}

	/// <summary>
	/// Executes the edit action in the specified <see cref="IEditorView"/>.
	/// </summary>
	/// <param name="view">The <see cref="IEditorView"/> in which to execute the edit action.</param>
	public override void Execute(IEditorView view) {
		view.InsertSurroundingText("<custom>", "</custom>");
	}

}

Binding New Edit Actions to Keys

Although there are many default key bindings for edit actions in SyntaxEditor, it's easy to add new ones.

Binding of edit actions to a keyboard shortcut is done in three steps. In the following example, we will bind the CustomAction edit action defined above to the Ctrl+R keyboard shortcut.

Create a Static Command

For ease in implementation, the EditActionBase type (base class for edit actions) implements ICommand. SyntaxEditor's built-in commands are defined as static properties on a static class named EditorCommands. This allows a single edit action instance (like SelectAllAction) to be reused anywhere a command or input binding for that edit action is needed. Therefore, it is recommended that custom edit actions also create a static instance that can be used the same way.

Add a Command Binding

Second, we need to create a command binding so that SyntaxEditor can associate a command with executing the CustomAction edit action's logic. EditActionBase defines a handy CreateCommandBinding method that generates a command binding.

Assume that a CustomAction instance is stored on a static MyStaticCommands.Custom property. This code binds the edit action to the editor:

editor.CommandBindings.Add(MyStaticCommands.Custom.CreateCommandBinding());

Add an Input Binding

Finally, add an input binding so that a keyboard shortcut (Ctrl+R) will trigger the edit action:

editor.InputBindings.Add(new KeyBinding(MyStaticCommands.Custom, Key.R, ModifierKeys.Control));

Binding Built-In Edit Actions to Other Keys

Changing or adding keyboard shortcuts to the built-in edit actions is easier. In that case it is only a matter of updating the input bindings (see above) since the EditorCommands already defines commands for the built-in edit actions and the SyntaxEditor control has handlers for all those commands.

This code makes Ctrl+R execute the copy to clipboard edit action:

editor.InputBindings.Add(new KeyBinding(EditorCommands.CopyToClipboard, Key.R, ModifierKeys.Control));