Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1020)

Unified Diff: pkg/analysis_server/lib/src/computer/computer_hover.dart

Issue 362893004: Implementation for 'analysis.getHover'. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/computer/computer_hover.dart
diff --git a/pkg/analysis_server/lib/src/computer/computer_hover.dart b/pkg/analysis_server/lib/src/computer/computer_hover.dart
new file mode 100644
index 0000000000000000000000000000000000000000..1b936b935fc65c409bf1392ea4e82f5daeb7b315
--- /dev/null
+++ b/pkg/analysis_server/lib/src/computer/computer_hover.dart
@@ -0,0 +1,115 @@
+// 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 'dart:collection';
+
+import 'package:analysis_server/src/constants.dart';
+import 'package:analyzer/src/generated/ast.dart';
+import 'package:analyzer/src/generated/element.dart';
+
+
+/**
+ * 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`.
+ */
+ Map<String, Object> compute() {
+ AstNode node = new NodeLocator.con1(_offset).searchWithin(_unit);
+ if (node is Expression) {
+ Hover hover = new Hover();
+ // 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();
+ // library
+ LibraryElement library = element.library;
+ if (library != null) {
+ hover.containingLibraryName = library.name;
+ hover.containingLibraryPath = library.source.fullName;
+ }
+ // documentation
+ hover.dartDoc = element.computeDocumentationComment();
+ }
+ // parameter
+ hover.parameter = _safeToString(node.bestParameterElement);
+ // types
+ hover.staticType = _safeToString(node.staticType);
+ hover.propagatedType = _safeToString(node.propagatedType);
+ // done
+ return hover.toJson();
+ }
+ // not an expression
+ return null;
+ }
+
+ static _safeToString(obj) => obj != null ? obj.toString() : null;
+}
+
+
+class Hover {
+ String containingLibraryName;
+ String containingLibraryPath;
+ String dartDoc;
+ String elementDescription;
+ String parameter;
+ String propagatedType;
+ String staticType;
+
+ Hover();
+
+ factory Hover.fromJson(Map<String, Object> map) {
+ Hover hover = new Hover();
+ hover.containingLibraryName = map[CONTAINING_LIBRARY_NAME];
+ hover.containingLibraryPath = map[CONTAINING_LIBRARY_PATH];
+ hover.dartDoc = map[DART_DOC];
+ hover.elementDescription = map[ELEMENT_DESCRIPTION];
+ hover.parameter = map[PARAMETER];
+ hover.propagatedType = map[PROPAGATED_TYPE];
+ hover.staticType = map[STATIC_TYPE];
+ return hover;
+ }
+
+ Map<String, Object> toJson() {
+ Map<String, Object> json = new HashMap<String, Object>();
+ if (containingLibraryName != null) {
+ json[CONTAINING_LIBRARY_NAME] = containingLibraryName;
+ }
+ if (containingLibraryName != null) {
+ json[CONTAINING_LIBRARY_PATH] = containingLibraryPath;
+ }
+ if (dartDoc != null) {
+ json[DART_DOC] = dartDoc;
+ }
+ if (elementDescription != null) {
+ json[ELEMENT_DESCRIPTION] = elementDescription;
+ }
+ if (parameter != null) {
+ json[PARAMETER] = parameter;
+ }
+ if (propagatedType != null) {
+ json[PROPAGATED_TYPE] = propagatedType;
+ }
+ if (staticType != null) {
+ json[STATIC_TYPE] = staticType;
+ }
+ return json;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698