Recent

Author Topic: All Things SynEdit/Highlighter - Lyrics Editor  (Read 7062 times)

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #15 on: May 08, 2019, 04:24:32 pm »
Code: Pascal  [Select][+][-]
  1. FAfterOpenSquareBracket: Boolean;
  2. FAfterSquareBracketTEXT: Boolean;

Those 2 variables must be fields of the object. So there state is kept, across several calls to "Next".

You have to see the "Next" method (and the entire parsing) as a "state" driven parser.

"Next" is called over and over again. Each time, it only sees a small bit of the text. In order to know, how to treat this small bit, it needs to know what state it is in.

- So if it is in "normal" state, and it encounters a "[", then it changes to "after [" state (FAfterOpenSquareBracket:=True)

- If it is in "after [" state, then it needs to find the "]" and the text inbetween is a chord token.
  Or better yet, it should test, if that text inbetween is any of the know chords (e.g. DM)
  ** Important: Since Next advances to the end of the token, it must also change the state, it is no longer "after [".
       It goes to "after [ text"  (changing the 2 fields)

- if state = "after [ text" then the "]" is to be highlighted

The last state ensures that if you encounter a "]" that is not the end of a chord, then it is not highlighted.


- The state must be kept in a field of the object, so it can be used across several calls to Next.
- You may want to clear it, in SetLine, or Get/SetRange
  ((( If you need it across lines, then you must store it as part of the "range". Lines can be scanned out of order, but the range will be restored correctly. )))
- It is your choice how to store it: 2 booleans, an enum, a set .....

I mostly understand what you are saying. I pretty much understood this from reading the WIKI (that it does explain)

But, the code I have so far is parsing 3 or 4 chords at a time.

The code HAS to deal with one set at a time and then, it should only make the actual chord (not the []'s) red and bold.

If you see screenshot above... it doesn't work.

How do I make this work right?
Because of the lack of docs, I am just shooting in the dark.

Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #16 on: May 08, 2019, 04:59:47 pm »
is there something similar to SynEdit that has decent docs.

Don't mention AtSynEdit either... won't install on my machine.

Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #17 on: May 08, 2019, 05:04:51 pm »
Are there any code examples where someone used SynEdit to edit say like html?
I would like to see how they handle the text in between html tags.
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11164
  • Debugger - SynEdit - and more
    • wiki
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #18 on: May 08, 2019, 06:35:37 pm »
There is SynFacilSyn which is a configurable Highlighter for the Lazarus SynEdit. It is by the same author as AtSynEdit, but it is for the real SynEdit.

Alternatively, if you want to upload the code you already have, (or sent in private) then I can have a look at that.
That is: entire sample project, ready to put into folder and compile.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #19 on: May 08, 2019, 06:43:52 pm »
There is SynFacilSyn which is a configurable Highlighter for the Lazarus SynEdit. It is by the same author as AtSynEdit, but it is for the real SynEdit.

Alternatively, if you want to upload the code you already have, (or sent in private) then I can have a look at that.
That is: entire sample project, ready to put into folder and compile.

This is my updated version from page 1

Thanks for taking your time on this. I know you must be busy.
 ;)

I wouldn't be asking for help, if someone would of  put some effort into better documentation and example code for real uses for this thing. It doesn't even list the class methods anywhere I can find.

All that is out there is so vague, no beginner (like myself - going on two years) is not going to figure it out.

Q: Has the LAZ forum ever considered a code bank on the forum, that just has code and project example... less people would be asking the same old questions over and over. VBForums has one and it was invaluable.
« Last Edit: May 08, 2019, 06:52:39 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11164
  • Debugger - SynEdit - and more
    • wiki
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #20 on: May 08, 2019, 08:10:42 pm »
Ok, I made quite some changes....

First of all, I realized the tutorial has a flaw.
In real live, when you write a HL based on ContextHl, you do not want to inherit from SimpleHL. You want the 2 classes be merged into one single class.

In the example it is not a problem, because the changes are very small. But in real live you want one HL that has you full logic in it. Not distribute your logic over several classes.
So ContextHL should be
      TSynDemoHlContext = class(TSynCustomHighlighter)
And the code from SimpleHL should be copied into it.

If I find time, I will improve the tutorial. But that may be a good while before it happens.
I did not fix that for your example either.
I do recommend you merge the 2 classes. But all your code into ContextHL.

I dropped the 2 variables, and kept the state in a set.
That set will also represent whatever the current token should be.

SynEdit uses the methods
    function GetToken: String; virtual; abstract;
    procedure GetTokenEx(out TokenStart: PChar; out TokenLength: integer); virtual; abstract;
    function GetEndOfLineAttribute: TSynHighlighterAttributes; virtual; // valid after line was scanned to EOL
    function GetTokenAttribute: TSynHighlighterAttributes; virtual; abstract;
    function GetTokenKind: integer; virtual; abstract;
    function GetTokenPos: Integer; virtual; abstract; // 0-based
to find out what attributes to use for each token, and where it start and ends.

SynEdit loops for each line approx like
while not HL.GetEOL do begin
   hl.GetTokenEx
   hl.GetTokenAttribute
   hl.next
end

For a line like
"  abc [bb] def"
this would get the following tokens
"  abc " textAttri
"[" bracketAttri
"bb" chordAttri
"]" bracketAttri
" def" textAttri

Hope that explains a bit of how it works
« Last Edit: May 08, 2019, 09:33:52 pm by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11164
  • Debugger - SynEdit - and more
    • wiki
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #21 on: May 08, 2019, 08:12:20 pm »
Note: the following line
 text [Dm[ text
will go wrong.

Think about it, you should be able to figure it out.

Hint: It goes wrong, when it believes that the [ is ttOpenForChord. It is not, because what follows is not a valid chord.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11164
  • Debugger - SynEdit - and more
    • wiki

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #23 on: May 08, 2019, 08:30:24 pm »
Note: the following line
 text [Dm[ text
will go wrong.

Think about it, you should be able to figure it out.

Hint: It goes wrong, when it believes that the [ is ttOpenForChord. It is not, because what follows is not a valid chord.

Okay... i will check this out.

Where did you see [Dm[
I don't see that in my file. Or are you just making a point, which already understood.

Thanks
« Last Edit: May 08, 2019, 08:33:18 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #24 on: May 08, 2019, 08:33:44 pm »
FYI... the tar file is corrupted, doesn't unzip
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Edson

  • Hero Member
  • *****
  • Posts: 1324
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #25 on: May 08, 2019, 09:04:45 pm »
There is SynFacilSyn which is a configurable Highlighter for the Lazarus SynEdit. It is by the same author as AtSynEdit, but it is for the real SynEdit.

Martin. The author of SynFacilSyn (me) is not the same author of AtSynEdit (Alexey) . We worked together in the SynFacilSyn adapter for ATSynEdit, but we are different persons  >:(

I have nothing to do with ATSynEdit (other way it would be EdsonSynEdit) and Alex has nothing to do with SynFacilSyn (otherway it would be SynAlexSyn).  %)

 I was thinking on creating a syntax editor but no time for that. I prefer to do compilers  :-X.

No criticism of your work intended (that is great!), but you do realize that most people on this forum Speak English.
It's no good to me.
Maybe it's because I'm not fluent writing books in english.   >:D
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11164
  • Debugger - SynEdit - and more
    • wiki
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #26 on: May 08, 2019, 09:32:13 pm »
Where did you see [Dm[
I don't see that in my file. Or are you just making a point, which already understood.

Just making a point. In case an invalid source is loaded.

re-uploaded the file as zip

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11164
  • Debugger - SynEdit - and more
    • wiki
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #27 on: May 08, 2019, 09:41:21 pm »
There is SynFacilSyn which is a configurable Highlighter for the Lazarus SynEdit. It is by the same author as AtSynEdit, but it is for the real SynEdit.

Martin. The author of SynFacilSyn (me) is not the same author of AtSynEdit (Alexey) . We worked together in the SynFacilSyn adapter for ATSynEdit, but we are different persons  >:(

I have nothing to do with ATSynEdit (other way it would be EdsonSynEdit) and Alex has nothing to do with SynFacilSyn (otherway it would be SynAlexSyn).  %)

 I was thinking on creating a syntax editor but no time for that. I prefer to do compilers  :-X.

Now I really owe you an apology. I am truly remorsefully inconsolably sorry.
The fact that I somehow took the 2 of you as a single person would explain why I wrongly was convinced SynFacilSyn was for AtSyn.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #28 on: May 08, 2019, 10:05:49 pm »
Maybe it's because I'm not fluent writing books in english.   >:D

Sorry, I said no criticism intended.
 :)

« Last Edit: May 08, 2019, 10:12:05 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 1260
Re: All Things SynEdit/Highlighter - Lyrics Editor
« Reply #29 on: May 08, 2019, 10:11:15 pm »
@MARTIN... THANK YOU!!

I would of never figured that out with docs and example code.

That looks very complicated.

Code: Pascal  [Select][+][-]
  1.  
  2. case FLineText[FTokenPos] of
  3.     '[': begin
  4.       inc(FTokenEnd); // after last char of token
  5.       // scan aheod that this is a known chord like DM
  6.       i := FTokenEnd+1;
  7.       while (i <= length(FLineText)) and not (FLineText[i] in ['[', ']']) do inc(i);
  8. s := UpperCase(copy(FLineText, FTokenPos+1, i - FTokenPos - 1)) ;
  9.       case UpperCase(copy(FLineText, FTokenPos+1, i - FTokenPos - 1)) of
  10.         'DM', 'BB','F','C':
  11.           FCurTokenType := ttOpenForChord;
  12.         else
  13.           FCurTokenType := ttBadOpen;
  14.       end;
  15.     end;
  16.     ']': begin
  17.       inc(FTokenEnd); // after last char of token
  18.       if FCurTokenType = ttChord then
  19.         FCurTokenType := ttCloseForChord
  20.       else
  21.         FCurTokenType := ttBadClose;
  22.     end;
  23.     else begin
  24.       while (FTokenEnd <= length(FLineText)) and not (FLineText[FTokenEND] in ['[', ']']) do
  25.         inc(FTokenEnd);
  26.       if FCurTokenType = ttOpenForChord then
  27.         FCurTokenType := ttChord
  28.       else
  29.         FCurTokenType := ttText;
  30.     end;
  31.   end;
  32.  
  33.  
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

 

TinyPortal © 2005-2018