Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Automatic coloring of comments in the code


  • Please log in to reply
59 replies to this topic
majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
I think this is something very important for this forum

It should be changed so comments that reside in the "code" "/code" section are colored automaticaly. The color can be set in options or can be hardcoded.

That will make scripts samples much readable, and understandable since you could understand separated parts of the script more easily.

Something like this: http://www.autohotke... ... 5333#75333

______________________________________________________
Current version of script:

Summary of my javascript, PhiLho copied my original code so most of it should apply to his:
Widely supported and cross-browser DOM compatible
Uses basic regex that all browsers should be able to handle
Predeclared and reused vars for speed
Only parses defined tags with the specified class (cont and cls vars) for memory utilization
Portable - use with the forum, docs and even your own site
Everything I mentioned earlier to Chris with added support for Opera's bad regex handling
Parses each code block in turn to prevent unresponsiveness
Highly compressible - 250 bytes with gzip lvl 2
Other benefits that I haven't thought of yet..
Titan's code             Philho's code:
Posted Image

BoBo
  • Guests
  • Last active:
  • Joined: --

or can be hardcoded

Nah :x

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
So, your only commentary is related to most un-important thing?

nah...
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Already mentioned/discussed there (and below):
http://www.autohotke... ... 4657#64657
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I'd like to see it too someday. The main problem is (as PhiLho is aware through his labors on SciTE) that the AHK syntax is difficult to parse. So building a coloring module for phpBB would be difficult. Even if it were achieved, it might make many mistakes that would cause coloring of unintended words, which would detract from the value.

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Well, perhaps a simplistic lexer can be done. As majkinetor suggested in his subject, even coloring only comments could be useful.
Mmm, even this is not so simple, we still have to watch out for escapes, continuation sections, strings, etc.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Well, I meant coloring only comments not everything.

PhilHo, that is the right reason to be concerned but I still think it is not too hard work... I don't know PHP but searching for comments in the text is something students usualy have to do for homework in first year of programming.

Also, I suspect that such representation of coded examples will influence the users to comment their code well
Posted Image

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Oh, I get it. I should have read your post more clearly.

I agree that coloring the comments differently than the code would a great thing to have. In fact, it's a brilliant idea. Furthermore, I'd like to apply the same coloring scheme to all the examples in the help file, if possible.

Since green is a "whitish" color in the spectrum, it seems to me to be slightly harder to read than other "blackish" colors like blue and purple. In addition, green seems to be the traditional color for comments in IDEs like Visual Studio (and they usually study ergonomics to decide such things). For this reason, perhaps comments should be dark green and everything else in the code section (except where explicitly overridden by a [color] tag) should be black, purple, blue, or some other blackish color.

If anyone has time to work on this, I'd greatly appreciate it. Otherwise, I will add it to my list because I really love the idea.

Thanks.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
You are welcome.
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

I don't know PHP but searching for comments in the text is something students usualy have to do for homework in first year of programming.

It is not the question of knowing PHP but knowing AHK here...
; This is a plain comment, easy to find
a = Foo ; Another simple comment, all right
b := "Foo ; This becomes a bit tricky"
c =
(
Continuation section
; This line ISN'T a comment...
)
/*
OK, these comments are straightforward to handle.
*/
d = "Foo ; This IS a comment..."
MsgBox e := "Foo ; This IS a comment too!"
Not so hard, not so simple, just a bit of work for tracking the current state and handling tricky cases. Much simplier than a full lexer, indeed.

Note: actually, unlike what I expected, the 'b' line raises a syntax error...
Bug or real intend, I don't know.

Chris, personnaly, I dislike using green for comments, I always change this setting in my editors to get some shade(s) of grey. I find this less distracting.

Along with comments, we could add support of commands and directives, although this is harder, and would probably lead to highlighting them in comments and strings, which isn't so problematic.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

b := "Foo ; This becomes a bit tricky"

That should be an error. It is not a bug.
This is not:
b := "Foo `; This becomes a bit tricky"

d = "Foo ; This IS a comment..."

Huh.... this one is bad .... I thought at first that it is also an error for the same reason as in b (I didn't test it thogh) but I remember some sentence in the help that there is some special case for ; and I suspect this to be that one.

MsgBox e := "Foo ; This IS a comment too!"

This is a comment ?
Who write comments like that :)

Along with comments, we could add support of commands and directives, although this is harder, and would probably lead to highlighting them in comments and strings, which isn't so problematic.

I agree, but please, don't insist since we already achieved something and this can turn Crhis off road.:)
One step at the time.
Posted Image

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I forgot to add that although I really like the idea, it would take me away from development of other AHK features. Due to site improvements and support tasks, I've been unable to add any significant new features to AHK for nearly four months now. That's why I was hoping for a volunteer.

Chris, personnaly, I dislike using green for comments, I always change this setting in my editors to get some shade(s) of grey.

Grey is good too and fits my green-theory because it's a color that has been skewed toward white and is thus (intentionally) a bit harder to read. We could have a poll to decide the defaults.

Along with comments, we could add support of commands and directives, although this is harder, and would probably lead to highlighting them in comments and strings, which isn't so problematic.

This would be good, but I'm wary of scope-creep. For now, perhaps it should be limited to comments to make the work less daunting.

The help file currently uses
 sections for all code examples. Assuming 
 doesn't allow any kind of coloring or CSS inside it, this would have to be changed (which might be a good idea anyway because it might also allow a custom tab stop to be used via   or other method). If anyone has ideas about this or would like to work on it (or the forum mod), please let me know. I believe phpBB already has a syntax highlighting mod/framework to which the syntax rules for a language can be applied.
					
					

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

b := "Foo ; This becomes a bit tricky"

That should be an error. It is not a bug.
This is not:
b := "Foo `; This becomes a bit tricky"

I know, but in most languages I know, a comment symbol inside a string is just some more ordinary chars:
a = "foo // bar" ; Lot of languages...
a = 'foo -- bar' ; Lua
a = "foo ' bar" ; VB
$a = "foo # bar" ; Perl
and so on, that's why I was surprised.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

I forgot to add that although I really like the idea, it would take me away from development of other AHK features.

Well, Chris, relax a little, take your time, nobody will crossify you if you don't do your homework. After all, this should be something that you like to do and enjoy it. Don't make this important in the way you can't sleep cuz of it... I would like too see improvements in AHK but the last thing I want to see is you in depression because you are only a human.

So, the directive from Belgrade is to relax ! :D
Posted Image

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

a comment symbol inside a string is just some more ordinary chars

I never pay much attention to comments syntax but you are probably right, and most of all it is logical to be that way.

2Chris
Are you sure that
 doesn't use CSS ?
I would be quite surprised if it doesn't....

This is what I found googling:
[url=http://www.netmechanic.com/news/vol6/PRE_example_no9.htm]http://www.netmechanic.com/news/vol6/PR ... le_no9.htm[/url]

Source will show this:
pre
{font-weight:bold; color:maroon;}

Posted Image