| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library services.correction.assist; | 5 library services.correction.assist; |
| 6 | 6 |
| 7 import 'package:analysis_server/plugin/edit/assist/assist_core.dart'; | 7 import 'package:analysis_server/plugin/edit/assist/assist_core.dart'; |
| 8 import 'package:analysis_server/src/plugin/server_plugin.dart'; | 8 import 'package:analysis_server/src/plugin/server_plugin.dart'; |
| 9 import 'package:analyzer/src/generated/engine.dart'; | 9 import 'package:analyzer/src/generated/engine.dart'; |
| 10 import 'package:analyzer/src/generated/java_engine.dart'; | 10 import 'package:analyzer/src/generated/java_engine.dart'; |
| 11 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/source.dart'; |
| 12 import 'dart:async'; |
| 12 | 13 |
| 13 /** | 14 /** |
| 14 * Compute and return the assists available at the given selection (described by | 15 * Compute and return the assists available at the given selection (described by |
| 15 * the [offset] and [length]) in the given [source]. The source was analyzed in | 16 * the [offset] and [length]) in the given [source]. The source was analyzed in |
| 16 * the given [context]. The [plugin] is used to get the list of assist | 17 * the given [analysisContext]. The [plugin] is used to get the list of assist |
| 17 * contributors. | 18 * contributors. |
| 18 */ | 19 */ |
| 19 List<Assist> computeAssists(ServerPlugin plugin, AnalysisContext context, | 20 Future<List<Assist>> computeAssists( |
| 20 Source source, int offset, int length) { | 21 ServerPlugin plugin, |
| 22 AnalysisContext analysisContext, |
| 23 Source source, |
| 24 int offset, |
| 25 int length) async { |
| 21 List<Assist> assists = <Assist>[]; | 26 List<Assist> assists = <Assist>[]; |
| 22 List<AssistContributor> contributors = plugin.assistContributors; | 27 List<AssistContributor> contributors = plugin.assistContributors; |
| 28 AssistContextImpl assistContext = |
| 29 new AssistContextImpl(analysisContext, source, offset, length); |
| 23 for (AssistContributor contributor in contributors) { | 30 for (AssistContributor contributor in contributors) { |
| 24 try { | 31 try { |
| 25 List<Assist> contributedAssists = | 32 List<Assist> contributedAssists = |
| 26 contributor.computeAssists(context, source, offset, length); | 33 await contributor.computeAssists(assistContext); |
| 27 if (contributedAssists != null) { | 34 if (contributedAssists != null) { |
| 28 assists.addAll(contributedAssists); | 35 assists.addAll(contributedAssists); |
| 29 } | 36 } |
| 30 } catch (exception, stackTrace) { | 37 } catch (exception, stackTrace) { |
| 31 AnalysisEngine.instance.logger.logError( | 38 AnalysisEngine.instance.logger.logError( |
| 32 'Exception from assist contributor: ${contributor.runtimeType}', | 39 'Exception from assist contributor: ${contributor.runtimeType}', |
| 33 new CaughtException(exception, stackTrace)); | 40 new CaughtException(exception, stackTrace)); |
| 34 } | 41 } |
| 35 } | 42 } |
| 36 assists.sort(Assist.SORT_BY_RELEVANCE); | 43 assists.sort(Assist.SORT_BY_RELEVANCE); |
| 37 return assists; | 44 return assists; |
| 38 } | 45 } |
| 39 | 46 |
| 40 /** | 47 /** |
| 48 * The implementation of [AssistContext]. |
| 49 */ |
| 50 class AssistContextImpl implements AssistContext { |
| 51 @override |
| 52 final AnalysisContext analysisContext; |
| 53 |
| 54 @override |
| 55 final Source source; |
| 56 |
| 57 @override |
| 58 final int selectionOffset; |
| 59 |
| 60 @override |
| 61 final int selectionLength; |
| 62 |
| 63 AssistContextImpl(this.analysisContext, this.source, this.selectionOffset, |
| 64 this.selectionLength); |
| 65 } |
| 66 |
| 67 /** |
| 41 * An enumeration of possible assist kinds. | 68 * An enumeration of possible assist kinds. |
| 42 */ | 69 */ |
| 43 class DartAssistKind { | 70 class DartAssistKind { |
| 44 static const ADD_PART_DIRECTIVE = | 71 static const ADD_PART_DIRECTIVE = |
| 45 const AssistKind('ADD_PART_DIRECTIVE', 30, "Add 'part' directive"); | 72 const AssistKind('ADD_PART_DIRECTIVE', 30, "Add 'part' directive"); |
| 46 static const ADD_TYPE_ANNOTATION = | 73 static const ADD_TYPE_ANNOTATION = |
| 47 const AssistKind('ADD_TYPE_ANNOTATION', 30, "Add type annotation"); | 74 const AssistKind('ADD_TYPE_ANNOTATION', 30, "Add type annotation"); |
| 48 static const ASSIGN_TO_LOCAL_VARIABLE = const AssistKind( | 75 static const ASSIGN_TO_LOCAL_VARIABLE = const AssistKind( |
| 49 'ASSIGN_TO_LOCAL_VARIABLE', 30, "Assign value to new local variable"); | 76 'ASSIGN_TO_LOCAL_VARIABLE', 30, "Assign value to new local variable"); |
| 50 static const CONVERT_DOCUMENTATION_INTO_BLOCK = const AssistKind( | 77 static const CONVERT_DOCUMENTATION_INTO_BLOCK = const AssistKind( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 const AssistKind('SURROUND_WITH_FOR_IN', 30, "Surround with 'for-in'"); | 134 const AssistKind('SURROUND_WITH_FOR_IN', 30, "Surround with 'for-in'"); |
| 108 static const SURROUND_WITH_IF = | 135 static const SURROUND_WITH_IF = |
| 109 const AssistKind('SURROUND_WITH_IF', 30, "Surround with 'if'"); | 136 const AssistKind('SURROUND_WITH_IF', 30, "Surround with 'if'"); |
| 110 static const SURROUND_WITH_TRY_CATCH = const AssistKind( | 137 static const SURROUND_WITH_TRY_CATCH = const AssistKind( |
| 111 'SURROUND_WITH_TRY_CATCH', 30, "Surround with 'try-catch'"); | 138 'SURROUND_WITH_TRY_CATCH', 30, "Surround with 'try-catch'"); |
| 112 static const SURROUND_WITH_TRY_FINALLY = const AssistKind( | 139 static const SURROUND_WITH_TRY_FINALLY = const AssistKind( |
| 113 'SURROUND_WITH_TRY_FINALLY', 30, "Surround with 'try-finally'"); | 140 'SURROUND_WITH_TRY_FINALLY', 30, "Surround with 'try-finally'"); |
| 114 static const SURROUND_WITH_WHILE = | 141 static const SURROUND_WITH_WHILE = |
| 115 const AssistKind('SURROUND_WITH_WHILE', 30, "Surround with 'while'"); | 142 const AssistKind('SURROUND_WITH_WHILE', 30, "Surround with 'while'"); |
| 116 } | 143 } |
| OLD | NEW |