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

Side by Side Diff: pkg/analysis_server/lib/src/services/completion/keyword_computer.dart

Issue 543393002: improve keyword suggestions (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 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.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/protocol.dart'; 9 import 'package:analysis_server/src/protocol.dart';
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 20 matching lines...) Expand all
31 31
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 visitBlock(Block node) {
42 if (_isOffsetAfterNode(node)) {
43 node.parent.accept(this);
44 } else {
45 _addSuggestions(
46 [
47 Keyword.ASSERT,
48 Keyword.CASE,
49 Keyword.CONTINUE,
50 Keyword.DO,
51 Keyword.FACTORY,
52 Keyword.FINAL,
53 Keyword.FOR,
54 Keyword.IF,
55 Keyword.NEW,
56 Keyword.RETHROW,
57 Keyword.RETURN,
58 Keyword.SUPER,
59 Keyword.SWITCH,
60 Keyword.THIS,
61 Keyword.THROW,
62 Keyword.TRY,
63 Keyword.VAR,
64 Keyword.VOID,
65 Keyword.WHILE]);
66 }
67 }
68
69 @override
41 visitClassDeclaration(ClassDeclaration node) { 70 visitClassDeclaration(ClassDeclaration node) {
71 // Inside the class declaration { }
72 if (request.offset > node.leftBracket.offset) {
73 _addSuggestions(
74 [
75 Keyword.CONST,
76 Keyword.DYNAMIC,
77 Keyword.FACTORY,
78 Keyword.FINAL,
79 Keyword.GET,
80 Keyword.OPERATOR,
81 Keyword.SET,
82 Keyword.STATIC,
83 Keyword.VAR,
84 Keyword.VOID]);
85 return;
86 }
42 // Very simplistic suggestion because analyzer will warn if 87 // Very simplistic suggestion because analyzer will warn if
43 // the extends / with / implements keywords are out of order 88 // the extends / with / implements keywords are out of order
44 if (node.extendsClause == null) { 89 if (node.extendsClause == null) {
45 _addSuggestion(Keyword.EXTENDS); 90 _addSuggestion(Keyword.EXTENDS);
46 } else if (node.withClause == null) { 91 } else if (node.withClause == null) {
47 _addSuggestion(Keyword.WITH); 92 _addSuggestion(Keyword.WITH);
48 } 93 }
49 if (node.implementsClause == null) { 94 if (node.implementsClause == null) {
50 _addSuggestion(Keyword.IMPLEMENTS); 95 _addSuggestion(Keyword.IMPLEMENTS);
51 } 96 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 Keyword.CLASS, 138 Keyword.CLASS,
94 Keyword.CONST, 139 Keyword.CONST,
95 Keyword.FINAL, 140 Keyword.FINAL,
96 Keyword.TYPEDEF, 141 Keyword.TYPEDEF,
97 Keyword.VAR]); 142 Keyword.VAR]);
98 } 143 }
99 } 144 }
100 145
101 @override 146 @override
102 visitNode(AstNode node) { 147 visitNode(AstNode node) {
103 if (request.offset == node.end) { 148 if (_isOffsetAfterNode(node)) {
104 Token token = node.endToken; 149 node.parent.accept(this);
105 if (token != null && !token.isSynthetic) {
106 if (token.lexeme == ';' || token.lexeme == '}') {
107 node.parent.accept(this);
108 }
109 }
110 } 150 }
111 } 151 }
112 152
113 visitSimpleIdentifier(SimpleIdentifier node) { 153 visitSimpleIdentifier(SimpleIdentifier node) {
114 AstNode parent = 154 AstNode parent = node.getAncestor((n) => n is TopLevelVariableDeclaration);
115 node.getAncestor((n) => n is TopLevelVariableDeclaration);
116 if (parent is TopLevelVariableDeclaration) { 155 if (parent is TopLevelVariableDeclaration) {
117 if (parent.variables != null && parent.variables.type != null 156 if (parent.variables != null &&
118 && parent.variables.type.name == node) { 157 parent.variables.type != null &&
158 parent.variables.type.name == node) {
119 AstNode unit = node.getAncestor((n) => n is CompilationUnit); 159 AstNode unit = node.getAncestor((n) => n is CompilationUnit);
120 if (unit is CompilationUnit) { 160 if (unit is CompilationUnit) {
121 visitCompilationUnit(unit); 161 visitCompilationUnit(unit);
122 } 162 }
123 } 163 }
124 } 164 }
125 } 165 }
126 166
127 void _addSuggestion(Keyword keyword) { 167 void _addSuggestion(Keyword keyword) {
128 String completion = keyword.syntax; 168 String completion = keyword.syntax;
129 request.suggestions.add( 169 request.suggestions.add(
130 new CompletionSuggestion( 170 new CompletionSuggestion(
131 CompletionSuggestionKind.KEYWORD, 171 CompletionSuggestionKind.KEYWORD,
132 CompletionRelevance.DEFAULT, 172 CompletionRelevance.DEFAULT,
133 completion, 173 completion,
134 completion.length, 174 completion.length,
135 0, 175 0,
136 false, 176 false,
137 false)); 177 false));
138 } 178 }
139 179
140 void _addSuggestions(List<Keyword> keywords) { 180 void _addSuggestions(List<Keyword> keywords) {
141 keywords.forEach((Keyword keyword) { 181 keywords.forEach((Keyword keyword) {
142 _addSuggestion(keyword); 182 _addSuggestion(keyword);
143 }); 183 });
144 } 184 }
185
186 bool _isOffsetAfterNode(AstNode node) {
187 if (request.offset == node.end) {
188 Token token = node.endToken;
189 if (token != null && !token.isSynthetic) {
190 if (token.lexeme == ';' || token.lexeme == '}') {
191 return true;
192 }
193 }
194 }
195 return false;
196 }
145 } 197 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698