Eugleo / magic-racket

The best coding experience for Racket in VS Code
https://marketplace.visualstudio.com/items?itemName=evzen-wybitul.magic-racket
GNU General Public License v3.0
200 stars 29 forks source link

Magic Racket is not recognized as a racket formatter in Jupyter Notebooks #120

Open Maelcom opened 10 months ago

Maelcom commented 10 months ago

Environment

Steps to reproduce

  1. Set "Magic Racket" as default formatter for Racket
  2. Open .ipynb file
  3. Create a code cell with cell language set to "Racket"
  4. Try to format with the "Format Cell" command

Expected

Magic Racket formats the cell.

Actual

VS Code error message: Extension 'Magic Racket' is configured as formatter but it cannot format 'Racket'-files

Additional context

  1. "Format Document With..." command inside .ipynb files reveals that Magic Racket is NOT present in the list of possible formatters. Same command inside .rkt files correctly displays Magic Racket.

  2. Same scenario with racket-fmt works correctly out of the box, so probably not complicated.

  3. Recording:

https://github.com/Eugleo/magic-racket/assets/1520199/ef27cfd2-bae8-4394-aab6-93d3a150b3c2

jryans commented 10 months ago

Very interesting, thanks for filing this issue.

I've tried to reproduce this on my side (VS Code 1.81.1 on macOS), but I've hit an obstacle along the way... At moment, I don't see a way to choose Racket as a notebook cell language.

When I create a new notebook, the cells start out as Python by default. I can click the "Python" marker in the bottom right of the cell to get a menu of languages, but it seems to be a subset of the languages that VS Code and my extensions are aware of, as only a few of them appear there, and Racket is not one of them.

I wonder how this list of "notebook ready" languages is determined...

Maelcom commented 10 months ago

I can click the "Python" marker in the bottom right of the cell to get a menu of languages, but it seems to be a subset of the languages that VS Code and my extensions are aware of, as only a few of them appear there, and Racket is not one of them.

Just to make sure, did you run the second command during IRacket installation?

raco pkg install iracket
raco iracket install

https://docs.racket-lang.org/iracket/index.html

On my system that was enough for Jupyter to start seeing the new language.

jryans commented 10 months ago

Ah thanks, that helped. I am now able to select Racket as a language in notebook cells.

Okay, it looks like this issue is similar to https://github.com/Eugleo/magic-racket/issues/113: this extension currently only registers itself for file documents. Notebook cells use the special vscode-notebook-cell scheme.

Just removing the filter and registered for all schemes is not enough, as presumably we need to do a bit of translation to get the content for notebook cells. Perhaps other extensions have found a good way to do that...

Maelcom commented 10 months ago

Perhaps other extensions have found a good way to do that

Well, racket-fmt uses a vscode.languages.registerDocumentFormattingEditProvider inside of which it just manually takes document.getText(), passes through CLI and gets the resulting text.

Don't know if this could be applicable to LSP.

jryans commented 10 months ago

Well, racket-fmt uses a vscode.languages.registerDocumentFormattingEditProvider inside of which it just manually takes document.getText(), passes through CLI and gets the resulting text.

Thanks, I noticed that as well. 🙂

In this case, it's a little different in that the language server currently assumes the document URI has a path-like shape to it. I believe the main options are either:

I need to review the language a bit more to make sure it does not actually use the file path to e.g. read content, and then from there I think a decent approach to move forward should become clear.

jryans commented 10 months ago

At the moment, there are a few places where the language server assumes a filesystem. I have filed https://github.com/jeapostrophe/racket-langserver/issues/122 to discuss this with others working on the language server.

jryans commented 10 months ago

We've had a bit of progress on this front!

https://github.com/jeapostrophe/racket-langserver/issues/122 has been resolved on the langserver side, which means it is now compatible with non-file documents.

The main issue that remains is that the langserver really wants #lang to be present, while IRacket wants the opposite, so it's challenging to use both for a given cell. (If you have any suggestions on what to do about this, please pass them along!)

To test out the current state:

  1. Update racket-langserver to https://github.com/jeapostrophe/racket-langserver/commit/3f23f437eac0206a45abe79e0edc2d1a3ed48161 or beyond
  2. Install this test version of Magic Racket (which adjusts the document selector to match all document URI schemes): magic-racket.vsix.zip (Unzip the file first, drag and drop onto VS Code, reload window)