View previous topic :: View next topic |
Author |
Message |
jfcarbel
Joined: 13 Jun 2006 Posts: 27
|
Posted: Thu Jun 15, 2006 6:06 am Post subject: Setting the syntax highlighter at runtime |
|
|
Here is what I tried, but when I set the text from the loaded file into the memo, it does not display any syntax highlighting.
In addition to the code below I also tried putting a SyntAnalyzer on my form and linking it to the SyntaxMenu via its SyntaxAnalyzer property. In addition I sent the LexerName of SyntAnalyzer to 'HTML documents' to force it to format and highlight the HTML text, but this still did not work.
NOTE: LexLib.LXL is in this applications directory.
Code: | procedure TFormMain.FormCreate (Sender: TObject);
procedure LoadFile(Comp: TLoadableComponent; const AFileName: string);
begin
if FileExists(AFileName) then
try
Comp.LoadFromFile(AFileName);
except
Application.HandleException(Self);
end;
end;
begin
LoadFile(SyntaxManager1, ExtractFilePath(Application.ExeName) + 'LexLib.LXL');
LoadFile(SyntStyles1, ChangeFileExt(Application.ExeName, '.ecs'));
end;
// Code in my open file dialog
OD.Filter := SyntaxManager1.GetFilesFilter;
if od.Execute then
begin
SyntaxManager1.CurrentLexer := SyntaxManager1.AnalyzerForFile(od.FileName);
Parse(od.FileName); // parses the HTML and then displays it in the SyntaxMemo
end; |
|
|
Back to top |
|
|
jfcarbel
Joined: 13 Jun 2006 Posts: 27
|
Posted: Thu Jun 15, 2006 6:10 am Post subject: |
|
|
Was able to get this to work, but not the way I would want (see below)
It would much easier with the below methods. This would allow us to programatically change the SyntaxAnalyzer and also turn off code folding which I do not want.
Code: | SyntaxManager1.CurrentLexer := SyntaxManager1.AnalyzerForType('HTML documents');
SyntaxManager1.CurrentLexer.CodeFolding := False; |
Was finally able to get it to work by using the below code and saving down a new version of LexLib.LXL where I removed the 'HTML with scripts'. The problem was that the AnalyzerForFile method was defaulting to 'HTML with scripts' rather than the 'HTML documents' one I wanted.
Code: | s := ExtractFilePath(Application.ExeName) + 'LexLib.LXL';
if FileExists(s) then
SyntaxManager1.LoadFromFile(s);
SyntaxManager1.CurrentLexer := SyntaxManager1.AnalyzerForFile(od.FileName);
SyntaxMemo1.SyntaxAnalyzer := SyntaxManager1.CurrentLexer; |
I realized I can also do this, but a AnalyzeForType would be much better since indexes will change if more Lexers are added:
Code: | SyntaxMemo1.SyntaxAnalyzer := SyntaxManager1.Analyzers[10]; |
|
|
Back to top |
|
|
jfcarbel
Joined: 13 Jun 2006 Posts: 27
|
Posted: Thu Jun 15, 2006 6:10 am Post subject: |
|
|
Posted by: econtrol (IP Logged)
Date: May 31, 2006 11:48AM
OK.
I shall implement TSyntaxManager.FindAnalyzer(const LexerName: string);
Michael. |
|
Back to top |
|
|
|