Recent

Author Topic: TSynEdit and TSynAnySyn  (Read 3551 times)

JdeHaan

  • Full Member
  • ***
  • Posts: 115
TSynEdit and TSynAnySyn
« on: September 02, 2018, 01:45:18 pm »
Hi,
I have a TSynEdit component and attached to it a TSynAnySyn highlighter. I want to load keywords dynamically at runtime from a file. Looked at SynEdit examples, but somehow I can't get the keywords to be highlighted. They do get highlighted if I put them directly in the Keywords property in the Object Inspector though...
Here's my code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ReadKeywords;
  2. var
  3.   KeywordFile: String;
  4.   KeyWordList: TStringList;
  5. begin
  6.   KeywordFile := ExtractFilePath(Application.ExeName) + 'keywords.txt';
  7.   try
  8.     KeyWordList := TStringList.Create;
  9.     try
  10.       KeyWordList.LoadFromFile(KeywordFile);
  11.  
  12.       SynAnySyn.KeyWords.Clear;
  13.       SynAnySyn.KeyWords.Assign(KeyWordList);
  14.       SynAnySyn.Enabled := True;
  15.  
  16.       SynEdit.Refresh;
  17.     except
  18.       ShowMessage('Keyword file - keywords.txt - does not exist.');
  19.     end;
  20.  
  21.   finally
  22.     KeyWordList.Free;
  23.     //ShowMessage(SynAnySyn.KeyWords.Text); // shows correctly loaded keywords
  24.   end;
  25. end;
  26.  

Any idea what I'm missing here?

Thanks

OS High Sierra, Laz 1.8.4, FPC 3.04

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • wiki
Re: TSynEdit and TSynAnySyn
« Reply #1 on: September 02, 2018, 02:38:09 pm »
Try
 SynAnySyn.KeyWords := KeyWordList;

This will internally assign them. So you still need to free your KeyWordList.

Also note, that SynAnySyn does not support utf8. And has the following hardcoded as letters that are treated as words
_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-?!


---------------------
If all you need is to highlight group of words, then you can also use TSynEditMarkupHighlightAllMulti (not a highlighter / needs to be added as Markup / sorry no doc avail)

JdeHaan

  • Full Member
  • ***
  • Posts: 115
Re: TSynEdit and TSynAnySyn
« Reply #2 on: September 02, 2018, 05:21:51 pm »
Thanks Martin for your suggestion, although it didn't work either. Didn't try your 2nd one BTW.
Finally solved it by creating the instance of TSynAnySyn also dynamically and then attach it to SynEdit at runtime in onFormCreate.
Don't know why this should have a different result ... anyway it works now.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • wiki
Re: TSynEdit and TSynAnySyn
« Reply #3 on: September 02, 2018, 06:44:37 pm »
I just did a test (Laz 1.9 svn, Win64)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   l: TStringList;
  4. begin
  5.   l:= TStringList.Create;
  6.   l.Add('abc');
  7.   l.Add('def');
  8.   SynAnySyn1.KeyWords := l;
  9.   l.Free;
  10. end;
  11.  
This code works. I did several buttons with different keywords.

Have you checked that your file does not have any spaces padding the keywords?

To check stringlist (l) items in the debugger:
Code: Pascal  [Select][+][-]
  1. l.Flist[0]

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: TSynEdit and TSynAnySyn
« Reply #4 on: September 03, 2018, 04:52:26 am »
You can use the highlighter SynFacilSyn: https://github.com/t-edson/SynFacilSyn
It's 10 times faster, 10 times more flexible and 10 times more documented than SynAnySyn  :-X
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

JdeHaan

  • Full Member
  • ***
  • Posts: 115
Re: TSynEdit and TSynAnySyn
« Reply #5 on: September 03, 2018, 08:57:46 pm »
Thx Edson,
I'll study your component. I indeed did find SynAnySyn quite slow...

 

TinyPortal © 2005-2018