From 2c61450e9eac89ba14308bffe7c3960cb5da44b3 Mon Sep 17 00:00:00 2001 From: Danny Tuppeny Date: Tue, 28 Nov 2023 09:40:49 +0000 Subject: [PATCH] [stable] [analysis_server] Fix missing closure completions in LSP Closure completions disappeared in eb73dba14d7b64a23d43dac301c4d1d56765ddf0 because we ended up wiping our the filterText/label when trying to clean them up for functions. Fixes https://github.com/Dart-Code/Dart-Code/issues/4837 Bug: https://github.com/Dart-Code/Dart-Code/issues/4837 Change-Id: I536ce38624ee3f6047229ac58c3fe173d162079a Cherry-pick: https://dart-review.googlesource.com/c/sdk/+/335822 Cherry-pick-request: https://github.com/dart-lang/sdk/issues/54112 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/337341 Reviewed-by: Alexander Thomas Commit-Queue: Alexander Thomas Reviewed-by: Brian Wilkerson --- CHANGELOG.md | 4 +++ pkg/analysis_server/lib/src/lsp/mapping.dart | 8 +++++- .../test/lsp/completion_dart_test.dart | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2f58a365629..a084c2614b82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ This is a patch release that: - Adjusts the nullablity computations in the implementation of the upper bound algorithm in the CFE (issue [#53999][]). +- Fixes missing closure code completion entries for function parameters + (issue [#54112][]) for LSP-based editors like VS Code. + [#53999]: https://github.com/dart-lang/sdk/issues/53999 +[#54112]: https://github.com/dart-lang/sdk/issues/54112 ## 3.2.1 - 2023-11-22 diff --git a/pkg/analysis_server/lib/src/lsp/mapping.dart b/pkg/analysis_server/lib/src/lsp/mapping.dart index 0e2c95c5e4e3..03eee9a27578 100644 --- a/pkg/analysis_server/lib/src/lsp/mapping.dart +++ b/pkg/analysis_server/lib/src/lsp/mapping.dart @@ -913,7 +913,13 @@ lsp.CompletionItem toCompletionItem( // Displayed labels may have additional info appended (for example '(...)' on // callables and ` => ` on getters) that should not be included in filterText, // so strip anything from the first paren/space. - final filterText = label.split(_completionFilterTextSplitPattern).first; + // + // Only do this if label doesn't start with the pattern, because if it does + // (for example for a closure `(a, b) {}`) we'll end up with an empty string + // but we should instead use the whole label. + final filterText = !label.startsWith(_completionFilterTextSplitPattern) + ? label.split(_completionFilterTextSplitPattern).first + : label; // If we're using label details, we also don't want the label to include any // additional symbols as noted above, because they will appear in the extra diff --git a/pkg/analysis_server/test/lsp/completion_dart_test.dart b/pkg/analysis_server/test/lsp/completion_dart_test.dart index 6fcb1be44bf2..8b4694369547 100644 --- a/pkg/analysis_server/test/lsp/completion_dart_test.dart +++ b/pkg/analysis_server/test/lsp/completion_dart_test.dart @@ -762,6 +762,32 @@ class B { expect(labels, contains('Deprecated(…)')); } + Future test_closure() async { + final content = ''' +void f({void Function(int a, String b) closure}) {} + +void g() { + f(closure: ^); +} +'''; + + final expectedContent = ''' +void f({void Function(int a, String b) closure}) {} + +void g() { + f(closure: (a, b) => ^,); +} +'''; + + await verifyCompletions( + mainFileUri, + content, + expectCompletions: ['(a, b) {}', '(a, b) =>'], + applyEditsFor: '(a, b) =>', + expectedContent: expectedContent, + ); + } + Future test_comment() async { final content = ''' // foo ^