| 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 library services.completion.computer.dart.keyword; | 5 library services.completion.computer.dart.keyword; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/services/completion/completion_suggestion.da
rt'; | 9 import 'package:analysis_server/src/services/completion/completion_suggestion.da
rt'; |
| 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 /** | 32 /** |
| 33 * A vistor for generating keyword suggestions. | 33 * A vistor for generating keyword suggestions. |
| 34 */ | 34 */ |
| 35 class _KeywordVisitor extends GeneralizingAstVisitor { | 35 class _KeywordVisitor extends GeneralizingAstVisitor { |
| 36 final DartCompletionRequest request; | 36 final DartCompletionRequest request; |
| 37 | 37 |
| 38 _KeywordVisitor(this.request); | 38 _KeywordVisitor(this.request); |
| 39 | 39 |
| 40 @override | 40 @override |
| 41 visitClassDeclaration(ClassDeclaration node) { | 41 visitClassDeclaration(ClassDeclaration node) { |
| 42 _addSuggestions([Keyword.EXTENDS, Keyword.IMPLEMENTS, Keyword.WITH]); | 42 // Very simplistic suggestion because analyzer will warn if |
| 43 // the extends / with / implements keywords are out of order |
| 44 if (node.extendsClause == null) { |
| 45 _addSuggestion(Keyword.EXTENDS); |
| 46 } else if (node.withClause == null) { |
| 47 _addSuggestion(Keyword.WITH); |
| 48 } |
| 49 if (node.implementsClause == null) { |
| 50 _addSuggestion(Keyword.IMPLEMENTS); |
| 51 } |
| 43 } | 52 } |
| 44 | 53 |
| 45 @override | 54 @override |
| 46 visitCompilationUnit(CompilationUnit node) { | 55 visitCompilationUnit(CompilationUnit node) { |
| 47 _addSuggestions( | 56 Directive firstDirective; |
| 48 [ | 57 int endOfDirectives = 0; |
| 49 Keyword.ABSTRACT, | 58 if (node.directives.length > 0) { |
| 50 Keyword.CLASS, | 59 firstDirective = node.directives[0]; |
| 51 Keyword.CONST, | 60 endOfDirectives = node.directives.last.end - 1; |
| 52 Keyword.EXPORT, | 61 } |
| 53 Keyword.FINAL, | 62 int startOfDeclarations = node.end; |
| 54 Keyword.IMPORT, | 63 if (node.declarations.length > 0) { |
| 55 Keyword.LIBRARY, | 64 startOfDeclarations = node.declarations[0].offset; |
| 56 Keyword.PART, | 65 } |
| 57 Keyword.TYPEDEF, | 66 |
| 58 Keyword.VAR]); | 67 // Simplistic check for library as first directive |
| 68 if (firstDirective is! LibraryDirective) { |
| 69 if (firstDirective != null) { |
| 70 if (request.offset <= firstDirective.offset) { |
| 71 _addSuggestions([Keyword.LIBRARY]); |
| 72 } |
| 73 } else { |
| 74 if (request.offset <= startOfDeclarations) { |
| 75 _addSuggestions([Keyword.LIBRARY]); |
| 76 } |
| 77 } |
| 78 } |
| 79 if (request.offset <= startOfDeclarations) { |
| 80 _addSuggestions([Keyword.EXPORT, Keyword.IMPORT, Keyword.PART]); |
| 81 } |
| 82 if (request.offset >= endOfDirectives) { |
| 83 _addSuggestions( |
| 84 [ |
| 85 Keyword.ABSTRACT, |
| 86 Keyword.CLASS, |
| 87 Keyword.CONST, |
| 88 Keyword.FINAL, |
| 89 Keyword.TYPEDEF, |
| 90 Keyword.VAR]); |
| 91 } |
| 59 } | 92 } |
| 60 | 93 |
| 61 @override | 94 @override |
| 62 visitNode(AstNode node) { | 95 visitNode(AstNode node) { |
| 63 // ignored | 96 if (request.offset == node.end) { |
| 97 Token token = node.endToken; |
| 98 if (token != null && !token.isSynthetic) { |
| 99 if (token.lexeme == ';' || token.lexeme == '}') { |
| 100 node.parent.accept(this); |
| 101 } |
| 102 } |
| 103 } |
| 104 } |
| 105 |
| 106 void _addSuggestion(Keyword keyword) { |
| 107 String completion = keyword.syntax; |
| 108 request.suggestions.add( |
| 109 new CompletionSuggestion( |
| 110 CompletionSuggestionKind.KEYWORD, |
| 111 CompletionRelevance.DEFAULT, |
| 112 completion, |
| 113 completion.length, |
| 114 0, |
| 115 false, |
| 116 false)); |
| 64 } | 117 } |
| 65 | 118 |
| 66 void _addSuggestions(List<Keyword> keywords) { | 119 void _addSuggestions(List<Keyword> keywords) { |
| 67 keywords.forEach((Keyword keyword) { | 120 keywords.forEach((Keyword keyword) { |
| 68 String completion = keyword.syntax; | 121 _addSuggestion(keyword); |
| 69 request.suggestions.add( | |
| 70 new CompletionSuggestion( | |
| 71 CompletionSuggestionKind.KEYWORD, | |
| 72 CompletionRelevance.DEFAULT, | |
| 73 completion, | |
| 74 completion.length, | |
| 75 0, | |
| 76 false, | |
| 77 false)); | |
| 78 }); | 122 }); |
| 79 } | 123 } |
| 80 } | 124 } |
| OLD | NEW |