Author Topic: SynEdit - improved highlighters to handle Code Folding?  (Read 16138 times)


  • Newbie
  • Posts: 2
SynEdit - improved highlighters to handle Code Folding?
« on: November 07, 2009, 06:21:51 pm »
 I considered porting my application from Delphi to Lazarus (or at least creating it's fork compatible with Lazarus, my app is too much dependent on JVCL). One of the main reasons is SynEdit which handles CodeFolding and other cool features (comparing to Delphi original version). This however seems to be a half-true, since I have no idea how to make code folding for HTML, PHP etc ... ? Did anyone found out how to make code-folding for languages other than pascal?

Thanks in advance!


  • Administrator
  • Hero Member
  • *
  • Posts: 10005
  • Debugger - SynEdit - and more
    • wiki
Re: SynEdit - improved highlighters to handle Code Folding?
« Reply #1 on: November 07, 2009, 08:11:56 pm »
Well first of all, which version of Lazarus do you use?

You should not attempt before 0.9.28. and if you do serious changes you may consider changing to SVN (though currently the highlighter code has not changed since 0.9.28)

First you must move the exiting highlighter to be a subclass of TSynCustomFoldHighlighter

You can look at how the pascal highlighter implements the methods introduced there. But you will not need all the bits that the pascal highlighter does.

Pascal supports 3 independent folding areas (begin/end - ifdef - region) => those 3 can overlap, they are not always nested.

in html/xml and many others, you only have one level, since all folds will be nested (a fold started inside another fold, will always end inside the fold in which it was started)

Now I can only give you hints here. A full description would never fit.

It is up to you how you store the folds (except for it must be suitable to work with very large files (the pascal highlighter is tested on source code with 300000 lines)

You must consider that the highlighter must scan each time a keystroke has happened


The general idea you need to support is that each line must know:
- how deep it is in the fold nesting (at the end of each line IIRC)
  Your text starts with 0 nest level at the begin of line 1
  - if no fold open in line 1 then at the end of line 1 it is still 0
  - each time  a fold opens it goes up one, each tim a fold closes it goes down one
- you also must store the smallest fold level present on the line
   writeln(1); // fold-level = 3
 end else begin // fold-level still 3 (again)

but in-between the foldlevel went down to 2.

This info is required, otherwhise the "end begin" can not be found


When you had a look around , you may ask specific questions.

in pascal you will find 2 sample
1) using TopCodeFoldBlockType, StartCodeFoldBlock , ....
This stores additional info on the type of the fold. (is it a begin end, a repeat until, a var,const, .....

Pascal needs that, to identify if a codefold block should be closed.
procedure a;

 if the parser hits the begin, it must close the "var" fold block. But not every begin is preceeded by a "var" block, so "begin" alone does not tell the parser to close the block. it must look at the type of the surrounding block

Now be warned, in html this an be nasty, if every tag (body, html, td, tr, p, ...) is a type of it's own => you have to many types, the data structure will explode and the whole thing will slow down to standstill

You can either map it to a small set of types, combining groups of tags.

you just say opening and closing tags, and you do not care if they match

The later case is easy, you just need to store 2 numbers on each line:

Sorry, it's not particular clear => but you need start looking at the code, and try to understand from there
(since I am not going to write a complete tutorial)


  • Administrator
  • Hero Member
  • *
  • Posts: 10005
  • Debugger - SynEdit - and more
    • wiki
Re: SynEdit - improved highlighters to handle Code Folding?
« Reply #2 on: November 11, 2009, 04:04:03 am »
Sorry, the last answer may have been more confusing than helpfull.

I have started a wiki page.
It isn't complete, and given time (or the deficit thereof), it may never be.

In any case, you will have to read the code. you can then discuss your ideas, before going to implement anything. This will help keeping them in line with possible future changes)

« Last Edit: November 11, 2009, 04:29:53 am by Martin_fr »


TinyPortal © 2005-2018