From 3bfe2d21a74cacf0bc45d83e805c95fe727f4543 Mon Sep 17 00:00:00 2001 From: Paulo Garcia Date: Sun, 17 Aug 2014 15:02:42 -0400 Subject: [PATCH] Bug 285126 - Added capability to show enum's integer value instead of its constant name. Change-Id: Iacda2b19dc22497db60f88cf301d01d8654ca0ad Signed-off-by: Paulo Garcia Reviewed-on: https://git.eclipse.org/r/31807 Reviewed-by: Sergey Prigogin Tested-by: Sergey Prigogin --- .../ui/text/c/hover/CSourceHover.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/c/hover/CSourceHover.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/c/hover/CSourceHover.java index 541357f797e..a39b43e203a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/c/hover/CSourceHover.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/c/hover/CSourceHover.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2012 QNX Software Systems and others. + * Copyright (c) 2002, 2014 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -9,6 +9,7 @@ * QNX Software Systems - Initial API and implementation * Anton Leherbauer (Wind River Systems) * Sergey Prigogin (Google) + * Paulo Garcia (BlackBerry) *******************************************************************************/ package org.eclipse.cdt.internal.ui.text.c.hover; @@ -209,6 +210,9 @@ public class CSourceHover extends AbstractCEditorTextHover { } } else if (binding instanceof IMacroBinding) { fSource= computeSourceForMacro(ast, name, binding); + } else if (binding instanceof IEnumerator) { + // Show integer value for enumerators (bug 285126). + fSource= computeSourceForEnumerator(ast, (IEnumerator) binding); } else { fSource= computeSourceForBinding(ast, binding); } @@ -249,6 +253,34 @@ public class CSourceHover extends AbstractCEditorTextHover { return null; } + /** + * Computes the source for a enumerator. If the value of the enumerator can be retrieved, + * the method will return a string with the value, otherwise it will fall back showing + * the enumerator constant. + * + * @param ast the AST of the translation unit + * @param binding the binding of the enumerator name + * @return the enumerator value, source or null + * @throws CoreException + */ + private String computeSourceForEnumerator(IASTTranslationUnit ast, IEnumerator binding) + throws CoreException { + Long numValue = binding.getValue().numericalValue(); + if (numValue != null) { + return numValue.toString(); + } else { + // Search for the enumerator definition + IName[] defs = ast.getDefinitions(binding); + for (IName def : defs) { + String source= computeSourceForName(def, binding); + if (source != null) { + return source; + } + } + } + return null; + } + /** * Find a definition or declaration for the given binding and returns the source for it. * Definitions are preferred over declarations. In case of multiple definitions or declarations,