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

jporkkahtc

  • Senior Community Member
  • Posts: 1684
  • Hero Points: 162
  • 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: 4560
  • Hero Points: 353
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

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2543
  • Hero Points: 374
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: 1684
  • Hero Points: 162
  • 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

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 2543
  • Hero Points: 374
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: 1684
  • Hero Points: 162
  • 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.