Recent

Author Topic: How to color boolean values (true, false) in the IDE  (Read 2153 times)

indydev

  • New Member
  • *
  • Posts: 21
How to color boolean values (true, false) in the IDE
« on: March 02, 2021, 07:21:44 pm »
Numbers and Strings (including char values), are colored in the IDE (or can be), but boolean values are not.  I would really like to change that in the IDE. I have looked, and even tried to find a way to force the words 'true' and 'falses to be colored, but can't seem to find a way to do that.

If anyone knows how to do that, I would appreciate some direction.

Thanks.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7440
  • Debugger - SynEdit - and more
    • wiki
Re: How to color boolean values (true, false) in the IDE
« Reply #1 on: March 02, 2021, 08:23:00 pm »
There is no perfect way.... But there is a way.

The Highlighter itself does not do that feature. In fact, it can not... But more later.

First
Code: Pascal  [Select][+][-]
  1. var true: integer;
  2. begin
  3.   if true = 99 then Foo;
  4.   if true = intFuncBar() then Foo;
  5. end.
  6.  
Perfectly valid code.

True and False are normal identifiers. They are not keywords.

And that is the crux, the highlighter is not a full pascal parser. It could see that in "var true: integer" the identifier true is not the constant you expect to be highlighted.
But on the next line "if true = 99" it would no longer know that, and assume "true" should be highlighted (if it did handle true).
The HL can also not be extended for that, because the "var true: integer" could be in another unit. And the HL does not look across unit boundaries. Codetools do.

Sure HL (or more likely Markup / technical distinction) could learn to work with codetools (like it is done for IFDEF). But currently that is not the case.



Yet you can highlight true and false. So long as you do not mind that it will include the false positives.

Tools > Options: Editor > Display > User defined markup
https://wiki.lazarus.freepascal.org/IDE_Window:_Editor_User_Defined_Words

You can highlight any word you like.
Create a new list, add the 2 words (match bounds), set the color. And they while be highlighted.

----
Well there is one more step needed. The above will highlight them. But it ill also find them in strings and in comments. Probably not what you want.
So in the list when you set the color, there is a field "priority". Set it to 1.

Then go to the Editor > Display > Color page.
- Find the entries for String and Comment.
  For each of them
- Make sure they have a Foreground color defined.
  (Assuming that your highlight changed the foreground,
   if you true/false HL changed the background or border, then those need to be defined for String/Comment too.
   Even if they are the same as for normal text (i.e white or black, whatever your general editor background is
  )
- Below "Element Attributes" you find a button "Priorities", it changes the page (like a tab). 
   Find Comment/String and move them to priority = 2

Now the Highlight will be as follows.

Source code HL has prior 0
True/False has prior 1 and overrides that
String/Comment has prior 2 and overrides true/false.

Other HL, like selection have even higher prior, and work as they always do.
« Last Edit: March 02, 2021, 08:24:37 pm by Martin_fr »

indydev

  • New Member
  • *
  • Posts: 21
Re: How to color boolean values (true, false) in the IDE
« Reply #2 on: March 04, 2021, 12:55:10 am »
Wow. I see why this is not so simple. Interesting that true and false are not included as keywords. I would never think to use them as anything other than boolean values.

Thank you for the explanation, and the solution.

 

TinyPortal © 2005-2018