1 | package com.framsticks.framclipse.ui; |
---|
2 | |
---|
3 | import org.eclipse.emf.ecore.EObject; |
---|
4 | import org.eclipse.jface.internal.text.html.HTMLTextPresenter; |
---|
5 | import org.eclipse.jface.text.DefaultInformationControl; |
---|
6 | import org.eclipse.jface.text.IInformationControl; |
---|
7 | import org.eclipse.jface.text.IInformationControlCreator; |
---|
8 | import org.eclipse.jface.text.IRegion; |
---|
9 | import org.eclipse.jface.text.ITextHover; |
---|
10 | import org.eclipse.jface.text.ITextViewer; |
---|
11 | import org.eclipse.jface.text.Region; |
---|
12 | import org.eclipse.jface.text.source.ISourceViewer; |
---|
13 | import org.eclipse.swt.widgets.Shell; |
---|
14 | import org.eclipse.xtext.nodemodel.ICompositeNode; |
---|
15 | import org.eclipse.xtext.nodemodel.ILeafNode; |
---|
16 | import org.eclipse.xtext.nodemodel.util.NodeModelUtils; |
---|
17 | import org.eclipse.xtext.resource.XtextResource; |
---|
18 | import org.eclipse.xtext.ui.editor.XtextSourceViewerConfiguration; |
---|
19 | import org.eclipse.xtext.ui.editor.model.IXtextDocument; |
---|
20 | import org.eclipse.xtext.util.concurrent.IUnitOfWork; |
---|
21 | |
---|
22 | import com.framsticks.framclipse.framScript.Function; |
---|
23 | import com.framsticks.framclipse.framScript.Invocation; |
---|
24 | import com.framsticks.framclipse.framScript.Proposable; |
---|
25 | import com.framsticks.framclipse.framScript.QualifiedExpression; |
---|
26 | import com.framsticks.framclipse.framScript.VariableDeclaration; |
---|
27 | import com.framsticks.framclipse.framScript.VariableRef; |
---|
28 | import com.framsticks.framclipse.ui.contentassist.DescriptorProvider; |
---|
29 | import com.google.inject.Inject; |
---|
30 | |
---|
31 | @SuppressWarnings("restriction") |
---|
32 | public class FramScriptSourceViewerConfiguration extends XtextSourceViewerConfiguration { |
---|
33 | |
---|
34 | @Inject |
---|
35 | private DescriptorProvider descripter; |
---|
36 | |
---|
37 | @Override |
---|
38 | public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) { |
---|
39 | return new IInformationControlCreator() { |
---|
40 | public IInformationControl createInformationControl(Shell parent) { |
---|
41 | return new DefaultInformationControl(parent, new HTMLTextPresenter(true)); |
---|
42 | } |
---|
43 | }; |
---|
44 | } |
---|
45 | |
---|
46 | @Override |
---|
47 | public ITextHover getTextHover(final ISourceViewer sourceViewer, String contentType) { |
---|
48 | return new ITextHover() { |
---|
49 | |
---|
50 | @Override |
---|
51 | public IRegion getHoverRegion(ITextViewer textViewer, int offset) { |
---|
52 | return new Region(offset, 0); |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) { |
---|
57 | final int offset = hoverRegion.getOffset(); |
---|
58 | final IXtextDocument model = (IXtextDocument) sourceViewer.getDocument(); |
---|
59 | return model.readOnly(new IUnitOfWork<String, XtextResource>() { |
---|
60 | |
---|
61 | @Override |
---|
62 | public String exec(XtextResource state) throws Exception { |
---|
63 | ICompositeNode root = state.getParseResult().getRootNode(); |
---|
64 | // AbstractNode current = ParseTreeUtil.getCurrentOrFollowingNodeByOffset(root, offset); |
---|
65 | ILeafNode current = NodeModelUtils.findLeafNodeAtOffset(root, offset); |
---|
66 | // EObject semantic = NodeUtil.getNearestSemanticObject(current); |
---|
67 | EObject semantic = current.getSemanticElement(); |
---|
68 | |
---|
69 | if (semantic instanceof QualifiedExpression) { |
---|
70 | return getMessage((QualifiedExpression) semantic); |
---|
71 | } |
---|
72 | return null; |
---|
73 | } |
---|
74 | }); |
---|
75 | } |
---|
76 | }; |
---|
77 | } |
---|
78 | |
---|
79 | private String getMessage(QualifiedExpression expression) { |
---|
80 | if (expression instanceof Invocation) { |
---|
81 | Function function = ((Invocation) expression).getFunction(); |
---|
82 | if (function instanceof Proposable) { |
---|
83 | return descripter.description(((Proposable) function).description()); |
---|
84 | } |
---|
85 | } else if (expression instanceof VariableRef) { |
---|
86 | VariableDeclaration var = ((VariableRef) expression).getVar(); |
---|
87 | if (var instanceof Proposable) { |
---|
88 | return descripter.description(((Proposable) var).description()); |
---|
89 | } |
---|
90 | } |
---|
91 | return null; |
---|
92 | } |
---|
93 | |
---|
94 | } |
---|