-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Default import prefixes for packages #60150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
My two worries about this is that:
The latter is not necessarily a problem. You can import more than one library with the same prefix, they then share the prefixed import scope, like they would have the top-level import scope, but without any other libraries to share them with. It's not clear which libraries should choose to have a default import prefix, or why the author would think that they do. It may be libraries that contain many top-level getters or functions, or which have other names that are more likely to conflict Getters and functions have lower-cased names, which makes it easier to get shadowed by local variables, and easier to have a "simple conceptual name" that might be relevant in more than one context. Should this be per package, or per library? I probably wouldn't add a default prefix to any of my own packages. I guess That said, if you use the IDE for adding the import, where you get the option of adding an import, you could instead get the option of importing with or without a prefix. Or even three options: No prefix, package-name as prefix and first letter of package-name as prefix. It's more noise in the suggestion menu. If there are multiple possible places to import from, then one might want to put all the non-prefixed fixes at the top, followed by the prefixed ones. (But there being multiple places to import a name from is also the case where you may want a prefix, so maybe the prefixes should go first. I'll leave that to the usability experts.) It does make a kind of sense for some packages, or individual libraries, to suggest that they're imported with a prefix. I'm not sure I'm sold on them getting to decide the prefix, because resonable people can disagree on which prefix-naming style to use (and the style guide doesn't say anything). (Then there is the issue that it requires the analyzer to read the |
I agree, this doesn't seem very practical
I should have been more clear, I don't think this is something that library authors should be defining. My suggestion is that this would go in the developers
This is something I haven't thought through. I think a configuration that works for both would be nice. // All imports
import:
path: p
// Just path/path.dart:
import:
package:path/path.dart: p
// Could work for individual items
// Use the syntax from TypeChecker.fromUri(...)
import:
dart:developer#log: dev
This would really clutter the namespace. Alternative IdeasEmmet StyleAnother idea is to have the auto-complete use an p.TextField
__________________________________________________
| TextField package:flutter/material.dart as p|
| _|
|___________________________________________________| Would add a import with the prefix
Quick FixIt would be really neat if there was a import "dart:math";
import 'dart:developer';
void main(){
log("");
^^^^
} A quickfix would:
import "dart:math";
import 'dart:developer' hide log;
import 'dart:developer' as developer show log;
void main(){
developer .log("");
} This would allow the |
What version of Dart are you using @dickermoshe? As of Dart 3.7.0, you already have a quick-fix to import libraries with a prefix: #55863. This would not ensure you always use the same prefix but should help you add the import with the prefix without the name clashing by mistake. |
I've also worked on this and is out on the main channel for the combinators Edit: My thoughts on this are to add an assist that does something like "Prefix import" or something like that, and this could be defined as a fix for ambiguous import. |
We already have a mechanism for adding a prefix to an import, but it isn't discoverable and hence needs to be replaced. I had already considered the question of whether to make this be an assist or a refactoring. Making it a refactoring would allow us to prompt the user for the name of the prefix. If we make it an assist then we'd have to synthesize a name and some users would need to rename the prefix in order to get the code they want. Making it a refactoring would also allow us to provide better feedback when the operation can't be completed (because of name shadowing). That probably isn't a factor in the assist case because we can synthesize a name that won't be shadowed, but we can't provide very good feedback on renames either. I suspect that we want to do this as a refactoring. That said, the idea of using an undefined identifier before the period as a prefix is an interesting one. |
Yes, @bwilkerson, we have an assist for adding an import with a prefix. It does use an undefined identifier as the suggested prefix. Added by df520c9 which closed:
@dickermoshe we also have a refactor-rename at the We also have this issue to track adding some work related to this at: This also mentions this rename refactor. Basically, the talk there considers adding an "add"/"remove" prefix refactor/assist. |
Problem
The dart extension is really helpful for automatically adding imports.
In fact, I rarely ever find myself writing imports manually.
When two packages import to items with the same name, dart does not let you reference them.
The solution for this is adding a prefix
import 'package:foo/foo.dart' as foo;
This really rears its head in Flutter, where the default material widgets clutters the namespace.
Every flutter app is cluttered with widgets like
MyFilledButton
,MyDialog
etc, because of these clashes.It would be really nice if the auto-add-importer could be configured to add imports using a custom prefix.
Potential Solution
pubspec.yaml
The analyzer and extension would use this when adding imports.
For instance the path/path.dart library would use
p
as a prefix automatically.The text was updated successfully, but these errors were encountered: