language-python icon indicating copy to clipboard operation
language-python copied to clipboard

Docstrings with SQL-like words in them break highlighting

Open birdonfire opened this issue 10 years ago • 21 comments
trafficstars

There's a problem with the syntax highlighting when viewing this file: https://github.com/18F/rdbms-subsetter/blob/master/subsetter.py

Something in the docstring at the top of the document is causing the problem. Perhaps something to do with the RST markup?

The syntax highlighting on the GitHub page suffers the same problem.

There are no actual syntax errors in the document, and the application code runs just fine.

SublimeText's highlighter renders it fine.

birdonfire avatar Oct 21 '15 15:10 birdonfire

If I lower-case the word "INSERTS" on line 43, it totally fixes the highlighting.

birdonfire avatar Oct 21 '15 15:10 birdonfire

It seems that the highlighter interprets the upper case INSERTS as the beginning of a SQL block that isn't then closed.

<div class="line cursor-line" data-screen-row="42">
    <span class="source python">
        <span class="string quoted double block sql python">
            rdbms-subsetter only performs the
            INSERTS; it
            <span class="string quoted single sql">
                <span class="punctuation definition string begin sql">'</span>
                s your responsibility to set
            </span>
        </span>
    </span>
    <span class="invisible-character">¶</span>
</div>

If I lower-case INSERTS, this block now looks like:

<div class="line cursor-line" data-screen-row="42">
    <span class="source python">
        <span class="string quoted double block sql python">
            rdbms-subsetter only performs the inserts; it's your responsibility to set
        </span>
    </span>
    <span class="invisible-character">¶</span>
</div>

birdonfire avatar Oct 21 '15 16:10 birdonfire

I'm experiencing the same issue, where we are extracting docstrings for usage in our API documentation, and therefore we are using markdown formatting inside the docstrings:

"""Delete Resource
`DELETE http://some-url.com/resource`
"""
def destroy(self):
    pass

Which messes up the rest of the file (and here as well, where I expected pass to be highlighted, and DELETE not to be).

To me it seems as the rule named string.quoted.double.block.sql.python is the cause of this behaviour, and I've been fiddling around with it for a while but haven't really understood the grammar definitions yet.

It seems as the rule accepts additional characters apart from only whitespace between the intial """ specified by begin, and the beginning of the SQL statement specified by the keywords + whitespace, but have not come any farther than that.

amlinger avatar Nov 25 '15 15:11 amlinger

Any progress on this?

I just ran into the same problem.

Seems the syntax file starts the SQL context and only closes it when it encounters three double quotes, which basically eats them and leaves nothing for the commend to be closed.

hvdklauw avatar Mar 29 '16 08:03 hvdklauw

I'm getting this for DELETE as well

):
    '''
    Accounts
    ###**Actions**:
      - `GET /:id/card`
      - `POST /:id/card`
      - `PUT /:id/`
      - `DELETE /:id/`
    '''
    serializers = {

highlights everything from Accounts to the end of the file.

morenoh149 avatar Jul 18 '16 21:07 morenoh149

I just stumbled upon this, it seems that

"""'SELECT'"""

is enough to trigger that bug.

krzysztof-sikorski avatar Dec 06 '16 09:12 krzysztof-sikorski

I figured out the cause, and am working on a solution but would love ideas. It's because when the rule is applied for the triple quote sql strings, it runs include: source.sql inside the matched pattern. That is why the html classes that show on the text after a single ' or " come from the language-sql grammar. So the actual root cause is the language-sql grammar seeing the beginning of a sql string and then never finishing itself. If anybody knows how to ignore the internal SQL strings inside of certain parts of triple quoted strings, or knows how to make it so it can't override that outer """, let me know.

UPDATE: A proposed solution: Write a package on top of the python grammar that makes python use a slightly changed SQL grammar, but only in python files (so we don't mess with SQL everywhere else). That changed grammar only has one changed thing: SQL strings don't just end on ', but also \n. If people are okay with no multiline strings in SQL in python files then this solution would work perfectly. Let me know if anyone wants this and I can write it.

dborstelmann avatar Dec 13 '16 22:12 dborstelmann

@dborstelmann Your proposed solution sounds reasonable to me, and I would definitely find it helpful.

jdw1996 avatar Feb 04 '17 04:02 jdw1996

@dborstelmann have you managed to write up your solution yet? It would definitely be useful!

nckswt avatar Apr 03 '17 20:04 nckswt

@nckswt @jdw1996 Sorry I've been super busy recently, it looks the above reference will fix it? Does that work for you guys?

dborstelmann avatar Apr 08 '17 12:04 dborstelmann

@dborstelmann True, the above PR should fix it, but some things to be aware of:

  1. It's probably going to take a while to be reviewed,
  2. I'm not even sure if that will get merged,
  3. We're probably still going to switch over to MagicPython in at least the short term

winstliu avatar Apr 08 '17 15:04 winstliu

@dborstelmann I didn't look too closely into that PR, but on @50Wliu's suggestion, I've now switched to MagicPython.

jdw1996 avatar Apr 08 '17 21:04 jdw1996

Swapping over to MagicPython to try it out as well. Thanks!

nckswt avatar Apr 10 '17 14:04 nckswt

Lemme know if you guys still want this, but for now I will assume MagicPython is a good stopgap.

dborstelmann avatar Apr 10 '17 17:04 dborstelmann

Thank you, the highlight issue happened for me too because of a capitalized WITH in a docstring. Switching to MagicPython did the trick for me (just install and it works).

alisianoi avatar Jul 26 '17 14:07 alisianoi

MagicPython helped me too ! (In my case a SOFT_DELETE in a docstring)

Hellzed avatar Aug 02 '17 01:08 Hellzed

The SQL keywords can actually appear anywhere within a word within a docstring and cause trouble. Example: `ALL_ITEMS_WITH_DATA` breaks highlighting for me. Of interest, it breaks highlighting in a different manner than just ALL_ITEMS_WITH_DATA does.

Wrapping the word in backticks causes all the text in the file following the backtick-enclosed word to appear commented. Including the word alone causes any following SQL keywords in the docstring to become highlighted.

ezarowny avatar Aug 23 '17 21:08 ezarowny

Just to pile on, I experience the same issue: screenshot 2017-12-22 10 21 30 MagicPython fixed it, but this really shouldn't be an issue...

goi42 avatar Dec 22 '17 09:12 goi42

Experiencing the same issue here. Someday...someday... screen shot 2018-04-17 at 7 47 21 pm

7inyu avatar Apr 18 '18 02:04 7inyu

Bumping...

image

emptyopen avatar Jun 06 '18 17:06 emptyopen

Easy fix perhaps, not sure whether this 2015 issue is still alive: paste it below the docstring as a commented block.

lorenzznerol avatar Mar 31 '22 12:03 lorenzznerol