| 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'; | |
| 9 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
| 10 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
| 11 | 10 |
| 12 /** | 11 /** |
| 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 /** |
| 13 * A computer for the hover at the specified offset of a Dart [CompilationUnit]. | 56 * A computer for the hover at the specified offset of a Dart [CompilationUnit]. |
| 14 */ | 57 */ |
| 15 class DartUnitHoverComputer { | 58 class DartUnitHoverComputer { |
| 16 final CompilationUnit _unit; | 59 final CompilationUnit _unit; |
| 17 final int _offset; | 60 final int _offset; |
| 18 | 61 |
| 19 DartUnitHoverComputer(this._unit, this._offset); | 62 DartUnitHoverComputer(this._unit, this._offset); |
| 20 | 63 |
| 21 /** | 64 /** |
| 22 * Returns the computed hover, maybe `null`. | 65 * Returns the computed hover, maybe `null`. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 105 } |
| 63 // containing library | 106 // containing library |
| 64 LibraryElement library = element.library; | 107 LibraryElement library = element.library; |
| 65 if (library != null) { | 108 if (library != null) { |
| 66 hover.containingLibraryName = library.name; | 109 hover.containingLibraryName = library.name; |
| 67 hover.containingLibraryPath = library.source.fullName; | 110 hover.containingLibraryPath = library.source.fullName; |
| 68 } | 111 } |
| 69 } | 112 } |
| 70 // documentation | 113 // documentation |
| 71 String dartDoc = element.computeDocumentationComment(); | 114 String dartDoc = element.computeDocumentationComment(); |
| 72 dartDoc = removeDartDocDelimiters(dartDoc); | 115 dartDoc = _removeDartDocDelimiters(dartDoc); |
| 73 hover.dartdoc = dartDoc; | 116 hover.dartdoc = dartDoc; |
| 74 } | 117 } |
| 75 // parameter | 118 // parameter |
| 76 hover.parameter = _safeToString(expression.bestParameterElement); | 119 hover.parameter = _safeToString(expression.bestParameterElement); |
| 77 // types | 120 // types |
| 78 hover.staticType = _safeToString(expression.staticType); | 121 hover.staticType = _safeToString(expression.staticType); |
| 79 hover.propagatedType = _safeToString(expression.propagatedType); | 122 hover.propagatedType = _safeToString(expression.propagatedType); |
| 80 // done | 123 // done |
| 81 return hover; | 124 return hover; |
| 82 } | 125 } |
| 83 // not an expression | 126 // not an expression |
| 84 return null; | 127 return null; |
| 85 } | 128 } |
| 86 | 129 |
| 87 static _safeToString(obj) => obj != null ? obj.toString() : null; | 130 static _safeToString(obj) => obj != null ? obj.toString() : null; |
| 88 } | 131 } |
| OLD | NEW |