8000 Auto complete on variable destruction can be improved · Issue #59854 · dart-lang/sdk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Auto complete on variable destruction can be improved #59854

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

Open
FMorschel opened this issue Jan 7, 2025 · 15 comments
Open

Auto complete on variable destruction can be improved #59854

FMorschel opened this issue Jan 7, 2025 · 15 comments
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-completion Issues with the analysis server's code completion feature P3 A lower priority bug or feature request

Comments

@FMorschel
Copy link
Contributor

Originally opened at Dart-Code/Dart-Code#5379.


Is your feature request related to a problem? Please describe.
When you have code like this:

void foo(Iterable iterable) {
  if (iterable case List()) {
    
  }
}

When inside List(^) you get prompted by auto-complete for all proprieties the class has. But when you auto-complete, it simply fills the propriety name and not :var isEmpty which is a valid syntax for getting that value (in for in loops you also can do this but the var is then before the Type name). We already have the rename refactor for changing the variable name to a new one if we want to, but I feel like this would help more than harm most of the time.

Describe the solution you'd like
For it to auto-complete with :var before the propriety name (if you know what lints are available maybe we can look at the ones that ask for the final keyword or the type name) on if cases and without the var keyword on for in loops.

@FMorschel
Copy link
Contributor Author

@srawlins can you think of any other case where we can have variable destruction like this? Did I forget anything?

@FMorschel
Copy link
Contributor Author

I also thought about code like destructing records:

var (: myVar) = record;

@bwilkerson bwilkerson added devexp-completion Issues with the analysis server's code completion feature P3 A lower priority bug or feature request labels Jan 21, 2025
@FMorschel
Copy link
Contributor Author
FMorschel commented Feb 19, 2025

Similar to #60118, lots of invalid things here:

({int value1, String myString}) func() => (value1: 0, myString: 'string');

void main() {
  var (:^) = func();
}

Image

But I'd also argue that the following (without :) should also show the same values as above and auto-add the : on completion. Things like A() and other classes are not valid here I don't think. Can someone confirm?

Image


Edit: Also, adding hashCode and whatever it showed there would make the assignment invalid so we sould only get the valid field names.

@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 21, 2025
@FMorschel
Copy link
Contributor Author

Can someone point me to where are extra code added on completion (do you know @DanTup)? I've already fixed the comment above locally, just trying to get it all in one go, it was really simple.

@DanTup
Copy link
Collaborator
DanTup commented Mar 20, 2025

Can someone point me to where are extra code added on completion (do you know @DanTup)?

Do you mean things like the (...)? If so, a lot of that comes from the LSP mapping (which has to build the different fields that are used in the completion) around here:

CompletionDetail _getCompletionDetail(

@FMorschel
Copy link
Contributor Author

Do you mean things like the (...)?

No, sorry for not being more clear. My idea is that for:

void f(({int myInt,} r) {
  if (r case (:^) {}
}

It would show myInt but when selecting that option, since we have not written var/final it would add this keyword (or int, depending on active lints).

@DanTup
Copy link
Collaborator
DanTup commented Mar 20, 2025

It would show myInt but when selecting that option, since we have not written var/final it would add this keyword

Oh, I see. I think this is handled by the difference in displayText and completion in CandidateSuggestion. For example, the subclass OverrideSuggestion overrides completion (which is the text to be inserted) like this:

  @override
  String get completion =>
      data?.completion ?? '@override ${element.displayName}';

@FMorschel
Copy link
Contributor Author

Thanks a lot! I'll take a look into that then.

@srawlins
Copy link
Member

Sorry I missed your earlier question; great find, that we have poor completion here. Thanks for looking into it!

There are a few cases like final and final MyType. I believe in the Flutter SDK, for example, they prefer / require final MyType. So in destructuring a record with type ({int one, List<Map<String, String>> two}), you must write final (:int one, :List<Map<String, String>> two) = ...

There's also the getterName: var variableName syntax. if (foo case String(length: var len)).

@FMorschel
Copy link
Contributor Author
FMorschel commented Mar 20, 2025

I'm looking into all of those! One thing I meant to ask is, do we have a specific test for "extra completion" text? Similar to testing out an assist result code.

I know we have pkg\analysis_server\test\services\completion\dart\location\record_pattern_test.dart for the main "kinds of suggestions" test.

And over at pkg\analysis_server\test\lsp\completion_dart_test.dart we can test the full output completion by doing some work, but do we have a better place for this?

@FMorschel
Copy link
Contributor Author

This should fix only part of this issue https://dart-review.googlesource.com/c/sdk/+/417143.

I'm splitting this because the second part is more complex so having its own CL sounded better. I'm still working on this second part.

@FMorschel
Copy link
Contributor Author

Here is the second CL https://dart-review.googlesource.com/c/sdk/+/417222.

[D]o we have a specific test for "extra completion" text? Similar to testing out an assist result code.

I know we have pkg\analysis_server\test\services\completion\dart\location\record_pattern_test.dart for the main "kinds of suggestions" test.

I understood the problem I was facing. The suggestions were not being built using the completion getter.

@FMorschel
Copy link
Contributor Author

Hey @DanTup, could you please take a look at https://dart-review.googlesource.com/c/sdk/+/417222?

I've just tested running it from source, and this is the visual output:

Image

My idea was actually to keep the member name only and to fill var or whatever when needed implicitly so that when we have always_specify_types on, it would not hide the member name.

Can you tell what I did wrong here? Thanks!

@DanTup
Copy link
Collaborator
DanTup commented Apr 9, 2025

@FMorschel hmm, on the surface the code looks reasonable, so I'd have to do some debugging. I wonder if we don't handle the difference between completion and displayText correctly for all completions (maybe only for overrides, etc.?).

If you can repro in a test, you could try debugging the code that builds the insert text, which I would expect is around here:

var insertTextInfo = buildInsertText(

@FMorschel
Copy link
Contributor Author

Thanks! With your guidance, I found that if these suggestions were SuggestionData they would work as I expected. Did that and it works now!

The label filling is actually done a bit after this line, but it is really close!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-completion Issues with the analysis server's code completion feature P3 A lower priority bug or feature request
Projects
None yet
Development

No branches or pull requests

4 participants
0