microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.7k stars 769 forks source link

Syntax highlighting does not support negative literals #6267

Open hologerry opened 1 month ago

hologerry commented 1 month ago

I am using latest VS Code and Python/Pylance extension on macOS. The color theme is the Dracula https://github.com/dracula/visual-studio-code The minus symbol - is not highligted with the numbers.

image

I suggest to add something like this -? at the begin of this regexp. Like this: (-?[0-9]+) A similar issue of other extension: https://github.com/hashicorp/vscode-terraform/issues/243

karthiknadig commented 4 weeks ago

This is likely coming from MagicPython. Just in case this is not, moving to pylance.

hologerry commented 4 weeks ago

Hi, @karthiknadig thanks! Hope we can have a solution to this.

rchiodo commented 4 weeks ago

I don't think this is an actual bug. You'd have to change the theme.

That character is identified as keyword.operator.arithmetic

Image

The same as *

Image

So - counts as an operator.

debonte commented 4 weeks ago

So - counts as an operator.

Counts as an operator in what sense? I see that the TextMate grammar is treating it that way, but I don't think that's true from Python's perspective.

Here's what I see in the Python grammar, which indicates that negative numbers are literals.

literal_pattern:
    | signed_number !('+' | '-') 
    | complex_number 
    | strings 
    | 'None' 
    | 'True' 
    | 'False' 

...

signed_number:
    | NUMBER
    | '-' NUMBER

Then for fun I tried https://github.com/iritkatriel/codoscope and it also indicates that there's no operator call involved here:

image

rchiodo commented 4 weeks ago

Wouldn't the UNARY_NEGATIVE be the operator? Oh I guess that was on X, but not 1. So for -1 it should be a literal but for -x there is an operator involved.

debonte commented 4 weeks ago

Yeah, I added the y = -x line to compare the two scenarios. Sorry that wasn't clear. The highlighted bytecode instructions on the right side are the entirety of the bytecode for x = -1.

debonte commented 4 weeks ago

Reopening since this is a bug. However, @hologerry, be aware that it may not get fixed anytime soon. See https://github.com/microsoft/pylance-release/issues/5824 for some context.

hologerry commented 4 weeks ago

Hi @debonte, Thanks for clarifying this! It’s great to know, and I hope we can get a better syntax highlighter in the future.