Processing and VSCode

I thought let's make a topic about VSCode and Processing.

There is a extension for it: https://github.com/TobiahZ/processing-vscode More about that here: https://forum.processing.org/two/discussion/20239/visual-studio-code-for-processing-and-p5

But I'm not using that cause I really like to use the debugger (so I use extends PApplet etc. for now). I hope I have time somewhere next year to make a extension that supports debugging processing in vscode.

Anyway, if you don't know what vscode is I suggest to look it up. It's a really nice editor which supports a lot of languages. (And I work in a lot of languages).

If you do use a debugger and switch between a lot of sketches that you like to run then the following might help. If not I suggest to stop reading now. P.s. this struggle below does not reflect VSCode in anyway! I make my projects with maven. It should be possible without but I didn't look into how to provide the class path. Here are the dependencies I use to set up a opengl project.

 <dependency>
      <groupId>org.processing</groupId>
      <artifactId>core</artifactId>
      <version>3.3.6</version>
    </dependency>
    <dependency>
      <groupId>org.jogamp.gluegen</groupId>
      <artifactId>gluegen-rt-main</artifactId>
      <version>2.3.2</version>
    </dependency>
    <dependency>
      <groupId>org.jogamp.jogl</groupId>
      <artifactId>jogl-all-main</artifactId>
      <version>2.3.2</version>
    </dependency>

Anyway, the reason for this topic. The java support is quite new and in development. And I missed a way to easy run different sketches without setting which one to run in the debugger. So I made a simple helper, which allows you to run from your current active sketch, and else it default to the previous valid one. It's far from perfect but I have no time to make a descent extension.

package nl.doekewartena;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.prefs.Preferences;


public class VSCode_Workaround_Launcher {



    public static void main (String[] args) {

        VSCode_Workaround_Launcher launcher = new VSCode_Workaround_Launcher();
        launcher.start(args);
    }

    public void start(String[] args) {
        String to_load = null;

        if (args != null) {
            if (args.length > 0) {
                if (args[0].startsWith("src/test/java/")) {
                    if (!args[0].contains("VSCode_Workaround_Launcher")) {
                        to_load = args[0];
                        to_load = to_load.replace("src/test/java/", "");
                        to_load = to_load.replace(".java", "");
                        to_load = to_load.replace("/", ".");

                        System.out.println("load: "+to_load); 
                    }
                }
            }
        }

        if (to_load == null) { // try the previous one
            System.out.println("load last one...");
            to_load = get_last_stored();
            System.out.println(to_load);

        }

        if (to_load != null) {
            try {
                Class<?> clazz = Class.forName(to_load);                
                Method main_method = clazz.getMethod("main", String[].class);

                store_last(to_load);

                String[] params = null;
                main_method.invoke(null, (Object) params); 

            } catch (ClassNotFoundException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }


    public static void store_last(String what) {
        Preferences prefs = Preferences.userNodeForPackage(nl.doekewartena.VSCode_Workaround_Launcher.class);
        prefs.put("to_load", what);
    }

    public static String get_last_stored() {
        Preferences prefs = Preferences.userNodeForPackage(nl.doekewartena.VSCode_Workaround_Launcher.class);
        return prefs.get("to_load", null);
    }

}

My launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug (Launch)- Last or current one!",
            "request": "launch",
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopOnEntry": false,
            "mainClass": "nl.doekewartena.VSCode_Workaround_Launcher",
            "projectName": "quadtree",
            "args": "${relativeFile}" 
        },
        {
            "type": "java",
            "name": "Debug (Attach)",
            "request": "attach",
            "hostName": "localhost",
            "port": 0
        }
    ]
}

Comments

Sign In or Register to comment.