 Chromium Code Reviews
 Chromium Code Reviews Issue 2918613002:
  Add completion support for plugins  (Closed)
    
  
    Issue 2918613002:
  Add completion support for plugins  (Closed) 
  | OLD | NEW | 
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:async'; | |
| 6 | |
| 7 import 'package:analyzer/dart/analysis/results.dart'; | |
| 8 import 'package:analyzer/file_system/file_system.dart'; | |
| 9 import 'package:analyzer_plugin/protocol/protocol.dart'; | |
| 10 import 'package:analyzer_plugin/protocol/protocol_common.dart'; | |
| 11 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; | |
| 12 import 'package:analyzer_plugin/src/utilities/completion.dart'; | |
| 13 import 'package:analyzer_plugin/utilities/generator.dart'; | |
| 14 | |
| 15 /** | |
| 16 * An exception that is thrown when the current completion request should be | |
| 17 * aborted because either the source changed since the request was made, or | |
| 18 * a new completion request was received. | |
| 19 */ | |
| 20 class AbortCompletion {} | |
| 21 | |
| 22 /** | |
| 23 * An object that [CompletionContributor]s use to record completion suggestions. | |
| 24 * | |
| 25 * Clients may not extend, implement or mix-in this class. | |
| 26 */ | |
| 27 abstract class CompletionCollector { | |
| 28 /** | |
| 29 * Set the length of the region of text that should be replaced by the | |
| 30 * selected completion suggestion. | |
| 31 */ | |
| 32 void set length(int length); | |
| 33 | |
| 34 /** | |
| 35 * Set the offset of the region of text that should be replaced by the | |
| 36 * selected completion suggestion. | |
| 37 */ | |
| 38 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
 | |
| 39 | |
| 40 /** | |
| 41 * Record the given completion [suggestion]. | |
| 42 */ | |
| 43 void addSuggestion(CompletionSuggestion suggestion); | |
| 44 } | |
| 45 | |
| 46 /** | |
| 47 * An object used to produce completion suggestions. | |
| 48 * | |
| 49 * Clients may implement this class when implementing plugins. | |
| 50 */ | |
| 51 abstract class CompletionContributor { | |
| 52 /** | |
| 53 * Contribute completion suggestions for the completion location specified by | |
| 54 * the given [request] into the given [collector]. | |
| 55 */ | |
| 56 Future<Null> computeSuggestions( | |
| 57 CompletionRequest request, CompletionCollector collector); | |
| 58 } | |
| 59 | |
| 60 /** | |
| 61 * A generator that will generate a 'completion.getSuggestions' response. | |
| 62 * | |
| 63 * Clients may not extend, implement or mix-in this class. | |
| 64 */ | |
| 65 class CompletionGenerator { | |
| 66 /** | |
| 67 * The contributors to be used to generate the completion suggestions. | |
| 68 */ | |
| 69 final List<CompletionContributor> contributors; | |
| 70 | |
| 71 /** | |
| 72 * Initialize a newly created completion generator. | |
| 73 */ | |
| 74 CompletionGenerator(this.contributors); | |
| 75 | |
| 76 /** | |
| 77 * Create a 'completion.getSuggestions' response for the file with the given | |
| 78 * [path]. If any of the contributors throws an exception, also create a | |
| 79 * non-fatal 'plugin.error' notification. | |
| 80 */ | |
| 81 Future<GeneratorResult> generateCompletionResponse( | |
| 82 CompletionRequest request) async { | |
| 83 List<Notification> notifications = <Notification>[]; | |
| 84 CompletionCollectorImpl collector = new CompletionCollectorImpl(); | |
| 85 try { | |
| 86 for (CompletionContributor contributor in contributors) { | |
| 87 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
 | |
| 88 try { | |
| 89 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
 | |
| 90 } 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
 | |
| 91 notifications.add(new PluginErrorParams( | |
| 92 false, exception.toString(), stackTrace.toString()) | |
| 93 .toNotification()); | |
| 94 } | |
| 95 } | |
| 96 } on AbortCompletion { | |
| 97 return new GeneratorResult(null, notifications); | |
| 98 } | |
| 99 CompletionGetSuggestionsResult result = new CompletionGetSuggestionsResult( | |
| 100 collector.offset, collector.length, collector.suggestions); | |
| 101 return new GeneratorResult(result, notifications); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 /** | |
| 106 * The information about a requested list of completions. | |
| 107 * | |
| 108 * Clients may not extend, implement or mix-in this class. | |
| 109 */ | |
| 110 abstract class CompletionRequest { | |
| 111 /** | |
| 112 * Return the offset within the source at which the completion is being | |
| 113 * requested. | |
| 114 */ | |
| 115 int get offset; | |
| 116 | |
| 117 /** | |
| 118 * Return the resource provider associated with this request. | |
| 119 */ | |
| 120 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
 | |
| 121 | |
| 122 /** | |
| 123 * The analysis result for the file in which the completion is being | |
| 124 * requested. | |
| 125 */ | |
| 126 ResolveResult get result; | |
| 127 | |
| 128 /** | |
| 129 * Throw an [AbortCompletion] if the completion request has been aborted. | |
| 130 */ | |
| 131 void checkAborted(); | |
| 132 } | |
| OLD | NEW |