posit-dev / positron

Positron, a next-generation data science IDE
https://positron.posit.co
Other
2.83k stars 91 forks source link

Positron should better recognize and highlight Python keywords #3731

Open ntluong95 opened 5 months ago

ntluong95 commented 5 months ago

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

In Vscode image

In Positron image

petetronic commented 5 months ago

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 commented 4 months ago

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

seeM commented 4 months ago

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

ntluong95 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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

ntluong95 commented 4 months ago

image

Attached is the screenshot

ntluong95 commented 4 months ago

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

juliasilge commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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 commented 2 months ago

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

lionel- commented 1 month ago

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

Giorgione1609 commented 2 weeks ago

+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 commented 2 weeks ago

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 commented 2 weeks ago

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 commented 2 days ago

@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 commented 2 days ago

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