| 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.hover; | 5 library computer.hover; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart' show HoverInformation; | 7 import 'package:analysis_server/src/protocol.dart' show HoverInformation; |
| 8 import 'package:analysis_server/src/utilities/documentation.dart'; |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 9 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart'; | 10 import 'package:analyzer/src/generated/element.dart'; |
| 10 | 11 |
| 11 /** | 12 /** |
| 12 * Converts [str] from a Dart Doc string with slashes and stars to a plain text | |
| 13 * representation of the comment. | |
| 14 */ | |
| 15 String _removeDartDocDelimiters(String str) { | |
| 16 if (str == null) { | |
| 17 return null; | |
| 18 } | |
| 19 // remove /** */ | |
| 20 if (str.startsWith('/**')) { | |
| 21 str = str.substring(3); | |
| 22 } | |
| 23 if (str.endsWith("*/")) { | |
| 24 str = str.substring(0, str.length - 2); | |
| 25 } | |
| 26 str = str.trim(); | |
| 27 // remove leading '* ' | |
| 28 List<String> lines = str.split('\n'); | |
| 29 StringBuffer sb = new StringBuffer(); | |
| 30 bool firstLine = true; | |
| 31 for (String line in lines) { | |
| 32 line = line.trim(); | |
| 33 if (line.startsWith("*")) { | |
| 34 line = line.substring(1); | |
| 35 if (line.startsWith(" ")) { | |
| 36 line = line.substring(1); | |
| 37 } | |
| 38 } else if (line.startsWith("///")) { | |
| 39 line = line.substring(3); | |
| 40 if (line.startsWith(" ")) { | |
| 41 line = line.substring(1); | |
| 42 } | |
| 43 } | |
| 44 if (!firstLine) { | |
| 45 sb.write('\n'); | |
| 46 } | |
| 47 firstLine = false; | |
| 48 sb.write(line); | |
| 49 } | |
| 50 str = sb.toString(); | |
| 51 // done | |
| 52 return str; | |
| 53 } | |
| 54 | |
| 55 /** | |
| 56 * A computer for the hover at the specified offset of a Dart [CompilationUnit]. | 13 * A computer for the hover at the specified offset of a Dart [CompilationUnit]. |
| 57 */ | 14 */ |
| 58 class DartUnitHoverComputer { | 15 class DartUnitHoverComputer { |
| 59 final CompilationUnit _unit; | 16 final CompilationUnit _unit; |
| 60 final int _offset; | 17 final int _offset; |
| 61 | 18 |
| 62 DartUnitHoverComputer(this._unit, this._offset); | 19 DartUnitHoverComputer(this._unit, this._offset); |
| 63 | 20 |
| 64 /** | 21 /** |
| 65 * Returns the computed hover, maybe `null`. | 22 * Returns the computed hover, maybe `null`. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } | 62 } |
| 106 // containing library | 63 // containing library |
| 107 LibraryElement library = element.library; | 64 LibraryElement library = element.library; |
| 108 if (library != null) { | 65 if (library != null) { |
| 109 hover.containingLibraryName = library.name; | 66 hover.containingLibraryName = library.name; |
| 110 hover.containingLibraryPath = library.source.fullName; | 67 hover.containingLibraryPath = library.source.fullName; |
| 111 } | 68 } |
| 112 } | 69 } |
| 113 // documentation | 70 // documentation |
| 114 String dartDoc = element.computeDocumentationComment(); | 71 String dartDoc = element.computeDocumentationComment(); |
| 115 dartDoc = _removeDartDocDelimiters(dartDoc); | 72 dartDoc = removeDartDocDelimiters(dartDoc); |
| 116 hover.dartdoc = dartDoc; | 73 hover.dartdoc = dartDoc; |
| 117 } | 74 } |
| 118 // parameter | 75 // parameter |
| 119 hover.parameter = _safeToString(expression.bestParameterElement); | 76 hover.parameter = _safeToString(expression.bestParameterElement); |
| 120 // types | 77 // types |
| 121 hover.staticType = _safeToString(expression.staticType); | 78 hover.staticType = _safeToString(expression.staticType); |
| 122 hover.propagatedType = _safeToString(expression.propagatedType); | 79 hover.propagatedType = _safeToString(expression.propagatedType); |
| 123 // done | 80 // done |
| 124 return hover; | 81 return hover; |
| 125 } | 82 } |
| 126 // not an expression | 83 // not an expression |
| 127 return null; | 84 return null; |
| 128 } | 85 } |
| 129 | 86 |
| 130 static _safeToString(obj) => obj != null ? obj.toString() : null; | 87 static _safeToString(obj) => obj != null ? obj.toString() : null; |
| 131 } | 88 } |
| OLD | NEW |