Using a Highlighter
A highlighter is a component (derived from TSynCustomHighlighter) that splits the text in tokens (keywords, comments, strings, numbers...) and gives each kind of token a set of attributes (foreground color, background color, font style).
At design time
- Drop a TSynEdit on the form.
- Drop a highlighter from the SynEdit Highlighters page, for example
TSynPasSyn. - Assign it to the
Highlighterproperty of the editor.
At run time
uses
SynEdit.Core, SynEdit.Highlighter.Pas;
SynEdit1.Highlighter := TSynPasSyn.Create(Self);The same highlighter can be assigned to more editors. A highlighter must live at least as long as the editors that use it.
Choosing the highlighter from the file name
Every highlighter has a DefaultFilter (for example Pascal Files (*.pas;*.pp;*.dpr;*.dpk;*.inc)|*.pas;*.pp;*.dpr;*.dpk;*.inc). The demos use it to pick the highlighter of a file from its extension: see the unit uHighlighterProcs in the Demos folder, that also builds the filter of the open and save dialogs.
Attributes
Each highlighter publishes its attributes as properties: CommentAttri, KeyAttri, StringAttri, NumberAttri, IdentifierAttri, SpaceAttri, SymbolAttri and the specific ones of the language. Each attribute has Foreground, Background and Style (a set of fsBold, fsItalic, fsUnderline, fsStrikeOut):
SynPasSyn1.KeyAttri.Foreground := clNavy;
SynPasSyn1.KeyAttri.Style := [fsBold];
SynPasSyn1.CommentAttri.Style := [fsItalic];The dialog of the unit SynEdit.FHighlightProp lets the user edit the attributes of a highlighter with a preview: see Options Dialog.
Highlighter options
TSynEditHighlighterOptions (unit SynEdit.HighlighterOptions, Options property of the highlighters) describes the language: Title, DefaultExtension, LineCommentarStart, LineCommentarEnd, Visible and the auto-detection of the language from the content (AutoDetectEnabled, AutoDetectLineLimit, AutoDetectMatchExpression). It is useful to build a menu of the languages or to comment and uncomment the lines.
Highlighters and code folding
The highlighters derived from TSynCustomCodeFoldingHighlighter provide the ranges of the code folding.
