| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library services.completion.computer.dart.local; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_services/completion/completion_computer.dart'; |
| 10 import 'package:analysis_services/completion/completion_suggestion.dart'; |
| 11 import 'package:analyzer/src/generated/ast.dart'; |
| 12 |
| 13 /** |
| 14 * A computer for calculating `completion.getSuggestions` request results |
| 15 * for the local library in which the completion is requested. |
| 16 */ |
| 17 class LocalComputer extends CompletionComputer { |
| 18 |
| 19 @override |
| 20 bool computeFast(CompilationUnit unit, |
| 21 List<CompletionSuggestion> suggestions) { |
| 22 |
| 23 // Find the specific child [AstNode] that contains the completion offset |
| 24 // and collect suggestions starting with that node |
| 25 AstNode node = new NodeLocator.con1(offset).searchWithin(unit); |
| 26 if (node != null) { |
| 27 node.accept(new _LocalVisitor(offset, suggestions)); |
| 28 } |
| 29 |
| 30 // If the unit is not a part and does not reference any parts |
| 31 // then work is complete |
| 32 return !unit.directives.any( |
| 33 (Directive directive) => |
| 34 directive is PartOfDirective || directive is PartDirective); |
| 35 } |
| 36 |
| 37 @override |
| 38 Future<bool> computeFull(CompilationUnit unit, |
| 39 List<CompletionSuggestion> suggestions) { |
| 40 // TODO: implement computeFull |
| 41 // include results from part files that are included in the library |
| 42 return new Future.value(false); |
| 43 } |
| 44 } |
| 45 |
| 46 /** |
| 47 * A visitor for collecting suggestions from the most specific child [AstNode] |
| 48 * that contains the completion offset to the [CompilationUnit]. |
| 49 */ |
| 50 class _LocalVisitor extends GeneralizingAstVisitor<dynamic> { |
| 51 final int offset; |
| 52 final List<CompletionSuggestion> suggestions; |
| 53 |
| 54 _LocalVisitor(this.offset, this.suggestions); |
| 55 |
| 56 void addSuggestion(SimpleIdentifier id, CompletionSuggestionKind kind) { |
| 57 if (id != null) { |
| 58 String completion = id.name; |
| 59 if (completion != null && completion.length > 0) { |
| 60 suggestions.add( |
| 61 new CompletionSuggestion( |
| 62 kind, |
| 63 CompletionRelevance.DEFAULT, |
| 64 completion, |
| 65 completion.length, |
| 66 0, |
| 67 false, |
| 68 false)); |
| 69 } |
| 70 } |
| 71 } |
| 72 |
| 73 void addSuggestions(VariableDeclarationList variables, |
| 74 CompletionSuggestionKind kind) { |
| 75 variables.variables.forEach((VariableDeclaration varDecl) { |
| 76 addSuggestion(varDecl.name, kind); |
| 77 }); |
| 78 } |
| 79 |
| 80 visitBlock(Block node) { |
| 81 node.statements.forEach((Statement stmt) { |
| 82 if (stmt.offset < offset) { |
| 83 if (stmt is LabeledStatement) { |
| 84 stmt.labels.forEach((Label label) { |
| 85 // addSuggestion(label.label, CompletionSuggestionKind.LABEL); |
| 86 }); |
| 87 } else if (stmt is VariableDeclarationStatement) { |
| 88 addSuggestions(stmt.variables, CompletionSuggestionKind.VARIABLE); |
| 89 } |
| 90 } |
| 91 }); |
| 92 visitNode(node); |
| 93 } |
| 94 |
| 95 visitCatchClause(CatchClause node) { |
| 96 addSuggestion(node.exceptionParameter, CompletionSuggestionKind.PARAMETER); |
| 97 addSuggestion(node.stackTraceParameter, CompletionSuggestionKind.PARAMETER); |
| 98 visitNode(node); |
| 99 } |
| 100 |
| 101 visitClassDeclaration(ClassDeclaration node) { |
| 102 node.members.forEach((ClassMember classMbr) { |
| 103 if (classMbr is FieldDeclaration) { |
| 104 addSuggestions(classMbr.fields, CompletionSuggestionKind.FIELD); |
| 105 } else if (classMbr is MethodDeclaration) { |
| 106 addSuggestion(classMbr.name, CompletionSuggestionKind.METHOD_NAME); |
| 107 } |
| 108 }); |
| 109 visitNode(node); |
| 110 } |
| 111 |
| 112 visitCompilationUnit(CompilationUnit node) { |
| 113 node.directives.forEach((Directive directive) { |
| 114 if (directive is ImportDirective) { |
| 115 addSuggestion( |
| 116 directive.prefix, |
| 117 CompletionSuggestionKind.LIBRARY_PREFIX); |
| 118 } |
| 119 }); |
| 120 node.declarations.forEach((Declaration declaration) { |
| 121 if (declaration is ClassDeclaration) { |
| 122 addSuggestion(declaration.name, CompletionSuggestionKind.CLASS); |
| 123 } else if (declaration is EnumDeclaration) { |
| 124 // addSuggestion(d.name, CompletionSuggestionKind.ENUM); |
| 125 } else if (declaration is FunctionDeclaration) { |
| 126 addSuggestion(declaration.name, CompletionSuggestionKind.FUNCTION); |
| 127 } else if (declaration is TopLevelVariableDeclaration) { |
| 128 addSuggestions( |
| 129 declaration.variables, |
| 130 CompletionSuggestionKind.TOP_LEVEL_VARIABLE); |
| 131 } else if (declaration is ClassTypeAlias) { |
| 132 addSuggestion(declaration.name, CompletionSuggestionKind.CLASS_ALIAS); |
| 133 } else if (declaration is FunctionTypeAlias) { |
| 134 addSuggestion( |
| 135 declaration.name, |
| 136 CompletionSuggestionKind.FUNCTION_TYPE_ALIAS); |
| 137 } |
| 138 }); |
| 139 } |
| 140 |
| 141 visitForEachStatement(ForEachStatement node) { |
| 142 addSuggestion(node.identifier, CompletionSuggestionKind.VARIABLE); |
| 143 visitNode(node); |
| 144 } |
| 145 |
| 146 visitForStatement(ForStatement node) { |
| 147 addSuggestions(node.variables, CompletionSuggestionKind.VARIABLE); |
| 148 visitNode(node); |
| 149 } |
| 150 |
| 151 visitFunctionDeclaration(FunctionDeclaration node) { |
| 152 addSuggestion(node.name, CompletionSuggestionKind.FUNCTION); |
| 153 visitNode(node); |
| 154 } |
| 155 |
| 156 visitFunctionExpression(FunctionExpression node) { |
| 157 node.parameters.parameters.forEach((FormalParameter param) { |
| 158 addSuggestion(param.identifier, CompletionSuggestionKind.PARAMETER); |
| 159 }); |
| 160 visitNode(node); |
| 161 } |
| 162 |
| 163 visitMethodDeclaration(MethodDeclaration node) { |
| 164 node.parameters.parameters.forEach((FormalParameter param) { |
| 165 if (param.identifier != null) { |
| 166 addSuggestion(param.identifier, CompletionSuggestionKind.PARAMETER); |
| 167 } |
| 168 }); |
| 169 visitNode(node); |
| 170 } |
| 171 |
| 172 visitNode(AstNode node) { |
| 173 node.parent.accept(this); |
| 174 } |
| 175 } |
| OLD | NEW |