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

Last change on this file since 437 was 437, checked in by Mateusz Poszwa, 8 years ago

Added Framclipse as developed by Bartosz Kukawka and Tomek Maciejewski in 2010

  • Property svn:eol-style set to native
File size: 3.4 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.parsetree.AbstractNode;
15import org.eclipse.xtext.parsetree.CompositeNode;
16import org.eclipse.xtext.parsetree.NodeUtil;
17import org.eclipse.xtext.parsetree.ParseTreeUtil;
18import org.eclipse.xtext.resource.XtextResource;
19import org.eclipse.xtext.ui.editor.XtextSourceViewerConfiguration;
20import org.eclipse.xtext.ui.editor.model.IXtextDocument;
21import org.eclipse.xtext.util.concurrent.IUnitOfWork;
22
23import com.framsticks.framclipse.framScript.Function;
24import com.framsticks.framclipse.framScript.Invocation;
25import com.framsticks.framclipse.framScript.Proposable;
26import com.framsticks.framclipse.framScript.QualifiedExpression;
27import com.framsticks.framclipse.framScript.VariableDeclaration;
28import com.framsticks.framclipse.framScript.VariableRef;
29import com.framsticks.framclipse.ui.contentassist.DescriptorProvider;
30import com.google.inject.Inject;
31
32@SuppressWarnings("restriction")
33public 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}
Note: See TracBrowser for help on using the repository browser.