Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Syntax highlighting for variables, enum and modules #12578

Closed
1 task done
nigelmj opened this issue Jun 2, 2024 · 13 comments
Closed
1 task done

Python Syntax highlighting for variables, enum and modules #12578

nigelmj opened this issue Jun 2, 2024 · 13 comments
Labels
enhancement [core label] language An umbrella label for all programming languages syntax behaviors python Python programming language support theme Feedback for theme configuration, accessibility, visual issues, etc

Comments

@nigelmj
Copy link
Contributor

nigelmj commented Jun 2, 2024

Check for existing issues

  • Completed

Describe the feature

Although variable highlighting for Python was introduced in the current release, it is only available in the case where one variable is defined per line. There are several areas where Python syntax highlighting can be improved in Zed:

  • Function parameters: Highlighting of arguments passed to the function in both the parameters list and within the function body
  • Multiple variable unpacking: Highlighting multiple variables defined in the same line or though unpacking assignments
  • Reused variables: Ensuring that variables already defined are always highlighted when resused (at the moment they are only highlighted when first defined)
  • Loop variables: Highlighting the loop variables would also be helpful
  • Self parameter: This could be highlighted the with the same colour as the name of the class as it represents an instance of it
  • Enum members: Currently they are highlighted as normal variables instead of what is specified under the enum property in the themes json file.
  • Module names: Names could be highlighted the same way as the classes being imported are. Currently they have the foreground colour. However I am not sure if this requires creating a new property for themes.

If applicable, add mockups / screenshots to help present your vision of the feature

Screenshot 2024-06-02 at 12 28 04
@nigelmj nigelmj added admin read Pending admin review enhancement [core label] triage Maintainer needs to classify the issue labels Jun 2, 2024
@Angelk90
Copy link

Angelk90 commented Jun 2, 2024

@Nigel007 : You can write the example code shown in the image so it is easier to copy them to zed to see the difference for those who will work with it.

@nigelmj
Copy link
Contributor Author

nigelmj commented Jun 2, 2024

@Angelk90 Yeah that makes sense. Here's the code:

import os
from enum import Enum

def sum(a, b):
    return a + b

m, n = (5, 5)

a = 1
a += 1

for index in range(5):
    print(index)

class Test:
    def __init__(self):
        self.a = 1
        self.b = 2

class A(Enum):
    a = 1
    b = 2
    c = 3

a_dict = {A.a: 1, A.b: 2, A.c: 3}

a_list = [1, 2, 3, 4, 5]
print(a_list[0])
print(a_list[-1])

@Angelk90
Copy link

Angelk90 commented Jun 2, 2024

@Nigel007 : This is how I currently see it:
Screenshot 2024-06-02 alle 13 54 38

@nigelmj
Copy link
Contributor Author

nigelmj commented Jun 2, 2024

@Angelk90 I think you've set the variable colour as white and the foreground as a off-white? If you notice, only variables that are defined for the first time have the bright white colour. All other variables, be it function parameters, reused variables (line 10: a+=1) or mutliple varialbes (line 7: m, n = (5, 5)), are off-white. I think the values inside a_dict are coloured according to the attribute property, which is why they show up as blue.

@Angelk90
Copy link

Angelk90 commented Jun 2, 2024

@Nigel007 : I created a custom theme for Zed, I just wanted to show you how I currently see things, in case anyone needs to see the difference.

@nigelmj
Copy link
Contributor Author

nigelmj commented Jun 2, 2024

@Angelk90 Oh apologies! I should have also uploaded an image of how it appears for me.

Screenshot 2024-06-02 at 13 08 05

I have my variable and attribute colours set to a reddish tone. My foreground colour is white.

@JosephTLyons JosephTLyons added python Python programming language support language An umbrella label for all programming languages syntax behaviors theme Feedback for theme configuration, accessibility, visual issues, etc and removed triage Maintainer needs to classify the issue admin read Pending admin review labels Jun 4, 2024
nigelmj added a commit to nigelmj/zed that referenced this issue Jun 7, 2024
All variables are now highlighted using the @variable property.
This covers assignments and reusages, parameters and loop variables.

Ref zed-industries#12578
nigelmj added a commit to nigelmj/zed that referenced this issue Jun 7, 2024
Self references in classes are now highlighted using the @type property.
This change was made as they refer to an instance of the class.

Ref zed-industries#12578
nigelmj added a commit to nigelmj/zed that referenced this issue Jun 8, 2024
All modules are now highlighted with the @type property.
This includes both import and import_from statements as
well as their aliases. Self parameter highlighting has
been tweaked to not include variables that are called
self.

Ref zed-industries#12578
@nigelmj
Copy link
Contributor Author

nigelmj commented Jun 9, 2024

I have added highlighting to all variables and set the self reference in classes to the variable.special property. Modules are highlighted in the import statements, however, they are highlighted as variables when used in the code. I assume this is because the parser is not context aware.

Further, all imports, be it classes, functions, etc are highlighted with the type property at the moment. This is non ideal as it would make more sense for different symbols to be highlighted differently. I am looking into possible solutions for this.

Screenshot 2024-06-10 at 01 02 35

The Enum class members are still highlighted as normal variables. I am guessing some semantic highlighting is needed in this case too.

@nigelmj
Copy link
Contributor Author

nigelmj commented Jun 10, 2024

The issues that are still existing seem to be related to semantic highlighting because they require context awareness. That would be beyond the scope of this issue as semantic highlighting pertains to all languages.

nigelmj added a commit to nigelmj/zed that referenced this issue Jun 10, 2024
All variables are now highlighted using the @variable property.
This covers assignments and reusages, parameters and loop variables.

Ref zed-industries#12578
nigelmj added a commit to nigelmj/zed that referenced this issue Jun 10, 2024
Self references in classes are now highlighted using the @type property.
This change was made as they refer to an instance of the class.

Ref zed-industries#12578
nigelmj added a commit to nigelmj/zed that referenced this issue Jun 10, 2024
All modules are now highlighted with the @type property.
This includes both import and import_from statements as
well as their aliases. Self parameter highlighting has
been tweaked to not include variables that are called
self.

Ref zed-industries#12578
@notmd
Copy link

notmd commented Jun 14, 2024

it seems to happen to Rust as well as many other languages, Zed doesn't highlight identifier as @variable.

nigelmj added a commit to nigelmj/zed that referenced this issue Jun 15, 2024
Changed the variable property to act on parameter tokens
rather than all identifiers

Ref zed-industries#12578
@nigelmj
Copy link
Contributor Author

nigelmj commented Jun 15, 2024

@notmd Thank you for pointing it out. At first I was a bit skeptical about using the identifier node for the @variable property as it was too broad and would give all nodes the same colour. This was why I placed it on the first line before any other highlighting rule, and I also thought there was no other way to highlight variables without individually specifying all cases where they would be used (assignment, augmentation, indexing, for loops, and other statements).

After a little trial and error, which mainly involved coming up with custom rules for each case, I discovered that the parameter node considers all variables and not just function parameters. This seemed like an appropriate fix to highlight the variables, so I've modified the first line in the file to set that token to the @variable property.

JosephTLyons added a commit that referenced this issue Jun 18, 2024
Release Notes:

- Improve syntax highlighting in Python #12578 

Before:
<img width="1181" alt="Screenshot 2024-06-08 at 01 44 54"
src="https://github.com/zed-industries/zed/assets/87859239/0b8ab26b-149b-477e-af08-8cd9f2b1c117">

After:

<img width="1184" alt="Screenshot 2024-06-10 at 01 02 35"
src="https://github.com/zed-industries/zed/assets/87859239/a319a5ea-54b7-4681-951d-130ea26aa390">

---------

Co-authored-by: Joseph T Lyons <JosephTLyons@gmail.com>
@JosephTLyons
Copy link
Contributor

Closed via: #12868

@ihoey
Copy link

ihoey commented Jun 28, 2024

@nigelmj Hello, what is the theme of this screenshot?

@nigelmj
Copy link
Contributor Author

nigelmj commented Jun 28, 2024

@ihoey This is a custom theme based on the One Dark Pro theme for VS Code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement [core label] language An umbrella label for all programming languages syntax behaviors python Python programming language support theme Feedback for theme configuration, accessibility, visual issues, etc
Projects
None yet
Development

No branches or pull requests

5 participants