Author Topic: Detecting if the current line is inside an {$IfDef} / {$Endif} Block.  (Read 1718 times)


  • Full Member
  • ***
  • Posts: 153
Consider this admittedly contrived bit of code:

Code: Pascal  [Select][+][-]
  1. {$Define Debug}
  3. {$IfDef Debug}
  4. {.$Define DebugXYZ}
  6. {$IfDef DebugXYZ}
  8. procedure XYZ;
  9. begin
  10.   {Some code here...}
  11.   Counter := Counter + 1; {first instance}
  12. end;
  14. {$Else}
  16. Procedure XYZ;
  17. begin
  18.   {Some other code...}
  19.   Counter := Counter + 1; {second instance}
  20. end;
  22. {$Endif}
  24. {$Else}
  26. Procedure OptimizedXYZ;
  27.    {Some code...}
  28.    Counter := Counter + 1; {3rd instance}
  29. end;
  31. {$EndIf}
  33. {$EndIf}

Code which is not active should be shown as dimmed. So assume the highlighter is working on the first instance of
Code: Pascal  [Select][+][-]
  1. Counter := Counter + 1;

To determine if the highlighting should be dimmed or not, it needs to find out the conditional context this line is in. If I can write a method that returns a list of pairs as in :

Code: Pascal  [Select][+][-]
  1. function ConditionalDirectiveContext(const aLineNumber: LineNumber): ;

Which returns a list of pairs as follows:
Code: Pascal  [Select][+][-]
  1. (
  2.   (xcfbt_IfDefDirective, 'DebugXYZ'),
  3.   (xcfbt_IfDefDirective, 'Debug'   )
  4. )

Then, I could quickly determine that the directive DebugXYZ is active but the directive Debug is not, therefore I should return a dimmed attribute.

That would do it.

If I could subclass TSynCustomCodeFoldBlock and add a text property called DirectiveName then
I should be home free.

But I'm a bit stumped. I know I can subclass TSynCustomCodeFoldBlock.I also know I can override override the GetRangeClass in TSynCustomFoldHighlighter to get replace TSynCustomHighlighterRange with my descendant.

Where I'm stumped is that I can't figure out how subclassing TSynCustomHighlighterRange would help. There doesn't seem to be a way to tell this class to use my subclass of TSynCustomCodeFoldBlock. What am I missing?


TinyPortal © 2005-2018