Feature Request: Extract classes/traits/objects into separate file

Unlike Java, Scala allows any number of classes/traits to be defined in the same file. Ie the logical structure of the classes is orthogonal to the physical arrangement in files.

I find this useful when prototyping a new component, service or app, because I can quickly spawn new classes in the same editor buffer as required. However, once the cocebase grows, and the architecture becomes clearer, it's desirable to break out classes into individual files or smaller groups.

I can't see any way to eg "Extract class/object to another file" automatically in the current scala Refactoring toolset?

Im basically looking to:

- Select a top-level class/trait/object and "Extract to another file".

- By default, the file would be named as "${extractedClass}.scala", and might already exist. But I could override this and select another existing or new filename.

- If the target file exists, the extracted code is appended. If the target file does not, it is first created. It is removed from origin file.

- If the selected class/object is not top-level (eg it is nested in another), it cannot be extracted.

- Not sure how to handle imports that are in scope in the source file. Honestly, I'd be happy enough to fix them up by hand. But a nicer option might be to replicate all imports across to the new file.

Comment actions Permalink

Extracting a class to a new file can be done via the Move refactoring.
Note that you have to explicitly invoke the refactoring, drag and drop in the project tree doesn't seem to work for this.

I don't know a way to move a class into an existing file.

Comment actions Permalink

As correctly noted by Maarten, you can extract classes to another package using Move refactoring. Extracting to the specified file or current package are not supported yet.
I created the issue:

Comment actions Permalink

Can you provide an example where drag and drop does not work for moving classes?


Please sign in to leave a comment.