sdk icon indicating copy to clipboard operation
sdk copied to clipboard

"Move class to file" ask to export new file from source

Open FMorschel opened this issue 1 year ago • 3 comments
trafficstars

If you have the following project:

In main.dart:

class A {}

class C extends A {}

In other.dart:

import 'main.dart';

void f(C c) {}

And you use "Move class to file" on C, the current output for other.dart is (https://github.com/dart-lang/sdk/issues/56658 for the unused_import):

import 'main.dart';
import 'c.dart';

void f(C c) {}

I'm not sure how this could be done, but I'd like to ask for a way for me to say if I want to add export 'c.dart'; inside main.dart so that other files don't need to change.


// CC: @DanTup

FMorschel avatar Sep 10 '24 13:09 FMorschel

Summary: The "Move class to file" refactoring currently adds an unnecessary import statement to files that already import the original file containing the moved class. The user requests a way to specify whether to add an export statement to the original file instead, to avoid unnecessary changes in other files.

dart-github-bot avatar Sep 10 '24 13:09 dart-github-bot

A little update, both of the following issues could also benefit from some form of "checking" before/after the assist. Imagine someone wants to add an import with both show and an alias/prefix. That might make the current list of options a little too big and noisy, so if we had some form of "check user" here, that might be interesting.

  • https://github.com/dart-lang/sdk/issues/32234
  • https://github.com/dart-lang/sdk/issues/55863

Also, as noted on (https://github.com/dart-lang/sdk/issues/56265) there could be some form of "hiding" some options in the list project-wise or user-wise. That could also help with this specific problem of filling up the options list.

FMorschel avatar Sep 23 '24 03:09 FMorschel

One more case: https://github.com/dart-lang/sdk/issues/56885

We could also ask (when making a new part file with this same feature - whenever it supports it) if we'd like to use the source file imports or add new imports on the specific file.

FMorschel avatar Oct 11 '24 11:10 FMorschel

... I'd like to ask for a way for me to say if I want to add export 'c.dart'; inside main.dart so that other files don't need to change.

That's a reasonable request.

I'm not sure what the UI would look like for that, but the easiest solution might be to introduce a "Move Top Level to File and export" refactoring.

Alternatively, we could ask users to do something like

  1. Write an export directive for the new file.
  2. Use the 'Create file' assist to create the file.
  3. Use "Move Top Level to File" to move the declaration.

(That assumes that move-top-level checks to see whether the target file is already exported and leaves the imports in other files unchanged in the case, which I'm fairly certain it currently doesn't do but should.)

bwilkerson avatar Oct 22 '24 17:10 bwilkerson

Something else that came to my attention recently was that if you have something like:

original_file.dart
barrel_file.dart //export 'original_file.dart';

Once you move the content from the original_file, the barrel_file is not updated but every file that was using that declaration (through the barrel file or directly) would import the new file.

FMorschel avatar Mar 24 '25 20:03 FMorschel