1 | package de.ugoe.cs.swe.bnftools.utils;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import org.eclipse.emf.ecore.EObject;
|
---|
7 | import org.eclipse.jface.text.BadLocationException;
|
---|
8 | import org.eclipse.jface.text.ITextSelection;
|
---|
9 | import org.eclipse.xtext.builder.builderState.impl.ReferenceDescriptionImpl;
|
---|
10 | import org.eclipse.xtext.parsetree.CompositeNode;
|
---|
11 | import org.eclipse.xtext.parsetree.NodeAdapter;
|
---|
12 | import org.eclipse.xtext.parsetree.NodeUtil;
|
---|
13 | import org.eclipse.xtext.resource.IEObjectDescription;
|
---|
14 | import org.eclipse.xtext.resource.IReferenceDescription;
|
---|
15 | import org.eclipse.xtext.resource.IResourceDescription;
|
---|
16 | import org.eclipse.xtext.resource.IResourceDescriptions;
|
---|
17 | import org.eclipse.xtext.ui.editor.XtextEditor;
|
---|
18 | import org.eclipse.xtext.ui.editor.model.IXtextDocument;
|
---|
19 |
|
---|
20 | import com.google.common.base.Predicate;
|
---|
21 | import com.google.common.collect.Iterables;
|
---|
22 |
|
---|
23 | import de.ugoe.cs.swe.bnftools.ebnf.BnfEntry;
|
---|
24 | import de.ugoe.cs.swe.bnftools.ebnf.DeltaEntry;
|
---|
25 | import de.ugoe.cs.swe.bnftools.ebnf.EtsiBnf;
|
---|
26 | import de.ugoe.cs.swe.bnftools.ebnf.MergeEntry;
|
---|
27 | import de.ugoe.cs.swe.bnftools.ebnf.Rule;
|
---|
28 |
|
---|
29 | public class Utils {
|
---|
30 |
|
---|
31 | static public NodeAdapter findNodeFromSelection(
|
---|
32 | final XtextEditor xtextEditor, ITextSelection mySelection) {
|
---|
33 | if ((xtextEditor == null) || (xtextEditor.getDocument() == null))
|
---|
34 | return null;
|
---|
35 | EtsiBnf root = (EtsiBnf) xtextEditor.getDocument().readOnly(
|
---|
36 | new RootEObjectFinder());
|
---|
37 |
|
---|
38 | NodeAdapter resultNode = null;
|
---|
39 | if (root.getBnfEntry() != null) {
|
---|
40 | System.out.println("bnf grammar!");
|
---|
41 | for (int i=0; i < root.getBnfEntry().size(); i++) {
|
---|
42 | BnfEntry entry = root.getBnfEntry().get(i);
|
---|
43 | if (entry.getRule() != null) {
|
---|
44 | resultNode = findNodeInRule(entry.getRule(), mySelection);
|
---|
45 | if (resultNode != null)
|
---|
46 | return resultNode;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | } else if (root.getDeltaEntry() != null) {
|
---|
50 | for (int i=0; i < root.getDeltaEntry().size(); i++) {
|
---|
51 | DeltaEntry entry = root.getDeltaEntry().get(i);
|
---|
52 | if (entry.getRule() != null) {
|
---|
53 | resultNode = findNodeInRule(entry.getRule(), mySelection);
|
---|
54 | if (resultNode != null)
|
---|
55 | return resultNode;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | } else if (root.getMergeEntry() != null) {
|
---|
59 | for (int i=0; i < root.getMergeEntry().size(); i++) {
|
---|
60 | MergeEntry entry = root.getMergeEntry().get(i);
|
---|
61 | if (entry.getMergeRule() != null) {
|
---|
62 | // TODO: doesn't work here...
|
---|
63 | // resultNode = findNodeInRule(entry.getgetRule(), mySelection);
|
---|
64 | // if (resultNode != null)
|
---|
65 | // return resultNode;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | return resultNode;
|
---|
71 | }
|
---|
72 |
|
---|
73 | // --------------------------------------------------------------------------------
|
---|
74 |
|
---|
75 | static private NodeAdapter findNodeInRule(Rule rule, ITextSelection mySelection) {
|
---|
76 | NodeAdapter node = NodeUtil.getNodeAdapter(rule);
|
---|
77 | if ((node.getParserNode().getOffset() <= mySelection.getOffset())
|
---|
78 | && (mySelection.getOffset() <= node.getParserNode()
|
---|
79 | .getOffset()
|
---|
80 | + node.getParserNode().getLength())) {
|
---|
81 | return node;
|
---|
82 | }
|
---|
83 |
|
---|
84 | return null;
|
---|
85 | }
|
---|
86 |
|
---|
87 | // --------------------------------------------------------------------------------
|
---|
88 |
|
---|
89 | static public List<CompositeNode> findReferenceDescriptions(
|
---|
90 | final IEObjectDescription eObjectDescription, IResourceDescriptions resourceDescriptions, XtextEditor editor) {
|
---|
91 | //TODO: resolved nur innerhalb derselben datei!
|
---|
92 | List<CompositeNode> references = new ArrayList<CompositeNode>();
|
---|
93 |
|
---|
94 | for (IResourceDescription resourceDescription : resourceDescriptions
|
---|
95 | .getAllResourceDescriptions()) {
|
---|
96 | Iterable<IReferenceDescription> matchingReferenceDescriptors = Iterables
|
---|
97 | .filter(resourceDescription.getReferenceDescriptions(),
|
---|
98 | new Predicate<IReferenceDescription>() {
|
---|
99 | public boolean apply(IReferenceDescription input) {
|
---|
100 | return eObjectDescription
|
---|
101 | .getEObjectURI()
|
---|
102 | .equals(input.getTargetEObjectUri());
|
---|
103 | }
|
---|
104 | });
|
---|
105 | for (IReferenceDescription matchingReferenceDescription : matchingReferenceDescriptors) {
|
---|
106 | //TODO: dieser check ignoriert links zu anderen dateien wg. exception
|
---|
107 | if ((matchingReferenceDescription instanceof org.eclipse.xtext.resource.impl.DefaultReferenceDescription) || (matchingReferenceDescription instanceof ReferenceDescriptionImpl)){
|
---|
108 | URIFragmentResolver uriFragmentResolver = new URIFragmentResolver(matchingReferenceDescription
|
---|
109 | .getSourceEObjectUri().fragment());
|
---|
110 | CompositeNode node = editor.getDocument().readOnly(uriFragmentResolver);
|
---|
111 | if (node != null)
|
---|
112 | references.add(node);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return references;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // --------------------------------------------------------------------------------
|
---|
120 |
|
---|
121 | static public List<CompositeNode> findReferences(EObject o, IResourceDescriptions resourceDescriptions, XtextEditor editor) {
|
---|
122 | final IEObjectDescription eObjectDescription = editor.getDocument()
|
---|
123 | .readOnly(new EObjectResolver(o, resourceDescriptions));
|
---|
124 |
|
---|
125 | List<CompositeNode> referenceDescriptions = Utils.findReferenceDescriptions(eObjectDescription, resourceDescriptions, editor);
|
---|
126 | return referenceDescriptions;
|
---|
127 | }
|
---|
128 |
|
---|
129 | // --------------------------------------------------------------------------------
|
---|
130 |
|
---|
131 | static public int stepBackNewlines(IXtextDocument document, int offset) {
|
---|
132 | if (offset <= 0)
|
---|
133 | return 0;
|
---|
134 |
|
---|
135 | try {
|
---|
136 | int tempOffset = offset;
|
---|
137 | String str = document.get(tempOffset-1, 1);
|
---|
138 | while (str.equals("\n") || str.equals("\r") || str.equals(" ")) {
|
---|
139 | tempOffset -= 1;
|
---|
140 | str = document.get(tempOffset-1, 1);
|
---|
141 | }
|
---|
142 | return offset - tempOffset;
|
---|
143 | } catch (BadLocationException e) {
|
---|
144 | e.printStackTrace();
|
---|
145 | }
|
---|
146 | return 0;
|
---|
147 | }
|
---|
148 |
|
---|
149 | }
|
---|