patchGNU nano - Patches: patch #9801, highlighting empty strings in...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

patch #9801: highlighting empty strings in Python

Submitter:  Ryan Westlund <yujiri>
Submitted:  Thu 02 May 2019 04:22:46 PM UTC
   
 
Priority:  3 - Low Status:  Done
Privacy:  Public Assigned to:  bens
Open/Closed:  Closed Release:  None

Jump to the original submission

Sat 11 May 2019 05:12:15 PM UTC, comment #8: 

The change has been pushed to master, commit 8f1cd50f.  Thanks for reporting.

Benno Schulenberg <bens>
Group administrator
Sat 11 May 2019 03:49:12 PM UTC, comment #7: 

I understand now how it works.  It is a combination of how regexes work and how nano processes them.  A regex matches at the first possible point, and then matches the largest amount of characters possible.  So the first part of the changed regex would match an empty string ("") but the second part will match a triple quote ("""), so the latter part is chosen, when possible.  And when a regex has matched something, then nano will start a search for a new match after the full length of the last match.  This means that matches cannot overlap.  And this means that in this case the final quote of a triple can never be the starting quote of a normal mono-quoted string.  This is what makes the premature-coloring prevention work.  It would not work if the regex were divided over two separate rules like this:

color brightgreen ""([^"\]|\\.)*""
color brightgreen """""

The small mentioned issue (that the first "" of a closing triple quote get colored like an empty string while the triple is being typed) can be prevented too, with a rule like this:

color normal start=""""" end=""""

But I don't think it's worth the trouble to add such a rule (rules with start= and end= are expensive, computationwise), as it's just a momentary miscoloring, and, while a docstring is being typed, words like "for" and "if" will be colored too, until the closing triple is typed.  So... I think that the rules proposed in comment #5 are good enough.

Benno Schulenberg <bens>
Group administrator
Fri 10 May 2019 03:57:21 PM UTC, comment #6: 

Huh, not sure why it makes a difference to remove the last parts of the first two rules. Regexes will always mystify me.

But I think in terms of functionality the rules you posted seem great (I got the same results as you). It seems like a small improvement to not color an unmatched triple quote, since it's like an unmatched single quote, and the only thing I feel is remotely dissatisfying about the result is coloring the final "" in an incomplete docstring. But that's such a small deal I don't think it matters since it would only come into play while typing the ending of a docstring and probably wouldn't even be noticed by some people.

Ryan Westlund <yujiri>
Mon 06 May 2019 07:27:42 AM UTC, comment #5: 

I'm thinking of changing the rules for mono-quoted strings to:

color brightgreen "'([^'\]|\\.)*'|'''"                                         
color brightgreen ""([^"\]|\\.)*"|""""                                         
color normal "'''|""""

With the latter rule, triple quotes become uncolored again.  This seems to make the |''' and |""" parts of the first two rules superfluous, but strangely things work differently without them.

Without them, when typing """ Docstring ", the Docstring plus the final " get colored, which is strange and not right.  With them, in """ Docstring " nothing gets colored, and in """ Docstring "" only the final "" get colored, and only when typing the third " does the whole """ Docstring """ get colored.

What do you think?

Benno Schulenberg <bens>
Group administrator
Sat 04 May 2019 01:39:49 PM UTC, comment #4: 

Ah, you're right. I was testing in a file with triple double quote docstrings farther down. They behave the same for me (the way single quotes did) in an empty file. (I'm on version 4.2).

Ryan Westlund <yujiri>
Fri 03 May 2019 04:06:20 PM UTC, comment #3: 

I don't see any difference between using triple single quotes (''') and triple double quotes (""") when making docstrings.  Maybe your tests gave different results because you have triple single quotes further down in the file but not triple double ones?

Which version of nano are you using, by the way?

That a docstring of a single character colors also everything between this docstring and the next docstring, I hadn't noticed before.  But... I think this is an acceptable weirdness: a docstring should really contain at least a sentence.

Why those backslashes are there, I don't know.  They were added five years ago, in commit b3306b22, to improve the behavior of multiline coloring.  But at that time the multiline coloring still contained some bugs, which were fixed only years later.  Probably the backslashes are superfluous now.  They ought to be.

Benno Schulenberg <bens>
Group administrator
Fri 03 May 2019 01:10:43 PM UTC, comment #2: 

Ah, that's something I hadn't noticed before. Apparently it only happens with triple quoted strings using single quotes. Your suggestion seems to fix that issue without causing any additional problems.

Playing with triple quotes some more, I've noticed that a triple-quoted string using double quotes colors the rest of the text below it if it has less than two characters inside, whereas a triple-quoted string using single quotes seems to not color inside itself when it has only one character.

I also can't find any situation that's changed by removing the backslashes on quotes in the regex for double-triple quotes.

Ryan Westlund <yujiri>
Fri 03 May 2019 08:00:57 AM UTC, comment #1: 

The reason for not highlighting empty strings is that it would make the starting triple quote of a doc string look weird while you are typing the string: the first two quotes would be colored but not the third.

But I agree that not coloring empty strings is kind of wrong.  And things getting miscolored when there are two empty strings on the same line, that is bad.  The latter will have to trump the little weirdness of partially colored triple quote.

Maybe that weirdness could be taken away by explicitly coloring triple quotes, like this:

color brightgreen "'([^'\]|\\.)*'|'''"                                         
color brightgreen ""([^"\]|\\.)*"|""""

What do you think?

Benno Schulenberg <bens>
Group administrator
Thu 02 May 2019 04:22:46 PM UTC, original submission:  

The syntax highlinging module for Python doesn't highlight empty strings. This looks ugly and can get really messed up when you have eg. two empty strings on one line; nano highlights the text between them as green. I found a simple fix. I'll attach the git diff output, since that seems to be how the other patches here are doing it.

Ryan Westlund <yujiri>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #46860:  diff added by yujiri (505B - application/octet-stream - Patch for syntax/python.nanorc)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by bens (Posted a comment)
  • -email is unavailable- added by yujiri (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 7 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-09-11 bens Open/ClosedOpen Closed
    2019-05-11 bens StatusIn Progress Done
    2019-05-11 bens StatusNeed Info In Progress
    2019-05-03 bens StatusNone Need Info
        Assigned toNone bens
        SummaryFix syntax highlighting for strings in Python highlighting empty strings in Python
    2019-05-02 yujiri Attached File- Added diff, #46860

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code