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