Copying existing classes in VSCode?

We’re going through the learning pains of VSCode. Putting the Java Extension Pack and GitLens has given us back some functionality that we were used to in Eclipse.

The one piece I can’t find for our new programmers is an easy way to copy a Java class. Eclipse would let you copy and paste, would prompt you for the new class name, and fix up the file name and copied code to reflect the new class name. The closest I can come is to have them copy and paste the Java file (which gets a name), rename the file, then go in and use Refactor (F2) to fix up the class name.

Not terrible, but could be better. Does anyone have a better way to do this?

This is all about creating new commands. I know that the WPIlib extension can create Commands, but I want the students to use an existing Command that has some logging code as a base for their new ones. I think it would be doable with a VSCode snippet, but am hoping that someone has a better/easier answer.

I just searched “duplicate” in the Extensions marketplace, there looks to be a lot of extensions that do what you want, I would try out a few and report back if there is one that you feel has a great user experience :slight_smile:

thanks for the tip: I hadn’t thought of using “duplicate” as a search term.

I see lots of extensions that duplicate lines or files, but I’m not seeing any that duplicate Java files and fix up the file name, classname, and name of the constructor… did I miss something?

Have you considered setting up a base command class that has the logging functionality you require, then extending that class for the new commands?

@ozdave, that’s a good idea.

We’re trying get some consistency in making sure that initialize(), end(), and interrupted() calls are logged, so we need to add code to the start of initialize(), end(), and interrupted(). There are shadow _initialize(), _end(), and _interrupted() methods in the wpilib Command, but _end() is called after end(), and _interrupted() is called after interrupted(), so doing the logging from _end() and _interrupted() doesn’t work.

I could subclass and have the students put their code in different methods than initialize(), end(), and interrupted() and have the superclass call those methods, but that would probably confuse the students.

I could ask them to put super.end() and super.interrupted() into their end() and interrupted() methods, but then I am right back into the “how do I make it dead simple for them to consistently get this code into their new commands?” problem.

The students will still end up subclassing from wpilib’s Command instead of the custom one by mistake on occasion, and that will be harder to spot than a missing line in the method.

With all that, I don’t know that I come out ahead with a team-specific Command…

I know I could solve this use-case off with no code if I use AOP and cutpoints, but that’s way overkill.

I’ll still have other places where we need to copy/paste; we can get through it, but I was hoping not to be giving up more functionality in the IDE thn we already have.

An extension to create files from templates looks promising.

I’ll also see if I can put the copy/paste funnctionality on the suggested enhancements list for both the Microsoft and RedHat Java extensions if I can’t find it buried in there somewhere.

In the mean time, if anyone else sees good Java class copy/paste-with-rename functionality in VScode, please reply to this. I can’t believe it’s missing, but I sure can’t find it.