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