Commit 37544b82 authored by Juergen Haug's avatar Juergen Haug
Browse files

[ui] support keyword hover for enum keywords

parent 2a869e81
......@@ -19,6 +19,7 @@ import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.xtext.EnumLiteralDeclaration;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
......@@ -32,7 +33,7 @@ import org.eclipse.xtext.util.Tuples;
import com.google.inject.Inject;
public class KeywordEObjectTextHover extends DispatchingEObjectTextHover {
@Inject
protected IEObjectHoverProvider hoverProvider;
......@@ -58,15 +59,26 @@ public class KeywordEObjectTextHover extends DispatchingEObjectTextHover {
public Pair<EObject, IRegion> resolveKeywordAt(XtextResource resource, int offset) {
IParseResult parseResult = resource.getParseResult();
if (parseResult != null) {
ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
if (leaf != null && leaf.isHidden() && leaf.getOffset() == offset) {
leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);
}
if (leaf != null && leaf.getGrammarElement() instanceof Keyword) {
Keyword keyword = (Keyword) leaf.getGrammarElement();
return Tuples.create((EObject) keyword, (IRegion) new Region(leaf.getOffset(), leaf.getLength()));
ILeafNode leaf = findNearestLeafNode(parseResult, offset);
if (leaf != null) {
if (leaf.getGrammarElement() instanceof Keyword) {
Keyword keyword = (Keyword) leaf.getGrammarElement();
return Tuples.create((EObject) keyword, (IRegion) new Region(leaf.getOffset(), leaf.getLength()));
} else if (leaf.getGrammarElement() instanceof EnumLiteralDeclaration) {
Keyword keyword = ((EnumLiteralDeclaration) leaf.getGrammarElement()).getLiteral();
return Tuples.create((EObject) keyword, (IRegion) new Region(leaf.getOffset(), leaf.getLength()));
}
}
}
return null;
}
private ILeafNode findNearestLeafNode(IParseResult parseResult, int offset) {
ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
if (leaf != null && leaf.isHidden() && leaf.getOffset() == offset) {
leaf = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);
}
return leaf;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment