Chromium Code Reviews| 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 class and top level variable | |
|
scheglov
2014/08/10 21:45:14
You probably want to update the comment.
danrubel
2014/08/11 03:04:38
Done.
| |
| 15 * `completion.getSuggestions` request results | |
| 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 (d) => d is PartOfDirective || d is PartDirective); | |
| 34 } | |
| 35 | |
| 36 @override | |
| 37 Future<bool> computeFull(CompilationUnit unit, | |
| 38 List<CompletionSuggestion> suggestions) { | |
| 39 // TODO: implement computeFull | |
| 40 // include results from part files that are included in the library | |
| 41 return new Future.value(false); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * A visitor for collecting suggestions from the most specific child [AstNode] | |
| 47 * that contains the completion offset to the [CompilationUnit]. | |
| 48 */ | |
| 49 class _LocalVisitor extends GeneralizingAstVisitor<dynamic> { | |
| 50 final int offset; | |
| 51 final List<CompletionSuggestion> suggestions; | |
| 52 | |
| 53 _LocalVisitor(this.offset, this.suggestions); | |
| 54 | |
| 55 void addSuggestion(SimpleIdentifier id, CompletionSuggestionKind kind) { | |
| 56 if (id != null) { | |
| 57 String completion = id.name; | |
| 58 if (completion != null && completion.length > 0) { | |
| 59 suggestions.add( | |
| 60 new CompletionSuggestion( | |
| 61 kind, | |
| 62 CompletionRelevance.DEFAULT, | |
| 63 completion, | |
| 64 completion.length, | |
| 65 0, | |
| 66 false, | |
| 67 false)); | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void addSuggestions(VariableDeclarationList variables, | |
| 73 CompletionSuggestionKind kind) { | |
| 74 variables.variables.forEach((VariableDeclaration v) { | |
|
scheglov
2014/08/10 21:45:14
Could you use full words, not just single characte
danrubel
2014/08/11 03:04:38
Good point. I have cleaned several places in this
| |
| 75 addSuggestion(v.name, kind); | |
| 76 }); | |
| 77 } | |
| 78 | |
| 79 visitBlock(Block node) { | |
| 80 node.statements.forEach((Statement s) { | |
| 81 if (s.offset < offset) { | |
|
scheglov
2014/08/10 21:45:14
Not quite so easy.
This code is valid and would b
danrubel
2014/08/11 03:04:38
Good point. https://codereview.chromium.org/459743
| |
| 82 if (s is LabeledStatement) { | |
| 83 s.labels.forEach((l) { | |
| 84 l.label; | |
|
scheglov
2014/08/10 21:45:15
I don't think this statement is useful.
danrubel
2014/08/11 03:04:38
Removed and commented out following line as it is
| |
| 85 addSuggestion(l.label, CompletionSuggestionKind.LABEL); | |
| 86 }); | |
| 87 } else if (s is VariableDeclarationStatement) { | |
| 88 addSuggestions(s.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 m) { | |
| 103 if (m is FieldDeclaration) { | |
| 104 addSuggestions(m.fields, CompletionSuggestionKind.FIELD); | |
| 105 } else if (m is MethodDeclaration) { | |
| 106 addSuggestion(m.name, CompletionSuggestionKind.METHOD_NAME); | |
| 107 } | |
| 108 }); | |
| 109 visitNode(node); | |
| 110 } | |
| 111 | |
| 112 visitCompilationUnit(CompilationUnit node) { | |
| 113 node.directives.forEach((Directive d) { | |
| 114 if (d is ImportDirective) { | |
| 115 addSuggestion(d.prefix, CompletionSuggestionKind.LIBRARY_PREFIX); | |
| 116 } | |
| 117 }); | |
| 118 node.declarations.forEach((Declaration d) { | |
| 119 if (d is ClassDeclaration) { | |
| 120 addSuggestion(d.name, CompletionSuggestionKind.CLASS); | |
| 121 } else if (d is EnumDeclaration) { | |
| 122 addSuggestion(d.name, CompletionSuggestionKind.ENUM); | |
| 123 } else if (d is FunctionDeclaration) { | |
| 124 addSuggestion(d.name, CompletionSuggestionKind.FUNCTION); | |
| 125 } else if (d is TopLevelVariableDeclaration) { | |
| 126 addSuggestions( | |
| 127 d.variables, | |
| 128 CompletionSuggestionKind.TOP_LEVEL_VARIABLE); | |
| 129 } else if (d is ClassTypeAlias) { | |
| 130 addSuggestion(d.name, CompletionSuggestionKind.CLASS_ALIAS); | |
| 131 } else if (d is FunctionTypeAlias) { | |
| 132 addSuggestion(d.name, CompletionSuggestionKind.FUNCTION_TYPE_ALIAS); | |
| 133 } | |
| 134 }); | |
| 135 } | |
| 136 | |
| 137 visitForEachStatement(ForEachStatement node) { | |
| 138 addSuggestion(node.identifier, CompletionSuggestionKind.VARIABLE); | |
| 139 visitNode(node); | |
| 140 } | |
| 141 | |
| 142 visitForStatement(ForStatement node) { | |
| 143 addSuggestions(node.variables, CompletionSuggestionKind.VARIABLE); | |
| 144 visitNode(node); | |
| 145 } | |
| 146 | |
| 147 visitFunctionDeclaration(FunctionDeclaration node) { | |
| 148 addSuggestion(node.name, CompletionSuggestionKind.FUNCTION); | |
| 149 visitNode(node); | |
| 150 } | |
| 151 | |
| 152 visitFunctionExpression(FunctionExpression node) { | |
| 153 node.parameters.parameters.forEach((FormalParameter p) { | |
| 154 addSuggestion(p.identifier, CompletionSuggestionKind.PARAMETER); | |
| 155 }); | |
| 156 visitNode(node); | |
| 157 } | |
| 158 | |
| 159 visitMethodDeclaration(MethodDeclaration node) { | |
| 160 node.parameters.parameters.forEach((FormalParameter p) { | |
| 161 if (p.identifier != null) { | |
| 162 addSuggestion(p.identifier, CompletionSuggestionKind.PARAMETER); | |
| 163 } | |
| 164 }); | |
| 165 visitNode(node); | |
| 166 } | |
| 167 | |
| 168 visitNode(AstNode node) { | |
| 169 node.parent.accept(this); | |
| 170 } | |
| 171 } | |
| OLD | NEW |