Does anyone know whether it is possible in IntellJ to display an image rather than text when it displays the toString value?
I’ve seen this done using Jupyter, and also using emacs. I don’t know how it’s done?
Basically each of the instances of a certain class in my application correspond to tree-like diagrams. I can generate the .png file via an external call to graphviz. It would be really cool for demo purposes if InteliJ scratch mode could simply display these trees, rather than cryptic text.
Agreed, but the graphviz integration probably won’t work – remember that Scalafiddle runs client-side, so you’re basically restricted to what’s available in it.
You could probably display the tree yourself using Scalafiddle: if you look at its docs and examples, it has machinery to render to the canvas, and a simple-looking line-drawing library. But I don’t see a straightforward way to call out to an external tool to generate a PNG…
My plan is to use the scratch mode of IntellJ during a live demo.
I can just write a function which takes such an object and opens up a separate window to display it. I haven’t looked, but I assume there’s a Scala function which lets me run a given shell command with specified arguments…
@nafg, is your idea that I would still need to make an explicit call to trigger something being displayed in the browser? Or are you suggesting that the image could display directly in the results pane of the Scala worksheet?
I see. well since i’m running on the Mac, I can just call a shell command “view file.png” to pop up an x-window displaying the image.
It would be really cool if it could show up in the worksheet incorporated into the printed representation of any object. I don’t know how that works in emacs nor in IntelliJ, but in emacs, you can put some kind of hook on the display of an object. Knowing emacs it is probably something connected to regular expression pattern matching and mode hooks. I have even less idea how it works in the Jupyter notebook.
Another alternative is to install an IntelliJ Markdown plugin. You can use for example this:

in markdown text. The IDE will the render the Markdown showing the image (default configuration). BTW combining this with Tut or MDoc will also allow you to incorporate code into your text. SBT plugins already exists.
will that allow the toString method to output a string such as """""" and have it show the image automatically when the system prints the object?
If you want to view a generated image in the browser without saving it to disk first, you could generate a base64 date uri and open that in the browser.
@nafg, sorry, let me clarify my response. I already have a method which I can call on an instance of my class which will popup a window and display the image, via a temp file. So calling that method with something like obj.viewObj() gets the job done. A solution such as typing text such like """"" does not really help if it displays the image elsewhere.
I was hope for a solution where the worksheet itself could recognize that the object is being printed, and rather than displaying the text of the toString method, instead would run my method convertToImageThisAndThat and display that image directly in the results window.
This model is already used successfully in environments such as emacs and Jupyter. Jupiter does this (for Python) by calling a method (whose name I don’t know) like .convertToSvg if such is callable, and displays that image, but if that method does not exist, then it simply displays the test of the equivalent of the .toString method.