 Chromium Code Reviews
 Chromium Code Reviews Issue 2918613002:
  Add completion support for plugins  (Closed)
    
  
    Issue 2918613002:
  Add completion support for plugins  (Closed) 
  | Index: pkg/analyzer_plugin/lib/utilities/completion.dart | 
| diff --git a/pkg/analyzer_plugin/lib/utilities/completion.dart b/pkg/analyzer_plugin/lib/utilities/completion.dart | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..05caa0fb002809c3403311e3dffe6a98320a2399 | 
| --- /dev/null | 
| +++ b/pkg/analyzer_plugin/lib/utilities/completion.dart | 
| @@ -0,0 +1,132 @@ | 
| +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 
| +// for details. All rights reserved. Use of this source code is governed by a | 
| +// BSD-style license that can be found in the LICENSE file. | 
| + | 
| +import 'dart:async'; | 
| + | 
| +import 'package:analyzer/dart/analysis/results.dart'; | 
| +import 'package:analyzer/file_system/file_system.dart'; | 
| +import 'package:analyzer_plugin/protocol/protocol.dart'; | 
| +import 'package:analyzer_plugin/protocol/protocol_common.dart'; | 
| +import 'package:analyzer_plugin/protocol/protocol_generated.dart'; | 
| +import 'package:analyzer_plugin/src/utilities/completion.dart'; | 
| +import 'package:analyzer_plugin/utilities/generator.dart'; | 
| + | 
| +/** | 
| + * An exception that is thrown when the current completion request should be | 
| + * aborted because either the source changed since the request was made, or | 
| + * a new completion request was received. | 
| + */ | 
| +class AbortCompletion {} | 
| + | 
| +/** | 
| + * An object that [CompletionContributor]s use to record completion suggestions. | 
| + * | 
| + * Clients may not extend, implement or mix-in this class. | 
| + */ | 
| +abstract class CompletionCollector { | 
| + /** | 
| + * Set the length of the region of text that should be replaced by the | 
| + * selected completion suggestion. | 
| + */ | 
| + void set length(int length); | 
| + | 
| + /** | 
| + * Set the offset of the region of text that should be replaced by the | 
| + * selected completion suggestion. | 
| + */ | 
| + void set offset(int offset); | 
| 
scheglov
2017/05/31 19:26:03
Should we throw an exception if a different value
 
Brian Wilkerson
2017/05/31 19:33:20
Maybe. I definitely considered that.
It would req
 
mfairhurst
2017/05/31 21:49:34
We could definitely make this change if necessary.
 
Brian Wilkerson
2017/06/03 16:44:37
Done
 | 
| + | 
| + /** | 
| + * Record the given completion [suggestion]. | 
| + */ | 
| + void addSuggestion(CompletionSuggestion suggestion); | 
| +} | 
| + | 
| +/** | 
| + * An object used to produce completion suggestions. | 
| + * | 
| + * Clients may implement this class when implementing plugins. | 
| + */ | 
| +abstract class CompletionContributor { | 
| + /** | 
| + * Contribute completion suggestions for the completion location specified by | 
| + * the given [request] into the given [collector]. | 
| + */ | 
| + Future<Null> computeSuggestions( | 
| + CompletionRequest request, CompletionCollector collector); | 
| +} | 
| + | 
| +/** | 
| + * A generator that will generate a 'completion.getSuggestions' response. | 
| + * | 
| + * Clients may not extend, implement or mix-in this class. | 
| + */ | 
| +class CompletionGenerator { | 
| + /** | 
| + * The contributors to be used to generate the completion suggestions. | 
| + */ | 
| + final List<CompletionContributor> contributors; | 
| + | 
| + /** | 
| + * Initialize a newly created completion generator. | 
| + */ | 
| + CompletionGenerator(this.contributors); | 
| + | 
| + /** | 
| + * Create a 'completion.getSuggestions' response for the file with the given | 
| + * [path]. If any of the contributors throws an exception, also create a | 
| + * non-fatal 'plugin.error' notification. | 
| + */ | 
| + Future<GeneratorResult> generateCompletionResponse( | 
| + CompletionRequest request) async { | 
| + List<Notification> notifications = <Notification>[]; | 
| + CompletionCollectorImpl collector = new CompletionCollectorImpl(); | 
| + try { | 
| + for (CompletionContributor contributor in contributors) { | 
| + request.checkAborted(); | 
| 
mfairhurst
2017/05/31 21:49:35
should this be checked at the end of the contribut
 
Brian Wilkerson
2017/05/31 22:51:47
I assume that the time it takes to get the next co
 
mfairhurst
2017/05/31 23:17:36
I mostly mean that the unrolled loop looks like:
 
Brian Wilkerson
2017/06/03 16:44:37
Yes, that's the purpose. The spec doesn't say what
 | 
| + try { | 
| + await contributor.computeSuggestions(request, collector); | 
| 
maxkim
2017/05/31 17:44:35
Really liking the new structure.
Let me see if I'
 
Brian Wilkerson
2017/05/31 18:39:43
Every plugin is running in a separate isolate, and
 
maxkim
2017/05/31 19:04:03
Understood. Just wanted to make sure it was clear
 | 
| + } catch (exception, stackTrace) { | 
| 
mfairhurst
2017/05/31 21:49:34
Maybe this shouldn't be caught here, but at the la
 
Brian Wilkerson
2017/05/31 22:51:47
If we catch this in an outer context, then this me
 
mfairhurst
2017/05/31 23:17:36
Interesting. Yeah, that makes sense.
I guess my m
 
Brian Wilkerson
2017/06/03 16:44:37
Your suggestion elsewhere, which I like, of defini
 | 
| + notifications.add(new PluginErrorParams( | 
| + false, exception.toString(), stackTrace.toString()) | 
| + .toNotification()); | 
| + } | 
| + } | 
| + } on AbortCompletion { | 
| + return new GeneratorResult(null, notifications); | 
| + } | 
| + CompletionGetSuggestionsResult result = new CompletionGetSuggestionsResult( | 
| + collector.offset, collector.length, collector.suggestions); | 
| + return new GeneratorResult(result, notifications); | 
| + } | 
| +} | 
| + | 
| +/** | 
| + * The information about a requested list of completions. | 
| + * | 
| + * Clients may not extend, implement or mix-in this class. | 
| + */ | 
| +abstract class CompletionRequest { | 
| + /** | 
| + * Return the offset within the source at which the completion is being | 
| + * requested. | 
| + */ | 
| + int get offset; | 
| + | 
| + /** | 
| + * Return the resource provider associated with this request. | 
| + */ | 
| + ResourceProvider get resourceProvider; | 
| 
mfairhurst
2017/05/31 21:49:35
What is the purpose of the resource provider?
 
Brian Wilkerson
2017/05/31 22:51:47
It's needed by the completion contributor that wor
 
mfairhurst
2017/05/31 23:17:36
I would say then that it makes more sense for the
 
Brian Wilkerson
2017/06/03 16:44:37
If the request object holds on to the resource pro
 | 
| + | 
| + /** | 
| + * The analysis result for the file in which the completion is being | 
| + * requested. | 
| + */ | 
| + ResolveResult get result; | 
| + | 
| + /** | 
| + * Throw an [AbortCompletion] if the completion request has been aborted. | 
| + */ | 
| + void checkAborted(); | 
| +} |