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

Unified Diff: pkg/analysis_server/lib/src/services/correction/assist_internal.dart

Issue 680133003: Issue 21458. Support for 'Add type' Quick Assist in for-each loops. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/services/correction/assist_internal.dart
diff --git a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
index 964c87bd7253b809e506ae6b94170a9f315db6e1..11c06feed9d87aff1654a314b3ca94ea6163f7a3 100644
--- a/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/assist_internal.dart
@@ -73,7 +73,8 @@ class AssistProcessor {
node =
new NodeLocator.con2(selectionOffset, selectionEnd).searchWithin(unit);
// try to add proposals
- _addProposal_addTypeAnnotation();
+ _addProposal_addTypeAnnotation_DeclaredIdentifier();
+ _addProposal_addTypeAnnotation_VariableDeclaration();
_addProposal_assignToLocalVariable();
_addProposal_convertToBlockFunctionBody();
_addProposal_convertToExpressionFunctionBody();
@@ -166,7 +167,49 @@ class AssistProcessor {
edits.add(edit);
}
- void _addProposal_addTypeAnnotation() {
+ void _addProposal_addTypeAnnotation_DeclaredIdentifier() {
+ DeclaredIdentifier declaredIdentifier =
+ node.getAncestor((n) => n is DeclaredIdentifier);
+ if (declaredIdentifier == null) {
+ ForEachStatement forEach = node.getAncestor((n) => n is ForEachStatement);
+ int offset = node.offset;
+ if (forEach != null &&
+ forEach.iterator != null &&
+ offset < forEach.iterator.offset) {
+ declaredIdentifier = forEach.loopVariable;
+ }
+ }
+ if (declaredIdentifier == null) {
+ _coverageMarker();
+ return;
+ }
+ // may be has type annotation already
+ if (declaredIdentifier.type != null) {
+ _coverageMarker();
+ return;
+ }
+ // prepare type source
+ String typeSource;
+ DartType type = declaredIdentifier.identifier.bestType;
+ if (type is InterfaceType || type is FunctionType) {
+ typeSource = utils.getTypeSource(type);
+ } else {
+ _coverageMarker();
+ return;
+ }
+ // add edit
+ Token keyword = declaredIdentifier.keyword;
+ if (keyword is KeywordToken && keyword.keyword == Keyword.VAR) {
+ SourceRange range = rangeToken(keyword);
+ _addReplaceEdit(range, typeSource);
+ } else {
+ _addInsertEdit(declaredIdentifier.identifier.offset, '$typeSource ');
+ }
+ // add proposal
+ _addAssist(AssistKind.ADD_TYPE_ANNOTATION, []);
+ }
+
+ void _addProposal_addTypeAnnotation_VariableDeclaration() {
// prepare VariableDeclarationList
VariableDeclarationList declarationList =
node.getAncestor((node) => node is VariableDeclarationList);
« no previous file with comments | « no previous file | pkg/analysis_server/test/mock_sdk.dart » ('j') | pkg/analysis_server/test/services/correction/assist_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698