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

Positron should better recognize and highlight Python keywords #3731

Open
ntluong95 opened this issue Jun 29, 2024 · 21 comments
Open

Positron should better recognize and highlight Python keywords #3731

ntluong95 opened this issue Jun 29, 2024 · 21 comments
Labels
Milestone

Comments

@ntluong95
Copy link

Currently, module keywords such as pandas, pd, etc. are not highlighted. See the example below

In Vscode
image

In Positron
image

@ntluong95 ntluong95 added the enhancement New feature or request label Jun 29, 2024
@petetronic
Copy link
Collaborator

Thanks for logging this, this is as a result of the token types from of the Jedi-based language server in use in Positron's Python LSP.

@ntluong95
Copy link
Author

Is there anyway to use Pylang, which is much better?

@seeM
Copy link
Contributor

seeM commented Jun 30, 2024

@ntluong95 I can't find any info on a "Pylang" syntax highlighter. Do you mean Pylance?

@ntluong95
Copy link
Author

Yes sorry I meant Pylance. Fortunately I found this one, which serve my need. Maybe something you can consider to integrate into Positron as I know Pylance is only for VSCode.
Link: https://open-vsx.org/extension/detachhead/basedpyright

@ntluong95
Copy link
Author

Yes sorry I meant Pylance. Fortunately I found this one, which serve my need. Maybe something you can consider to integrate into Positron as I know Pylance is only for VSCode. Link: https://open-vsx.org/extension/detachhead/basedpyright

Here is the result after installing the package. However, the Quarto file could not display syntax highlighting for Python module. I think Positron should have more support for Quarto extension since now the Jupyter Notebook is far more better
image

@seeM
Copy link
Contributor

seeM commented Jul 1, 2024

Thank you for pointing out basedpyright, we'll look into it!

Aside from the Quarto issue you mentioned, how well does basedpyright currently work in Positron? At the very least, we may be able to iron some of those issues so that users can choose alternative language servers – although it is worth noting that completions would no longer be aware of your session's variables and would not benefit from other Positron-specific language server features in future.

@ntluong95
Copy link
Author

It works well, excepted that in the Outlines, you will see 2 language servers. I think if you can look at their code and cherry-pick the part that help to recognize Python module keywords, and also attach it to Quarto, the problem will be solved. Just my naive idea

@seeM
Copy link
Contributor

seeM commented Jul 1, 2024

Could you please share a screenshot of the 2 language servers in Outlines?

@ntluong95
Copy link
Author

image

Attached is the screenshot

@juliasilge juliasilge added this to the Future milestone Jul 1, 2024
@ntluong95
Copy link
Author

But is there any plan to support display syntax highlighting of Python code for Quarto too?

@juliasilge
Copy link
Contributor

We do have syntax highlighting for Python code in Quarto, yes:

Screenshot 2024-07-09 at 10 32 32 AM

Is there a specific syntax highlighting feature you are missing or problem you are observing?

@ntluong95
Copy link
Author

Currently, module keywords such as pandas, pd, etc. are not highlighted. See the example below

In Vscode image

In Positron image

I think module keywords, and semantic highlighting from Pylance is missing. That's why I open this thread

@ntluong95
Copy link
Author

Found an interesting extension that can improve the situation although it is not the sustainable solution:
image
Settings:

"highlight.regexes": {
    "(\\bimport\\s+(?:\\w+(?:\\.\\w+)*(?:\\s*,\\s*)?)+|\\bfrom\\s+\\w+(?:\\.\\w+)*\\s+import\\s+(?:\\w+(?:\\s*,\\s*)?)+|\\bas\\s+\\w+)": {
      "regexFlags": "g",
      "filterLanguageRegex": ".*",
      "filterFileRegex": "\\.qmd$",
      "decorations": [
          { "color": "#e5c07b" }
      ]
  },
  "(?<!\\bself\\.)(\\b\\w+\\b(?=\\.(?:\\w+\\s*\\())|(?<=\\bfrom\\s+)[\\w.]+(?=\\s+import))": {
      "regexFlags": "g",
      "filterLanguageRegex": ".*",
      "filterFileRegex": "\\.qmd$",
      "decorations": [
          { "color": "#e5c07b" }
      ]
  },
    "(#\\|.*)": { // This regex matches comments starting with #|
    "regexFlags": "g", // Global flag to match all instances
    "filterLanguageRegex": ".*", // Apply to all languages
    "filterFileRegex": ".*", // Apply to all files
    "decorations": [ // Decoration options to apply to the capturing groups
      { "color": "#b38c31" } // Decoration options to apply to the entire comment starting with #|
    ]
  }
}
    "editor.tokenColorCustomizations": {
        "textMateRules": [
           {"scope": ["meta.embedded.block.python"],
           "settings": {"foreground": "#e06c75"}},
           {"scope": ["variable.parameter.function-call.python"],
           "settings": {"foreground": "#b0dddb"}},
           {"scope": ["meta.attribute.python"],
           "settings": {"foreground": "#85c0df", "fontStyle": "italic bold"}},
           {"scope": ["variable.parameter.function.language.python"],
           "settings": {"foreground": "#85c0df", "fontStyle": "italic bold"}} ,
           {"scope": ["constant.other.caps.python"],
           "settings": {"foreground": "#4fc1ff"}}                                               
          ]}

Output:
image
image

@KJHards
Copy link

KJHards commented Sep 4, 2024

+1 for this change. Tried to switch from VScode but too much of my python code has no syntax highlighting

@lionel-
Copy link
Contributor

lionel- commented Oct 16, 2024

The ruff team is "considering" semantic highlighting "but not in the near future" (see astral-sh/ruff-vscode#438 (comment)). Tracking issue: astral-sh/ruff-lsp#237

@Giorgione1609
Copy link

Giorgione1609 commented Nov 9, 2024

+1 for this change. Tried to switch from VScode but too much of my python code has no syntax highlighting

Same here. Are there any news? It's such a pity to drop Positron because of this.

@seeM
Copy link
Contributor

seeM commented Nov 12, 2024

Unfortunately this isn't a priority at the moment, but I did spend a little time seeing what it would take to support semantic highlighting with Jedi on the 3731-investigate-jedi-lsp-semantic-tokens branch if anyone is interested in pursuing this further.

That should get basic semantic highlighting. From my understanding, it's easy with Jedi to classify declarations (e.g. in def f(): pass, f is a declaration of a function). The trickier part will be correctly classifying references (e.g. calling f later in your code), especially references to objects in larger external modules. I think the correct Jedi API is infer() which might be too slow for the semantic highlighting usecase.

The language server protocol also supports updating semantic tokens given edits to the document (rather than starting from scratch each time). I haven't looked into whether that's necessary (or possible) with Jedi. Looking at this comment, Jedi's parser parso heavily utilizes caching of different parts of the document for performance, so it might not be necessary to support semantic token edits.

It would be helpful to know from you all which token types are the most important, and we can look into the feasibility of getting that to work with Jedi.

@ntluong95
Copy link
Author

I don't have the knowledge on this, but this document might be helpful to know all of the token type.

For prioritizing purpose, for me at least module keywords, class methods, function parameters,
This one is not so good, but have some key takeaways that we can start from I hope

@lbeiby
Copy link

lbeiby commented Nov 24, 2024

@seeM how is the progress. I think Jedi is quite good in general but hope Positron can implement something like customized Jedi like Microsoft did with Pylance, which improve a lot of UX

@ntluong95
Copy link
Author

I tested and it works quite well. May be add also "variable.declaration", "property.declaration" and "function.builtin". Those are something I have set syntax highlighting when using Pylance.

The problem is it doesn't work on Jupyter notebook. But Pylance doesn't work on Quarto document. I think the current highlighting is good enough for me at least to move forward, as long as Jupyter Notebook and Quarto document support for it, I may not need Pylance. I can use Ruff for type checking and linting

@ntluong95
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants