Recent

Author Topic: Code Editor: HOW TO get different colors for operators and keywords?  (Read 2324 times)

Researching

  • Full Member
  • ***
  • Posts: 121
At present there is a category "reserved words" - with single color for keywords and operators.
I want to have:
color_1 for
<for ... to ... do>, <while...do>, <repeat ... until>, <if..else...><case.. of ... else>
color_2 for
<program/procedure/function begin...end>
color_3 (and colorization) for
___ <.Method>
What is possible and how to ?
« Last Edit: June 12, 2023, 04:23:41 pm by Researching »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9900
  • Debugger - SynEdit - and more
    • wiki
Re: Code Editor: HOW TO get different colors for operators and keywords?
« Reply #1 on: June 12, 2023, 04:49:04 pm »
Not too much....

You can have a look at "User defined Markup" (2 pages below the color settings).
It allows you to define colors for any word you want. But...
It is not context sensitive. If you define "procedure" to be e.g. clRed, then it will be red in code, comments, string, everywhere....
You can use priorities, so the color of comments and string take precedence over your settings. That will come pretty close to what you want. (for those keywords).

You can also (maybe..) use "Markup and Matches" > "Outline" and limit it to just those. (It wont do "program").

As for adding it... Not sure. What is the reason for this split.
I can see that it is "keywords in code" versus "keywords surrounding code".
Except "begin"/"end" => those are also in code "for ... do begin" is like "repeat". So then coloring "begin" in color_1 would be wrong?






For the method identifiers Foo.MyMethod => no solution at current.

As for implementing it... Probably join the list. Like "highlight type identifiers" or "goto labels".

Do you only want methods? I.e. functions/procedures?
Or also fields?

Because parsing Someword.OtherWord just looking for the "." in the middle is relatively easy. But it gets all sort of things.

Highlighting based on "Is it a function" is much more work. (and will internally go under "markup").
The highlighter only acts on syntax. It does not know if Foo.Bar is a function or field or .... Codetool knows. Markup could combine them. But its on the longtime wait list.


Researching

  • Full Member
  • ***
  • Posts: 121
Re: Code Editor: HOW TO get different colors for operators and keywords?
« Reply #2 on: June 12, 2023, 05:18:59 pm »
Let's see:
All highlighting is about easiness of code comprehension and code-error checking.
based on structure which is highlighted by colors.

If you want - try the OpenOffice/LibreOffice extension "Coooder" - this might help considering how much does it give or not.

If the syntax highlighter is separate from compiler - would be helpfull to know which files in IDE (?and how) to change to implement separate highlighting for optional tokens.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9900
  • Debugger - SynEdit - and more
    • wiki
Re: Code Editor: HOW TO get different colors for operators and keywords?
« Reply #3 on: June 12, 2023, 06:41:39 pm »
Let's see:
All highlighting is about easiness of code comprehension and code-error checking.
No doubt about that. But then "easiness" is very subjective. For example "being used to something" can make something easier, and at the same time that "something" could make it harder for someone else.

Of course having the option doesn't hurt... But having the option, needs to be done.
And more, it needs to be maintained afterwards.

And there are other - also useful - similar requests already (typenames /goto labels).
=> But I also have a very long list of useful (and/or also "of personal interest to me") things to do.

Quote
If the syntax highlighter is separate from compiler - would be helpfull to know which files in IDE (?and how) to change to implement separate highlighting for optional tokens.

It is different. And not only different from the compiler.

The broad overview

1) Compiler: Only for compiling
(Not for highlighting)

2) Codetools (part of the IDE): Parses the code for code completion, etc. Codetools actually understands the code (like the compiler). That is codetools knows if an identifier is a variable, function, method, ....
(Not for highlighting, but could be used in markup)

3a) Highlighter: Only syntactical parsing. That is it does not check if an identifier exist, or if it may be a variable or function.
3b) Markup: All kind of other Highlight


This may sound a bit over complicated but there are good reasons for it. Highlighting can continue, even if the code is broken (e.g. when you are in the middle of editing it).

Highlighting also is much faster than all the other codetools. Highlighting needs to update with every key stroke (well it could wait, but ...). I know most computers today are so fast it does not matter. But people do run the IDE on their Raspberry. And also on laptops, where it is not about the speed but battery usage.

Again: to be clear, none of that means there shouldn't be those features. It just says those features should not make the above uses impossible.



The highlighting of Procedure/begin vs for/repeat => that could be done in components\synedit\synhighlighterpas.pp
All the info is already there.

Only you did not make it clear if
- the "outline" solves your issue (the vertical lines can be switched off)
- the "user defined markup" (with correct priorities)? Because that has solved very similar issues in the past.
- the "begin" keyword of a procedure/function should be the same as "begin" of a "for ...to... do begin" block.

Also I fully acknowledge that it comes down to personal preferences, I still seek to understand. So I did ask for more details on how that helps.
Others may have similar needs, and if anything gets added, and without understanding the issue, I have a harder time judging what makes the most sense.



The "foo.method" => well I did ask, if it is just: any identifier after a dot?

Because, if yes, it is do-able in the Highlighter.

But if it is: only if it is a function/procedure/method call => then it needs the understanding of codetools.
That can not go into the Highlighter. But it can be done in a "Markup" which delivers the same end result.
This would be a new unit, a new Markup class.
You can look at the existing SynEditMarkup... units/classes. Like syneditmarkupfoldcoloring
None of them currently uses codetools. That is something new that needs to be done (it also means the code goes into the IDE, not into SynEdit).

But if it is done, you can have a color for lots of different things
- methods / functions
- type name
- goto labels
- local vars / params (if you want that)
....

As for codetools, I haven't got much info on how to get the required info from it. So I can't currently give any insight on that.








Researching

  • Full Member
  • ***
  • Posts: 121
Re: Code Editor: HOW TO get different colors for operators and keywords?
« Reply #4 on: June 12, 2023, 10:51:22 pm »
May be will answer in-detail later...

In short:
-- foo.method - yes just any word after dot is enough
-- about begin...end - let it be the same color in all cases,
Indeed, if it will be settable to be in different color for cases as
a) PROGRAM...TYPE...VAR...BEGIN... END;
b) For ... to ... do ... begin .. end;
c) while ... do begin.. end; with repeate ... until ....
d) if ... then ... begin ... end else begin end;
e) case ... of ..... 1: begin...end else begin end;
-- would be extremely convenient to check the corresponding closing "end".|
By now I have to print:
end; {end Program}
end; {end of FOR}
etc....

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9900
  • Debugger - SynEdit - and more
    • wiki
Re: Code Editor: HOW TO get different colors for operators and keywords?
« Reply #5 on: June 12, 2023, 11:42:26 pm »
-- would be extremely convenient to check the corresponding closing "end".|
By now I have to print:
end; {end Program}
end; {end of FOR}
etc....

https://wiki.freepascal.org/New_IDE_features_since#Outline_for_Pascal_Sources
https://forum.lazarus.freepascal.org/index.php/topic,30122.msg236054.html#msg236054

You can completely configure the colors (7 level before they repeat)
- for the keywords
- for vertical lines (also continuous or dotted)

You will immediately see what matches.

However, it does not (yet) have different colors for different types of blocks.
E.g. it would be nice if "try...except/finally" would have a different set of colors.



Btw, exclude "procedures" => then they will always be default color. While all the other blocks will get colors. (You can make that just one, but more than one imho is better)
« Last Edit: June 13, 2023, 12:08:03 am by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9900
  • Debugger - SynEdit - and more
    • wiki
Re: Code Editor: HOW TO get different colors for operators and keywords?
« Reply #6 on: June 13, 2023, 12:26:09 am »
Here is how it looks / how to set it up
(I have a couple of mismatched line colors that differ from the words => my setup only)

2nd picture:
- Check "Outline Global"
- Foreach keyword in the list set the "Outline" checkbox, if you want it to be highlighted

3rd picture
- Choose color for keywords
- choose color/style for vertical lines
- up to 10 times to get different colors




The highlighted "exit" is "user defined markup"
« Last Edit: June 13, 2023, 12:28:36 am by Martin_fr »

Researching

  • Full Member
  • ***
  • Posts: 121
Re: Code Editor: HOW TO get different colors for operators and keywords?
« Reply #7 on: June 13, 2023, 01:20:58 am »
Tanks to all !
The Markup and matches pointed by Martin_fr - has got the point.
Maybe I did not notice if someone else has pointed this before.

Anyway, if someone can tell what and where to change in the source of Lazarus to make two separate highlighting sections for keywords and operators - this would be nice.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9900
  • Debugger - SynEdit - and more
    • wiki
Re: Code Editor: HOW TO get different colors for operators and keywords?
« Reply #8 on: June 13, 2023, 08:46:16 am »
Anyway, if someone can tell what and where to change in the source of Lazarus to make two separate highlighting sections for keywords and operators - this would be nice.

components\synedit\synhighlighterpas.pp

Find the names you want to change
Code: Pascal  [Select][+][-]
  1.   if KeyComp('Begin') then begin
  2. ...
  3.     Result := tkKey;
  4.  
You need a new enum/constant, to replace "tkKey".


And you need a new attribute like
Code: Pascal  [Select][+][-]
  1.     property KeyAttri: TSynHighlighterAttributes read fKeyAttri write fKeyAttri;
Find all places where there is "fKeyAttri" and duplicate with your new Attribute (and new key)

Somewhere is a function that maps the "tkKey" into the attribute => map the newly added. "function TSynPasSyn.GetTokenAttribute: TSynHighlighterAttributes;"


Depending on what exactly you want to do there may be more. But it should be in that file.
(I think the Option dialog will detect the new color, but if not "ide/EditorOptions" and ide/frame/editer....color...)

« Last Edit: June 13, 2023, 08:47:58 am by Martin_fr »

Researching

  • Full Member
  • ***
  • Posts: 121
Re: Code Editor: HOW TO get different colors for operators and keywords?
« Reply #9 on: June 14, 2023, 07:10:54 am »
Thank you, Martin_fr.
At certain moment this seems too complicated for me - indeed of the too poor knowledge of FPC ObjPascal.
May be later...

 

TinyPortal © 2005-2018