Articles in this section
Category / Section

How to configure the syntax coloring for custom Language?

2 mins read

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

 

  1. C#
  2. Visual Basic
  3. XAML
  4. XML
  5. SQL

 

It also allows the user to create Custom Language configurations to apply syntax highlighting. It can be achieved by following step.

 

Step 1: Add EditControl into application.

 

Code Example: [Xaml]

 

<!-- EditControl-->
 
<Grid>
 
<syncfusion:EditControl x:Name="editControl1" FontSize="16" Background="White" Foreground="Black" />
 
</Grid>

 

Step 2: Create custom language class inherited from any one of EditControl  base classes.

 

For Example,

 

We have created the PowershellLanguage [custom language] class inheriting from ProceduralLanguageBase and set basic properties of the language. The following code demonstrates the same.

 

Code Example: [C#]

 

/// <summary>
/// PowershellLanguage class contains implementations related to custom language implementations
/// </summary>
 
public class PowershellLanguage : ProceduralLanguageBase
{
 
#region Initialization
 
/// <summary>
/// Initializes a new instance of the 
/// <param name="control">represents the EditControl to which to which this instance
/// must be hooked</param>
 
public PowershellLanguage(EditControl control): base(control)
{
 
this.Name = "Powershell";
this.FileExtension = "PS1";
this.ApplyColoring = true;
this.SupportsIntellisense = false;
this.SupportsOutlining = true;
this.TextForeground = Brushes.Black;
this.CaseSensitive = false;
 
}
 
#endregion
}

 

Step 3: Create a Format collection.

 

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 demonstrates the same.

 

Code Example: [Xaml]

 

<syncfusion:FormatsCollection x:Key="powershellLanguageFormats">
 
<syncfusion:EditFormats Foreground="Black" FormatName="TextFormat"/>
 
<syncfusion:EditFormats Foreground="Blue" FormatName="KeyWordFormat"/>
 
<syncfusion:EditFormats Foreground="Gray" FormatName="OperatorFormat"/>
 
<syncfusion:EditFormats Foreground="Red" FormatName="VariableFormat"/>
 
<syncfusion:EditFormats Foreground="Green" FormatName="CommentFormat"/>
 
<syncfusion:EditFormats Foreground="Navy" FormatName="NumberFormat"/>
 
</syncfusion:FormatsCollection>

 

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

 

Step 4: Create a Lexem collection.

 

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

 

  1. LexemType: Used for standard predefined types of the lexems.
  2. FormatName: Used to assigns the syntax coloring for the keywords, text, comment tag, etc.

 

The following code demonstrates the same.

 

Code Example: [Xaml]

 

<syncfusion:LexemCollection x:Key="powershellLanguageLexems">
 
<syncfusion:Lexem StartText="\w+-\w+" IsRegex="True" IsMultiline="False" ContainsEndText="False" LexemType="Keyword"  FormatName="KeyWordFormat"/>
 
<syncfusion:Lexem StartText="begin" ContainsEndText="False" IsMultiline="False" LexemType="Keyword"  FormatName="KeyWordFormat"/>
 
<syncfusion:Lexem StartText="process" ContainsEndText="False" IsMultiline="False" LexemType="Keyword"  FormatName="KeyWordFormat"/>
 
<syncfusion:Lexem StartText="end" ContainsEndText="False" IsMultiline="False" LexemType="Keyword"  FormatName="KeyWordFormat"/>
 
<syncfusion:Lexem StartText="break" ContainsEndText="False" IsMultiline="False" LexemType="Keyword"  FormatName="KeyWordFormat"/>
 
<syncfusion:Lexem StartText="if" ContainsEndText="False" IsMultiline="False" LexemType="Keyword"  FormatName="KeyWordFormat"/>
 
<syncfusion:Lexem StartText="else" ContainsEndText="False" IsMultiline="False" LexemType="Keyword"  FormatName="KeyWordFormat"/>
 
<syncfusion:Lexem StartText="elseif" ContainsEndText="False" IsMultiline="False" LexemType="Keyword"  FormatName="KeyWordFormat"/>
 
<syncfusion:Lexem StartText="\$\w+" IsRegex="True" IsMultiline="False" ContainsEndText="False" LexemType="Keyword" FormatName="VariableFormat"/>
 
<syncfusion:Lexem StartText="#" EndText="\r\n" IsMultiline="False" ContainsEndText="True" LexemType="Comment" FormatName="CommentFormat"/>
 
<syncfusion:Lexem StartText="-eq" ContainsEndText="False" IsMultiline="False" LexemType="Operator"  FormatName="OperatorFormat"/>
 
<syncfusion:Lexem StartText="-ne" ContainsEndText="False" IsMultiline="False" LexemType="Operator"  FormatName="OperatorFormat"/>
 
<syncfusion:Lexem StartText="-gt" ContainsEndText="False" IsMultiline="False" LexemType="Operator"  FormatName="OperatorFormat"/>
 
<syncfusion:Lexem StartText="-lt" ContainsEndText="False" IsMultiline="False" LexemType="Operator"  FormatName="OperatorFormat"/>
 
<syncfusion:Lexem StartText="-ge" ContainsEndText="False" IsMultiline="False" LexemType="Operator"  FormatName="OperatorFormat"/>
 
<syncfusion:Lexem StartText="-le" ContainsEndText="False" IsMultiline="False" LexemType="Operator"  FormatName="OperatorFormat"/>
 
</syncfusion:LexemCollection>

 

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

 

Step 5: Assign the lexem and Format properties to Custom Language.

 

The following code demonstrates the same.

 

Code Example: [C#]

 

//Initialize the Powershell language
 
PowershellLanguage customLanguage = new PowershellLanguage(editControl1);
 
//Assign the lexem collection to custom language
 
customLanguage.Lexem = this.Resources["powershellLanguageLexems"] as LexemCollection;
 
//Assign the format collection to custom language
 
customLanguage.Formats = this.Resources["powershellLanguageFormats"] as FormatsCollection;

 

Step 6: Apply the created custom language to Editcontrol language.

 

The following code demonstrates the same.

 

Code Example: [C#]

 

//Set Editcontrol document language to custom
 
editControl1.DocumentLanguage = Syncfusion.Windows.Edit.Languages.Custom;
 
//Set powershell language to CustomLanguage    
 
editControl1.CustomLanguage = customLanguage;
 
//EditControl load file
 
editControl1.DocumentSource = "../../HelloWorld.PS1";

 

Screenshot

 

Figure: EditControl configured with Powershell language.

 

Sample:  PowershellLanguageExample

 

 

 

 

 

 

 

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