| 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 import 'dart:async'; | |
| 6 | |
| 7 import 'package:analysis_server/plugin/edit/assist/assist_core.dart'; | 5 import 'package:analysis_server/plugin/edit/assist/assist_core.dart'; |
| 8 import 'package:analysis_server/src/plugin/server_plugin.dart'; | 6 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 9 import 'package:analyzer/exception/exception.dart'; | |
| 10 import 'package:analyzer/src/generated/engine.dart'; | |
| 11 import 'package:analyzer/src/generated/source.dart'; | 7 import 'package:analyzer/src/generated/source.dart'; |
| 12 | 8 |
| 13 /** | 9 /** |
| 14 * 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 given [analysisContext]. The [plugin] is used to get the list of assist | |
| 17 * contributors. | |
| 18 */ | |
| 19 Future<List<Assist>> computeAssists( | |
| 20 ServerPlugin plugin, | |
| 21 AnalysisContext analysisContext, | |
| 22 Source source, | |
| 23 int offset, | |
| 24 int length) async { | |
| 25 List<Assist> assists = <Assist>[]; | |
| 26 List<AssistContributor> contributors = plugin.assistContributors; | |
| 27 AssistContextImpl assistContext = | |
| 28 new AssistContextImpl(analysisContext, source, offset, length); | |
| 29 for (AssistContributor contributor in contributors) { | |
| 30 try { | |
| 31 List<Assist> contributedAssists = | |
| 32 await contributor.computeAssists(assistContext); | |
| 33 if (contributedAssists != null) { | |
| 34 assists.addAll(contributedAssists); | |
| 35 } | |
| 36 } catch (exception, stackTrace) { | |
| 37 AnalysisEngine.instance.logger.logError( | |
| 38 'Exception from assist contributor: ${contributor.runtimeType}', | |
| 39 new CaughtException(exception, stackTrace)); | |
| 40 } | |
| 41 } | |
| 42 assists.sort(Assist.SORT_BY_RELEVANCE); | |
| 43 return assists; | |
| 44 } | |
| 45 | |
| 46 /** | |
| 47 * The implementation of [AssistContext]. | 10 * The implementation of [AssistContext]. |
| 48 */ | 11 */ |
| 49 class AssistContextImpl implements AssistContext { | 12 class AssistContextImpl implements AssistContext { |
| 50 @override | 13 @override |
| 51 final AnalysisContext analysisContext; | 14 final AnalysisDriver analysisDriver; |
| 52 | 15 |
| 53 @override | 16 @override |
| 54 final Source source; | 17 final Source source; |
| 55 | 18 |
| 56 @override | 19 @override |
| 57 final int selectionOffset; | 20 final int selectionOffset; |
| 58 | 21 |
| 59 @override | 22 @override |
| 60 final int selectionLength; | 23 final int selectionLength; |
| 61 | 24 |
| 62 AssistContextImpl(this.analysisContext, this.source, this.selectionOffset, | 25 AssistContextImpl(this.analysisDriver, this.source, this.selectionOffset, |
| 63 this.selectionLength); | 26 this.selectionLength); |
| 64 } | 27 } |
| 65 | 28 |
| 66 /** | 29 /** |
| 67 * An enumeration of possible assist kinds. | 30 * An enumeration of possible assist kinds. |
| 68 */ | 31 */ |
| 69 class DartAssistKind { | 32 class DartAssistKind { |
| 70 static const ADD_PART_DIRECTIVE = | 33 static const ADD_PART_DIRECTIVE = |
| 71 const AssistKind('ADD_PART_DIRECTIVE', 30, "Add 'part' directive"); | 34 const AssistKind('ADD_PART_DIRECTIVE', 30, "Add 'part' directive"); |
| 72 static const ADD_TYPE_ANNOTATION = | 35 static const ADD_TYPE_ANNOTATION = |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 const AssistKind('SURROUND_WITH_FOR_IN', 30, "Surround with 'for-in'"); | 114 const AssistKind('SURROUND_WITH_FOR_IN', 30, "Surround with 'for-in'"); |
| 152 static const SURROUND_WITH_IF = | 115 static const SURROUND_WITH_IF = |
| 153 const AssistKind('SURROUND_WITH_IF', 30, "Surround with 'if'"); | 116 const AssistKind('SURROUND_WITH_IF', 30, "Surround with 'if'"); |
| 154 static const SURROUND_WITH_TRY_CATCH = const AssistKind( | 117 static const SURROUND_WITH_TRY_CATCH = const AssistKind( |
| 155 'SURROUND_WITH_TRY_CATCH', 30, "Surround with 'try-catch'"); | 118 'SURROUND_WITH_TRY_CATCH', 30, "Surround with 'try-catch'"); |
| 156 static const SURROUND_WITH_TRY_FINALLY = const AssistKind( | 119 static const SURROUND_WITH_TRY_FINALLY = const AssistKind( |
| 157 'SURROUND_WITH_TRY_FINALLY', 30, "Surround with 'try-finally'"); | 120 'SURROUND_WITH_TRY_FINALLY', 30, "Surround with 'try-finally'"); |
| 158 static const SURROUND_WITH_WHILE = | 121 static const SURROUND_WITH_WHILE = |
| 159 const AssistKind('SURROUND_WITH_WHILE', 30, "Surround with 'while'"); | 122 const AssistKind('SURROUND_WITH_WHILE', 30, "Surround with 'while'"); |
| 160 } | 123 } |
| OLD | NEW |