Hello all,
I wanted to revisit a discussion about auto completes to work on Slicer source code. I find myself always reading the source code to map many of the C++ code to python wrappers and etc. I visited this issue but it hasnt been discussed much further after 2023. Code autocompletion on VSCode fails
I know the debugger extension is there but its still very difficult how everything interacts
it being to get python.util function and seeing which function are available the in a text editor do work best.
Here are some questions i wanted to inquire:
If i build from source, does vscode support good LSP integration in the code base? Are there way’s to build this from source in a isolated environment. (I ran into soo many dependencies issues especially since im running this on a arm mac.)
Any advice on debugging code beside vscode debugger since the session always needs interupts and takes a long time to set up?
My current wrap around solution is using cursor to study the code base and really just control -f functions and using How to find a Python function for any Slicer features. However if you guys have any more tips, I would love to hear more!
My personal experience is that developing and debugging using the python console in Slicer is more productive than trying to use VSCode because you can run computations and explore the API of allocated variables and not just static symbols. It’s not perfect, and could be improved, but it’s valuable enough for me that I don’t bother with IDE debugging for the most part.
One feature people may not be aware of is that you can access the widget and logic of any scripted module at runtime. E.g.
Being able to run methods and work with the results can help you explore and understand the API in ways that most IDEs don’t support. Combining this with IDE support for searching the source code is usually the best approach for me.
I’d like to expand on a core frustration I have with my development workflow: the constant context-switching between the Slicer Python console and my text editor. I really wish I could have the live Python runtime—with its powerful autocompletion and access to object references—connected directly within my editor.
This leads to my second major challenge: the API can feel unintuitive, making it hard to map an objective to the correct sequence of steps espcially when you lack domain knowledge with VTK and how Slicer works.
Discovering this on my own feels nearly impossible without the FAQ. In scenarios where the documentation doesn’t have the exact answer, I often get stuck in a time-consuming and frustrating cycle of trial and error. Do you have any tips to overcome this?
Yes, I agree. We started working on the SlicerScriptEditor for exactly this purpose. The current form of that module is probably not exactly what you want, but I think it has the potential. The way this is implemented the monaco editor (the one used in VSCode) is embedded in Slicer and the completion has access to the Slicer python environment. This way it can autocomplete like the python console except even better because the monaco search function matches any substring and not just the prefix.
Since we wanted the ScriptEditor to meet the needs of the SlicerMorph project I didn’t pursue the features that would make it a console-replacement, but I sometimes find myself wanting to do that. In my case though I’m very wedded to gvim and I’ve gotten used to just copy pasting code back and forth. (While experimenting, I avoid writing functions and always paste whole blocks into the python console; this way all the variables are in the global scope and I can probe them and autocomplete methods).
Yes, that’s unfortunate and I don’t think you are alone in that. That’s why the script repository was started and why we encourage people to post their solutions here to help all of us in the future. Plus of course don’t forget all the extensions are full of example code too. I’m hoping the chatbots learn from all this and make things easier in the future (for now they are helpful, but hit or miss in my experience).