Author Topic: Python "block comments"  (Read 3124 times)

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Python "block comments"
« on: October 30, 2017, 11:06:07 PM »
Unfortunately Python doesn't support block comments.
So, people tend to use block quotes, """ or '''.
Ugh, but they do.

Slick oughta recognize this and treat them as comments.

For example if I have
Code: [Select]
a='''hello'''then clearly, this is not a comment.
But, a stand alone string that isn't a function parameter or an assignment or otherwise part of an expression is a comment
Code: [Select]
'''
This is a block comment
'''


Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6826
  • Hero Points: 526
Re: Python "block comments"
« Reply #1 on: November 01, 2017, 02:50:58 PM »
A regex can do a good job for color coding. This will require regex’s which do look behind.

Dennis

  • Senior Community Member
  • Posts: 3955
  • Hero Points: 515
Re: Python "block comments"
« Reply #2 on: November 01, 2017, 11:37:59 PM »
Would it not be adequate to check if the triple-quote starts at the beginning of the line? 

Even if that would work, we would also have to watch out for this:
Code: [Select]
   a = (
     """
     This is a real string.
     Not a comment. 
     Strings should be strings.
     Comments should be comments.
     Don't cross the streams.
     Why?
     It would be bad.
     """
   )

« Last Edit: November 01, 2017, 11:43:09 PM by Dennis »

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: Python "block comments"
« Reply #3 on: November 01, 2017, 11:55:53 PM »
WRT beginning of the line: Unfortunately, no.
They can appear anywhere, so they must also get indented.

Worse yet doc strings: https://www.python.org/dev/peps/pep-0257/#rationale
So, if it is the first thing in a module, function or class, then it is assigned to the __doc__ attribute of the same.

So, does that count as a comment or a string?
I'd vote for comment - as it typically isn't going to be used by the script itself.


Dennis

  • Senior Community Member
  • Posts: 3955
  • Hero Points: 515
Re: Python "block comments"
« Reply #4 on: November 02, 2017, 12:02:34 AM »
By beginning of line, I was referring to first-non-blank, not literally column 1.

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: Python "block comments"
« Reply #5 on: November 02, 2017, 12:26:48 AM »
Right, of course :-)
Thats probably pretty good.

A new color coding tag might be in order?
block-string
raw-string might be good too.