Message Boards Message Boards

What is the best way to refactor code within a notebook?

Posted 2 years ago

Recently, I needed to rename some variables within a notebook. The only way I could find was to use search and replace. Although that worked, it was a bit error prone, and it made me think that there must be a better approach of which I was unaware.

Could someone enlighten me about the best way to refactor code within a notebook?

Cheers, Andy

POSTED BY: Andrew Burnett
5 Replies

For large code development, I'd suggest using an IDE that supports it. AFAIK, there isn't one that works with notebooks, so this is sort of a half-answer: Someone has written a nice IDE for working with packages (".m"/".wl" files):

https://wlplugin.halirutan.de

There's also Wolfram Workbench.

You can save a .nb file to a .wl if you change all the input cells to be initialization cells; then Save As .wl. Can be a bit of a pain, though. Not sure if you can convert back to a .nb file.

Not a quick solution to your problem -- sorry about that.

POSTED BY: Michael Rogers

Because the package file is autogenerated from the notebook, every time the notebook is saved, the package file gets rewritten. Therefore one should never edit the package file.

Regarding conversion of a code file (script or package) into a notebook, it can be done, but it requires working with raw text or held expressions. If might be easiest to copy the plain text code from the IntelliJ file and paste it into a Code style cell in a new notebook, and then manually divide the cell between successive expressions.

POSTED BY: Robert Nachbar
Posted 2 years ago

You can save a .nb file to a .wl if you change all the input cells to be initialization cells; then Save As .wl. Can be a bit of a pain, though. Not sure if you can convert back to a .nb file.

Evaluate this in a notebook.

SetOptions[InputNotebook[], AutoGeneratedPackage -> Automatic]

Whenever the notebook is saved, a corresponding .m file is saved with all of the initialization cell definitions. The convenience of working with a notebook, and generating code for re-use in other notebooks.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thank you to both of you. Michael, the plugin looks superb. It is exactly what I was looking for. I am not a Jetbrains user, but I think I need to become one immediately.

Rohit that is a very useful one-liner. Is there something similar that would allow me to go the other way? I.e. develop in IntelliJ, and then parse the file with Mathematica, so that it becomes a Notebook again? It isn't a problem if not - I would Mathematica would simply bring it all in as a single cell. However, for debugging purposes, it could be very useful to be able to single step through the code, and Notebooks are great for that.

Cheers Andy

POSTED BY: Andrew Burnett
Posted 2 years ago

Thanks Robert. That's very helpful. I will do some experiments with importing the code into a notebook. But, on the basis of your thoughts, it sounds as though the process of editing the code in IntelliJ is really just a one-way process. I.e. once I have decided that my programming task demands a more capable editor, I won't be going to back to Mathematica, except to run the code.

POSTED BY: Andrew Burnett
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract