Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(788)

Side by Side Diff: pkg/analysis_server/lib/src/services/correction/assist.dart

Issue 1073383002: Create a public API for contributing assists and make assists pluggable (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/src/protocol.dart'; 7 import 'package:analysis_server/edit/assist/assist_core.dart';
8 import 'package:analysis_server/src/services/correction/assist_internal.dart'; 8 import 'package:analysis_server/src/plugin/server_plugin.dart';
9 import 'package:analyzer/src/generated/ast.dart'; 9 import 'package:analyzer/src/generated/engine.dart';
10 import 'package:analyzer/src/generated/java_engine.dart';
10 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
11 12
12 /** 13 /**
13 * Computes [Assist]s at the given location. 14 * Compute and return the assists available at the given selection (described by
14 * 15 * the [offset] and [length]) in the given [source]. The source was analyzed in
15 * Returns the computed [Assist]s, not `null`. 16 * the given [context]. The [plugin] is used to get the list of assist
17 * contributors.
16 */ 18 */
17 List<Assist> computeAssists(CompilationUnit unit, int offset, int length) { 19 List<Assist> computeAssists(ServerPlugin plugin, AnalysisContext context,
18 Source source = unit.element.source; 20 Source source, int offset, int length) {
19 String file = source.fullName; 21 List<Assist> assists = <Assist>[];
20 AssistProcessor processor = 22 List<AssistContributor> contributors = plugin.assistContributors();
21 new AssistProcessor(source, file, unit, offset, length); 23 for (AssistContributor contributor in contributors) {
22 return processor.compute(); 24 try {
25 List<Assist> contributedAssists =
26 contributor.compute(context, source, offset, length);
27 if (contributedAssists != null) {
28 assists.addAll(contributedAssists);
29 }
30 } catch (exception, stackTrace) {
31 AnalysisEngine.instance.logger.logError(
32 'Exception from assist contributor: ${contributor.runtimeType}',
33 new CaughtException(exception, stackTrace));
34 }
35 }
36 assists.sort(Assist.SORT_BY_RELEVANCE);
37 return assists;
23 } 38 }
24 39
25 /** 40 /**
26 * A description of a single proposed assist. 41 * An enumeration of possible assist kinds.
27 */ 42 */
28 class Assist { 43 class DartAssistKind {
29 final AssistKind kind;
30 final SourceChange change;
31
32 Assist(this.kind, this.change);
33
34 @override
35 String toString() {
36 return 'Assist(kind=$kind, change=$change)';
37 }
38 }
39
40 /**
41 * An enumeration of possible quick assist kinds.
42 */
43 class AssistKind {
44 static const ADD_PART_DIRECTIVE = 44 static const ADD_PART_DIRECTIVE =
45 const AssistKind('ADD_PART_DIRECTIVE', 30, "Add 'part' directive"); 45 const AssistKind('ADD_PART_DIRECTIVE', 30, "Add 'part' directive");
46 static const ADD_TYPE_ANNOTATION = 46 static const ADD_TYPE_ANNOTATION =
47 const AssistKind('ADD_TYPE_ANNOTATION', 30, "Add type annotation"); 47 const AssistKind('ADD_TYPE_ANNOTATION', 30, "Add type annotation");
48 static const ASSIGN_TO_LOCAL_VARIABLE = const AssistKind( 48 static const ASSIGN_TO_LOCAL_VARIABLE = const AssistKind(
49 'ASSIGN_TO_LOCAL_VARIABLE', 30, "Assign value to new local variable"); 49 'ASSIGN_TO_LOCAL_VARIABLE', 30, "Assign value to new local variable");
50 static const CONVERT_INTO_BLOCK_BODY = const AssistKind( 50 static const CONVERT_INTO_BLOCK_BODY = const AssistKind(
51 'CONVERT_INTO_BLOCK_BODY', 30, "Convert into block body"); 51 'CONVERT_INTO_BLOCK_BODY', 30, "Convert into block body");
52 static const CONVERT_INTO_EXPRESSION_BODY = const AssistKind( 52 static const CONVERT_INTO_EXPRESSION_BODY = const AssistKind(
53 'CONVERT_INTO_EXPRESSION_BODY', 30, "Convert into expression body"); 53 'CONVERT_INTO_EXPRESSION_BODY', 30, "Convert into expression body");
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 static const SURROUND_WITH_FOR_IN = 96 static const SURROUND_WITH_FOR_IN =
97 const AssistKind('SURROUND_WITH_FOR_IN', 30, "Surround with 'for-in'"); 97 const AssistKind('SURROUND_WITH_FOR_IN', 30, "Surround with 'for-in'");
98 static const SURROUND_WITH_IF = 98 static const SURROUND_WITH_IF =
99 const AssistKind('SURROUND_WITH_IF', 30, "Surround with 'if'"); 99 const AssistKind('SURROUND_WITH_IF', 30, "Surround with 'if'");
100 static const SURROUND_WITH_TRY_CATCH = const AssistKind( 100 static const SURROUND_WITH_TRY_CATCH = const AssistKind(
101 'SURROUND_WITH_TRY_CATCH', 30, "Surround with 'try-catch'"); 101 'SURROUND_WITH_TRY_CATCH', 30, "Surround with 'try-catch'");
102 static const SURROUND_WITH_TRY_FINALLY = const AssistKind( 102 static const SURROUND_WITH_TRY_FINALLY = const AssistKind(
103 'SURROUND_WITH_TRY_FINALLY', 30, "Surround with 'try-finally'"); 103 'SURROUND_WITH_TRY_FINALLY', 30, "Surround with 'try-finally'");
104 static const SURROUND_WITH_WHILE = 104 static const SURROUND_WITH_WHILE =
105 const AssistKind('SURROUND_WITH_WHILE', 30, "Surround with 'while'"); 105 const AssistKind('SURROUND_WITH_WHILE', 30, "Surround with 'while'");
106
107 final name;
108 final int relevance;
109 final String message;
110
111 const AssistKind(this.name, this.relevance, this.message);
112
113 @override
114 String toString() => name;
115 } 106 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698