Articles in this section
Category / Section

How to configure the custom language in EditControl?

3 mins read

The EditControl has a built-in syntax highlighting support for the following languages.

  1. C Sharp
  2. VB.NET
  3. HTML
  4. XML
  5. Pascal
  6. Delphi
  7. SQL
  8. Java
  9. VBScript
  10.  Jscript

 

Additionally, the custom language can be configured in the EditControl by using the configuration file. By configuring the custom language in the EditControl, you can define the syntax coloring based on the requirement. The following steps help you to configure the syntax coloring for the PowerShell language.

 

Custom Language Configuration

  1. Initialize the configuration language.
  2. Extensions.
  3. Splits.
  4. Formats.
  5. Lexems.
  6. AutoReplaceTriggers.

 

Initialize the Configuration Language

Set the configured language by using the ConfigLanguage tag attribute. When the language is case insensitive, you should set the CaseInsensitive attribute to true.

The following code example demonstrates the same.

XML

<!--Initializes the Configuration language-->.
<ConfigLanguage name="PowerShell" StartComment="#" CaseInsensitive="true" >
  <formats>
  </formats>
  <extensions>
  </extensions>
  <lexems>
  </lexems>
  <splits>
  </splits>
</ConfigLanguage>

 

Extensions

Extensions contain a list of extensions that are associated with that particular language.

 

Splits

Splits contain a list of expressions that must be treated as one word. “=” and “+” are splitters, by default. So, each of them is returned by the tokenizer as a single char. For example, when you want to specify a configuration for “+=”, you should specify “+=” in the splits section.

You can combine multiple keywords as a single keyword. To configure the splits, specify the required word in the split section.

The following code example demonstrates the same.

 

XML

<!--configures the Split section-->.
<splits>
  <split>Write-Host</split>
  <split>Write-Debug</split>
  <split>Write-Error</split>
</splits>

 

See also to configure the Splits in the EditControl

 

Formats

This section helps you to initialize the Format strings. The Format assigns the syntax coloring for the keywords, text, comment tag, etc.

 The following code example demonstrates the same.

 

XML

<!--Defines the formats-->.
<formats>
  <format name="Text" Font="Courier New, 10pt" FontColor="Black" />
  <format name="Whitespace" Font="Courier New, 10pt" FontColor="Black" />
  <format name="KeyWord" Font="Courier New, 10pt" FontColor="Blue" />
</formats>

 

Note:

Every format contains the attributes such as name, font, fore color, font color, back color, style, weight, underline, and line color.

 

Lexems

The Lexem contains rules for parsing the text. There are two attributes to specify the format of the lexem.

  1. Type: The Type is used for standard predefined types of the lexems.
  2. FormatName: The FormatName is used only when Type is Custom.

The following code example demonstrates the same.

 

XML

<!--Defines the lexems-->.
<lexems>
  <!--Adds the Keywords in the lexem-->.
  <lexem BeginBlock="Begin" Type="KeyWord" />
  <lexem BeginBlock="$args" Type="KeyWord" />
  <!--Adds the operator in the Lexem-->.
  <lexem BeginBlock="=" Type="Operator" />
  <lexem BeginBlock="+" Type="Operator" />
</lexems>

 

Note:

Lexems are used to initialize the lexem option and to define the keyword, operands, number, string, etc.

 

AutoReplaceTriggers

The AutoReplaceTrigger option helps you to autocorrect the mistakes in the EditControl.

The following code example demonstrates the same.

 

XML

<!--Defines the AutoReplaceTrigger-->.
<AutoReplaceTriggers>
  <AutoReplaceTrigger From="teh" To="the" />
  <AutoReplaceTrigger From="itn" To="int" />
</AutoReplaceTriggers>

 

After configuring the custom language, the configured file can be applied to the EditControl by using the Configurator.Open function in the EditControl. The text coloring of the specified language can be applied to the EditControl by using the ApplyConfiguration function in the EditControl.

The following code examples help you to configure the custom language in the EditControl.

 

C#

// Opens the PowerShell file.
this.editControl1.LoadFile(@"..\\..\\HelloWorld.PS1");
//Configures the Custom Language File.
this.editControl1.Configurator.Open(@"..\\..\\Config.xml");
//Applies the Custom Language configuration to the EditControl.
this.editControl1.ApplyConfiguration("PowerShell");

 

VB

'Opens the PowerShell file.
Me.editControl1.LoadFile("..\\..\\HelloWorld.PS1")
'Configures the Custom Language File.
Me.editControl1.Configurator.Open("..\\..\\Config.xml")
'Applies the Custom Language configuration to the EditControl.
Me.editControl1.ApplyConfiguration("PowerShell")

 

Note:

The custom specified language files can be opened by using the LoadFile function in the EditControl.

 

Intellisense support in EditControl

You can provide the intellisence support to the EditControl by handling the ContextChoiceOpen event.

The following code example demonstrates the same.

C#

// Raises the event when auto-complete dialog is opened.
private void editControl1_ContextChoiceOpen(Syncfusion.Windows.Forms.Edit.Interfaces.IContextChoiceController controller)
{       
    int index = 0;
    ICollection list = GetList();
    foreach (IConfigLexem conf in list)
    {
        //Adds the items in the Intellisense popup.
        controller.Items.Add(conf.BeginBlock, (string)m_MethodComments[conf.ID], this.editControl1.ContextChoiceController.Images["Image" + index.ToString()]);
        index++;
    }
}

 

VB

'Raises the event when auto-complete dialog is opened.
 
Private Sub editControl1_ContextChoiceOpen(ByVal controller As Syncfusion.Windows.Forms.Edit.Interfaces.IContextChoiceController) Handles editControl1.ContextChoiceOpen
 Dim index As Integer = 0
 Dim list As ICollection = GetList()
 For Each conf As IConfigLexem In list
  'Adds the items in the Intellisense popup.
  controller.Items.Add(conf.BeginBlock, CStr(m_MethodComments(conf.ID)), Me.editControl1.ContextChoiceController.Images("Image" & index.ToString()))
  index += 1
 Next conf
End Sub

 

 

EditControl configured with PowerShell language

Figure 1: EditControl configured with PowerShell language

Showing intellisense in EditControl

Figure 2: Intellisence in EditControl

Sample Links:

C#: EditControl_PowerShellLanguage_C#

VB: EditControl_PowerShellLanguage_VB

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied