Jump to content
Clément

IDE extension to fade inactive {$IFDEF} {$ELSE} {$ENDIF} block

Recommended Posts

Hi,

 

Lately I've been using a lot more $IFDEF than I use to. And modifying code in the wrong block is what worries me, since there's no difference between active and unactive block.
For example:
{$IFDEF USE_FEATURE}

{ .. procedures and functions using a feature }

{$else}

{ .. procedures and functions not  using this feature }

{$endif}

 

USE_FEATURE might be defined as a project conditional define or in an included unit.( that really depends on the project)
Anyway, I would like to know if there is an extension that would use syntax highlight in the {$IFDEF} block when USE_FEATURE is defined, fade the {$ELSE} block, and of course if USE_FEATURE is not defined, the $ELSE block would be colorful and $IFDEF block grayed out.

 

TIA,

 

Edited by Clément
  • Like 8

Share this post


Link to post

I'm not aware of one.

The first thought is a al CnPack and Parnassum and hack the editor and draw of the top of it but on reflection there may be an easier way, you could write an enhanced highlighter for the IDE and use that instead of the built in one.

Share this post


Link to post
Guest

I usually use the debug dots when I need to be sure. Nice suggestion of its doable. 

Share this post


Link to post

Having written a few highlights for the IDE for other scripts I know it could in essence be done however the highlighter would need to know more about what it is parsing than just the line of code it currently expects, i.e. what IFDEFs are in play. For those defined in the Project Options not so bad but for those that may be define in code it's more difficult as the highlighter would have to pre-parse the code. This would be a performance issue if not handled in a background thread, i.e. parse some code (assuming the current module only not the project and place a list of active defines in the wizard that the highlighter can use). One other thing would be that the list of colours / fonts the IDE uses is fixed so the colour for the disabled IFDEF section would have to be configurable by the wizard interface.

I'll caveat all of the above with - this is just theory :classic_biggrin:

  • Like 1

Share this post


Link to post
On 7/26/2019 at 7:08 PM, Clément said:

USE_FEATURE might be defined as a project conditional define or in an included unit.( that really depends on the project)
Anyway, I would like to know if there is an extension that would use syntax highlight in the {$IFDEF} block when USE_FEATURE is defined, fade the {$ELSE} block, and of course if USE_FEATURE is not defined, the $ELSE block would be colorful and $IFDEF block grayed out.

I'm not aware of any extensions with such effect. May be mighty LSP will bring this feature (no).:classic_rolleyes:

I-Pascal (Pascal support plugin for Intellij IDEA which I develop) does this.

Also I think I saw Lazarus does this too but can't find it in 2.0.2.

And I agree with @David Hoyle - it's not an easy to implement feature.

  • Like 1

Share this post


Link to post

Not an easy job, but would be very useful. Let's see what LSP will bring, and decisions shall be made.

Share this post


Link to post

Not that it helps you here, but I try to separate code into separate units, as much as possible.
So that I can bind the right routines like here:

{$DEFINE ___USE_VERSION1}
{$DEFINE _X_USE_VERSION2}
{$DEFINE ___USE_VERSION3}

uses
    ...
{$IF     DEFINED _X_USE_VERSION1}    
    Feature.Version1
{$ELSEIF DEFINED _X_USE_VERSION2}    
    Feature.Version2
{$ELSEIF DEFINED _X_USE_VERSION3}    
    Feature.Version3
{$ENDIF}    

Which makes matters much more clear and readable.

 

And YES: I also would like to see if I'm in the active part of an IFDEF, good feature.
Wouldn't like to see it as comment, but as a kind of comment with slightly different color (maybe darker green), that would be OK for me.

  • Thanks 1

Share this post


Link to post

I haven't used it really, but isn't code folding meant for exactly that use case? There is code that you don't want to see, so fold it away.

Of course automating that would be really nice, which brings us back to the parsing requirement.

Share this post


Link to post
10 hours ago, dummzeuch said:

I haven't used it really, but isn't code folding meant for exactly that use case? There is code that you don't want to see, so fold it away.

Of course automating that would be really nice, which brings us back to the parsing requirement.

{$IFDEF} doesn't have code folding per se.
I need to do some extra writing:

{$IFDEF USE_FEATURE}
{$REGION "USE_FEATURE"}
// changes in methods to use feature(s)
{$ENDREGION}
{$ELSE}
{$REGION "NO_FEATURE"}
// Original code
{$ENDREGION}
{$ENDIF}

Even so, this will not beat a fancy parser.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×