Looking for ideas on how to use TSynEditMarkup lazarus / 31-LCL ---------------------------------------------------------------------- Henppy · 2014-01-24 01:26 Hello gents and gals. I once again come to you for some pointers. I'm starting to mutilate my code with failed ideas and I think I need a new perspective. I have a TSynEdit derived component where I add markups via a TSynEditMarkupHighlightMatches descendant. Those marks are used to signal syntax errors with red underlines. The job of detecting such errors is done from a syntax checker class. This custom TMarkupHighlightAll descendant class allows the checker to pass coordinates directly so the marks are added to the FMatches list on a per point basis instead of using something like the SearchString property. This was my original approach: at each iteration of the syntax checker, remove all marks, then run the checker code and call the markup class to add a mark at specific points where the checker finds an error. Now, checking all the code all the time is not efficient at all because most of the time you don't need to recheck lines of code above the one you're working on because you didn't modify them, so the my syntax check only goes from the current line to the bottom. (not really, it uses the line index passed to the SetLine method in my custom highlighter which sometimes is not the current line for some reason, but just for the sake of the argument...) But now I have a problem. How do I maintain such a list of markups? The FMatches property of the TSynEditMarkupHighlightMatches is of course just a list. It doesn't know anything about lines, so now I can't just go and delete all markups waiting for the syntax checker to add them all again, because the checker can only add/remove marks from certain line to the bottom of the code. Also, since this is a graphic thing, I wanted to prevent the unnecessary removal of marks that will be added again a couple of milliseconds later. So I've been trying different ideas, none of them seems to work without extensive searches through multiple lists and stuff like that. I also tried looking into the TSynEditMarkupHighlightAll class to see what it did. That class uses the SearchString property to look for points to mark and somehow manages to understand when and where they are and whether they need to be updated/moved or not... But man, I couldn't understand a thing! So if you guys have any suggestion... I'm all ears. And as always, thanks for your time. ---------------------------------------------------------------------- Martin_fr · 2014-01-24 01:56 There are notifications within SynEdit which lines got edited. IIRC, the markup has even a virtual base method, to receive them. Including added, or deleted line count difference. So you only invalidate that range. Another approach is to only scan detail in the visible screen (combined with only those lines that changed. Keep 300 lines on either side for scrolling. More later.... The syntax checker, how much context does it need? Can it just start at any line, without knowing, or does it have to have extra info (like in pascal, you may be 100 lines into a "(*" comment, a syntax check would need to know that. Context info is best stored in the ranges of the highlighter. That can then be queried from the outside as needed. ---------------------------------------------------------------------- Henppy · 2014-01-24 02:09 It does have to look back at some point: {} blocks and ( ) in function calls with lots of arguments. Ranges are not necessary as comments and strings are single line. So no tokens spread in more than one line. My original idea was for the checker to look from the first line with errors downwards (from a previous run), or from the first modified line downwards, whichever is smaller (in terms of Y value). So it either checks for errors in new code, or reevaluates errors in old code and new code alike. The code I'm using has some 100 lines of code, given that the checker is not fully functional yet, it marked pretty much each and every line. And you could notice the marks going off and on line by line... . Sure 100 errors seems unrealistically high, but still, that shows me that I'm doing things the wrong way. ---------------------------------------------------------------------- Martin_fr · 2014-01-24 02:37 Yes, with 100 lines you should be fine doing full scans all the time. And if you see error going on off, that would mean you trigger paints in between. Does it Run in Begin/EndUpdate of the SynEdit? It should.  ---------- For {} and () you should use ranges. That is what they are for. With nestlevel you can find the opening line very quick. And {} can be foldable, while () would  not have to be foldable. Ok more tomorrow ---------------------------------------------------------------------- Henppy · 2014-01-24 03:05 Hmm... Interesting, I thought ranges were only used when a single token was multi line like a comment or string. I'll check them. The BeginUpdate / EndUpdate thing is new to me. I'll check that too. The syntax checker right now is independent of the highlighter. It's triggered every 500 milliseconds by a timer. I wasn't sure how to link the two things, but I'll have a look at those methods for sure. Thanks! ---------------------------------------------------------------------- Martin_fr · 2014-01-24 11:15 Ranges are used for begin/end in pascal. as well as (**){}. Even store ";2 rsAfterSemicolon => The next token is begin of a statement. You do not need to use a Timer. SyNEdit has OnChanged. But even better, Markup has. Code: [Select]        procedure DoEnabledChanged(Sender: TObject); virtual; // The Markup enabled property     procedure DoCaretChanged(Sender: TObject); virtual;     procedure DoTopLineChanged(OldTopLine : Integer); virtual;     procedure DoLinesInWindoChanged(OldLinesInWindow : Integer); virtual;     procedure DoTextChanged(StartLine, EndLine, ACountDiff: Integer); virtual; // 1 based     procedure DoMarkupChanged(AMarkup: TSynSelectedColor); virtual;     procedure DoVisibleChanged(AVisible: Boolean); virtual; All those will be called as thinks happen. Just override DoTextChanged(StartLine, EndLine, ACountDiff: Integer); And call the SyntaxChecker, and forward the needed lines. And that will be in BeginUpdate automatically. However that is on every KeyStroke. If you want to defer work,  then use Application.QueueAsyncCall, or start an IdleTimer, or... e.g the SyncroEdit, when you select text, must search for duplicate words, so it can display the icon, that it can be activated. But SElectAll of 10000 lines, and it would take too long. So it uses ASync. And then it does 100 lines, and if there is more, it schedules another ASync and returns leaving the jop pending. That means the user can continue edit,