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 computer.outline; | 5 library computer.outline; |
6 | 6 |
7 import 'package:analysis_server/src/collections.dart'; | 7 import 'package:analysis_server/src/collections.dart'; |
8 import 'package:analysis_server/src/protocol.dart'; | 8 import 'package:analysis_server/src/protocol.dart'; |
9 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/ast.dart'; |
10 import 'package:analyzer/src/generated/element.dart' as engine; | 10 import 'package:analyzer/src/generated/element.dart' as engine; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 if (variableIndex == 0) { | 130 if (variableIndex == 0) { |
131 node = parent.parent; | 131 node = parent.parent; |
132 parent = node.parent; | 132 parent = node.parent; |
133 } else if (variableIndex >= 1) { | 133 } else if (variableIndex >= 1) { |
134 firstOffset = variables[variableIndex - 1].end; | 134 firstOffset = variables[variableIndex - 1].end; |
135 return new _SourceRegion(firstOffset, endOffset - firstOffset); | 135 return new _SourceRegion(firstOffset, endOffset - firstOffset); |
136 } | 136 } |
137 } | 137 } |
138 // unit or class member | 138 // unit or class member |
139 if (parent is CompilationUnit) { | 139 if (parent is CompilationUnit) { |
140 firstOffset = 0; | 140 firstOffset = node.offset; |
141 siblings = parent.declarations; | 141 siblings = parent.declarations; |
142 } else if (parent is ClassDeclaration) { | 142 } else if (parent is ClassDeclaration) { |
143 firstOffset = parent.leftBracket.end; | 143 firstOffset = parent.leftBracket.end; |
144 siblings = parent.members; | 144 siblings = parent.members; |
145 } else { | 145 } else { |
146 int offset = node.offset; | 146 int offset = node.offset; |
147 return new _SourceRegion(offset, endOffset - offset); | 147 return new _SourceRegion(offset, endOffset - offset); |
148 } | 148 } |
149 // first child: [endOfParent, endOfNode] | 149 // first child: [endOfParent, endOfNode] |
150 int index = siblings.indexOf(node); | 150 int index = siblings.indexOf(node); |
151 if (index == 0) { | 151 if (index == 0) { |
152 return new _SourceRegion(firstOffset, endOffset - firstOffset); | 152 return new _SourceRegion(firstOffset, endOffset - firstOffset); |
153 } | 153 } |
154 // not first child: [endOfPreviousSibling, endOfNode] | 154 // not first child: [endOfPreviousSibling, endOfNode] |
155 int prevSiblingEnd = siblings[index - 1].end; | 155 int prevSiblingEnd = siblings[index - 1].end; |
156 return new _SourceRegion(prevSiblingEnd, endOffset - prevSiblingEnd); | 156 return new _SourceRegion(prevSiblingEnd, endOffset - prevSiblingEnd); |
157 } | 157 } |
158 | 158 |
159 Outline _newClassOutline(ClassDeclaration node, List<Outline> classContents) { | 159 Outline _newClassOutline(ClassDeclaration node, List<Outline> classContents) { |
160 SimpleIdentifier nameNode = node.name; | 160 SimpleIdentifier nameNode = node.name; |
161 String name = nameNode.name; | 161 String name = nameNode.name; |
162 _SourceRegion sourceRegion = _getSourceRegion(node); | 162 _SourceRegion sourceRegion = _getSourceRegion(node); |
163 Element element = new Element(ElementKind.CLASS, name, Element.makeFlags( | 163 Element element = new Element( |
| 164 ElementKind.CLASS, |
| 165 name, |
| 166 Element.makeFlags( |
164 isPrivate: Identifier.isPrivateName(name), | 167 isPrivate: Identifier.isPrivateName(name), |
165 isDeprecated: _isDeprecated(node), | 168 isDeprecated: _isDeprecated(node), |
166 isAbstract: node.isAbstract), | 169 isAbstract: node.isAbstract), |
167 location: _getLocationNode(nameNode), | 170 location: _getLocationNode(nameNode), |
168 typeParameters: _getTypeParametersStr(node.typeParameters)); | 171 typeParameters: _getTypeParametersStr(node.typeParameters)); |
169 return new Outline(element, sourceRegion.offset, sourceRegion.length, | 172 return new Outline(element, sourceRegion.offset, sourceRegion.length, |
170 children: nullIfEmpty(classContents)); | 173 children: nullIfEmpty(classContents)); |
171 } | 174 } |
172 | 175 |
173 Outline _newClassTypeAlias(ClassTypeAlias node) { | 176 Outline _newClassTypeAlias(ClassTypeAlias node) { |
174 SimpleIdentifier nameNode = node.name; | 177 SimpleIdentifier nameNode = node.name; |
175 String name = nameNode.name; | 178 String name = nameNode.name; |
176 _SourceRegion sourceRegion = _getSourceRegion(node); | 179 _SourceRegion sourceRegion = _getSourceRegion(node); |
177 Element element = new Element(ElementKind.CLASS_TYPE_ALIAS, name, Element | 180 Element element = new Element( |
178 .makeFlags( | 181 ElementKind.CLASS_TYPE_ALIAS, |
179 isPrivate: Identifier.isPrivateName(name), | 182 name, |
180 isDeprecated: _isDeprecated(node), | 183 Element.makeFlags( |
181 isAbstract: node.isAbstract), | 184 isPrivate: Identifier.isPrivateName(name), |
| 185 isDeprecated: _isDeprecated(node), |
| 186 isAbstract: node.isAbstract), |
182 location: _getLocationNode(nameNode), | 187 location: _getLocationNode(nameNode), |
183 typeParameters: _getTypeParametersStr(node.typeParameters)); | 188 typeParameters: _getTypeParametersStr(node.typeParameters)); |
184 return new Outline(element, sourceRegion.offset, sourceRegion.length); | 189 return new Outline(element, sourceRegion.offset, sourceRegion.length); |
185 } | 190 } |
186 | 191 |
187 Outline _newConstructorOutline(ConstructorDeclaration constructor) { | 192 Outline _newConstructorOutline(ConstructorDeclaration constructor) { |
188 Identifier returnType = constructor.returnType; | 193 Identifier returnType = constructor.returnType; |
189 String name = returnType.name; | 194 String name = returnType.name; |
190 int offset = returnType.offset; | 195 int offset = returnType.offset; |
191 int length = returnType.length; | 196 int length = returnType.length; |
192 SimpleIdentifier constructorNameNode = constructor.name; | 197 SimpleIdentifier constructorNameNode = constructor.name; |
193 bool isPrivate = false; | 198 bool isPrivate = false; |
194 if (constructorNameNode != null) { | 199 if (constructorNameNode != null) { |
195 String constructorName = constructorNameNode.name; | 200 String constructorName = constructorNameNode.name; |
196 isPrivate = Identifier.isPrivateName(constructorName); | 201 isPrivate = Identifier.isPrivateName(constructorName); |
197 name += '.${constructorName}'; | 202 name += '.${constructorName}'; |
198 offset = constructorNameNode.offset; | 203 offset = constructorNameNode.offset; |
199 length = constructorNameNode.length; | 204 length = constructorNameNode.length; |
200 } | 205 } |
201 _SourceRegion sourceRegion = _getSourceRegion(constructor); | 206 _SourceRegion sourceRegion = _getSourceRegion(constructor); |
202 FormalParameterList parameters = constructor.parameters; | 207 FormalParameterList parameters = constructor.parameters; |
203 String parametersStr = parameters != null ? parameters.toSource() : ''; | 208 String parametersStr = parameters != null ? parameters.toSource() : ''; |
204 Element element = new Element(ElementKind.CONSTRUCTOR, name, Element | 209 Element element = new Element( |
205 .makeFlags( | 210 ElementKind.CONSTRUCTOR, |
206 isPrivate: isPrivate, isDeprecated: _isDeprecated(constructor)), | 211 name, |
| 212 Element.makeFlags( |
| 213 isPrivate: isPrivate, isDeprecated: _isDeprecated(constructor)), |
207 location: _getLocationOffsetLength(offset, length), | 214 location: _getLocationOffsetLength(offset, length), |
208 parameters: parametersStr); | 215 parameters: parametersStr); |
209 List<Outline> contents = _addLocalFunctionOutlines(constructor.body); | 216 List<Outline> contents = _addLocalFunctionOutlines(constructor.body); |
210 Outline outline = new Outline( | 217 Outline outline = new Outline( |
211 element, sourceRegion.offset, sourceRegion.length, | 218 element, sourceRegion.offset, sourceRegion.length, |
212 children: nullIfEmpty(contents)); | 219 children: nullIfEmpty(contents)); |
213 return outline; | 220 return outline; |
214 } | 221 } |
215 | 222 |
216 Outline _newEnumConstant(EnumConstantDeclaration node) { | 223 Outline _newEnumConstant(EnumConstantDeclaration node) { |
217 SimpleIdentifier nameNode = node.name; | 224 SimpleIdentifier nameNode = node.name; |
218 String name = nameNode.name; | 225 String name = nameNode.name; |
219 _SourceRegion sourceRegion = _getSourceRegion(node); | 226 _SourceRegion sourceRegion = _getSourceRegion(node); |
220 Element element = new Element(ElementKind.ENUM_CONSTANT, name, Element | 227 Element element = new Element( |
221 .makeFlags( | 228 ElementKind.ENUM_CONSTANT, |
222 isPrivate: Identifier.isPrivateName(name), | 229 name, |
223 isDeprecated: _isDeprecated(node)), | 230 Element.makeFlags( |
| 231 isPrivate: Identifier.isPrivateName(name), |
| 232 isDeprecated: _isDeprecated(node)), |
224 location: _getLocationNode(nameNode)); | 233 location: _getLocationNode(nameNode)); |
225 return new Outline(element, sourceRegion.offset, sourceRegion.length); | 234 return new Outline(element, sourceRegion.offset, sourceRegion.length); |
226 } | 235 } |
227 | 236 |
228 Outline _newEnumOutline(EnumDeclaration node, List<Outline> children) { | 237 Outline _newEnumOutline(EnumDeclaration node, List<Outline> children) { |
229 SimpleIdentifier nameNode = node.name; | 238 SimpleIdentifier nameNode = node.name; |
230 String name = nameNode.name; | 239 String name = nameNode.name; |
231 _SourceRegion sourceRegion = _getSourceRegion(node); | 240 _SourceRegion sourceRegion = _getSourceRegion(node); |
232 Element element = new Element(ElementKind.ENUM, name, Element.makeFlags( | 241 Element element = new Element( |
| 242 ElementKind.ENUM, |
| 243 name, |
| 244 Element.makeFlags( |
233 isPrivate: Identifier.isPrivateName(name), | 245 isPrivate: Identifier.isPrivateName(name), |
234 isDeprecated: _isDeprecated(node)), | 246 isDeprecated: _isDeprecated(node)), |
235 location: _getLocationNode(nameNode)); | 247 location: _getLocationNode(nameNode)); |
236 return new Outline(element, sourceRegion.offset, sourceRegion.length, | 248 return new Outline(element, sourceRegion.offset, sourceRegion.length, |
237 children: nullIfEmpty(children)); | 249 children: nullIfEmpty(children)); |
238 } | 250 } |
239 | 251 |
240 Outline _newFunctionOutline(FunctionDeclaration function, bool isStatic) { | 252 Outline _newFunctionOutline(FunctionDeclaration function, bool isStatic) { |
241 TypeName returnType = function.returnType; | 253 TypeName returnType = function.returnType; |
242 SimpleIdentifier nameNode = function.name; | 254 SimpleIdentifier nameNode = function.name; |
243 String name = nameNode.name; | 255 String name = nameNode.name; |
244 FunctionExpression functionExpression = function.functionExpression; | 256 FunctionExpression functionExpression = function.functionExpression; |
245 FormalParameterList parameters = functionExpression.parameters; | 257 FormalParameterList parameters = functionExpression.parameters; |
246 ElementKind kind; | 258 ElementKind kind; |
247 if (function.isGetter) { | 259 if (function.isGetter) { |
248 kind = ElementKind.GETTER; | 260 kind = ElementKind.GETTER; |
249 } else if (function.isSetter) { | 261 } else if (function.isSetter) { |
250 kind = ElementKind.SETTER; | 262 kind = ElementKind.SETTER; |
251 } else { | 263 } else { |
252 kind = ElementKind.FUNCTION; | 264 kind = ElementKind.FUNCTION; |
253 } | 265 } |
254 _SourceRegion sourceRegion = _getSourceRegion(function); | 266 _SourceRegion sourceRegion = _getSourceRegion(function); |
255 String parametersStr = parameters != null ? parameters.toSource() : ''; | 267 String parametersStr = parameters != null ? parameters.toSource() : ''; |
256 String returnTypeStr = returnType != null ? returnType.toSource() : ''; | 268 String returnTypeStr = returnType != null ? returnType.toSource() : ''; |
257 Element element = new Element(kind, name, Element.makeFlags( | 269 Element element = new Element( |
| 270 kind, |
| 271 name, |
| 272 Element.makeFlags( |
258 isPrivate: Identifier.isPrivateName(name), | 273 isPrivate: Identifier.isPrivateName(name), |
259 isDeprecated: _isDeprecated(function), | 274 isDeprecated: _isDeprecated(function), |
260 isStatic: isStatic), | 275 isStatic: isStatic), |
261 location: _getLocationNode(nameNode), | 276 location: _getLocationNode(nameNode), |
262 parameters: parametersStr, | 277 parameters: parametersStr, |
263 returnType: returnTypeStr); | 278 returnType: returnTypeStr); |
264 List<Outline> contents = _addLocalFunctionOutlines(functionExpression.body); | 279 List<Outline> contents = _addLocalFunctionOutlines(functionExpression.body); |
265 Outline outline = new Outline( | 280 Outline outline = new Outline( |
266 element, sourceRegion.offset, sourceRegion.length, | 281 element, sourceRegion.offset, sourceRegion.length, |
267 children: nullIfEmpty(contents)); | 282 children: nullIfEmpty(contents)); |
268 return outline; | 283 return outline; |
269 } | 284 } |
270 | 285 |
271 Outline _newFunctionTypeAliasOutline(FunctionTypeAlias node) { | 286 Outline _newFunctionTypeAliasOutline(FunctionTypeAlias node) { |
272 TypeName returnType = node.returnType; | 287 TypeName returnType = node.returnType; |
273 SimpleIdentifier nameNode = node.name; | 288 SimpleIdentifier nameNode = node.name; |
274 String name = nameNode.name; | 289 String name = nameNode.name; |
275 _SourceRegion sourceRegion = _getSourceRegion(node); | 290 _SourceRegion sourceRegion = _getSourceRegion(node); |
276 FormalParameterList parameters = node.parameters; | 291 FormalParameterList parameters = node.parameters; |
277 String parametersStr = parameters != null ? parameters.toSource() : ''; | 292 String parametersStr = parameters != null ? parameters.toSource() : ''; |
278 String returnTypeStr = returnType != null ? returnType.toSource() : ''; | 293 String returnTypeStr = returnType != null ? returnType.toSource() : ''; |
279 Element element = new Element(ElementKind.FUNCTION_TYPE_ALIAS, name, Element | 294 Element element = new Element( |
280 .makeFlags( | 295 ElementKind.FUNCTION_TYPE_ALIAS, |
281 isPrivate: Identifier.isPrivateName(name), | 296 name, |
282 isDeprecated: _isDeprecated(node)), | 297 Element.makeFlags( |
| 298 isPrivate: Identifier.isPrivateName(name), |
| 299 isDeprecated: _isDeprecated(node)), |
283 location: _getLocationNode(nameNode), | 300 location: _getLocationNode(nameNode), |
284 parameters: parametersStr, | 301 parameters: parametersStr, |
285 returnType: returnTypeStr, | 302 returnType: returnTypeStr, |
286 typeParameters: _getTypeParametersStr(node.typeParameters)); | 303 typeParameters: _getTypeParametersStr(node.typeParameters)); |
287 return new Outline(element, sourceRegion.offset, sourceRegion.length); | 304 return new Outline(element, sourceRegion.offset, sourceRegion.length); |
288 } | 305 } |
289 | 306 |
290 Outline _newMethodOutline(MethodDeclaration method) { | 307 Outline _newMethodOutline(MethodDeclaration method) { |
291 TypeName returnType = method.returnType; | 308 TypeName returnType = method.returnType; |
292 SimpleIdentifier nameNode = method.name; | 309 SimpleIdentifier nameNode = method.name; |
293 String name = nameNode.name; | 310 String name = nameNode.name; |
294 FormalParameterList parameters = method.parameters; | 311 FormalParameterList parameters = method.parameters; |
295 ElementKind kind; | 312 ElementKind kind; |
296 if (method.isGetter) { | 313 if (method.isGetter) { |
297 kind = ElementKind.GETTER; | 314 kind = ElementKind.GETTER; |
298 } else if (method.isSetter) { | 315 } else if (method.isSetter) { |
299 kind = ElementKind.SETTER; | 316 kind = ElementKind.SETTER; |
300 } else { | 317 } else { |
301 kind = ElementKind.METHOD; | 318 kind = ElementKind.METHOD; |
302 } | 319 } |
303 _SourceRegion sourceRegion = _getSourceRegion(method); | 320 _SourceRegion sourceRegion = _getSourceRegion(method); |
304 String parametersStr = parameters != null ? parameters.toSource() : null; | 321 String parametersStr = parameters != null ? parameters.toSource() : null; |
305 String returnTypeStr = returnType != null ? returnType.toSource() : ''; | 322 String returnTypeStr = returnType != null ? returnType.toSource() : ''; |
306 Element element = new Element(kind, name, Element.makeFlags( | 323 Element element = new Element( |
| 324 kind, |
| 325 name, |
| 326 Element.makeFlags( |
307 isPrivate: Identifier.isPrivateName(name), | 327 isPrivate: Identifier.isPrivateName(name), |
308 isDeprecated: _isDeprecated(method), | 328 isDeprecated: _isDeprecated(method), |
309 isAbstract: method.isAbstract, | 329 isAbstract: method.isAbstract, |
310 isStatic: method.isStatic), | 330 isStatic: method.isStatic), |
311 location: _getLocationNode(nameNode), | 331 location: _getLocationNode(nameNode), |
312 parameters: parametersStr, | 332 parameters: parametersStr, |
313 returnType: returnTypeStr); | 333 returnType: returnTypeStr); |
314 List<Outline> contents = _addLocalFunctionOutlines(method.body); | 334 List<Outline> contents = _addLocalFunctionOutlines(method.body); |
315 Outline outline = new Outline( | 335 Outline outline = new Outline( |
316 element, sourceRegion.offset, sourceRegion.length, | 336 element, sourceRegion.offset, sourceRegion.length, |
(...skipping 14 matching lines...) Expand all Loading... |
331 return null; | 351 return null; |
332 } | 352 } |
333 return parameters.toSource(); | 353 return parameters.toSource(); |
334 } | 354 } |
335 | 355 |
336 Outline _newVariableOutline(String typeName, ElementKind kind, | 356 Outline _newVariableOutline(String typeName, ElementKind kind, |
337 VariableDeclaration variable, bool isStatic) { | 357 VariableDeclaration variable, bool isStatic) { |
338 SimpleIdentifier nameNode = variable.name; | 358 SimpleIdentifier nameNode = variable.name; |
339 String name = nameNode.name; | 359 String name = nameNode.name; |
340 _SourceRegion sourceRegion = _getSourceRegion(variable); | 360 _SourceRegion sourceRegion = _getSourceRegion(variable); |
341 Element element = new Element(kind, name, Element.makeFlags( | 361 Element element = new Element( |
| 362 kind, |
| 363 name, |
| 364 Element.makeFlags( |
342 isPrivate: Identifier.isPrivateName(name), | 365 isPrivate: Identifier.isPrivateName(name), |
343 isDeprecated: _isDeprecated(variable), | 366 isDeprecated: _isDeprecated(variable), |
344 isStatic: isStatic, | 367 isStatic: isStatic, |
345 isConst: variable.isConst, | 368 isConst: variable.isConst, |
346 isFinal: variable.isFinal), | 369 isFinal: variable.isFinal), |
347 location: _getLocationNode(nameNode), returnType: typeName); | 370 location: _getLocationNode(nameNode), |
| 371 returnType: typeName); |
348 Outline outline = | 372 Outline outline = |
349 new Outline(element, sourceRegion.offset, sourceRegion.length); | 373 new Outline(element, sourceRegion.offset, sourceRegion.length); |
350 return outline; | 374 return outline; |
351 } | 375 } |
352 | 376 |
353 /** | 377 /** |
354 * Returns `true` if the given [element] is not `null` and deprecated. | 378 * Returns `true` if the given [element] is not `null` and deprecated. |
355 */ | 379 */ |
356 static bool _isDeprecated(Declaration declaration) { | 380 static bool _isDeprecated(Declaration declaration) { |
357 engine.Element element = declaration.element; | 381 engine.Element element = declaration.element; |
(...skipping 17 matching lines...) Expand all Loading... |
375 } | 399 } |
376 | 400 |
377 /** | 401 /** |
378 * A range of characters. | 402 * A range of characters. |
379 */ | 403 */ |
380 class _SourceRegion { | 404 class _SourceRegion { |
381 final int length; | 405 final int length; |
382 final int offset; | 406 final int offset; |
383 _SourceRegion(this.offset, this.length); | 407 _SourceRegion(this.offset, this.length); |
384 } | 408 } |
OLD | NEW |