source: java/Framclipse/com.framsticks.framclipse.ui/src/com/framsticks/framclipse/ui/FramScriptSourceViewerConfiguration.java @ 438

Last change on this file since 438 was 438, checked in by Mateusz Poszwa, 9 years ago

Ported Framclipse to Xtext 2.8.4

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1package com.framsticks.framclipse.ui;
2
3import org.eclipse.emf.ecore.EObject;
4import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
5import org.eclipse.jface.text.DefaultInformationControl;
6import org.eclipse.jface.text.IInformationControl;
7import org.eclipse.jface.text.IInformationControlCreator;
8import org.eclipse.jface.text.IRegion;
9import org.eclipse.jface.text.ITextHover;
10import org.eclipse.jface.text.ITextViewer;
11import org.eclipse.jface.text.Region;
12import org.eclipse.jface.text.source.ISourceViewer;
13import org.eclipse.swt.widgets.Shell;
14import org.eclipse.xtext.nodemodel.ICompositeNode;
15import org.eclipse.xtext.nodemodel.ILeafNode;
16import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
17import org.eclipse.xtext.resource.XtextResource;
18import org.eclipse.xtext.ui.editor.XtextSourceViewerConfiguration;
19import org.eclipse.xtext.ui.editor.model.IXtextDocument;
20import org.eclipse.xtext.util.concurrent.IUnitOfWork;
21
22import com.framsticks.framclipse.framScript.Function;
23import com.framsticks.framclipse.framScript.Invocation;
24import com.framsticks.framclipse.framScript.Proposable;
25import com.framsticks.framclipse.framScript.QualifiedExpression;
26import com.framsticks.framclipse.framScript.VariableDeclaration;
27import com.framsticks.framclipse.framScript.VariableRef;
28import com.framsticks.framclipse.ui.contentassist.DescriptorProvider;
29import com.google.inject.Inject;
30
31@SuppressWarnings("restriction")
32public 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}
Note: See TracBrowser for help on using the repository browser.