Files
sdk/pkg/analysis_server/lib/src/computer/computer_hover.dart
T
paulberry@google.com 0c0fd777a6 Finish modifying analysis server to make use of code generation.
Generated code is used to produce the response to these requests:
- analysis.updateOptions
- analysis.updateContent
- analysis.setSubscriptions
- analysis.setPriorityFiles
- analysis.setAnalysisRoots
- server.shutdown
- server.setSubscriptions

Generated code is used to produce these requests:
- server.shutdown
- server.getVersion
- server.shutdown
- server.connected

Generated code is used to produce these notifications:
- completion.results
- analysis.overrides
- analysis.outline
- analysis.occurrences

Generated code is used to decode these notifications:
- server.status

In addition, some dead code was removed, the 'protocol.dart' and
'protocol2.dart' files were combined, and the fields Response.result,
Request.params, and Notification.params were made private (to
encourage future use of the generated code).  Also, some overly
verbose import statements were cleaned up by using "hide" rather than
"show".

R=danrubel@google.com

Review URL: https://codereview.chromium.org//506433002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@39516 260f80e4-7a28-3924-810f-c04153c831b5
2014-08-25 10:48:16 +00:00

111 lines
3.1 KiB
Dart

// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library computer.hover;
import 'package:analysis_server/src/protocol.dart' show HoverInformation;
import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/element.dart';
/**
* Converts [str] from a Dart Doc string with slashes and stars to a plain text
* representation of the comment.
*/
String _removeDartDocDelimiters(String str) {
if (str == null) {
return null;
}
// remove /** */
if (str.startsWith('/**')) {
str = str.substring(3);
}
if (str.endsWith("*/")) {
str = str.substring(0, str.length - 2);
}
str = str.trim();
// remove leading '* '
List<String> lines = str.split('\n');
StringBuffer sb = new StringBuffer();
bool firstLine = true;
for (String line in lines) {
line = line.trim();
if (line.startsWith("*")) {
line = line.substring(1);
if (line.startsWith(" ")) {
line = line.substring(1);
}
} else if (line.startsWith("///")) {
line = line.substring(3);
if (line.startsWith(" ")) {
line = line.substring(1);
}
}
if (!firstLine) {
sb.write('\n');
}
firstLine = false;
sb.write(line);
}
str = sb.toString();
// done
return str;
}
/**
* A computer for the hover at the specified offset of a Dart [CompilationUnit].
*/
class DartUnitHoverComputer {
final CompilationUnit _unit;
final int _offset;
DartUnitHoverComputer(this._unit, this._offset);
/**
* Returns the computed hover, maybe `null`.
*/
HoverInformation compute() {
AstNode node = new NodeLocator.con1(_offset).searchWithin(_unit);
if (node is Expression) {
HoverInformation hover = new HoverInformation(node.offset, node.length);
// element
Element element = ElementLocator.locateWithOffset(node, _offset);
if (element != null) {
// variable, if synthetic accessor
if (element is PropertyAccessorElement) {
PropertyAccessorElement accessor = element;
if (accessor.isSynthetic) {
element = accessor.variable;
}
}
// description
hover.elementDescription = element.toString();
hover.elementKind = element.kind.displayName;
// library
LibraryElement library = element.library;
if (library != null) {
hover.containingLibraryName = library.name;
hover.containingLibraryPath = library.source.fullName;
}
// documentation
String dartDoc = element.computeDocumentationComment();
dartDoc = _removeDartDocDelimiters(dartDoc);
hover.dartdoc = dartDoc;
}
// parameter
hover.parameter = _safeToString(node.bestParameterElement);
// types
hover.staticType = _safeToString(node.staticType);
hover.propagatedType = _safeToString(node.propagatedType);
// done
return hover;
}
// not an expression
return null;
}
static _safeToString(obj) => obj != null ? obj.toString() : null;
}